In gcc/: 2010-12-18 Nicola Pero <nicola.pero@meta-innovation.com>
[platform/upstream/gcc.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 void cp_parser_for_init_statement
1867   (cp_parser *);
1868 static tree  cp_parser_c_for
1869   (cp_parser *);
1870 static tree  cp_parser_range_for
1871   (cp_parser *);
1872 static tree cp_parser_jump_statement
1873   (cp_parser *);
1874 static void cp_parser_declaration_statement
1875   (cp_parser *);
1876
1877 static tree cp_parser_implicitly_scoped_statement
1878   (cp_parser *, bool *);
1879 static void cp_parser_already_scoped_statement
1880   (cp_parser *);
1881
1882 /* Declarations [gram.dcl.dcl] */
1883
1884 static void cp_parser_declaration_seq_opt
1885   (cp_parser *);
1886 static void cp_parser_declaration
1887   (cp_parser *);
1888 static void cp_parser_block_declaration
1889   (cp_parser *, bool);
1890 static void cp_parser_simple_declaration
1891   (cp_parser *, bool);
1892 static void cp_parser_decl_specifier_seq
1893   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1894 static tree cp_parser_storage_class_specifier_opt
1895   (cp_parser *);
1896 static tree cp_parser_function_specifier_opt
1897   (cp_parser *, cp_decl_specifier_seq *);
1898 static tree cp_parser_type_specifier
1899   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1900    int *, bool *);
1901 static tree cp_parser_simple_type_specifier
1902   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1903 static tree cp_parser_type_name
1904   (cp_parser *);
1905 static tree cp_parser_nonclass_name 
1906   (cp_parser* parser);
1907 static tree cp_parser_elaborated_type_specifier
1908   (cp_parser *, bool, bool);
1909 static tree cp_parser_enum_specifier
1910   (cp_parser *);
1911 static void cp_parser_enumerator_list
1912   (cp_parser *, tree);
1913 static void cp_parser_enumerator_definition
1914   (cp_parser *, tree);
1915 static tree cp_parser_namespace_name
1916   (cp_parser *);
1917 static void cp_parser_namespace_definition
1918   (cp_parser *);
1919 static void cp_parser_namespace_body
1920   (cp_parser *);
1921 static tree cp_parser_qualified_namespace_specifier
1922   (cp_parser *);
1923 static void cp_parser_namespace_alias_definition
1924   (cp_parser *);
1925 static bool cp_parser_using_declaration
1926   (cp_parser *, bool);
1927 static void cp_parser_using_directive
1928   (cp_parser *);
1929 static void cp_parser_asm_definition
1930   (cp_parser *);
1931 static void cp_parser_linkage_specification
1932   (cp_parser *);
1933 static void cp_parser_static_assert
1934   (cp_parser *, bool);
1935 static tree cp_parser_decltype
1936   (cp_parser *);
1937
1938 /* Declarators [gram.dcl.decl] */
1939
1940 static tree cp_parser_init_declarator
1941   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1942 static cp_declarator *cp_parser_declarator
1943   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1944 static cp_declarator *cp_parser_direct_declarator
1945   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1946 static enum tree_code cp_parser_ptr_operator
1947   (cp_parser *, tree *, cp_cv_quals *);
1948 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1949   (cp_parser *);
1950 static tree cp_parser_late_return_type_opt
1951   (cp_parser *);
1952 static tree cp_parser_declarator_id
1953   (cp_parser *, bool);
1954 static tree cp_parser_type_id
1955   (cp_parser *);
1956 static tree cp_parser_template_type_arg
1957   (cp_parser *);
1958 static tree cp_parser_trailing_type_id (cp_parser *);
1959 static tree cp_parser_type_id_1
1960   (cp_parser *, bool, bool);
1961 static void cp_parser_type_specifier_seq
1962   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1963 static tree cp_parser_parameter_declaration_clause
1964   (cp_parser *);
1965 static tree cp_parser_parameter_declaration_list
1966   (cp_parser *, bool *);
1967 static cp_parameter_declarator *cp_parser_parameter_declaration
1968   (cp_parser *, bool, bool *);
1969 static tree cp_parser_default_argument 
1970   (cp_parser *, bool);
1971 static void cp_parser_function_body
1972   (cp_parser *);
1973 static tree cp_parser_initializer
1974   (cp_parser *, bool *, bool *);
1975 static tree cp_parser_initializer_clause
1976   (cp_parser *, bool *);
1977 static tree cp_parser_braced_list
1978   (cp_parser*, bool*);
1979 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1980   (cp_parser *, bool *);
1981
1982 static bool cp_parser_ctor_initializer_opt_and_function_body
1983   (cp_parser *);
1984
1985 /* Classes [gram.class] */
1986
1987 static tree cp_parser_class_name
1988   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1989 static tree cp_parser_class_specifier
1990   (cp_parser *);
1991 static tree cp_parser_class_head
1992   (cp_parser *, bool *, tree *, tree *);
1993 static enum tag_types cp_parser_class_key
1994   (cp_parser *);
1995 static void cp_parser_member_specification_opt
1996   (cp_parser *);
1997 static void cp_parser_member_declaration
1998   (cp_parser *);
1999 static tree cp_parser_pure_specifier
2000   (cp_parser *);
2001 static tree cp_parser_constant_initializer
2002   (cp_parser *);
2003
2004 /* Derived classes [gram.class.derived] */
2005
2006 static tree cp_parser_base_clause
2007   (cp_parser *);
2008 static tree cp_parser_base_specifier
2009   (cp_parser *);
2010
2011 /* Special member functions [gram.special] */
2012
2013 static tree cp_parser_conversion_function_id
2014   (cp_parser *);
2015 static tree cp_parser_conversion_type_id
2016   (cp_parser *);
2017 static cp_declarator *cp_parser_conversion_declarator_opt
2018   (cp_parser *);
2019 static bool cp_parser_ctor_initializer_opt
2020   (cp_parser *);
2021 static void cp_parser_mem_initializer_list
2022   (cp_parser *);
2023 static tree cp_parser_mem_initializer
2024   (cp_parser *);
2025 static tree cp_parser_mem_initializer_id
2026   (cp_parser *);
2027
2028 /* Overloading [gram.over] */
2029
2030 static tree cp_parser_operator_function_id
2031   (cp_parser *);
2032 static tree cp_parser_operator
2033   (cp_parser *);
2034
2035 /* Templates [gram.temp] */
2036
2037 static void cp_parser_template_declaration
2038   (cp_parser *, bool);
2039 static tree cp_parser_template_parameter_list
2040   (cp_parser *);
2041 static tree cp_parser_template_parameter
2042   (cp_parser *, bool *, bool *);
2043 static tree cp_parser_type_parameter
2044   (cp_parser *, bool *);
2045 static tree cp_parser_template_id
2046   (cp_parser *, bool, bool, bool);
2047 static tree cp_parser_template_name
2048   (cp_parser *, bool, bool, bool, bool *);
2049 static tree cp_parser_template_argument_list
2050   (cp_parser *);
2051 static tree cp_parser_template_argument
2052   (cp_parser *);
2053 static void cp_parser_explicit_instantiation
2054   (cp_parser *);
2055 static void cp_parser_explicit_specialization
2056   (cp_parser *);
2057
2058 /* Exception handling [gram.exception] */
2059
2060 static tree cp_parser_try_block
2061   (cp_parser *);
2062 static bool cp_parser_function_try_block
2063   (cp_parser *);
2064 static void cp_parser_handler_seq
2065   (cp_parser *);
2066 static void cp_parser_handler
2067   (cp_parser *);
2068 static tree cp_parser_exception_declaration
2069   (cp_parser *);
2070 static tree cp_parser_throw_expression
2071   (cp_parser *);
2072 static tree cp_parser_exception_specification_opt
2073   (cp_parser *);
2074 static tree cp_parser_type_id_list
2075   (cp_parser *);
2076
2077 /* GNU Extensions */
2078
2079 static tree cp_parser_asm_specification_opt
2080   (cp_parser *);
2081 static tree cp_parser_asm_operand_list
2082   (cp_parser *);
2083 static tree cp_parser_asm_clobber_list
2084   (cp_parser *);
2085 static tree cp_parser_asm_label_list
2086   (cp_parser *);
2087 static tree cp_parser_attributes_opt
2088   (cp_parser *);
2089 static tree cp_parser_attribute_list
2090   (cp_parser *);
2091 static bool cp_parser_extension_opt
2092   (cp_parser *, int *);
2093 static void cp_parser_label_declaration
2094   (cp_parser *);
2095
2096 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2097 static bool cp_parser_pragma
2098   (cp_parser *, enum pragma_context);
2099
2100 /* Objective-C++ Productions */
2101
2102 static tree cp_parser_objc_message_receiver
2103   (cp_parser *);
2104 static tree cp_parser_objc_message_args
2105   (cp_parser *);
2106 static tree cp_parser_objc_message_expression
2107   (cp_parser *);
2108 static tree cp_parser_objc_encode_expression
2109   (cp_parser *);
2110 static tree cp_parser_objc_defs_expression
2111   (cp_parser *);
2112 static tree cp_parser_objc_protocol_expression
2113   (cp_parser *);
2114 static tree cp_parser_objc_selector_expression
2115   (cp_parser *);
2116 static tree cp_parser_objc_expression
2117   (cp_parser *);
2118 static bool cp_parser_objc_selector_p
2119   (enum cpp_ttype);
2120 static tree cp_parser_objc_selector
2121   (cp_parser *);
2122 static tree cp_parser_objc_protocol_refs_opt
2123   (cp_parser *);
2124 static void cp_parser_objc_declaration
2125   (cp_parser *, tree);
2126 static tree cp_parser_objc_statement
2127   (cp_parser *);
2128 static bool cp_parser_objc_valid_prefix_attributes
2129   (cp_parser *, tree *);
2130 static void cp_parser_objc_at_property_declaration 
2131   (cp_parser *) ;
2132 static void cp_parser_objc_at_synthesize_declaration 
2133   (cp_parser *) ;
2134 static void cp_parser_objc_at_dynamic_declaration
2135   (cp_parser *) ;
2136 static tree cp_parser_objc_struct_declaration
2137   (cp_parser *) ;
2138
2139 /* Utility Routines */
2140
2141 static tree cp_parser_lookup_name
2142   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2143 static tree cp_parser_lookup_name_simple
2144   (cp_parser *, tree, location_t);
2145 static tree cp_parser_maybe_treat_template_as_class
2146   (tree, bool);
2147 static bool cp_parser_check_declarator_template_parameters
2148   (cp_parser *, cp_declarator *, location_t);
2149 static bool cp_parser_check_template_parameters
2150   (cp_parser *, unsigned, location_t, cp_declarator *);
2151 static tree cp_parser_simple_cast_expression
2152   (cp_parser *);
2153 static tree cp_parser_global_scope_opt
2154   (cp_parser *, bool);
2155 static bool cp_parser_constructor_declarator_p
2156   (cp_parser *, bool);
2157 static tree cp_parser_function_definition_from_specifiers_and_declarator
2158   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2159 static tree cp_parser_function_definition_after_declarator
2160   (cp_parser *, bool);
2161 static void cp_parser_template_declaration_after_export
2162   (cp_parser *, bool);
2163 static void cp_parser_perform_template_parameter_access_checks
2164   (VEC (deferred_access_check,gc)*);
2165 static tree cp_parser_single_declaration
2166   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2167 static tree cp_parser_functional_cast
2168   (cp_parser *, tree);
2169 static tree cp_parser_save_member_function_body
2170   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2171 static tree cp_parser_enclosed_template_argument_list
2172   (cp_parser *);
2173 static void cp_parser_save_default_args
2174   (cp_parser *, tree);
2175 static void cp_parser_late_parsing_for_member
2176   (cp_parser *, tree);
2177 static void cp_parser_late_parsing_default_args
2178   (cp_parser *, tree);
2179 static tree cp_parser_sizeof_operand
2180   (cp_parser *, enum rid);
2181 static tree cp_parser_trait_expr
2182   (cp_parser *, enum rid);
2183 static bool cp_parser_declares_only_class_p
2184   (cp_parser *);
2185 static void cp_parser_set_storage_class
2186   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2187 static void cp_parser_set_decl_spec_type
2188   (cp_decl_specifier_seq *, tree, location_t, bool);
2189 static bool cp_parser_friend_p
2190   (const cp_decl_specifier_seq *);
2191 static void cp_parser_required_error
2192   (cp_parser *, required_token, bool);
2193 static cp_token *cp_parser_require
2194   (cp_parser *, enum cpp_ttype, required_token);
2195 static cp_token *cp_parser_require_keyword
2196   (cp_parser *, enum rid, required_token);
2197 static bool cp_parser_token_starts_function_definition_p
2198   (cp_token *);
2199 static bool cp_parser_next_token_starts_class_definition_p
2200   (cp_parser *);
2201 static bool cp_parser_next_token_ends_template_argument_p
2202   (cp_parser *);
2203 static bool cp_parser_nth_token_starts_template_argument_list_p
2204   (cp_parser *, size_t);
2205 static enum tag_types cp_parser_token_is_class_key
2206   (cp_token *);
2207 static void cp_parser_check_class_key
2208   (enum tag_types, tree type);
2209 static void cp_parser_check_access_in_redeclaration
2210   (tree type, location_t location);
2211 static bool cp_parser_optional_template_keyword
2212   (cp_parser *);
2213 static void cp_parser_pre_parsed_nested_name_specifier
2214   (cp_parser *);
2215 static bool cp_parser_cache_group
2216   (cp_parser *, enum cpp_ttype, unsigned);
2217 static void cp_parser_parse_tentatively
2218   (cp_parser *);
2219 static void cp_parser_commit_to_tentative_parse
2220   (cp_parser *);
2221 static void cp_parser_abort_tentative_parse
2222   (cp_parser *);
2223 static bool cp_parser_parse_definitely
2224   (cp_parser *);
2225 static inline bool cp_parser_parsing_tentatively
2226   (cp_parser *);
2227 static bool cp_parser_uncommitted_to_tentative_parse_p
2228   (cp_parser *);
2229 static void cp_parser_error
2230   (cp_parser *, const char *);
2231 static void cp_parser_name_lookup_error
2232   (cp_parser *, tree, tree, name_lookup_error, location_t);
2233 static bool cp_parser_simulate_error
2234   (cp_parser *);
2235 static bool cp_parser_check_type_definition
2236   (cp_parser *);
2237 static void cp_parser_check_for_definition_in_return_type
2238   (cp_declarator *, tree, location_t type_location);
2239 static void cp_parser_check_for_invalid_template_id
2240   (cp_parser *, tree, location_t location);
2241 static bool cp_parser_non_integral_constant_expression
2242   (cp_parser *, non_integral_constant);
2243 static void cp_parser_diagnose_invalid_type_name
2244   (cp_parser *, tree, tree, location_t);
2245 static bool cp_parser_parse_and_diagnose_invalid_type_name
2246   (cp_parser *);
2247 static int cp_parser_skip_to_closing_parenthesis
2248   (cp_parser *, bool, bool, bool);
2249 static void cp_parser_skip_to_end_of_statement
2250   (cp_parser *);
2251 static void cp_parser_consume_semicolon_at_end_of_statement
2252   (cp_parser *);
2253 static void cp_parser_skip_to_end_of_block_or_statement
2254   (cp_parser *);
2255 static bool cp_parser_skip_to_closing_brace
2256   (cp_parser *);
2257 static void cp_parser_skip_to_end_of_template_parameter_list
2258   (cp_parser *);
2259 static void cp_parser_skip_to_pragma_eol
2260   (cp_parser*, cp_token *);
2261 static bool cp_parser_error_occurred
2262   (cp_parser *);
2263 static bool cp_parser_allow_gnu_extensions_p
2264   (cp_parser *);
2265 static bool cp_parser_is_string_literal
2266   (cp_token *);
2267 static bool cp_parser_is_keyword
2268   (cp_token *, enum rid);
2269 static tree cp_parser_make_typename_type
2270   (cp_parser *, tree, tree, location_t location);
2271 static cp_declarator * cp_parser_make_indirect_declarator
2272   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2273
2274 /* Returns nonzero if we are parsing tentatively.  */
2275
2276 static inline bool
2277 cp_parser_parsing_tentatively (cp_parser* parser)
2278 {
2279   return parser->context->next != NULL;
2280 }
2281
2282 /* Returns nonzero if TOKEN is a string literal.  */
2283
2284 static bool
2285 cp_parser_is_string_literal (cp_token* token)
2286 {
2287   return (token->type == CPP_STRING ||
2288           token->type == CPP_STRING16 ||
2289           token->type == CPP_STRING32 ||
2290           token->type == CPP_WSTRING ||
2291           token->type == CPP_UTF8STRING);
2292 }
2293
2294 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2295
2296 static bool
2297 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2298 {
2299   return token->keyword == keyword;
2300 }
2301
2302 /* If not parsing tentatively, issue a diagnostic of the form
2303       FILE:LINE: MESSAGE before TOKEN
2304    where TOKEN is the next token in the input stream.  MESSAGE
2305    (specified by the caller) is usually of the form "expected
2306    OTHER-TOKEN".  */
2307
2308 static void
2309 cp_parser_error (cp_parser* parser, const char* gmsgid)
2310 {
2311   if (!cp_parser_simulate_error (parser))
2312     {
2313       cp_token *token = cp_lexer_peek_token (parser->lexer);
2314       /* This diagnostic makes more sense if it is tagged to the line
2315          of the token we just peeked at.  */
2316       cp_lexer_set_source_position_from_token (token);
2317
2318       if (token->type == CPP_PRAGMA)
2319         {
2320           error_at (token->location,
2321                     "%<#pragma%> is not allowed here");
2322           cp_parser_skip_to_pragma_eol (parser, token);
2323           return;
2324         }
2325
2326       c_parse_error (gmsgid,
2327                      /* Because c_parser_error does not understand
2328                         CPP_KEYWORD, keywords are treated like
2329                         identifiers.  */
2330                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2331                      token->u.value, token->flags);
2332     }
2333 }
2334
2335 /* Issue an error about name-lookup failing.  NAME is the
2336    IDENTIFIER_NODE DECL is the result of
2337    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2338    the thing that we hoped to find.  */
2339
2340 static void
2341 cp_parser_name_lookup_error (cp_parser* parser,
2342                              tree name,
2343                              tree decl,
2344                              name_lookup_error desired,
2345                              location_t location)
2346 {
2347   /* If name lookup completely failed, tell the user that NAME was not
2348      declared.  */
2349   if (decl == error_mark_node)
2350     {
2351       if (parser->scope && parser->scope != global_namespace)
2352         error_at (location, "%<%E::%E%> has not been declared",
2353                   parser->scope, name);
2354       else if (parser->scope == global_namespace)
2355         error_at (location, "%<::%E%> has not been declared", name);
2356       else if (parser->object_scope
2357                && !CLASS_TYPE_P (parser->object_scope))
2358         error_at (location, "request for member %qE in non-class type %qT",
2359                   name, parser->object_scope);
2360       else if (parser->object_scope)
2361         error_at (location, "%<%T::%E%> has not been declared",
2362                   parser->object_scope, name);
2363       else
2364         error_at (location, "%qE has not been declared", name);
2365     }
2366   else if (parser->scope && parser->scope != global_namespace)
2367     {
2368       switch (desired)
2369         {
2370           case NLE_TYPE:
2371             error_at (location, "%<%E::%E%> is not a type",
2372                                 parser->scope, name);
2373             break;
2374           case NLE_CXX98:
2375             error_at (location, "%<%E::%E%> is not a class or namespace",
2376                                 parser->scope, name);
2377             break;
2378           case NLE_NOT_CXX98:
2379             error_at (location,
2380                       "%<%E::%E%> is not a class, namespace, or enumeration",
2381                       parser->scope, name);
2382             break;
2383           default:
2384             gcc_unreachable ();
2385             
2386         }
2387     }
2388   else if (parser->scope == global_namespace)
2389     {
2390       switch (desired)
2391         {
2392           case NLE_TYPE:
2393             error_at (location, "%<::%E%> is not a type", name);
2394             break;
2395           case NLE_CXX98:
2396             error_at (location, "%<::%E%> is not a class or namespace", name);
2397             break;
2398           case NLE_NOT_CXX98:
2399             error_at (location,
2400                       "%<::%E%> is not a class, namespace, or enumeration",
2401                       name);
2402             break;
2403           default:
2404             gcc_unreachable ();
2405         }
2406     }
2407   else
2408     {
2409       switch (desired)
2410         {
2411           case NLE_TYPE:
2412             error_at (location, "%qE is not a type", name);
2413             break;
2414           case NLE_CXX98:
2415             error_at (location, "%qE is not a class or namespace", name);
2416             break;
2417           case NLE_NOT_CXX98:
2418             error_at (location,
2419                       "%qE is not a class, namespace, or enumeration", name);
2420             break;
2421           default:
2422             gcc_unreachable ();
2423         }
2424     }
2425 }
2426
2427 /* If we are parsing tentatively, remember that an error has occurred
2428    during this tentative parse.  Returns true if the error was
2429    simulated; false if a message should be issued by the caller.  */
2430
2431 static bool
2432 cp_parser_simulate_error (cp_parser* parser)
2433 {
2434   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2435     {
2436       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2437       return true;
2438     }
2439   return false;
2440 }
2441
2442 /* Check for repeated decl-specifiers.  */
2443
2444 static void
2445 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2446                            location_t location)
2447 {
2448   int ds;
2449
2450   for (ds = ds_first; ds != ds_last; ++ds)
2451     {
2452       unsigned count = decl_specs->specs[ds];
2453       if (count < 2)
2454         continue;
2455       /* The "long" specifier is a special case because of "long long".  */
2456       if (ds == ds_long)
2457         {
2458           if (count > 2)
2459             error_at (location, "%<long long long%> is too long for GCC");
2460           else 
2461             pedwarn_cxx98 (location, OPT_Wlong_long, 
2462                            "ISO C++ 1998 does not support %<long long%>");
2463         }
2464       else if (count > 1)
2465         {
2466           static const char *const decl_spec_names[] = {
2467             "signed",
2468             "unsigned",
2469             "short",
2470             "long",
2471             "const",
2472             "volatile",
2473             "restrict",
2474             "inline",
2475             "virtual",
2476             "explicit",
2477             "friend",
2478             "typedef",
2479             "constexpr",
2480             "__complex",
2481             "__thread"
2482           };
2483           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2484         }
2485     }
2486 }
2487
2488 /* This function is called when a type is defined.  If type
2489    definitions are forbidden at this point, an error message is
2490    issued.  */
2491
2492 static bool
2493 cp_parser_check_type_definition (cp_parser* parser)
2494 {
2495   /* If types are forbidden here, issue a message.  */
2496   if (parser->type_definition_forbidden_message)
2497     {
2498       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2499          in the message need to be interpreted.  */
2500       error (parser->type_definition_forbidden_message);
2501       return false;
2502     }
2503   return true;
2504 }
2505
2506 /* This function is called when the DECLARATOR is processed.  The TYPE
2507    was a type defined in the decl-specifiers.  If it is invalid to
2508    define a type in the decl-specifiers for DECLARATOR, an error is
2509    issued. TYPE_LOCATION is the location of TYPE and is used
2510    for error reporting.  */
2511
2512 static void
2513 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2514                                                tree type, location_t type_location)
2515 {
2516   /* [dcl.fct] forbids type definitions in return types.
2517      Unfortunately, it's not easy to know whether or not we are
2518      processing a return type until after the fact.  */
2519   while (declarator
2520          && (declarator->kind == cdk_pointer
2521              || declarator->kind == cdk_reference
2522              || declarator->kind == cdk_ptrmem))
2523     declarator = declarator->declarator;
2524   if (declarator
2525       && declarator->kind == cdk_function)
2526     {
2527       error_at (type_location,
2528                 "new types may not be defined in a return type");
2529       inform (type_location, 
2530               "(perhaps a semicolon is missing after the definition of %qT)",
2531               type);
2532     }
2533 }
2534
2535 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2536    "<" in any valid C++ program.  If the next token is indeed "<",
2537    issue a message warning the user about what appears to be an
2538    invalid attempt to form a template-id. LOCATION is the location
2539    of the type-specifier (TYPE) */
2540
2541 static void
2542 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2543                                          tree type, location_t location)
2544 {
2545   cp_token_position start = 0;
2546
2547   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2548     {
2549       if (TYPE_P (type))
2550         error_at (location, "%qT is not a template", type);
2551       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2552         error_at (location, "%qE is not a template", type);
2553       else
2554         error_at (location, "invalid template-id");
2555       /* Remember the location of the invalid "<".  */
2556       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2557         start = cp_lexer_token_position (parser->lexer, true);
2558       /* Consume the "<".  */
2559       cp_lexer_consume_token (parser->lexer);
2560       /* Parse the template arguments.  */
2561       cp_parser_enclosed_template_argument_list (parser);
2562       /* Permanently remove the invalid template arguments so that
2563          this error message is not issued again.  */
2564       if (start)
2565         cp_lexer_purge_tokens_after (parser->lexer, start);
2566     }
2567 }
2568
2569 /* If parsing an integral constant-expression, issue an error message
2570    about the fact that THING appeared and return true.  Otherwise,
2571    return false.  In either case, set
2572    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2573
2574 static bool
2575 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2576                                             non_integral_constant thing)
2577 {
2578   parser->non_integral_constant_expression_p = true;
2579   if (parser->integral_constant_expression_p)
2580     {
2581       if (!parser->allow_non_integral_constant_expression_p)
2582         {
2583           const char *msg = NULL;
2584           switch (thing)
2585             {
2586               case NIC_FLOAT:
2587                 error ("floating-point literal "
2588                        "cannot appear in a constant-expression");
2589                 return true;
2590               case NIC_CAST:
2591                 error ("a cast to a type other than an integral or "
2592                        "enumeration type cannot appear in a "
2593                        "constant-expression");
2594                 return true;
2595               case NIC_TYPEID:
2596                 error ("%<typeid%> operator "
2597                        "cannot appear in a constant-expression");
2598                 return true;
2599               case NIC_NCC:
2600                 error ("non-constant compound literals "
2601                        "cannot appear in a constant-expression");
2602                 return true;
2603               case NIC_FUNC_CALL:
2604                 error ("a function call "
2605                        "cannot appear in a constant-expression");
2606                 return true;
2607               case NIC_INC:
2608                 error ("an increment "
2609                        "cannot appear in a constant-expression");
2610                 return true;
2611               case NIC_DEC:
2612                 error ("an decrement "
2613                        "cannot appear in a constant-expression");
2614                 return true;
2615               case NIC_ARRAY_REF:
2616                 error ("an array reference "
2617                        "cannot appear in a constant-expression");
2618                 return true;
2619               case NIC_ADDR_LABEL:
2620                 error ("the address of a label "
2621                        "cannot appear in a constant-expression");
2622                 return true;
2623               case NIC_OVERLOADED:
2624                 error ("calls to overloaded operators "
2625                        "cannot appear in a constant-expression");
2626                 return true;
2627               case NIC_ASSIGNMENT:
2628                 error ("an assignment cannot appear in a constant-expression");
2629                 return true;
2630               case NIC_COMMA:
2631                 error ("a comma operator "
2632                        "cannot appear in a constant-expression");
2633                 return true;
2634               case NIC_CONSTRUCTOR:
2635                 error ("a call to a constructor "
2636                        "cannot appear in a constant-expression");
2637                 return true;
2638               case NIC_THIS:
2639                 msg = "this";
2640                 break;
2641               case NIC_FUNC_NAME:
2642                 msg = "__FUNCTION__";
2643                 break;
2644               case NIC_PRETTY_FUNC:
2645                 msg = "__PRETTY_FUNCTION__";
2646                 break;
2647               case NIC_C99_FUNC:
2648                 msg = "__func__";
2649                 break;
2650               case NIC_VA_ARG:
2651                 msg = "va_arg";
2652                 break;
2653               case NIC_ARROW:
2654                 msg = "->";
2655                 break;
2656               case NIC_POINT:
2657                 msg = ".";
2658                 break;
2659               case NIC_STAR:
2660                 msg = "*";
2661                 break;
2662               case NIC_ADDR:
2663                 msg = "&";
2664                 break;
2665               case NIC_PREINCREMENT:
2666                 msg = "++";
2667                 break;
2668               case NIC_PREDECREMENT:
2669                 msg = "--";
2670                 break;
2671               case NIC_NEW:
2672                 msg = "new";
2673                 break;
2674               case NIC_DEL:
2675                 msg = "delete";
2676                 break;
2677               default:
2678                 gcc_unreachable ();
2679             }
2680           if (msg)
2681             error ("%qs cannot appear in a constant-expression", msg);
2682           return true;
2683         }
2684     }
2685   return false;
2686 }
2687
2688 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2689    qualifying scope (or NULL, if none) for ID.  This function commits
2690    to the current active tentative parse, if any.  (Otherwise, the
2691    problematic construct might be encountered again later, resulting
2692    in duplicate error messages.) LOCATION is the location of ID.  */
2693
2694 static void
2695 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2696                                       tree scope, tree id,
2697                                       location_t location)
2698 {
2699   tree decl, old_scope;
2700   /* Try to lookup the identifier.  */
2701   old_scope = parser->scope;
2702   parser->scope = scope;
2703   decl = cp_parser_lookup_name_simple (parser, id, location);
2704   parser->scope = old_scope;
2705   /* If the lookup found a template-name, it means that the user forgot
2706   to specify an argument list. Emit a useful error message.  */
2707   if (TREE_CODE (decl) == TEMPLATE_DECL)
2708     error_at (location,
2709               "invalid use of template-name %qE without an argument list",
2710               decl);
2711   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2712     error_at (location, "invalid use of destructor %qD as a type", id);
2713   else if (TREE_CODE (decl) == TYPE_DECL)
2714     /* Something like 'unsigned A a;'  */
2715     error_at (location, "invalid combination of multiple type-specifiers");
2716   else if (!parser->scope)
2717     {
2718       /* Issue an error message.  */
2719       error_at (location, "%qE does not name a type", id);
2720       /* If we're in a template class, it's possible that the user was
2721          referring to a type from a base class.  For example:
2722
2723            template <typename T> struct A { typedef T X; };
2724            template <typename T> struct B : public A<T> { X x; };
2725
2726          The user should have said "typename A<T>::X".  */
2727       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2728         inform (location, "C++0x %<constexpr%> only available with "
2729                 "-std=c++0x or -std=gnu++0x");
2730       else if (processing_template_decl && current_class_type
2731                && TYPE_BINFO (current_class_type))
2732         {
2733           tree b;
2734
2735           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2736                b;
2737                b = TREE_CHAIN (b))
2738             {
2739               tree base_type = BINFO_TYPE (b);
2740               if (CLASS_TYPE_P (base_type)
2741                   && dependent_type_p (base_type))
2742                 {
2743                   tree field;
2744                   /* Go from a particular instantiation of the
2745                      template (which will have an empty TYPE_FIELDs),
2746                      to the main version.  */
2747                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2748                   for (field = TYPE_FIELDS (base_type);
2749                        field;
2750                        field = DECL_CHAIN (field))
2751                     if (TREE_CODE (field) == TYPE_DECL
2752                         && DECL_NAME (field) == id)
2753                       {
2754                         inform (location, 
2755                                 "(perhaps %<typename %T::%E%> was intended)",
2756                                 BINFO_TYPE (b), id);
2757                         break;
2758                       }
2759                   if (field)
2760                     break;
2761                 }
2762             }
2763         }
2764     }
2765   /* Here we diagnose qualified-ids where the scope is actually correct,
2766      but the identifier does not resolve to a valid type name.  */
2767   else if (parser->scope != error_mark_node)
2768     {
2769       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2770         error_at (location, "%qE in namespace %qE does not name a type",
2771                   id, parser->scope);
2772       else if (CLASS_TYPE_P (parser->scope)
2773                && constructor_name_p (id, parser->scope))
2774         {
2775           /* A<T>::A<T>() */
2776           error_at (location, "%<%T::%E%> names the constructor, not"
2777                     " the type", parser->scope, id);
2778           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2779             error_at (location, "and %qT has no template constructors",
2780                       parser->scope);
2781         }
2782       else if (TYPE_P (parser->scope)
2783                && dependent_scope_p (parser->scope))
2784         error_at (location, "need %<typename%> before %<%T::%E%> because "
2785                   "%qT is a dependent scope",
2786                   parser->scope, id, parser->scope);
2787       else if (TYPE_P (parser->scope))
2788         error_at (location, "%qE in class %qT does not name a type",
2789                   id, parser->scope);
2790       else
2791         gcc_unreachable ();
2792     }
2793   cp_parser_commit_to_tentative_parse (parser);
2794 }
2795
2796 /* Check for a common situation where a type-name should be present,
2797    but is not, and issue a sensible error message.  Returns true if an
2798    invalid type-name was detected.
2799
2800    The situation handled by this function are variable declarations of the
2801    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2802    Usually, `ID' should name a type, but if we got here it means that it
2803    does not. We try to emit the best possible error message depending on
2804    how exactly the id-expression looks like.  */
2805
2806 static bool
2807 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2808 {
2809   tree id;
2810   cp_token *token = cp_lexer_peek_token (parser->lexer);
2811
2812   /* Avoid duplicate error about ambiguous lookup.  */
2813   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2814     {
2815       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2816       if (next->type == CPP_NAME && next->ambiguous_p)
2817         goto out;
2818     }
2819
2820   cp_parser_parse_tentatively (parser);
2821   id = cp_parser_id_expression (parser,
2822                                 /*template_keyword_p=*/false,
2823                                 /*check_dependency_p=*/true,
2824                                 /*template_p=*/NULL,
2825                                 /*declarator_p=*/true,
2826                                 /*optional_p=*/false);
2827   /* If the next token is a (, this is a function with no explicit return
2828      type, i.e. constructor, destructor or conversion op.  */
2829   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2830       || TREE_CODE (id) == TYPE_DECL)
2831     {
2832       cp_parser_abort_tentative_parse (parser);
2833       return false;
2834     }
2835   if (!cp_parser_parse_definitely (parser))
2836     return false;
2837
2838   /* Emit a diagnostic for the invalid type.  */
2839   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2840                                         id, token->location);
2841  out:
2842   /* If we aren't in the middle of a declarator (i.e. in a
2843      parameter-declaration-clause), skip to the end of the declaration;
2844      there's no point in trying to process it.  */
2845   if (!parser->in_declarator_p)
2846     cp_parser_skip_to_end_of_block_or_statement (parser);
2847   return true;
2848 }
2849
2850 /* Consume tokens up to, and including, the next non-nested closing `)'.
2851    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2852    are doing error recovery. Returns -1 if OR_COMMA is true and we
2853    found an unnested comma.  */
2854
2855 static int
2856 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2857                                        bool recovering,
2858                                        bool or_comma,
2859                                        bool consume_paren)
2860 {
2861   unsigned paren_depth = 0;
2862   unsigned brace_depth = 0;
2863   unsigned square_depth = 0;
2864
2865   if (recovering && !or_comma
2866       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2867     return 0;
2868
2869   while (true)
2870     {
2871       cp_token * token = cp_lexer_peek_token (parser->lexer);
2872
2873       switch (token->type)
2874         {
2875         case CPP_EOF:
2876         case CPP_PRAGMA_EOL:
2877           /* If we've run out of tokens, then there is no closing `)'.  */
2878           return 0;
2879
2880         /* This is good for lambda expression capture-lists.  */
2881         case CPP_OPEN_SQUARE:
2882           ++square_depth;
2883           break;
2884         case CPP_CLOSE_SQUARE:
2885           if (!square_depth--)
2886             return 0;
2887           break;
2888
2889         case CPP_SEMICOLON:
2890           /* This matches the processing in skip_to_end_of_statement.  */
2891           if (!brace_depth)
2892             return 0;
2893           break;
2894
2895         case CPP_OPEN_BRACE:
2896           ++brace_depth;
2897           break;
2898         case CPP_CLOSE_BRACE:
2899           if (!brace_depth--)
2900             return 0;
2901           break;
2902
2903         case CPP_COMMA:
2904           if (recovering && or_comma && !brace_depth && !paren_depth
2905               && !square_depth)
2906             return -1;
2907           break;
2908
2909         case CPP_OPEN_PAREN:
2910           if (!brace_depth)
2911             ++paren_depth;
2912           break;
2913
2914         case CPP_CLOSE_PAREN:
2915           if (!brace_depth && !paren_depth--)
2916             {
2917               if (consume_paren)
2918                 cp_lexer_consume_token (parser->lexer);
2919               return 1;
2920             }
2921           break;
2922
2923         default:
2924           break;
2925         }
2926
2927       /* Consume the token.  */
2928       cp_lexer_consume_token (parser->lexer);
2929     }
2930 }
2931
2932 /* Consume tokens until we reach the end of the current statement.
2933    Normally, that will be just before consuming a `;'.  However, if a
2934    non-nested `}' comes first, then we stop before consuming that.  */
2935
2936 static void
2937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2938 {
2939   unsigned nesting_depth = 0;
2940
2941   while (true)
2942     {
2943       cp_token *token = cp_lexer_peek_token (parser->lexer);
2944
2945       switch (token->type)
2946         {
2947         case CPP_EOF:
2948         case CPP_PRAGMA_EOL:
2949           /* If we've run out of tokens, stop.  */
2950           return;
2951
2952         case CPP_SEMICOLON:
2953           /* If the next token is a `;', we have reached the end of the
2954              statement.  */
2955           if (!nesting_depth)
2956             return;
2957           break;
2958
2959         case CPP_CLOSE_BRACE:
2960           /* If this is a non-nested '}', stop before consuming it.
2961              That way, when confronted with something like:
2962
2963                { 3 + }
2964
2965              we stop before consuming the closing '}', even though we
2966              have not yet reached a `;'.  */
2967           if (nesting_depth == 0)
2968             return;
2969
2970           /* If it is the closing '}' for a block that we have
2971              scanned, stop -- but only after consuming the token.
2972              That way given:
2973
2974                 void f g () { ... }
2975                 typedef int I;
2976
2977              we will stop after the body of the erroneously declared
2978              function, but before consuming the following `typedef'
2979              declaration.  */
2980           if (--nesting_depth == 0)
2981             {
2982               cp_lexer_consume_token (parser->lexer);
2983               return;
2984             }
2985
2986         case CPP_OPEN_BRACE:
2987           ++nesting_depth;
2988           break;
2989
2990         default:
2991           break;
2992         }
2993
2994       /* Consume the token.  */
2995       cp_lexer_consume_token (parser->lexer);
2996     }
2997 }
2998
2999 /* This function is called at the end of a statement or declaration.
3000    If the next token is a semicolon, it is consumed; otherwise, error
3001    recovery is attempted.  */
3002
3003 static void
3004 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3005 {
3006   /* Look for the trailing `;'.  */
3007   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3008     {
3009       /* If there is additional (erroneous) input, skip to the end of
3010          the statement.  */
3011       cp_parser_skip_to_end_of_statement (parser);
3012       /* If the next token is now a `;', consume it.  */
3013       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3014         cp_lexer_consume_token (parser->lexer);
3015     }
3016 }
3017
3018 /* Skip tokens until we have consumed an entire block, or until we
3019    have consumed a non-nested `;'.  */
3020
3021 static void
3022 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3023 {
3024   int nesting_depth = 0;
3025
3026   while (nesting_depth >= 0)
3027     {
3028       cp_token *token = cp_lexer_peek_token (parser->lexer);
3029
3030       switch (token->type)
3031         {
3032         case CPP_EOF:
3033         case CPP_PRAGMA_EOL:
3034           /* If we've run out of tokens, stop.  */
3035           return;
3036
3037         case CPP_SEMICOLON:
3038           /* Stop if this is an unnested ';'. */
3039           if (!nesting_depth)
3040             nesting_depth = -1;
3041           break;
3042
3043         case CPP_CLOSE_BRACE:
3044           /* Stop if this is an unnested '}', or closes the outermost
3045              nesting level.  */
3046           nesting_depth--;
3047           if (nesting_depth < 0)
3048             return;
3049           if (!nesting_depth)
3050             nesting_depth = -1;
3051           break;
3052
3053         case CPP_OPEN_BRACE:
3054           /* Nest. */
3055           nesting_depth++;
3056           break;
3057
3058         default:
3059           break;
3060         }
3061
3062       /* Consume the token.  */
3063       cp_lexer_consume_token (parser->lexer);
3064     }
3065 }
3066
3067 /* Skip tokens until a non-nested closing curly brace is the next
3068    token, or there are no more tokens. Return true in the first case,
3069    false otherwise.  */
3070
3071 static bool
3072 cp_parser_skip_to_closing_brace (cp_parser *parser)
3073 {
3074   unsigned nesting_depth = 0;
3075
3076   while (true)
3077     {
3078       cp_token *token = cp_lexer_peek_token (parser->lexer);
3079
3080       switch (token->type)
3081         {
3082         case CPP_EOF:
3083         case CPP_PRAGMA_EOL:
3084           /* If we've run out of tokens, stop.  */
3085           return false;
3086
3087         case CPP_CLOSE_BRACE:
3088           /* If the next token is a non-nested `}', then we have reached
3089              the end of the current block.  */
3090           if (nesting_depth-- == 0)
3091             return true;
3092           break;
3093
3094         case CPP_OPEN_BRACE:
3095           /* If it the next token is a `{', then we are entering a new
3096              block.  Consume the entire block.  */
3097           ++nesting_depth;
3098           break;
3099
3100         default:
3101           break;
3102         }
3103
3104       /* Consume the token.  */
3105       cp_lexer_consume_token (parser->lexer);
3106     }
3107 }
3108
3109 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3110    parameter is the PRAGMA token, allowing us to purge the entire pragma
3111    sequence.  */
3112
3113 static void
3114 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3115 {
3116   cp_token *token;
3117
3118   parser->lexer->in_pragma = false;
3119
3120   do
3121     token = cp_lexer_consume_token (parser->lexer);
3122   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3123
3124   /* Ensure that the pragma is not parsed again.  */
3125   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3126 }
3127
3128 /* Require pragma end of line, resyncing with it as necessary.  The
3129    arguments are as for cp_parser_skip_to_pragma_eol.  */
3130
3131 static void
3132 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3133 {
3134   parser->lexer->in_pragma = false;
3135   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3136     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3137 }
3138
3139 /* This is a simple wrapper around make_typename_type. When the id is
3140    an unresolved identifier node, we can provide a superior diagnostic
3141    using cp_parser_diagnose_invalid_type_name.  */
3142
3143 static tree
3144 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3145                               tree id, location_t id_location)
3146 {
3147   tree result;
3148   if (TREE_CODE (id) == IDENTIFIER_NODE)
3149     {
3150       result = make_typename_type (scope, id, typename_type,
3151                                    /*complain=*/tf_none);
3152       if (result == error_mark_node)
3153         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3154       return result;
3155     }
3156   return make_typename_type (scope, id, typename_type, tf_error);
3157 }
3158
3159 /* This is a wrapper around the
3160    make_{pointer,ptrmem,reference}_declarator functions that decides
3161    which one to call based on the CODE and CLASS_TYPE arguments. The
3162    CODE argument should be one of the values returned by
3163    cp_parser_ptr_operator. */
3164 static cp_declarator *
3165 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3166                                     cp_cv_quals cv_qualifiers,
3167                                     cp_declarator *target)
3168 {
3169   if (code == ERROR_MARK)
3170     return cp_error_declarator;
3171
3172   if (code == INDIRECT_REF)
3173     if (class_type == NULL_TREE)
3174       return make_pointer_declarator (cv_qualifiers, target);
3175     else
3176       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3177   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3178     return make_reference_declarator (cv_qualifiers, target, false);
3179   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3180     return make_reference_declarator (cv_qualifiers, target, true);
3181   gcc_unreachable ();
3182 }
3183
3184 /* Create a new C++ parser.  */
3185
3186 static cp_parser *
3187 cp_parser_new (void)
3188 {
3189   cp_parser *parser;
3190   cp_lexer *lexer;
3191   unsigned i;
3192
3193   /* cp_lexer_new_main is called before doing GC allocation because
3194      cp_lexer_new_main might load a PCH file.  */
3195   lexer = cp_lexer_new_main ();
3196
3197   /* Initialize the binops_by_token so that we can get the tree
3198      directly from the token.  */
3199   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3200     binops_by_token[binops[i].token_type] = binops[i];
3201
3202   parser = ggc_alloc_cleared_cp_parser ();
3203   parser->lexer = lexer;
3204   parser->context = cp_parser_context_new (NULL);
3205
3206   /* For now, we always accept GNU extensions.  */
3207   parser->allow_gnu_extensions_p = 1;
3208
3209   /* The `>' token is a greater-than operator, not the end of a
3210      template-id.  */
3211   parser->greater_than_is_operator_p = true;
3212
3213   parser->default_arg_ok_p = true;
3214
3215   /* We are not parsing a constant-expression.  */
3216   parser->integral_constant_expression_p = false;
3217   parser->allow_non_integral_constant_expression_p = false;
3218   parser->non_integral_constant_expression_p = false;
3219
3220   /* Local variable names are not forbidden.  */
3221   parser->local_variables_forbidden_p = false;
3222
3223   /* We are not processing an `extern "C"' declaration.  */
3224   parser->in_unbraced_linkage_specification_p = false;
3225
3226   /* We are not processing a declarator.  */
3227   parser->in_declarator_p = false;
3228
3229   /* We are not processing a template-argument-list.  */
3230   parser->in_template_argument_list_p = false;
3231
3232   /* We are not in an iteration statement.  */
3233   parser->in_statement = 0;
3234
3235   /* We are not in a switch statement.  */
3236   parser->in_switch_statement_p = false;
3237
3238   /* We are not parsing a type-id inside an expression.  */
3239   parser->in_type_id_in_expr_p = false;
3240
3241   /* Declarations aren't implicitly extern "C".  */
3242   parser->implicit_extern_c = false;
3243
3244   /* String literals should be translated to the execution character set.  */
3245   parser->translate_strings_p = true;
3246
3247   /* We are not parsing a function body.  */
3248   parser->in_function_body = false;
3249
3250   /* We can correct until told otherwise.  */
3251   parser->colon_corrects_to_scope_p = true;
3252
3253   /* The unparsed function queue is empty.  */
3254   push_unparsed_function_queues (parser);
3255
3256   /* There are no classes being defined.  */
3257   parser->num_classes_being_defined = 0;
3258
3259   /* No template parameters apply.  */
3260   parser->num_template_parameter_lists = 0;
3261
3262   return parser;
3263 }
3264
3265 /* Create a cp_lexer structure which will emit the tokens in CACHE
3266    and push it onto the parser's lexer stack.  This is used for delayed
3267    parsing of in-class method bodies and default arguments, and should
3268    not be confused with tentative parsing.  */
3269 static void
3270 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3271 {
3272   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3273   lexer->next = parser->lexer;
3274   parser->lexer = lexer;
3275
3276   /* Move the current source position to that of the first token in the
3277      new lexer.  */
3278   cp_lexer_set_source_position_from_token (lexer->next_token);
3279 }
3280
3281 /* Pop the top lexer off the parser stack.  This is never used for the
3282    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3283 static void
3284 cp_parser_pop_lexer (cp_parser *parser)
3285 {
3286   cp_lexer *lexer = parser->lexer;
3287   parser->lexer = lexer->next;
3288   cp_lexer_destroy (lexer);
3289
3290   /* Put the current source position back where it was before this
3291      lexer was pushed.  */
3292   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3293 }
3294
3295 /* Lexical conventions [gram.lex]  */
3296
3297 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3298    identifier.  */
3299
3300 static tree
3301 cp_parser_identifier (cp_parser* parser)
3302 {
3303   cp_token *token;
3304
3305   /* Look for the identifier.  */
3306   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3307   /* Return the value.  */
3308   return token ? token->u.value : error_mark_node;
3309 }
3310
3311 /* Parse a sequence of adjacent string constants.  Returns a
3312    TREE_STRING representing the combined, nul-terminated string
3313    constant.  If TRANSLATE is true, translate the string to the
3314    execution character set.  If WIDE_OK is true, a wide string is
3315    invalid here.
3316
3317    C++98 [lex.string] says that if a narrow string literal token is
3318    adjacent to a wide string literal token, the behavior is undefined.
3319    However, C99 6.4.5p4 says that this results in a wide string literal.
3320    We follow C99 here, for consistency with the C front end.
3321
3322    This code is largely lifted from lex_string() in c-lex.c.
3323
3324    FUTURE: ObjC++ will need to handle @-strings here.  */
3325 static tree
3326 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3327 {
3328   tree value;
3329   size_t count;
3330   struct obstack str_ob;
3331   cpp_string str, istr, *strs;
3332   cp_token *tok;
3333   enum cpp_ttype type;
3334
3335   tok = cp_lexer_peek_token (parser->lexer);
3336   if (!cp_parser_is_string_literal (tok))
3337     {
3338       cp_parser_error (parser, "expected string-literal");
3339       return error_mark_node;
3340     }
3341
3342   type = tok->type;
3343
3344   /* Try to avoid the overhead of creating and destroying an obstack
3345      for the common case of just one string.  */
3346   if (!cp_parser_is_string_literal
3347       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3348     {
3349       cp_lexer_consume_token (parser->lexer);
3350
3351       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3352       str.len = TREE_STRING_LENGTH (tok->u.value);
3353       count = 1;
3354
3355       strs = &str;
3356     }
3357   else
3358     {
3359       gcc_obstack_init (&str_ob);
3360       count = 0;
3361
3362       do
3363         {
3364           cp_lexer_consume_token (parser->lexer);
3365           count++;
3366           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3367           str.len = TREE_STRING_LENGTH (tok->u.value);
3368
3369           if (type != tok->type)
3370             {
3371               if (type == CPP_STRING)
3372                 type = tok->type;
3373               else if (tok->type != CPP_STRING)
3374                 error_at (tok->location,
3375                           "unsupported non-standard concatenation "
3376                           "of string literals");
3377             }
3378
3379           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3380
3381           tok = cp_lexer_peek_token (parser->lexer);
3382         }
3383       while (cp_parser_is_string_literal (tok));
3384
3385       strs = (cpp_string *) obstack_finish (&str_ob);
3386     }
3387
3388   if (type != CPP_STRING && !wide_ok)
3389     {
3390       cp_parser_error (parser, "a wide string is invalid in this context");
3391       type = CPP_STRING;
3392     }
3393
3394   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3395       (parse_in, strs, count, &istr, type))
3396     {
3397       value = build_string (istr.len, (const char *)istr.text);
3398       free (CONST_CAST (unsigned char *, istr.text));
3399
3400       switch (type)
3401         {
3402         default:
3403         case CPP_STRING:
3404         case CPP_UTF8STRING:
3405           TREE_TYPE (value) = char_array_type_node;
3406           break;
3407         case CPP_STRING16:
3408           TREE_TYPE (value) = char16_array_type_node;
3409           break;
3410         case CPP_STRING32:
3411           TREE_TYPE (value) = char32_array_type_node;
3412           break;
3413         case CPP_WSTRING:
3414           TREE_TYPE (value) = wchar_array_type_node;
3415           break;
3416         }
3417
3418       value = fix_string_type (value);
3419     }
3420   else
3421     /* cpp_interpret_string has issued an error.  */
3422     value = error_mark_node;
3423
3424   if (count > 1)
3425     obstack_free (&str_ob, 0);
3426
3427   return value;
3428 }
3429
3430
3431 /* Basic concepts [gram.basic]  */
3432
3433 /* Parse a translation-unit.
3434
3435    translation-unit:
3436      declaration-seq [opt]
3437
3438    Returns TRUE if all went well.  */
3439
3440 static bool
3441 cp_parser_translation_unit (cp_parser* parser)
3442 {
3443   /* The address of the first non-permanent object on the declarator
3444      obstack.  */
3445   static void *declarator_obstack_base;
3446
3447   bool success;
3448
3449   /* Create the declarator obstack, if necessary.  */
3450   if (!cp_error_declarator)
3451     {
3452       gcc_obstack_init (&declarator_obstack);
3453       /* Create the error declarator.  */
3454       cp_error_declarator = make_declarator (cdk_error);
3455       /* Create the empty parameter list.  */
3456       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3457       /* Remember where the base of the declarator obstack lies.  */
3458       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3459     }
3460
3461   cp_parser_declaration_seq_opt (parser);
3462
3463   /* If there are no tokens left then all went well.  */
3464   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3465     {
3466       /* Get rid of the token array; we don't need it any more.  */
3467       cp_lexer_destroy (parser->lexer);
3468       parser->lexer = NULL;
3469
3470       /* This file might have been a context that's implicitly extern
3471          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3472       if (parser->implicit_extern_c)
3473         {
3474           pop_lang_context ();
3475           parser->implicit_extern_c = false;
3476         }
3477
3478       /* Finish up.  */
3479       finish_translation_unit ();
3480
3481       success = true;
3482     }
3483   else
3484     {
3485       cp_parser_error (parser, "expected declaration");
3486       success = false;
3487     }
3488
3489   /* Make sure the declarator obstack was fully cleaned up.  */
3490   gcc_assert (obstack_next_free (&declarator_obstack)
3491               == declarator_obstack_base);
3492
3493   /* All went well.  */
3494   return success;
3495 }
3496
3497 /* Expressions [gram.expr] */
3498
3499 /* Parse a primary-expression.
3500
3501    primary-expression:
3502      literal
3503      this
3504      ( expression )
3505      id-expression
3506
3507    GNU Extensions:
3508
3509    primary-expression:
3510      ( compound-statement )
3511      __builtin_va_arg ( assignment-expression , type-id )
3512      __builtin_offsetof ( type-id , offsetof-expression )
3513
3514    C++ Extensions:
3515      __has_nothrow_assign ( type-id )   
3516      __has_nothrow_constructor ( type-id )
3517      __has_nothrow_copy ( type-id )
3518      __has_trivial_assign ( type-id )   
3519      __has_trivial_constructor ( type-id )
3520      __has_trivial_copy ( type-id )
3521      __has_trivial_destructor ( type-id )
3522      __has_virtual_destructor ( type-id )     
3523      __is_abstract ( type-id )
3524      __is_base_of ( type-id , type-id )
3525      __is_class ( type-id )
3526      __is_convertible_to ( type-id , type-id )     
3527      __is_empty ( type-id )
3528      __is_enum ( type-id )
3529      __is_pod ( type-id )
3530      __is_polymorphic ( type-id )
3531      __is_union ( type-id )
3532
3533    Objective-C++ Extension:
3534
3535    primary-expression:
3536      objc-expression
3537
3538    literal:
3539      __null
3540
3541    ADDRESS_P is true iff this expression was immediately preceded by
3542    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3543    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3544    true iff this expression is a template argument.
3545
3546    Returns a representation of the expression.  Upon return, *IDK
3547    indicates what kind of id-expression (if any) was present.  */
3548
3549 static tree
3550 cp_parser_primary_expression (cp_parser *parser,
3551                               bool address_p,
3552                               bool cast_p,
3553                               bool template_arg_p,
3554                               cp_id_kind *idk)
3555 {
3556   cp_token *token = NULL;
3557
3558   /* Assume the primary expression is not an id-expression.  */
3559   *idk = CP_ID_KIND_NONE;
3560
3561   /* Peek at the next token.  */
3562   token = cp_lexer_peek_token (parser->lexer);
3563   switch (token->type)
3564     {
3565       /* literal:
3566            integer-literal
3567            character-literal
3568            floating-literal
3569            string-literal
3570            boolean-literal  */
3571     case CPP_CHAR:
3572     case CPP_CHAR16:
3573     case CPP_CHAR32:
3574     case CPP_WCHAR:
3575     case CPP_NUMBER:
3576       token = cp_lexer_consume_token (parser->lexer);
3577       if (TREE_CODE (token->u.value) == FIXED_CST)
3578         {
3579           error_at (token->location,
3580                     "fixed-point types not supported in C++");
3581           return error_mark_node;
3582         }
3583       /* Floating-point literals are only allowed in an integral
3584          constant expression if they are cast to an integral or
3585          enumeration type.  */
3586       if (TREE_CODE (token->u.value) == REAL_CST
3587           && parser->integral_constant_expression_p
3588           && pedantic)
3589         {
3590           /* CAST_P will be set even in invalid code like "int(2.7 +
3591              ...)".   Therefore, we have to check that the next token
3592              is sure to end the cast.  */
3593           if (cast_p)
3594             {
3595               cp_token *next_token;
3596
3597               next_token = cp_lexer_peek_token (parser->lexer);
3598               if (/* The comma at the end of an
3599                      enumerator-definition.  */
3600                   next_token->type != CPP_COMMA
3601                   /* The curly brace at the end of an enum-specifier.  */
3602                   && next_token->type != CPP_CLOSE_BRACE
3603                   /* The end of a statement.  */
3604                   && next_token->type != CPP_SEMICOLON
3605                   /* The end of the cast-expression.  */
3606                   && next_token->type != CPP_CLOSE_PAREN
3607                   /* The end of an array bound.  */
3608                   && next_token->type != CPP_CLOSE_SQUARE
3609                   /* The closing ">" in a template-argument-list.  */
3610                   && (next_token->type != CPP_GREATER
3611                       || parser->greater_than_is_operator_p)
3612                   /* C++0x only: A ">>" treated like two ">" tokens,
3613                      in a template-argument-list.  */
3614                   && (next_token->type != CPP_RSHIFT
3615                       || (cxx_dialect == cxx98)
3616                       || parser->greater_than_is_operator_p))
3617                 cast_p = false;
3618             }
3619
3620           /* If we are within a cast, then the constraint that the
3621              cast is to an integral or enumeration type will be
3622              checked at that point.  If we are not within a cast, then
3623              this code is invalid.  */
3624           if (!cast_p)
3625             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3626         }
3627       return token->u.value;
3628
3629     case CPP_STRING:
3630     case CPP_STRING16:
3631     case CPP_STRING32:
3632     case CPP_WSTRING:
3633     case CPP_UTF8STRING:
3634       /* ??? Should wide strings be allowed when parser->translate_strings_p
3635          is false (i.e. in attributes)?  If not, we can kill the third
3636          argument to cp_parser_string_literal.  */
3637       return cp_parser_string_literal (parser,
3638                                        parser->translate_strings_p,
3639                                        true);
3640
3641     case CPP_OPEN_PAREN:
3642       {
3643         tree expr;
3644         bool saved_greater_than_is_operator_p;
3645
3646         /* Consume the `('.  */
3647         cp_lexer_consume_token (parser->lexer);
3648         /* Within a parenthesized expression, a `>' token is always
3649            the greater-than operator.  */
3650         saved_greater_than_is_operator_p
3651           = parser->greater_than_is_operator_p;
3652         parser->greater_than_is_operator_p = true;
3653         /* If we see `( { ' then we are looking at the beginning of
3654            a GNU statement-expression.  */
3655         if (cp_parser_allow_gnu_extensions_p (parser)
3656             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3657           {
3658             /* Statement-expressions are not allowed by the standard.  */
3659             pedwarn (token->location, OPT_pedantic, 
3660                      "ISO C++ forbids braced-groups within expressions");
3661
3662             /* And they're not allowed outside of a function-body; you
3663                cannot, for example, write:
3664
3665                  int i = ({ int j = 3; j + 1; });
3666
3667                at class or namespace scope.  */
3668             if (!parser->in_function_body
3669                 || parser->in_template_argument_list_p)
3670               {
3671                 error_at (token->location,
3672                           "statement-expressions are not allowed outside "
3673                           "functions nor in template-argument lists");
3674                 cp_parser_skip_to_end_of_block_or_statement (parser);
3675                 expr = error_mark_node;
3676               }
3677             else
3678               {
3679                 /* Start the statement-expression.  */
3680                 expr = begin_stmt_expr ();
3681                 /* Parse the compound-statement.  */
3682                 cp_parser_compound_statement (parser, expr, false);
3683                 /* Finish up.  */
3684                 expr = finish_stmt_expr (expr, false);
3685               }
3686           }
3687         else
3688           {
3689             /* Parse the parenthesized expression.  */
3690             expr = cp_parser_expression (parser, cast_p, idk);
3691             /* Let the front end know that this expression was
3692                enclosed in parentheses. This matters in case, for
3693                example, the expression is of the form `A::B', since
3694                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3695                not.  */
3696             finish_parenthesized_expr (expr);
3697           }
3698         /* The `>' token might be the end of a template-id or
3699            template-parameter-list now.  */
3700         parser->greater_than_is_operator_p
3701           = saved_greater_than_is_operator_p;
3702         /* Consume the `)'.  */
3703         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3704           cp_parser_skip_to_end_of_statement (parser);
3705
3706         return expr;
3707       }
3708
3709     case CPP_OPEN_SQUARE:
3710       if (c_dialect_objc ())
3711         /* We have an Objective-C++ message. */
3712         return cp_parser_objc_expression (parser);
3713       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3714       return cp_parser_lambda_expression (parser);
3715
3716     case CPP_OBJC_STRING:
3717       if (c_dialect_objc ())
3718         /* We have an Objective-C++ string literal. */
3719         return cp_parser_objc_expression (parser);
3720       cp_parser_error (parser, "expected primary-expression");
3721       return error_mark_node;
3722
3723     case CPP_KEYWORD:
3724       switch (token->keyword)
3725         {
3726           /* These two are the boolean literals.  */
3727         case RID_TRUE:
3728           cp_lexer_consume_token (parser->lexer);
3729           return boolean_true_node;
3730         case RID_FALSE:
3731           cp_lexer_consume_token (parser->lexer);
3732           return boolean_false_node;
3733
3734           /* The `__null' literal.  */
3735         case RID_NULL:
3736           cp_lexer_consume_token (parser->lexer);
3737           return null_node;
3738
3739           /* The `nullptr' literal.  */
3740         case RID_NULLPTR:
3741           cp_lexer_consume_token (parser->lexer);
3742           return nullptr_node;
3743
3744           /* Recognize the `this' keyword.  */
3745         case RID_THIS:
3746           cp_lexer_consume_token (parser->lexer);
3747           if (parser->local_variables_forbidden_p)
3748             {
3749               error_at (token->location,
3750                         "%<this%> may not be used in this context");
3751               return error_mark_node;
3752             }
3753           /* Pointers cannot appear in constant-expressions.  */
3754           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3755             return error_mark_node;
3756           return finish_this_expr ();
3757
3758           /* The `operator' keyword can be the beginning of an
3759              id-expression.  */
3760         case RID_OPERATOR:
3761           goto id_expression;
3762
3763         case RID_FUNCTION_NAME:
3764         case RID_PRETTY_FUNCTION_NAME:
3765         case RID_C99_FUNCTION_NAME:
3766           {
3767             non_integral_constant name;
3768
3769             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3770                __func__ are the names of variables -- but they are
3771                treated specially.  Therefore, they are handled here,
3772                rather than relying on the generic id-expression logic
3773                below.  Grammatically, these names are id-expressions.
3774
3775                Consume the token.  */
3776             token = cp_lexer_consume_token (parser->lexer);
3777
3778             switch (token->keyword)
3779               {
3780               case RID_FUNCTION_NAME:
3781                 name = NIC_FUNC_NAME;
3782                 break;
3783               case RID_PRETTY_FUNCTION_NAME:
3784                 name = NIC_PRETTY_FUNC;
3785                 break;
3786               case RID_C99_FUNCTION_NAME:
3787                 name = NIC_C99_FUNC;
3788                 break;
3789               default:
3790                 gcc_unreachable ();
3791               }
3792
3793             if (cp_parser_non_integral_constant_expression (parser, name))
3794               return error_mark_node;
3795
3796             /* Look up the name.  */
3797             return finish_fname (token->u.value);
3798           }
3799
3800         case RID_VA_ARG:
3801           {
3802             tree expression;
3803             tree type;
3804
3805             /* The `__builtin_va_arg' construct is used to handle
3806                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3807             cp_lexer_consume_token (parser->lexer);
3808             /* Look for the opening `('.  */
3809             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3810             /* Now, parse the assignment-expression.  */
3811             expression = cp_parser_assignment_expression (parser,
3812                                                           /*cast_p=*/false, NULL);
3813             /* Look for the `,'.  */
3814             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3815             /* Parse the type-id.  */
3816             type = cp_parser_type_id (parser);
3817             /* Look for the closing `)'.  */
3818             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3819             /* Using `va_arg' in a constant-expression is not
3820                allowed.  */
3821             if (cp_parser_non_integral_constant_expression (parser,
3822                                                             NIC_VA_ARG))
3823               return error_mark_node;
3824             return build_x_va_arg (expression, type);
3825           }
3826
3827         case RID_OFFSETOF:
3828           return cp_parser_builtin_offsetof (parser);
3829
3830         case RID_HAS_NOTHROW_ASSIGN:
3831         case RID_HAS_NOTHROW_CONSTRUCTOR:
3832         case RID_HAS_NOTHROW_COPY:        
3833         case RID_HAS_TRIVIAL_ASSIGN:
3834         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3835         case RID_HAS_TRIVIAL_COPY:        
3836         case RID_HAS_TRIVIAL_DESTRUCTOR:
3837         case RID_HAS_VIRTUAL_DESTRUCTOR:
3838         case RID_IS_ABSTRACT:
3839         case RID_IS_BASE_OF:
3840         case RID_IS_CLASS:
3841         case RID_IS_CONVERTIBLE_TO:
3842         case RID_IS_EMPTY:
3843         case RID_IS_ENUM:
3844         case RID_IS_POD:
3845         case RID_IS_POLYMORPHIC:
3846         case RID_IS_STD_LAYOUT:
3847         case RID_IS_TRIVIAL:
3848         case RID_IS_UNION:
3849         case RID_IS_LITERAL_TYPE:
3850           return cp_parser_trait_expr (parser, token->keyword);
3851
3852         /* Objective-C++ expressions.  */
3853         case RID_AT_ENCODE:
3854         case RID_AT_PROTOCOL:
3855         case RID_AT_SELECTOR:
3856           return cp_parser_objc_expression (parser);
3857
3858         case RID_TEMPLATE:
3859           if (parser->in_function_body
3860               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861                   == CPP_LESS))
3862             {
3863               error_at (token->location,
3864                         "a template declaration cannot appear at block scope");
3865               cp_parser_skip_to_end_of_block_or_statement (parser);
3866               return error_mark_node;
3867             }
3868         default:
3869           cp_parser_error (parser, "expected primary-expression");
3870           return error_mark_node;
3871         }
3872
3873       /* An id-expression can start with either an identifier, a
3874          `::' as the beginning of a qualified-id, or the "operator"
3875          keyword.  */
3876     case CPP_NAME:
3877     case CPP_SCOPE:
3878     case CPP_TEMPLATE_ID:
3879     case CPP_NESTED_NAME_SPECIFIER:
3880       {
3881         tree id_expression;
3882         tree decl;
3883         const char *error_msg;
3884         bool template_p;
3885         bool done;
3886         cp_token *id_expr_token;
3887
3888       id_expression:
3889         /* Parse the id-expression.  */
3890         id_expression
3891           = cp_parser_id_expression (parser,
3892                                      /*template_keyword_p=*/false,
3893                                      /*check_dependency_p=*/true,
3894                                      &template_p,
3895                                      /*declarator_p=*/false,
3896                                      /*optional_p=*/false);
3897         if (id_expression == error_mark_node)
3898           return error_mark_node;
3899         id_expr_token = token;
3900         token = cp_lexer_peek_token (parser->lexer);
3901         done = (token->type != CPP_OPEN_SQUARE
3902                 && token->type != CPP_OPEN_PAREN
3903                 && token->type != CPP_DOT
3904                 && token->type != CPP_DEREF
3905                 && token->type != CPP_PLUS_PLUS
3906                 && token->type != CPP_MINUS_MINUS);
3907         /* If we have a template-id, then no further lookup is
3908            required.  If the template-id was for a template-class, we
3909            will sometimes have a TYPE_DECL at this point.  */
3910         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3911                  || TREE_CODE (id_expression) == TYPE_DECL)
3912           decl = id_expression;
3913         /* Look up the name.  */
3914         else
3915           {
3916             tree ambiguous_decls;
3917
3918             /* If we already know that this lookup is ambiguous, then
3919                we've already issued an error message; there's no reason
3920                to check again.  */
3921             if (id_expr_token->type == CPP_NAME
3922                 && id_expr_token->ambiguous_p)
3923               {
3924                 cp_parser_simulate_error (parser);
3925                 return error_mark_node;
3926               }
3927
3928             decl = cp_parser_lookup_name (parser, id_expression,
3929                                           none_type,
3930                                           template_p,
3931                                           /*is_namespace=*/false,
3932                                           /*check_dependency=*/true,
3933                                           &ambiguous_decls,
3934                                           id_expr_token->location);
3935             /* If the lookup was ambiguous, an error will already have
3936                been issued.  */
3937             if (ambiguous_decls)
3938               return error_mark_node;
3939
3940             /* In Objective-C++, we may have an Objective-C 2.0
3941                dot-syntax for classes here.  */
3942             if (c_dialect_objc ()
3943                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3944                 && TREE_CODE (decl) == TYPE_DECL
3945                 && objc_is_class_name (decl))
3946               {
3947                 tree component;
3948                 cp_lexer_consume_token (parser->lexer);
3949                 component = cp_parser_identifier (parser);
3950                 if (component == error_mark_node)
3951                   return error_mark_node;
3952
3953                 return objc_build_class_component_ref (id_expression, component);
3954               }
3955
3956             /* In Objective-C++, an instance variable (ivar) may be preferred
3957                to whatever cp_parser_lookup_name() found.  */
3958             decl = objc_lookup_ivar (decl, id_expression);
3959
3960             /* If name lookup gives us a SCOPE_REF, then the
3961                qualifying scope was dependent.  */
3962             if (TREE_CODE (decl) == SCOPE_REF)
3963               {
3964                 /* At this point, we do not know if DECL is a valid
3965                    integral constant expression.  We assume that it is
3966                    in fact such an expression, so that code like:
3967
3968                       template <int N> struct A {
3969                         int a[B<N>::i];
3970                       };
3971                      
3972                    is accepted.  At template-instantiation time, we
3973                    will check that B<N>::i is actually a constant.  */
3974                 return decl;
3975               }
3976             /* Check to see if DECL is a local variable in a context
3977                where that is forbidden.  */
3978             if (parser->local_variables_forbidden_p
3979                 && local_variable_p (decl))
3980               {
3981                 /* It might be that we only found DECL because we are
3982                    trying to be generous with pre-ISO scoping rules.
3983                    For example, consider:
3984
3985                      int i;
3986                      void g() {
3987                        for (int i = 0; i < 10; ++i) {}
3988                        extern void f(int j = i);
3989                      }
3990
3991                    Here, name look up will originally find the out
3992                    of scope `i'.  We need to issue a warning message,
3993                    but then use the global `i'.  */
3994                 decl = check_for_out_of_scope_variable (decl);
3995                 if (local_variable_p (decl))
3996                   {
3997                     error_at (id_expr_token->location,
3998                               "local variable %qD may not appear in this context",
3999                               decl);
4000                     return error_mark_node;
4001                   }
4002               }
4003           }
4004
4005         decl = (finish_id_expression
4006                 (id_expression, decl, parser->scope,
4007                  idk,
4008                  parser->integral_constant_expression_p,
4009                  parser->allow_non_integral_constant_expression_p,
4010                  &parser->non_integral_constant_expression_p,
4011                  template_p, done, address_p,
4012                  template_arg_p,
4013                  &error_msg,
4014                  id_expr_token->location));
4015         if (error_msg)
4016           cp_parser_error (parser, error_msg);
4017         return decl;
4018       }
4019
4020       /* Anything else is an error.  */
4021     default:
4022       cp_parser_error (parser, "expected primary-expression");
4023       return error_mark_node;
4024     }
4025 }
4026
4027 /* Parse an id-expression.
4028
4029    id-expression:
4030      unqualified-id
4031      qualified-id
4032
4033    qualified-id:
4034      :: [opt] nested-name-specifier template [opt] unqualified-id
4035      :: identifier
4036      :: operator-function-id
4037      :: template-id
4038
4039    Return a representation of the unqualified portion of the
4040    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4041    a `::' or nested-name-specifier.
4042
4043    Often, if the id-expression was a qualified-id, the caller will
4044    want to make a SCOPE_REF to represent the qualified-id.  This
4045    function does not do this in order to avoid wastefully creating
4046    SCOPE_REFs when they are not required.
4047
4048    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4049    `template' keyword.
4050
4051    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4052    uninstantiated templates.
4053
4054    If *TEMPLATE_P is non-NULL, it is set to true iff the
4055    `template' keyword is used to explicitly indicate that the entity
4056    named is a template.
4057
4058    If DECLARATOR_P is true, the id-expression is appearing as part of
4059    a declarator, rather than as part of an expression.  */
4060
4061 static tree
4062 cp_parser_id_expression (cp_parser *parser,
4063                          bool template_keyword_p,
4064                          bool check_dependency_p,
4065                          bool *template_p,
4066                          bool declarator_p,
4067                          bool optional_p)
4068 {
4069   bool global_scope_p;
4070   bool nested_name_specifier_p;
4071
4072   /* Assume the `template' keyword was not used.  */
4073   if (template_p)
4074     *template_p = template_keyword_p;
4075
4076   /* Look for the optional `::' operator.  */
4077   global_scope_p
4078     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4079        != NULL_TREE);
4080   /* Look for the optional nested-name-specifier.  */
4081   nested_name_specifier_p
4082     = (cp_parser_nested_name_specifier_opt (parser,
4083                                             /*typename_keyword_p=*/false,
4084                                             check_dependency_p,
4085                                             /*type_p=*/false,
4086                                             declarator_p)
4087        != NULL_TREE);
4088   /* If there is a nested-name-specifier, then we are looking at
4089      the first qualified-id production.  */
4090   if (nested_name_specifier_p)
4091     {
4092       tree saved_scope;
4093       tree saved_object_scope;
4094       tree saved_qualifying_scope;
4095       tree unqualified_id;
4096       bool is_template;
4097
4098       /* See if the next token is the `template' keyword.  */
4099       if (!template_p)
4100         template_p = &is_template;
4101       *template_p = cp_parser_optional_template_keyword (parser);
4102       /* Name lookup we do during the processing of the
4103          unqualified-id might obliterate SCOPE.  */
4104       saved_scope = parser->scope;
4105       saved_object_scope = parser->object_scope;
4106       saved_qualifying_scope = parser->qualifying_scope;
4107       /* Process the final unqualified-id.  */
4108       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4109                                                  check_dependency_p,
4110                                                  declarator_p,
4111                                                  /*optional_p=*/false);
4112       /* Restore the SAVED_SCOPE for our caller.  */
4113       parser->scope = saved_scope;
4114       parser->object_scope = saved_object_scope;
4115       parser->qualifying_scope = saved_qualifying_scope;
4116
4117       return unqualified_id;
4118     }
4119   /* Otherwise, if we are in global scope, then we are looking at one
4120      of the other qualified-id productions.  */
4121   else if (global_scope_p)
4122     {
4123       cp_token *token;
4124       tree id;
4125
4126       /* Peek at the next token.  */
4127       token = cp_lexer_peek_token (parser->lexer);
4128
4129       /* If it's an identifier, and the next token is not a "<", then
4130          we can avoid the template-id case.  This is an optimization
4131          for this common case.  */
4132       if (token->type == CPP_NAME
4133           && !cp_parser_nth_token_starts_template_argument_list_p
4134                (parser, 2))
4135         return cp_parser_identifier (parser);
4136
4137       cp_parser_parse_tentatively (parser);
4138       /* Try a template-id.  */
4139       id = cp_parser_template_id (parser,
4140                                   /*template_keyword_p=*/false,
4141                                   /*check_dependency_p=*/true,
4142                                   declarator_p);
4143       /* If that worked, we're done.  */
4144       if (cp_parser_parse_definitely (parser))
4145         return id;
4146
4147       /* Peek at the next token.  (Changes in the token buffer may
4148          have invalidated the pointer obtained above.)  */
4149       token = cp_lexer_peek_token (parser->lexer);
4150
4151       switch (token->type)
4152         {
4153         case CPP_NAME:
4154           return cp_parser_identifier (parser);
4155
4156         case CPP_KEYWORD:
4157           if (token->keyword == RID_OPERATOR)
4158             return cp_parser_operator_function_id (parser);
4159           /* Fall through.  */
4160
4161         default:
4162           cp_parser_error (parser, "expected id-expression");
4163           return error_mark_node;
4164         }
4165     }
4166   else
4167     return cp_parser_unqualified_id (parser, template_keyword_p,
4168                                      /*check_dependency_p=*/true,
4169                                      declarator_p,
4170                                      optional_p);
4171 }
4172
4173 /* Parse an unqualified-id.
4174
4175    unqualified-id:
4176      identifier
4177      operator-function-id
4178      conversion-function-id
4179      ~ class-name
4180      template-id
4181
4182    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4183    keyword, in a construct like `A::template ...'.
4184
4185    Returns a representation of unqualified-id.  For the `identifier'
4186    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4187    production a BIT_NOT_EXPR is returned; the operand of the
4188    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4189    other productions, see the documentation accompanying the
4190    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4191    names are looked up in uninstantiated templates.  If DECLARATOR_P
4192    is true, the unqualified-id is appearing as part of a declarator,
4193    rather than as part of an expression.  */
4194
4195 static tree
4196 cp_parser_unqualified_id (cp_parser* parser,
4197                           bool template_keyword_p,
4198                           bool check_dependency_p,
4199                           bool declarator_p,
4200                           bool optional_p)
4201 {
4202   cp_token *token;
4203
4204   /* Peek at the next token.  */
4205   token = cp_lexer_peek_token (parser->lexer);
4206
4207   switch (token->type)
4208     {
4209     case CPP_NAME:
4210       {
4211         tree id;
4212
4213         /* We don't know yet whether or not this will be a
4214            template-id.  */
4215         cp_parser_parse_tentatively (parser);
4216         /* Try a template-id.  */
4217         id = cp_parser_template_id (parser, template_keyword_p,
4218                                     check_dependency_p,
4219                                     declarator_p);
4220         /* If it worked, we're done.  */
4221         if (cp_parser_parse_definitely (parser))
4222           return id;
4223         /* Otherwise, it's an ordinary identifier.  */
4224         return cp_parser_identifier (parser);
4225       }
4226
4227     case CPP_TEMPLATE_ID:
4228       return cp_parser_template_id (parser, template_keyword_p,
4229                                     check_dependency_p,
4230                                     declarator_p);
4231
4232     case CPP_COMPL:
4233       {
4234         tree type_decl;
4235         tree qualifying_scope;
4236         tree object_scope;
4237         tree scope;
4238         bool done;
4239
4240         /* Consume the `~' token.  */
4241         cp_lexer_consume_token (parser->lexer);
4242         /* Parse the class-name.  The standard, as written, seems to
4243            say that:
4244
4245              template <typename T> struct S { ~S (); };
4246              template <typename T> S<T>::~S() {}
4247
4248            is invalid, since `~' must be followed by a class-name, but
4249            `S<T>' is dependent, and so not known to be a class.
4250            That's not right; we need to look in uninstantiated
4251            templates.  A further complication arises from:
4252
4253              template <typename T> void f(T t) {
4254                t.T::~T();
4255              }
4256
4257            Here, it is not possible to look up `T' in the scope of `T'
4258            itself.  We must look in both the current scope, and the
4259            scope of the containing complete expression.
4260
4261            Yet another issue is:
4262
4263              struct S {
4264                int S;
4265                ~S();
4266              };
4267
4268              S::~S() {}
4269
4270            The standard does not seem to say that the `S' in `~S'
4271            should refer to the type `S' and not the data member
4272            `S::S'.  */
4273
4274         /* DR 244 says that we look up the name after the "~" in the
4275            same scope as we looked up the qualifying name.  That idea
4276            isn't fully worked out; it's more complicated than that.  */
4277         scope = parser->scope;
4278         object_scope = parser->object_scope;
4279         qualifying_scope = parser->qualifying_scope;
4280
4281         /* Check for invalid scopes.  */
4282         if (scope == error_mark_node)
4283           {
4284             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4285               cp_lexer_consume_token (parser->lexer);
4286             return error_mark_node;
4287           }
4288         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4289           {
4290             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4291               error_at (token->location,
4292                         "scope %qT before %<~%> is not a class-name",
4293                         scope);
4294             cp_parser_simulate_error (parser);
4295             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4296               cp_lexer_consume_token (parser->lexer);
4297             return error_mark_node;
4298           }
4299         gcc_assert (!scope || TYPE_P (scope));
4300
4301         /* If the name is of the form "X::~X" it's OK even if X is a
4302            typedef.  */
4303         token = cp_lexer_peek_token (parser->lexer);
4304         if (scope
4305             && token->type == CPP_NAME
4306             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4307                 != CPP_LESS)
4308             && (token->u.value == TYPE_IDENTIFIER (scope)
4309                 || constructor_name_p (token->u.value, scope)))
4310           {
4311             cp_lexer_consume_token (parser->lexer);
4312             return build_nt (BIT_NOT_EXPR, scope);
4313           }
4314
4315         /* If there was an explicit qualification (S::~T), first look
4316            in the scope given by the qualification (i.e., S).
4317
4318            Note: in the calls to cp_parser_class_name below we pass
4319            typename_type so that lookup finds the injected-class-name
4320            rather than the constructor.  */
4321         done = false;
4322         type_decl = NULL_TREE;
4323         if (scope)
4324           {
4325             cp_parser_parse_tentatively (parser);
4326             type_decl = cp_parser_class_name (parser,
4327                                               /*typename_keyword_p=*/false,
4328                                               /*template_keyword_p=*/false,
4329                                               typename_type,
4330                                               /*check_dependency=*/false,
4331                                               /*class_head_p=*/false,
4332                                               declarator_p);
4333             if (cp_parser_parse_definitely (parser))
4334               done = true;
4335           }
4336         /* In "N::S::~S", look in "N" as well.  */
4337         if (!done && scope && qualifying_scope)
4338           {
4339             cp_parser_parse_tentatively (parser);
4340             parser->scope = qualifying_scope;
4341             parser->object_scope = NULL_TREE;
4342             parser->qualifying_scope = NULL_TREE;
4343             type_decl
4344               = cp_parser_class_name (parser,
4345                                       /*typename_keyword_p=*/false,
4346                                       /*template_keyword_p=*/false,
4347                                       typename_type,
4348                                       /*check_dependency=*/false,
4349                                       /*class_head_p=*/false,
4350                                       declarator_p);
4351             if (cp_parser_parse_definitely (parser))
4352               done = true;
4353           }
4354         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4355         else if (!done && object_scope)
4356           {
4357             cp_parser_parse_tentatively (parser);
4358             parser->scope = object_scope;
4359             parser->object_scope = NULL_TREE;
4360             parser->qualifying_scope = NULL_TREE;
4361             type_decl
4362               = cp_parser_class_name (parser,
4363                                       /*typename_keyword_p=*/false,
4364                                       /*template_keyword_p=*/false,
4365                                       typename_type,
4366                                       /*check_dependency=*/false,
4367                                       /*class_head_p=*/false,
4368                                       declarator_p);
4369             if (cp_parser_parse_definitely (parser))
4370               done = true;
4371           }
4372         /* Look in the surrounding context.  */
4373         if (!done)
4374           {
4375             parser->scope = NULL_TREE;
4376             parser->object_scope = NULL_TREE;
4377             parser->qualifying_scope = NULL_TREE;
4378             if (processing_template_decl)
4379               cp_parser_parse_tentatively (parser);
4380             type_decl
4381               = cp_parser_class_name (parser,
4382                                       /*typename_keyword_p=*/false,
4383                                       /*template_keyword_p=*/false,
4384                                       typename_type,
4385                                       /*check_dependency=*/false,
4386                                       /*class_head_p=*/false,
4387                                       declarator_p);
4388             if (processing_template_decl
4389                 && ! cp_parser_parse_definitely (parser))
4390               {
4391                 /* We couldn't find a type with this name, so just accept
4392                    it and check for a match at instantiation time.  */
4393                 type_decl = cp_parser_identifier (parser);
4394                 if (type_decl != error_mark_node)
4395                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4396                 return type_decl;
4397               }
4398           }
4399         /* If an error occurred, assume that the name of the
4400            destructor is the same as the name of the qualifying
4401            class.  That allows us to keep parsing after running
4402            into ill-formed destructor names.  */
4403         if (type_decl == error_mark_node && scope)
4404           return build_nt (BIT_NOT_EXPR, scope);
4405         else if (type_decl == error_mark_node)
4406           return error_mark_node;
4407
4408         /* Check that destructor name and scope match.  */
4409         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4410           {
4411             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412               error_at (token->location,
4413                         "declaration of %<~%T%> as member of %qT",
4414                         type_decl, scope);
4415             cp_parser_simulate_error (parser);
4416             return error_mark_node;
4417           }
4418
4419         /* [class.dtor]
4420
4421            A typedef-name that names a class shall not be used as the
4422            identifier in the declarator for a destructor declaration.  */
4423         if (declarator_p
4424             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4425             && !DECL_SELF_REFERENCE_P (type_decl)
4426             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4427           error_at (token->location,
4428                     "typedef-name %qD used as destructor declarator",
4429                     type_decl);
4430
4431         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4432       }
4433
4434     case CPP_KEYWORD:
4435       if (token->keyword == RID_OPERATOR)
4436         {
4437           tree id;
4438
4439           /* This could be a template-id, so we try that first.  */
4440           cp_parser_parse_tentatively (parser);
4441           /* Try a template-id.  */
4442           id = cp_parser_template_id (parser, template_keyword_p,
4443                                       /*check_dependency_p=*/true,
4444                                       declarator_p);
4445           /* If that worked, we're done.  */
4446           if (cp_parser_parse_definitely (parser))
4447             return id;
4448           /* We still don't know whether we're looking at an
4449              operator-function-id or a conversion-function-id.  */
4450           cp_parser_parse_tentatively (parser);
4451           /* Try an operator-function-id.  */
4452           id = cp_parser_operator_function_id (parser);
4453           /* If that didn't work, try a conversion-function-id.  */
4454           if (!cp_parser_parse_definitely (parser))
4455             id = cp_parser_conversion_function_id (parser);
4456
4457           return id;
4458         }
4459       /* Fall through.  */
4460
4461     default:
4462       if (optional_p)
4463         return NULL_TREE;
4464       cp_parser_error (parser, "expected unqualified-id");
4465       return error_mark_node;
4466     }
4467 }
4468
4469 /* Parse an (optional) nested-name-specifier.
4470
4471    nested-name-specifier: [C++98]
4472      class-or-namespace-name :: nested-name-specifier [opt]
4473      class-or-namespace-name :: template nested-name-specifier [opt]
4474
4475    nested-name-specifier: [C++0x]
4476      type-name ::
4477      namespace-name ::
4478      nested-name-specifier identifier ::
4479      nested-name-specifier template [opt] simple-template-id ::
4480
4481    PARSER->SCOPE should be set appropriately before this function is
4482    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4483    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4484    in name lookups.
4485
4486    Sets PARSER->SCOPE to the class (TYPE) or namespace
4487    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4488    it unchanged if there is no nested-name-specifier.  Returns the new
4489    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4490
4491    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4492    part of a declaration and/or decl-specifier.  */
4493
4494 static tree
4495 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4496                                      bool typename_keyword_p,
4497                                      bool check_dependency_p,
4498                                      bool type_p,
4499                                      bool is_declaration)
4500 {
4501   bool success = false;
4502   cp_token_position start = 0;
4503   cp_token *token;
4504
4505   /* Remember where the nested-name-specifier starts.  */
4506   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4507     {
4508       start = cp_lexer_token_position (parser->lexer, false);
4509       push_deferring_access_checks (dk_deferred);
4510     }
4511
4512   while (true)
4513     {
4514       tree new_scope;
4515       tree old_scope;
4516       tree saved_qualifying_scope;
4517       bool template_keyword_p;
4518
4519       /* Spot cases that cannot be the beginning of a
4520          nested-name-specifier.  */
4521       token = cp_lexer_peek_token (parser->lexer);
4522
4523       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4524          the already parsed nested-name-specifier.  */
4525       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4526         {
4527           /* Grab the nested-name-specifier and continue the loop.  */
4528           cp_parser_pre_parsed_nested_name_specifier (parser);
4529           /* If we originally encountered this nested-name-specifier
4530              with IS_DECLARATION set to false, we will not have
4531              resolved TYPENAME_TYPEs, so we must do so here.  */
4532           if (is_declaration
4533               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4534             {
4535               new_scope = resolve_typename_type (parser->scope,
4536                                                  /*only_current_p=*/false);
4537               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4538                 parser->scope = new_scope;
4539             }
4540           success = true;
4541           continue;
4542         }
4543
4544       /* Spot cases that cannot be the beginning of a
4545          nested-name-specifier.  On the second and subsequent times
4546          through the loop, we look for the `template' keyword.  */
4547       if (success && token->keyword == RID_TEMPLATE)
4548         ;
4549       /* A template-id can start a nested-name-specifier.  */
4550       else if (token->type == CPP_TEMPLATE_ID)
4551         ;
4552       else
4553         {
4554           /* If the next token is not an identifier, then it is
4555              definitely not a type-name or namespace-name.  */
4556           if (token->type != CPP_NAME)
4557             break;
4558           /* If the following token is neither a `<' (to begin a
4559              template-id), nor a `::', then we are not looking at a
4560              nested-name-specifier.  */
4561           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4562
4563           if (token->type == CPP_COLON
4564               && parser->colon_corrects_to_scope_p
4565               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4566             {
4567               error_at (token->location,
4568                         "found %<:%> in nested-name-specifier, expected %<::%>");
4569               token->type = CPP_SCOPE;
4570             }
4571
4572           if (token->type != CPP_SCOPE
4573               && !cp_parser_nth_token_starts_template_argument_list_p
4574                   (parser, 2))
4575             break;
4576         }
4577
4578       /* The nested-name-specifier is optional, so we parse
4579          tentatively.  */
4580       cp_parser_parse_tentatively (parser);
4581
4582       /* Look for the optional `template' keyword, if this isn't the
4583          first time through the loop.  */
4584       if (success)
4585         template_keyword_p = cp_parser_optional_template_keyword (parser);
4586       else
4587         template_keyword_p = false;
4588
4589       /* Save the old scope since the name lookup we are about to do
4590          might destroy it.  */
4591       old_scope = parser->scope;
4592       saved_qualifying_scope = parser->qualifying_scope;
4593       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4594          look up names in "X<T>::I" in order to determine that "Y" is
4595          a template.  So, if we have a typename at this point, we make
4596          an effort to look through it.  */
4597       if (is_declaration
4598           && !typename_keyword_p
4599           && parser->scope
4600           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4601         parser->scope = resolve_typename_type (parser->scope,
4602                                                /*only_current_p=*/false);
4603       /* Parse the qualifying entity.  */
4604       new_scope
4605         = cp_parser_qualifying_entity (parser,
4606                                        typename_keyword_p,
4607                                        template_keyword_p,
4608                                        check_dependency_p,
4609                                        type_p,
4610                                        is_declaration);
4611       /* Look for the `::' token.  */
4612       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4613
4614       /* If we found what we wanted, we keep going; otherwise, we're
4615          done.  */
4616       if (!cp_parser_parse_definitely (parser))
4617         {
4618           bool error_p = false;
4619
4620           /* Restore the OLD_SCOPE since it was valid before the
4621              failed attempt at finding the last
4622              class-or-namespace-name.  */
4623           parser->scope = old_scope;
4624           parser->qualifying_scope = saved_qualifying_scope;
4625           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4626             break;
4627           /* If the next token is an identifier, and the one after
4628              that is a `::', then any valid interpretation would have
4629              found a class-or-namespace-name.  */
4630           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4631                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4632                      == CPP_SCOPE)
4633                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4634                      != CPP_COMPL))
4635             {
4636               token = cp_lexer_consume_token (parser->lexer);
4637               if (!error_p)
4638                 {
4639                   if (!token->ambiguous_p)
4640                     {
4641                       tree decl;
4642                       tree ambiguous_decls;
4643
4644                       decl = cp_parser_lookup_name (parser, token->u.value,
4645                                                     none_type,
4646                                                     /*is_template=*/false,
4647                                                     /*is_namespace=*/false,
4648                                                     /*check_dependency=*/true,
4649                                                     &ambiguous_decls,
4650                                                     token->location);
4651                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4652                         error_at (token->location,
4653                                   "%qD used without template parameters",
4654                                   decl);
4655                       else if (ambiguous_decls)
4656                         {
4657                           error_at (token->location,
4658                                     "reference to %qD is ambiguous",
4659                                     token->u.value);
4660                           print_candidates (ambiguous_decls);
4661                           decl = error_mark_node;
4662                         }
4663                       else
4664                         {
4665                           if (cxx_dialect != cxx98)
4666                             cp_parser_name_lookup_error
4667                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4668                              token->location);
4669                           else
4670                             cp_parser_name_lookup_error
4671                             (parser, token->u.value, decl, NLE_CXX98,
4672                              token->location);
4673                         }
4674                     }
4675                   parser->scope = error_mark_node;
4676                   error_p = true;
4677                   /* Treat this as a successful nested-name-specifier
4678                      due to:
4679
4680                      [basic.lookup.qual]
4681
4682                      If the name found is not a class-name (clause
4683                      _class_) or namespace-name (_namespace.def_), the
4684                      program is ill-formed.  */
4685                   success = true;
4686                 }
4687               cp_lexer_consume_token (parser->lexer);
4688             }
4689           break;
4690         }
4691       /* We've found one valid nested-name-specifier.  */
4692       success = true;
4693       /* Name lookup always gives us a DECL.  */
4694       if (TREE_CODE (new_scope) == TYPE_DECL)
4695         new_scope = TREE_TYPE (new_scope);
4696       /* Uses of "template" must be followed by actual templates.  */
4697       if (template_keyword_p
4698           && !(CLASS_TYPE_P (new_scope)
4699                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4700                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4701                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4702           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4703                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4704                    == TEMPLATE_ID_EXPR)))
4705         permerror (input_location, TYPE_P (new_scope)
4706                    ? "%qT is not a template"
4707                    : "%qD is not a template",
4708                    new_scope);
4709       /* If it is a class scope, try to complete it; we are about to
4710          be looking up names inside the class.  */
4711       if (TYPE_P (new_scope)
4712           /* Since checking types for dependency can be expensive,
4713              avoid doing it if the type is already complete.  */
4714           && !COMPLETE_TYPE_P (new_scope)
4715           /* Do not try to complete dependent types.  */
4716           && !dependent_type_p (new_scope))
4717         {
4718           new_scope = complete_type (new_scope);
4719           /* If it is a typedef to current class, use the current
4720              class instead, as the typedef won't have any names inside
4721              it yet.  */
4722           if (!COMPLETE_TYPE_P (new_scope)
4723               && currently_open_class (new_scope))
4724             new_scope = TYPE_MAIN_VARIANT (new_scope);
4725         }
4726       /* Make sure we look in the right scope the next time through
4727          the loop.  */
4728       parser->scope = new_scope;
4729     }
4730
4731   /* If parsing tentatively, replace the sequence of tokens that makes
4732      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4733      token.  That way, should we re-parse the token stream, we will
4734      not have to repeat the effort required to do the parse, nor will
4735      we issue duplicate error messages.  */
4736   if (success && start)
4737     {
4738       cp_token *token;
4739
4740       token = cp_lexer_token_at (parser->lexer, start);
4741       /* Reset the contents of the START token.  */
4742       token->type = CPP_NESTED_NAME_SPECIFIER;
4743       /* Retrieve any deferred checks.  Do not pop this access checks yet
4744          so the memory will not be reclaimed during token replacing below.  */
4745       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4746       token->u.tree_check_value->value = parser->scope;
4747       token->u.tree_check_value->checks = get_deferred_access_checks ();
4748       token->u.tree_check_value->qualifying_scope =
4749         parser->qualifying_scope;
4750       token->keyword = RID_MAX;
4751
4752       /* Purge all subsequent tokens.  */
4753       cp_lexer_purge_tokens_after (parser->lexer, start);
4754     }
4755
4756   if (start)
4757     pop_to_parent_deferring_access_checks ();
4758
4759   return success ? parser->scope : NULL_TREE;
4760 }
4761
4762 /* Parse a nested-name-specifier.  See
4763    cp_parser_nested_name_specifier_opt for details.  This function
4764    behaves identically, except that it will an issue an error if no
4765    nested-name-specifier is present.  */
4766
4767 static tree
4768 cp_parser_nested_name_specifier (cp_parser *parser,
4769                                  bool typename_keyword_p,
4770                                  bool check_dependency_p,
4771                                  bool type_p,
4772                                  bool is_declaration)
4773 {
4774   tree scope;
4775
4776   /* Look for the nested-name-specifier.  */
4777   scope = cp_parser_nested_name_specifier_opt (parser,
4778                                                typename_keyword_p,
4779                                                check_dependency_p,
4780                                                type_p,
4781                                                is_declaration);
4782   /* If it was not present, issue an error message.  */
4783   if (!scope)
4784     {
4785       cp_parser_error (parser, "expected nested-name-specifier");
4786       parser->scope = NULL_TREE;
4787     }
4788
4789   return scope;
4790 }
4791
4792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4793    this is either a class-name or a namespace-name (which corresponds
4794    to the class-or-namespace-name production in the grammar). For
4795    C++0x, it can also be a type-name that refers to an enumeration
4796    type.
4797
4798    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4799    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4800    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4801    TYPE_P is TRUE iff the next name should be taken as a class-name,
4802    even the same name is declared to be another entity in the same
4803    scope.
4804
4805    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4806    specified by the class-or-namespace-name.  If neither is found the
4807    ERROR_MARK_NODE is returned.  */
4808
4809 static tree
4810 cp_parser_qualifying_entity (cp_parser *parser,
4811                              bool typename_keyword_p,
4812                              bool template_keyword_p,
4813                              bool check_dependency_p,
4814                              bool type_p,
4815                              bool is_declaration)
4816 {
4817   tree saved_scope;
4818   tree saved_qualifying_scope;
4819   tree saved_object_scope;
4820   tree scope;
4821   bool only_class_p;
4822   bool successful_parse_p;
4823
4824   /* Before we try to parse the class-name, we must save away the
4825      current PARSER->SCOPE since cp_parser_class_name will destroy
4826      it.  */
4827   saved_scope = parser->scope;
4828   saved_qualifying_scope = parser->qualifying_scope;
4829   saved_object_scope = parser->object_scope;
4830   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4831      there is no need to look for a namespace-name.  */
4832   only_class_p = template_keyword_p 
4833     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4834   if (!only_class_p)
4835     cp_parser_parse_tentatively (parser);
4836   scope = cp_parser_class_name (parser,
4837                                 typename_keyword_p,
4838                                 template_keyword_p,
4839                                 type_p ? class_type : none_type,
4840                                 check_dependency_p,
4841                                 /*class_head_p=*/false,
4842                                 is_declaration);
4843   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4844   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4845   if (!only_class_p 
4846       && cxx_dialect != cxx98
4847       && !successful_parse_p)
4848     {
4849       /* Restore the saved scope.  */
4850       parser->scope = saved_scope;
4851       parser->qualifying_scope = saved_qualifying_scope;
4852       parser->object_scope = saved_object_scope;
4853
4854       /* Parse tentatively.  */
4855       cp_parser_parse_tentatively (parser);
4856      
4857       /* Parse a typedef-name or enum-name.  */
4858       scope = cp_parser_nonclass_name (parser);
4859
4860       /* "If the name found does not designate a namespace or a class,
4861          enumeration, or dependent type, the program is ill-formed."
4862
4863          We cover classes and dependent types above and namespaces below,
4864          so this code is only looking for enums.  */
4865       if (!scope || TREE_CODE (scope) != TYPE_DECL
4866           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4867         cp_parser_simulate_error (parser);
4868
4869       successful_parse_p = cp_parser_parse_definitely (parser);
4870     }
4871   /* If that didn't work, try for a namespace-name.  */
4872   if (!only_class_p && !successful_parse_p)
4873     {
4874       /* Restore the saved scope.  */
4875       parser->scope = saved_scope;
4876       parser->qualifying_scope = saved_qualifying_scope;
4877       parser->object_scope = saved_object_scope;
4878       /* If we are not looking at an identifier followed by the scope
4879          resolution operator, then this is not part of a
4880          nested-name-specifier.  (Note that this function is only used
4881          to parse the components of a nested-name-specifier.)  */
4882       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4883           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4884         return error_mark_node;
4885       scope = cp_parser_namespace_name (parser);
4886     }
4887
4888   return scope;
4889 }
4890
4891 /* Parse a postfix-expression.
4892
4893    postfix-expression:
4894      primary-expression
4895      postfix-expression [ expression ]
4896      postfix-expression ( expression-list [opt] )
4897      simple-type-specifier ( expression-list [opt] )
4898      typename :: [opt] nested-name-specifier identifier
4899        ( expression-list [opt] )
4900      typename :: [opt] nested-name-specifier template [opt] template-id
4901        ( expression-list [opt] )
4902      postfix-expression . template [opt] id-expression
4903      postfix-expression -> template [opt] id-expression
4904      postfix-expression . pseudo-destructor-name
4905      postfix-expression -> pseudo-destructor-name
4906      postfix-expression ++
4907      postfix-expression --
4908      dynamic_cast < type-id > ( expression )
4909      static_cast < type-id > ( expression )
4910      reinterpret_cast < type-id > ( expression )
4911      const_cast < type-id > ( expression )
4912      typeid ( expression )
4913      typeid ( type-id )
4914
4915    GNU Extension:
4916
4917    postfix-expression:
4918      ( type-id ) { initializer-list , [opt] }
4919
4920    This extension is a GNU version of the C99 compound-literal
4921    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4922    but they are essentially the same concept.)
4923
4924    If ADDRESS_P is true, the postfix expression is the operand of the
4925    `&' operator.  CAST_P is true if this expression is the target of a
4926    cast.
4927
4928    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4929    class member access expressions [expr.ref].
4930
4931    Returns a representation of the expression.  */
4932
4933 static tree
4934 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4935                               bool member_access_only_p,
4936                               cp_id_kind * pidk_return)
4937 {
4938   cp_token *token;
4939   enum rid keyword;
4940   cp_id_kind idk = CP_ID_KIND_NONE;
4941   tree postfix_expression = NULL_TREE;
4942   bool is_member_access = false;
4943
4944   /* Peek at the next token.  */
4945   token = cp_lexer_peek_token (parser->lexer);
4946   /* Some of the productions are determined by keywords.  */
4947   keyword = token->keyword;
4948   switch (keyword)
4949     {
4950     case RID_DYNCAST:
4951     case RID_STATCAST:
4952     case RID_REINTCAST:
4953     case RID_CONSTCAST:
4954       {
4955         tree type;
4956         tree expression;
4957         const char *saved_message;
4958
4959         /* All of these can be handled in the same way from the point
4960            of view of parsing.  Begin by consuming the token
4961            identifying the cast.  */
4962         cp_lexer_consume_token (parser->lexer);
4963
4964         /* New types cannot be defined in the cast.  */
4965         saved_message = parser->type_definition_forbidden_message;
4966         parser->type_definition_forbidden_message
4967           = G_("types may not be defined in casts");
4968
4969         /* Look for the opening `<'.  */
4970         cp_parser_require (parser, CPP_LESS, RT_LESS);
4971         /* Parse the type to which we are casting.  */
4972         type = cp_parser_type_id (parser);
4973         /* Look for the closing `>'.  */
4974         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4975         /* Restore the old message.  */
4976         parser->type_definition_forbidden_message = saved_message;
4977
4978         /* And the expression which is being cast.  */
4979         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4980         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4981         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4982
4983         /* Only type conversions to integral or enumeration types
4984            can be used in constant-expressions.  */
4985         if (!cast_valid_in_integral_constant_expression_p (type)
4986             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4987           return error_mark_node;
4988
4989         switch (keyword)
4990           {
4991           case RID_DYNCAST:
4992             postfix_expression
4993               = build_dynamic_cast (type, expression, tf_warning_or_error);
4994             break;
4995           case RID_STATCAST:
4996             postfix_expression
4997               = build_static_cast (type, expression, tf_warning_or_error);
4998             break;
4999           case RID_REINTCAST:
5000             postfix_expression
5001               = build_reinterpret_cast (type, expression, 
5002                                         tf_warning_or_error);
5003             break;
5004           case RID_CONSTCAST:
5005             postfix_expression
5006               = build_const_cast (type, expression, tf_warning_or_error);
5007             break;
5008           default:
5009             gcc_unreachable ();
5010           }
5011       }
5012       break;
5013
5014     case RID_TYPEID:
5015       {
5016         tree type;
5017         const char *saved_message;
5018         bool saved_in_type_id_in_expr_p;
5019
5020         /* Consume the `typeid' token.  */
5021         cp_lexer_consume_token (parser->lexer);
5022         /* Look for the `(' token.  */
5023         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5024         /* Types cannot be defined in a `typeid' expression.  */
5025         saved_message = parser->type_definition_forbidden_message;
5026         parser->type_definition_forbidden_message
5027           = G_("types may not be defined in a %<typeid%> expression");
5028         /* We can't be sure yet whether we're looking at a type-id or an
5029            expression.  */
5030         cp_parser_parse_tentatively (parser);
5031         /* Try a type-id first.  */
5032         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5033         parser->in_type_id_in_expr_p = true;
5034         type = cp_parser_type_id (parser);
5035         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5036         /* Look for the `)' token.  Otherwise, we can't be sure that
5037            we're not looking at an expression: consider `typeid (int
5038            (3))', for example.  */
5039         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5040         /* If all went well, simply lookup the type-id.  */
5041         if (cp_parser_parse_definitely (parser))
5042           postfix_expression = get_typeid (type);
5043         /* Otherwise, fall back to the expression variant.  */
5044         else
5045           {
5046             tree expression;
5047
5048             /* Look for an expression.  */
5049             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5050             /* Compute its typeid.  */
5051             postfix_expression = build_typeid (expression);
5052             /* Look for the `)' token.  */
5053             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5054           }
5055         /* Restore the saved message.  */
5056         parser->type_definition_forbidden_message = saved_message;
5057         /* `typeid' may not appear in an integral constant expression.  */
5058         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5059           return error_mark_node;
5060       }
5061       break;
5062
5063     case RID_TYPENAME:
5064       {
5065         tree type;
5066         /* The syntax permitted here is the same permitted for an
5067            elaborated-type-specifier.  */
5068         type = cp_parser_elaborated_type_specifier (parser,
5069                                                     /*is_friend=*/false,
5070                                                     /*is_declaration=*/false);
5071         postfix_expression = cp_parser_functional_cast (parser, type);
5072       }
5073       break;
5074
5075     default:
5076       {
5077         tree type;
5078
5079         /* If the next thing is a simple-type-specifier, we may be
5080            looking at a functional cast.  We could also be looking at
5081            an id-expression.  So, we try the functional cast, and if
5082            that doesn't work we fall back to the primary-expression.  */
5083         cp_parser_parse_tentatively (parser);
5084         /* Look for the simple-type-specifier.  */
5085         type = cp_parser_simple_type_specifier (parser,
5086                                                 /*decl_specs=*/NULL,
5087                                                 CP_PARSER_FLAGS_NONE);
5088         /* Parse the cast itself.  */
5089         if (!cp_parser_error_occurred (parser))
5090           postfix_expression
5091             = cp_parser_functional_cast (parser, type);
5092         /* If that worked, we're done.  */
5093         if (cp_parser_parse_definitely (parser))
5094           break;
5095
5096         /* If the functional-cast didn't work out, try a
5097            compound-literal.  */
5098         if (cp_parser_allow_gnu_extensions_p (parser)
5099             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5100           {
5101             VEC(constructor_elt,gc) *initializer_list = NULL;
5102             bool saved_in_type_id_in_expr_p;
5103
5104             cp_parser_parse_tentatively (parser);
5105             /* Consume the `('.  */
5106             cp_lexer_consume_token (parser->lexer);
5107             /* Parse the type.  */
5108             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5109             parser->in_type_id_in_expr_p = true;
5110             type = cp_parser_type_id (parser);
5111             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5112             /* Look for the `)'.  */
5113             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5114             /* Look for the `{'.  */
5115             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5116             /* If things aren't going well, there's no need to
5117                keep going.  */
5118             if (!cp_parser_error_occurred (parser))
5119               {
5120                 bool non_constant_p;
5121                 /* Parse the initializer-list.  */
5122                 initializer_list
5123                   = cp_parser_initializer_list (parser, &non_constant_p);
5124                 /* Allow a trailing `,'.  */
5125                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5126                   cp_lexer_consume_token (parser->lexer);
5127                 /* Look for the final `}'.  */
5128                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5129               }
5130             /* If that worked, we're definitely looking at a
5131                compound-literal expression.  */
5132             if (cp_parser_parse_definitely (parser))
5133               {
5134                 /* Warn the user that a compound literal is not
5135                    allowed in standard C++.  */
5136                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5137                 /* For simplicity, we disallow compound literals in
5138                    constant-expressions.  We could
5139                    allow compound literals of integer type, whose
5140                    initializer was a constant, in constant
5141                    expressions.  Permitting that usage, as a further
5142                    extension, would not change the meaning of any
5143                    currently accepted programs.  (Of course, as
5144                    compound literals are not part of ISO C++, the
5145                    standard has nothing to say.)  */
5146                 if (cp_parser_non_integral_constant_expression (parser,
5147                                                                 NIC_NCC))
5148                   {
5149                     postfix_expression = error_mark_node;
5150                     break;
5151                   }
5152                 /* Form the representation of the compound-literal.  */
5153                 postfix_expression
5154                   = (finish_compound_literal
5155                      (type, build_constructor (init_list_type_node,
5156                                                initializer_list)));
5157                 break;
5158               }
5159           }
5160
5161         /* It must be a primary-expression.  */
5162         postfix_expression
5163           = cp_parser_primary_expression (parser, address_p, cast_p,
5164                                           /*template_arg_p=*/false,
5165                                           &idk);
5166       }
5167       break;
5168     }
5169
5170   /* Keep looping until the postfix-expression is complete.  */
5171   while (true)
5172     {
5173       if (idk == CP_ID_KIND_UNQUALIFIED
5174           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5175           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5176         /* It is not a Koenig lookup function call.  */
5177         postfix_expression
5178           = unqualified_name_lookup_error (postfix_expression);
5179
5180       /* Peek at the next token.  */
5181       token = cp_lexer_peek_token (parser->lexer);
5182
5183       switch (token->type)
5184         {
5185         case CPP_OPEN_SQUARE:
5186           postfix_expression
5187             = cp_parser_postfix_open_square_expression (parser,
5188                                                         postfix_expression,
5189                                                         false);
5190           idk = CP_ID_KIND_NONE;
5191           is_member_access = false;
5192           break;
5193
5194         case CPP_OPEN_PAREN:
5195           /* postfix-expression ( expression-list [opt] ) */
5196           {
5197             bool koenig_p;
5198             bool is_builtin_constant_p;
5199             bool saved_integral_constant_expression_p = false;
5200             bool saved_non_integral_constant_expression_p = false;
5201             VEC(tree,gc) *args;
5202
5203             is_member_access = false;
5204
5205             is_builtin_constant_p
5206               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5207             if (is_builtin_constant_p)
5208               {
5209                 /* The whole point of __builtin_constant_p is to allow
5210                    non-constant expressions to appear as arguments.  */
5211                 saved_integral_constant_expression_p
5212                   = parser->integral_constant_expression_p;
5213                 saved_non_integral_constant_expression_p
5214                   = parser->non_integral_constant_expression_p;
5215                 parser->integral_constant_expression_p = false;
5216               }
5217             args = (cp_parser_parenthesized_expression_list
5218                     (parser, non_attr,
5219                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5220                      /*non_constant_p=*/NULL));
5221             if (is_builtin_constant_p)
5222               {
5223                 parser->integral_constant_expression_p
5224                   = saved_integral_constant_expression_p;
5225                 parser->non_integral_constant_expression_p
5226                   = saved_non_integral_constant_expression_p;
5227               }
5228
5229             if (args == NULL)
5230               {
5231                 postfix_expression = error_mark_node;
5232                 break;
5233               }
5234
5235             /* Function calls are not permitted in
5236                constant-expressions.  */
5237             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5238                 && cp_parser_non_integral_constant_expression (parser,
5239                                                                NIC_FUNC_CALL))
5240               {
5241                 postfix_expression = error_mark_node;
5242                 release_tree_vector (args);
5243                 break;
5244               }
5245
5246             koenig_p = false;
5247             if (idk == CP_ID_KIND_UNQUALIFIED
5248                 || idk == CP_ID_KIND_TEMPLATE_ID)
5249               {
5250                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5251                   {
5252                     if (!VEC_empty (tree, args))
5253                       {
5254                         koenig_p = true;
5255                         if (!any_type_dependent_arguments_p (args))
5256                           postfix_expression
5257                             = perform_koenig_lookup (postfix_expression, args,
5258                                                      /*include_std=*/false);
5259                       }
5260                     else
5261                       postfix_expression
5262                         = unqualified_fn_lookup_error (postfix_expression);
5263                   }
5264                 /* We do not perform argument-dependent lookup if
5265                    normal lookup finds a non-function, in accordance
5266                    with the expected resolution of DR 218.  */
5267                 else if (!VEC_empty (tree, args)
5268                          && is_overloaded_fn (postfix_expression))
5269                   {
5270                     tree fn = get_first_fn (postfix_expression);
5271                     fn = STRIP_TEMPLATE (fn);
5272
5273                     /* Do not do argument dependent lookup if regular
5274                        lookup finds a member function or a block-scope
5275                        function declaration.  [basic.lookup.argdep]/3  */
5276                     if (!DECL_FUNCTION_MEMBER_P (fn)
5277                         && !DECL_LOCAL_FUNCTION_P (fn))
5278                       {
5279                         koenig_p = true;
5280                         if (!any_type_dependent_arguments_p (args))
5281                           postfix_expression
5282                             = perform_koenig_lookup (postfix_expression, args,
5283                                                      /*include_std=*/false);
5284                       }
5285                   }
5286               }
5287
5288             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5289               {
5290                 tree instance = TREE_OPERAND (postfix_expression, 0);
5291                 tree fn = TREE_OPERAND (postfix_expression, 1);
5292
5293                 if (processing_template_decl
5294                     && (type_dependent_expression_p (instance)
5295                         || (!BASELINK_P (fn)
5296                             && TREE_CODE (fn) != FIELD_DECL)
5297                         || type_dependent_expression_p (fn)
5298                         || any_type_dependent_arguments_p (args)))
5299                   {
5300                     postfix_expression
5301                       = build_nt_call_vec (postfix_expression, args);
5302                     release_tree_vector (args);
5303                     break;
5304                   }
5305
5306                 if (BASELINK_P (fn))
5307                   {
5308                   postfix_expression
5309                     = (build_new_method_call
5310                        (instance, fn, &args, NULL_TREE,
5311                         (idk == CP_ID_KIND_QUALIFIED
5312                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5313                         /*fn_p=*/NULL,
5314                         tf_warning_or_error));
5315                   }
5316                 else
5317                   postfix_expression
5318                     = finish_call_expr (postfix_expression, &args,
5319                                         /*disallow_virtual=*/false,
5320                                         /*koenig_p=*/false,
5321                                         tf_warning_or_error);
5322               }
5323             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5324                      || TREE_CODE (postfix_expression) == MEMBER_REF
5325                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5326               postfix_expression = (build_offset_ref_call_from_tree
5327                                     (postfix_expression, &args));
5328             else if (idk == CP_ID_KIND_QUALIFIED)
5329               /* A call to a static class member, or a namespace-scope
5330                  function.  */
5331               postfix_expression
5332                 = finish_call_expr (postfix_expression, &args,
5333                                     /*disallow_virtual=*/true,
5334                                     koenig_p,
5335                                     tf_warning_or_error);
5336             else
5337               /* All other function calls.  */
5338               postfix_expression
5339                 = finish_call_expr (postfix_expression, &args,
5340                                     /*disallow_virtual=*/false,
5341                                     koenig_p,
5342                                     tf_warning_or_error);
5343
5344             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5345             idk = CP_ID_KIND_NONE;
5346
5347             release_tree_vector (args);
5348           }
5349           break;
5350
5351         case CPP_DOT:
5352         case CPP_DEREF:
5353           /* postfix-expression . template [opt] id-expression
5354              postfix-expression . pseudo-destructor-name
5355              postfix-expression -> template [opt] id-expression
5356              postfix-expression -> pseudo-destructor-name */
5357
5358           /* Consume the `.' or `->' operator.  */
5359           cp_lexer_consume_token (parser->lexer);
5360
5361           postfix_expression
5362             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5363                                                       postfix_expression,
5364                                                       false, &idk,
5365                                                       token->location);
5366
5367           is_member_access = true;
5368           break;
5369
5370         case CPP_PLUS_PLUS:
5371           /* postfix-expression ++  */
5372           /* Consume the `++' token.  */
5373           cp_lexer_consume_token (parser->lexer);
5374           /* Generate a representation for the complete expression.  */
5375           postfix_expression
5376             = finish_increment_expr (postfix_expression,
5377                                      POSTINCREMENT_EXPR);
5378           /* Increments may not appear in constant-expressions.  */
5379           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5380             postfix_expression = error_mark_node;
5381           idk = CP_ID_KIND_NONE;
5382           is_member_access = false;
5383           break;
5384
5385         case CPP_MINUS_MINUS:
5386           /* postfix-expression -- */
5387           /* Consume the `--' token.  */
5388           cp_lexer_consume_token (parser->lexer);
5389           /* Generate a representation for the complete expression.  */
5390           postfix_expression
5391             = finish_increment_expr (postfix_expression,
5392                                      POSTDECREMENT_EXPR);
5393           /* Decrements may not appear in constant-expressions.  */
5394           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5395             postfix_expression = error_mark_node;
5396           idk = CP_ID_KIND_NONE;
5397           is_member_access = false;
5398           break;
5399
5400         default:
5401           if (pidk_return != NULL)
5402             * pidk_return = idk;
5403           if (member_access_only_p)
5404             return is_member_access? postfix_expression : error_mark_node;
5405           else
5406             return postfix_expression;
5407         }
5408     }
5409
5410   /* We should never get here.  */
5411   gcc_unreachable ();
5412   return error_mark_node;
5413 }
5414
5415 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5416    by cp_parser_builtin_offsetof.  We're looking for
5417
5418      postfix-expression [ expression ]
5419
5420    FOR_OFFSETOF is set if we're being called in that context, which
5421    changes how we deal with integer constant expressions.  */
5422
5423 static tree
5424 cp_parser_postfix_open_square_expression (cp_parser *parser,
5425                                           tree postfix_expression,
5426                                           bool for_offsetof)
5427 {
5428   tree index;
5429
5430   /* Consume the `[' token.  */
5431   cp_lexer_consume_token (parser->lexer);
5432
5433   /* Parse the index expression.  */
5434   /* ??? For offsetof, there is a question of what to allow here.  If
5435      offsetof is not being used in an integral constant expression context,
5436      then we *could* get the right answer by computing the value at runtime.
5437      If we are in an integral constant expression context, then we might
5438      could accept any constant expression; hard to say without analysis.
5439      Rather than open the barn door too wide right away, allow only integer
5440      constant expressions here.  */
5441   if (for_offsetof)
5442     index = cp_parser_constant_expression (parser, false, NULL);
5443   else
5444     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5445
5446   /* Look for the closing `]'.  */
5447   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5448
5449   /* Build the ARRAY_REF.  */
5450   postfix_expression = grok_array_decl (postfix_expression, index);
5451
5452   /* When not doing offsetof, array references are not permitted in
5453      constant-expressions.  */
5454   if (!for_offsetof
5455       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5456     postfix_expression = error_mark_node;
5457
5458   return postfix_expression;
5459 }
5460
5461 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5462    by cp_parser_builtin_offsetof.  We're looking for
5463
5464      postfix-expression . template [opt] id-expression
5465      postfix-expression . pseudo-destructor-name
5466      postfix-expression -> template [opt] id-expression
5467      postfix-expression -> pseudo-destructor-name
5468
5469    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5470    limits what of the above we'll actually accept, but nevermind.
5471    TOKEN_TYPE is the "." or "->" token, which will already have been
5472    removed from the stream.  */
5473
5474 static tree
5475 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5476                                         enum cpp_ttype token_type,
5477                                         tree postfix_expression,
5478                                         bool for_offsetof, cp_id_kind *idk,
5479                                         location_t location)
5480 {
5481   tree name;
5482   bool dependent_p;
5483   bool pseudo_destructor_p;
5484   tree scope = NULL_TREE;
5485
5486   /* If this is a `->' operator, dereference the pointer.  */
5487   if (token_type == CPP_DEREF)
5488     postfix_expression = build_x_arrow (postfix_expression);
5489   /* Check to see whether or not the expression is type-dependent.  */
5490   dependent_p = type_dependent_expression_p (postfix_expression);
5491   /* The identifier following the `->' or `.' is not qualified.  */
5492   parser->scope = NULL_TREE;
5493   parser->qualifying_scope = NULL_TREE;
5494   parser->object_scope = NULL_TREE;
5495   *idk = CP_ID_KIND_NONE;
5496
5497   /* Enter the scope corresponding to the type of the object
5498      given by the POSTFIX_EXPRESSION.  */
5499   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5500     {
5501       scope = TREE_TYPE (postfix_expression);
5502       /* According to the standard, no expression should ever have
5503          reference type.  Unfortunately, we do not currently match
5504          the standard in this respect in that our internal representation
5505          of an expression may have reference type even when the standard
5506          says it does not.  Therefore, we have to manually obtain the
5507          underlying type here.  */
5508       scope = non_reference (scope);
5509       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5510       if (scope == unknown_type_node)
5511         {
5512           error_at (location, "%qE does not have class type",
5513                     postfix_expression);
5514           scope = NULL_TREE;
5515         }
5516       else
5517         scope = complete_type_or_else (scope, NULL_TREE);
5518       /* Let the name lookup machinery know that we are processing a
5519          class member access expression.  */
5520       parser->context->object_type = scope;
5521       /* If something went wrong, we want to be able to discern that case,
5522          as opposed to the case where there was no SCOPE due to the type
5523          of expression being dependent.  */
5524       if (!scope)
5525         scope = error_mark_node;
5526       /* If the SCOPE was erroneous, make the various semantic analysis
5527          functions exit quickly -- and without issuing additional error
5528          messages.  */
5529       if (scope == error_mark_node)
5530         postfix_expression = error_mark_node;
5531     }
5532
5533   /* Assume this expression is not a pseudo-destructor access.  */
5534   pseudo_destructor_p = false;
5535
5536   /* If the SCOPE is a scalar type, then, if this is a valid program,
5537      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5538      is type dependent, it can be pseudo-destructor-name or something else.
5539      Try to parse it as pseudo-destructor-name first.  */
5540   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5541     {
5542       tree s;
5543       tree type;
5544
5545       cp_parser_parse_tentatively (parser);
5546       /* Parse the pseudo-destructor-name.  */
5547       s = NULL_TREE;
5548       cp_parser_pseudo_destructor_name (parser, &s, &type);
5549       if (dependent_p
5550           && (cp_parser_error_occurred (parser)
5551               || TREE_CODE (type) != TYPE_DECL
5552               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5553         cp_parser_abort_tentative_parse (parser);
5554       else if (cp_parser_parse_definitely (parser))
5555         {
5556           pseudo_destructor_p = true;
5557           postfix_expression
5558             = finish_pseudo_destructor_expr (postfix_expression,
5559                                              s, TREE_TYPE (type));
5560         }
5561     }
5562
5563   if (!pseudo_destructor_p)
5564     {
5565       /* If the SCOPE is not a scalar type, we are looking at an
5566          ordinary class member access expression, rather than a
5567          pseudo-destructor-name.  */
5568       bool template_p;
5569       cp_token *token = cp_lexer_peek_token (parser->lexer);
5570       /* Parse the id-expression.  */
5571       name = (cp_parser_id_expression
5572               (parser,
5573                cp_parser_optional_template_keyword (parser),
5574                /*check_dependency_p=*/true,
5575                &template_p,
5576                /*declarator_p=*/false,
5577                /*optional_p=*/false));
5578       /* In general, build a SCOPE_REF if the member name is qualified.
5579          However, if the name was not dependent and has already been
5580          resolved; there is no need to build the SCOPE_REF.  For example;
5581
5582              struct X { void f(); };
5583              template <typename T> void f(T* t) { t->X::f(); }
5584
5585          Even though "t" is dependent, "X::f" is not and has been resolved
5586          to a BASELINK; there is no need to include scope information.  */
5587
5588       /* But we do need to remember that there was an explicit scope for
5589          virtual function calls.  */
5590       if (parser->scope)
5591         *idk = CP_ID_KIND_QUALIFIED;
5592
5593       /* If the name is a template-id that names a type, we will get a
5594          TYPE_DECL here.  That is invalid code.  */
5595       if (TREE_CODE (name) == TYPE_DECL)
5596         {
5597           error_at (token->location, "invalid use of %qD", name);
5598           postfix_expression = error_mark_node;
5599         }
5600       else
5601         {
5602           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5603             {
5604               name = build_qualified_name (/*type=*/NULL_TREE,
5605                                            parser->scope,
5606                                            name,
5607                                            template_p);
5608               parser->scope = NULL_TREE;
5609               parser->qualifying_scope = NULL_TREE;
5610               parser->object_scope = NULL_TREE;
5611             }
5612           if (scope && name && BASELINK_P (name))
5613             adjust_result_of_qualified_name_lookup
5614               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5615           postfix_expression
5616             = finish_class_member_access_expr (postfix_expression, name,
5617                                                template_p, 
5618                                                tf_warning_or_error);
5619         }
5620     }
5621
5622   /* We no longer need to look up names in the scope of the object on
5623      the left-hand side of the `.' or `->' operator.  */
5624   parser->context->object_type = NULL_TREE;
5625
5626   /* Outside of offsetof, these operators may not appear in
5627      constant-expressions.  */
5628   if (!for_offsetof
5629       && (cp_parser_non_integral_constant_expression
5630           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5631     postfix_expression = error_mark_node;
5632
5633   return postfix_expression;
5634 }
5635
5636 /* Parse a parenthesized expression-list.
5637
5638    expression-list:
5639      assignment-expression
5640      expression-list, assignment-expression
5641
5642    attribute-list:
5643      expression-list
5644      identifier
5645      identifier, expression-list
5646
5647    CAST_P is true if this expression is the target of a cast.
5648
5649    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5650    argument pack.
5651
5652    Returns a vector of trees.  Each element is a representation of an
5653    assignment-expression.  NULL is returned if the ( and or ) are
5654    missing.  An empty, but allocated, vector is returned on no
5655    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5656    if we are parsing an attribute list for an attribute that wants a
5657    plain identifier argument, normal_attr for an attribute that wants
5658    an expression, or non_attr if we aren't parsing an attribute list.  If
5659    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5660    not all of the expressions in the list were constant.  */
5661
5662 static VEC(tree,gc) *
5663 cp_parser_parenthesized_expression_list (cp_parser* parser,
5664                                          int is_attribute_list,
5665                                          bool cast_p,
5666                                          bool allow_expansion_p,
5667                                          bool *non_constant_p)
5668 {
5669   VEC(tree,gc) *expression_list;
5670   bool fold_expr_p = is_attribute_list != non_attr;
5671   tree identifier = NULL_TREE;
5672   bool saved_greater_than_is_operator_p;
5673
5674   /* Assume all the expressions will be constant.  */
5675   if (non_constant_p)
5676     *non_constant_p = false;
5677
5678   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5679     return NULL;
5680
5681   expression_list = make_tree_vector ();
5682
5683   /* Within a parenthesized expression, a `>' token is always
5684      the greater-than operator.  */
5685   saved_greater_than_is_operator_p
5686     = parser->greater_than_is_operator_p;
5687   parser->greater_than_is_operator_p = true;
5688
5689   /* Consume expressions until there are no more.  */
5690   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5691     while (true)
5692       {
5693         tree expr;
5694
5695         /* At the beginning of attribute lists, check to see if the
5696            next token is an identifier.  */
5697         if (is_attribute_list == id_attr
5698             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5699           {
5700             cp_token *token;
5701
5702             /* Consume the identifier.  */
5703             token = cp_lexer_consume_token (parser->lexer);
5704             /* Save the identifier.  */
5705             identifier = token->u.value;
5706           }
5707         else
5708           {
5709             bool expr_non_constant_p;
5710
5711             /* Parse the next assignment-expression.  */
5712             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5713               {
5714                 /* A braced-init-list.  */
5715                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5716                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5717                 if (non_constant_p && expr_non_constant_p)
5718                   *non_constant_p = true;
5719               }
5720             else if (non_constant_p)
5721               {
5722                 expr = (cp_parser_constant_expression
5723                         (parser, /*allow_non_constant_p=*/true,
5724                          &expr_non_constant_p));
5725                 if (expr_non_constant_p)
5726                   *non_constant_p = true;
5727               }
5728             else
5729               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5730
5731             if (fold_expr_p)
5732               expr = fold_non_dependent_expr (expr);
5733
5734             /* If we have an ellipsis, then this is an expression
5735                expansion.  */
5736             if (allow_expansion_p
5737                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5738               {
5739                 /* Consume the `...'.  */
5740                 cp_lexer_consume_token (parser->lexer);
5741
5742                 /* Build the argument pack.  */
5743                 expr = make_pack_expansion (expr);
5744               }
5745
5746              /* Add it to the list.  We add error_mark_node
5747                 expressions to the list, so that we can still tell if
5748                 the correct form for a parenthesized expression-list
5749                 is found. That gives better errors.  */
5750             VEC_safe_push (tree, gc, expression_list, expr);
5751
5752             if (expr == error_mark_node)
5753               goto skip_comma;
5754           }
5755
5756         /* After the first item, attribute lists look the same as
5757            expression lists.  */
5758         is_attribute_list = non_attr;
5759
5760       get_comma:;
5761         /* If the next token isn't a `,', then we are done.  */
5762         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5763           break;
5764
5765         /* Otherwise, consume the `,' and keep going.  */
5766         cp_lexer_consume_token (parser->lexer);
5767       }
5768
5769   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5770     {
5771       int ending;
5772
5773     skip_comma:;
5774       /* We try and resync to an unnested comma, as that will give the
5775          user better diagnostics.  */
5776       ending = cp_parser_skip_to_closing_parenthesis (parser,
5777                                                       /*recovering=*/true,
5778                                                       /*or_comma=*/true,
5779                                                       /*consume_paren=*/true);
5780       if (ending < 0)
5781         goto get_comma;
5782       if (!ending)
5783         {
5784           parser->greater_than_is_operator_p
5785             = saved_greater_than_is_operator_p;
5786           return NULL;
5787         }
5788     }
5789
5790   parser->greater_than_is_operator_p
5791     = saved_greater_than_is_operator_p;
5792
5793   if (identifier)
5794     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5795
5796   return expression_list;
5797 }
5798
5799 /* Parse a pseudo-destructor-name.
5800
5801    pseudo-destructor-name:
5802      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5803      :: [opt] nested-name-specifier template template-id :: ~ type-name
5804      :: [opt] nested-name-specifier [opt] ~ type-name
5805
5806    If either of the first two productions is used, sets *SCOPE to the
5807    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5808    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5809    or ERROR_MARK_NODE if the parse fails.  */
5810
5811 static void
5812 cp_parser_pseudo_destructor_name (cp_parser* parser,
5813                                   tree* scope,
5814                                   tree* type)
5815 {
5816   bool nested_name_specifier_p;
5817
5818   /* Assume that things will not work out.  */
5819   *type = error_mark_node;
5820
5821   /* Look for the optional `::' operator.  */
5822   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5823   /* Look for the optional nested-name-specifier.  */
5824   nested_name_specifier_p
5825     = (cp_parser_nested_name_specifier_opt (parser,
5826                                             /*typename_keyword_p=*/false,
5827                                             /*check_dependency_p=*/true,
5828                                             /*type_p=*/false,
5829                                             /*is_declaration=*/false)
5830        != NULL_TREE);
5831   /* Now, if we saw a nested-name-specifier, we might be doing the
5832      second production.  */
5833   if (nested_name_specifier_p
5834       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5835     {
5836       /* Consume the `template' keyword.  */
5837       cp_lexer_consume_token (parser->lexer);
5838       /* Parse the template-id.  */
5839       cp_parser_template_id (parser,
5840                              /*template_keyword_p=*/true,
5841                              /*check_dependency_p=*/false,
5842                              /*is_declaration=*/true);
5843       /* Look for the `::' token.  */
5844       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5845     }
5846   /* If the next token is not a `~', then there might be some
5847      additional qualification.  */
5848   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5849     {
5850       /* At this point, we're looking for "type-name :: ~".  The type-name
5851          must not be a class-name, since this is a pseudo-destructor.  So,
5852          it must be either an enum-name, or a typedef-name -- both of which
5853          are just identifiers.  So, we peek ahead to check that the "::"
5854          and "~" tokens are present; if they are not, then we can avoid
5855          calling type_name.  */
5856       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5857           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5858           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5859         {
5860           cp_parser_error (parser, "non-scalar type");
5861           return;
5862         }
5863
5864       /* Look for the type-name.  */
5865       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5866       if (*scope == error_mark_node)
5867         return;
5868
5869       /* Look for the `::' token.  */
5870       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5871     }
5872   else
5873     *scope = NULL_TREE;
5874
5875   /* Look for the `~'.  */
5876   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5877   /* Look for the type-name again.  We are not responsible for
5878      checking that it matches the first type-name.  */
5879   *type = cp_parser_nonclass_name (parser);
5880 }
5881
5882 /* Parse a unary-expression.
5883
5884    unary-expression:
5885      postfix-expression
5886      ++ cast-expression
5887      -- cast-expression
5888      unary-operator cast-expression
5889      sizeof unary-expression
5890      sizeof ( type-id )
5891      new-expression
5892      delete-expression
5893
5894    GNU Extensions:
5895
5896    unary-expression:
5897      __extension__ cast-expression
5898      __alignof__ unary-expression
5899      __alignof__ ( type-id )
5900      __real__ cast-expression
5901      __imag__ cast-expression
5902      && identifier
5903
5904    ADDRESS_P is true iff the unary-expression is appearing as the
5905    operand of the `&' operator.   CAST_P is true if this expression is
5906    the target of a cast.
5907
5908    Returns a representation of the expression.  */
5909
5910 static tree
5911 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5912                             cp_id_kind * pidk)
5913 {
5914   cp_token *token;
5915   enum tree_code unary_operator;
5916
5917   /* Peek at the next token.  */
5918   token = cp_lexer_peek_token (parser->lexer);
5919   /* Some keywords give away the kind of expression.  */
5920   if (token->type == CPP_KEYWORD)
5921     {
5922       enum rid keyword = token->keyword;
5923
5924       switch (keyword)
5925         {
5926         case RID_ALIGNOF:
5927         case RID_SIZEOF:
5928           {
5929             tree operand;
5930             enum tree_code op;
5931
5932             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5933             /* Consume the token.  */
5934             cp_lexer_consume_token (parser->lexer);
5935             /* Parse the operand.  */
5936             operand = cp_parser_sizeof_operand (parser, keyword);
5937
5938             if (TYPE_P (operand))
5939               return cxx_sizeof_or_alignof_type (operand, op, true);
5940             else
5941               return cxx_sizeof_or_alignof_expr (operand, op, true);
5942           }
5943
5944         case RID_NEW:
5945           return cp_parser_new_expression (parser);
5946
5947         case RID_DELETE:
5948           return cp_parser_delete_expression (parser);
5949
5950         case RID_EXTENSION:
5951           {
5952             /* The saved value of the PEDANTIC flag.  */
5953             int saved_pedantic;
5954             tree expr;
5955
5956             /* Save away the PEDANTIC flag.  */
5957             cp_parser_extension_opt (parser, &saved_pedantic);
5958             /* Parse the cast-expression.  */
5959             expr = cp_parser_simple_cast_expression (parser);
5960             /* Restore the PEDANTIC flag.  */
5961             pedantic = saved_pedantic;
5962
5963             return expr;
5964           }
5965
5966         case RID_REALPART:
5967         case RID_IMAGPART:
5968           {
5969             tree expression;
5970
5971             /* Consume the `__real__' or `__imag__' token.  */
5972             cp_lexer_consume_token (parser->lexer);
5973             /* Parse the cast-expression.  */
5974             expression = cp_parser_simple_cast_expression (parser);
5975             /* Create the complete representation.  */
5976             return build_x_unary_op ((keyword == RID_REALPART
5977                                       ? REALPART_EXPR : IMAGPART_EXPR),
5978                                      expression,
5979                                      tf_warning_or_error);
5980           }
5981           break;
5982
5983         case RID_NOEXCEPT:
5984           {
5985             tree expr;
5986             const char *saved_message;
5987             bool saved_integral_constant_expression_p;
5988             bool saved_non_integral_constant_expression_p;
5989             bool saved_greater_than_is_operator_p;
5990
5991             cp_lexer_consume_token (parser->lexer);
5992             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5993
5994             saved_message = parser->type_definition_forbidden_message;
5995             parser->type_definition_forbidden_message
5996               = G_("types may not be defined in %<noexcept%> expressions");
5997
5998             saved_integral_constant_expression_p
5999               = parser->integral_constant_expression_p;
6000             saved_non_integral_constant_expression_p
6001               = parser->non_integral_constant_expression_p;
6002             parser->integral_constant_expression_p = false;
6003
6004             saved_greater_than_is_operator_p
6005               = parser->greater_than_is_operator_p;
6006             parser->greater_than_is_operator_p = true;
6007
6008             ++cp_unevaluated_operand;
6009             ++c_inhibit_evaluation_warnings;
6010             expr = cp_parser_expression (parser, false, NULL);
6011             --c_inhibit_evaluation_warnings;
6012             --cp_unevaluated_operand;
6013
6014             parser->greater_than_is_operator_p
6015               = saved_greater_than_is_operator_p;
6016
6017             parser->integral_constant_expression_p
6018               = saved_integral_constant_expression_p;
6019             parser->non_integral_constant_expression_p
6020               = saved_non_integral_constant_expression_p;
6021
6022             parser->type_definition_forbidden_message = saved_message;
6023
6024             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6025             return finish_noexcept_expr (expr, tf_warning_or_error);
6026           }
6027
6028         default:
6029           break;
6030         }
6031     }
6032
6033   /* Look for the `:: new' and `:: delete', which also signal the
6034      beginning of a new-expression, or delete-expression,
6035      respectively.  If the next token is `::', then it might be one of
6036      these.  */
6037   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6038     {
6039       enum rid keyword;
6040
6041       /* See if the token after the `::' is one of the keywords in
6042          which we're interested.  */
6043       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6044       /* If it's `new', we have a new-expression.  */
6045       if (keyword == RID_NEW)
6046         return cp_parser_new_expression (parser);
6047       /* Similarly, for `delete'.  */
6048       else if (keyword == RID_DELETE)
6049         return cp_parser_delete_expression (parser);
6050     }
6051
6052   /* Look for a unary operator.  */
6053   unary_operator = cp_parser_unary_operator (token);
6054   /* The `++' and `--' operators can be handled similarly, even though
6055      they are not technically unary-operators in the grammar.  */
6056   if (unary_operator == ERROR_MARK)
6057     {
6058       if (token->type == CPP_PLUS_PLUS)
6059         unary_operator = PREINCREMENT_EXPR;
6060       else if (token->type == CPP_MINUS_MINUS)
6061         unary_operator = PREDECREMENT_EXPR;
6062       /* Handle the GNU address-of-label extension.  */
6063       else if (cp_parser_allow_gnu_extensions_p (parser)
6064                && token->type == CPP_AND_AND)
6065         {
6066           tree identifier;
6067           tree expression;
6068           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6069
6070           /* Consume the '&&' token.  */
6071           cp_lexer_consume_token (parser->lexer);
6072           /* Look for the identifier.  */
6073           identifier = cp_parser_identifier (parser);
6074           /* Create an expression representing the address.  */
6075           expression = finish_label_address_expr (identifier, loc);
6076           if (cp_parser_non_integral_constant_expression (parser,
6077                                                           NIC_ADDR_LABEL))
6078             expression = error_mark_node;
6079           return expression;
6080         }
6081     }
6082   if (unary_operator != ERROR_MARK)
6083     {
6084       tree cast_expression;
6085       tree expression = error_mark_node;
6086       non_integral_constant non_constant_p = NIC_NONE;
6087
6088       /* Consume the operator token.  */
6089       token = cp_lexer_consume_token (parser->lexer);
6090       /* Parse the cast-expression.  */
6091       cast_expression
6092         = cp_parser_cast_expression (parser,
6093                                      unary_operator == ADDR_EXPR,
6094                                      /*cast_p=*/false, pidk);
6095       /* Now, build an appropriate representation.  */
6096       switch (unary_operator)
6097         {
6098         case INDIRECT_REF:
6099           non_constant_p = NIC_STAR;
6100           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6101                                              tf_warning_or_error);
6102           break;
6103
6104         case ADDR_EXPR:
6105            non_constant_p = NIC_ADDR;
6106           /* Fall through.  */
6107         case BIT_NOT_EXPR:
6108           expression = build_x_unary_op (unary_operator, cast_expression,
6109                                          tf_warning_or_error);
6110           break;
6111
6112         case PREINCREMENT_EXPR:
6113         case PREDECREMENT_EXPR:
6114           non_constant_p = unary_operator == PREINCREMENT_EXPR
6115                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6116           /* Fall through.  */
6117         case UNARY_PLUS_EXPR:
6118         case NEGATE_EXPR:
6119         case TRUTH_NOT_EXPR:
6120           expression = finish_unary_op_expr (unary_operator, cast_expression);
6121           break;
6122
6123         default:
6124           gcc_unreachable ();
6125         }
6126
6127       if (non_constant_p != NIC_NONE
6128           && cp_parser_non_integral_constant_expression (parser,
6129                                                          non_constant_p))
6130         expression = error_mark_node;
6131
6132       return expression;
6133     }
6134
6135   return cp_parser_postfix_expression (parser, address_p, cast_p,
6136                                        /*member_access_only_p=*/false,
6137                                        pidk);
6138 }
6139
6140 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6141    unary-operator, the corresponding tree code is returned.  */
6142
6143 static enum tree_code
6144 cp_parser_unary_operator (cp_token* token)
6145 {
6146   switch (token->type)
6147     {
6148     case CPP_MULT:
6149       return INDIRECT_REF;
6150
6151     case CPP_AND:
6152       return ADDR_EXPR;
6153
6154     case CPP_PLUS:
6155       return UNARY_PLUS_EXPR;
6156
6157     case CPP_MINUS:
6158       return NEGATE_EXPR;
6159
6160     case CPP_NOT:
6161       return TRUTH_NOT_EXPR;
6162
6163     case CPP_COMPL:
6164       return BIT_NOT_EXPR;
6165
6166     default:
6167       return ERROR_MARK;
6168     }
6169 }
6170
6171 /* Parse a new-expression.
6172
6173    new-expression:
6174      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6175      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6176
6177    Returns a representation of the expression.  */
6178
6179 static tree
6180 cp_parser_new_expression (cp_parser* parser)
6181 {
6182   bool global_scope_p;
6183   VEC(tree,gc) *placement;
6184   tree type;
6185   VEC(tree,gc) *initializer;
6186   tree nelts;
6187   tree ret;
6188
6189   /* Look for the optional `::' operator.  */
6190   global_scope_p
6191     = (cp_parser_global_scope_opt (parser,
6192                                    /*current_scope_valid_p=*/false)
6193        != NULL_TREE);
6194   /* Look for the `new' operator.  */
6195   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6196   /* There's no easy way to tell a new-placement from the
6197      `( type-id )' construct.  */
6198   cp_parser_parse_tentatively (parser);
6199   /* Look for a new-placement.  */
6200   placement = cp_parser_new_placement (parser);
6201   /* If that didn't work out, there's no new-placement.  */
6202   if (!cp_parser_parse_definitely (parser))
6203     {
6204       if (placement != NULL)
6205         release_tree_vector (placement);
6206       placement = NULL;
6207     }
6208
6209   /* If the next token is a `(', then we have a parenthesized
6210      type-id.  */
6211   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6212     {
6213       cp_token *token;
6214       /* Consume the `('.  */
6215       cp_lexer_consume_token (parser->lexer);
6216       /* Parse the type-id.  */
6217       type = cp_parser_type_id (parser);
6218       /* Look for the closing `)'.  */
6219       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6220       token = cp_lexer_peek_token (parser->lexer);
6221       /* There should not be a direct-new-declarator in this production,
6222          but GCC used to allowed this, so we check and emit a sensible error
6223          message for this case.  */
6224       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6225         {
6226           error_at (token->location,
6227                     "array bound forbidden after parenthesized type-id");
6228           inform (token->location, 
6229                   "try removing the parentheses around the type-id");
6230           cp_parser_direct_new_declarator (parser);
6231         }
6232       nelts = NULL_TREE;
6233     }
6234   /* Otherwise, there must be a new-type-id.  */
6235   else
6236     type = cp_parser_new_type_id (parser, &nelts);
6237
6238   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6239   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6240       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6241     initializer = cp_parser_new_initializer (parser);
6242   else
6243     initializer = NULL;
6244
6245   /* A new-expression may not appear in an integral constant
6246      expression.  */
6247   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6248     ret = error_mark_node;
6249   else
6250     {
6251       /* Create a representation of the new-expression.  */
6252       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6253                        tf_warning_or_error);
6254     }
6255
6256   if (placement != NULL)
6257     release_tree_vector (placement);
6258   if (initializer != NULL)
6259     release_tree_vector (initializer);
6260
6261   return ret;
6262 }
6263
6264 /* Parse a new-placement.
6265
6266    new-placement:
6267      ( expression-list )
6268
6269    Returns the same representation as for an expression-list.  */
6270
6271 static VEC(tree,gc) *
6272 cp_parser_new_placement (cp_parser* parser)
6273 {
6274   VEC(tree,gc) *expression_list;
6275
6276   /* Parse the expression-list.  */
6277   expression_list = (cp_parser_parenthesized_expression_list
6278                      (parser, non_attr, /*cast_p=*/false,
6279                       /*allow_expansion_p=*/true,
6280                       /*non_constant_p=*/NULL));
6281
6282   return expression_list;
6283 }
6284
6285 /* Parse a new-type-id.
6286
6287    new-type-id:
6288      type-specifier-seq new-declarator [opt]
6289
6290    Returns the TYPE allocated.  If the new-type-id indicates an array
6291    type, *NELTS is set to the number of elements in the last array
6292    bound; the TYPE will not include the last array bound.  */
6293
6294 static tree
6295 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6296 {
6297   cp_decl_specifier_seq type_specifier_seq;
6298   cp_declarator *new_declarator;
6299   cp_declarator *declarator;
6300   cp_declarator *outer_declarator;
6301   const char *saved_message;
6302   tree type;
6303
6304   /* The type-specifier sequence must not contain type definitions.
6305      (It cannot contain declarations of new types either, but if they
6306      are not definitions we will catch that because they are not
6307      complete.)  */
6308   saved_message = parser->type_definition_forbidden_message;
6309   parser->type_definition_forbidden_message
6310     = G_("types may not be defined in a new-type-id");
6311   /* Parse the type-specifier-seq.  */
6312   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6313                                 /*is_trailing_return=*/false,
6314                                 &type_specifier_seq);
6315   /* Restore the old message.  */
6316   parser->type_definition_forbidden_message = saved_message;
6317   /* Parse the new-declarator.  */
6318   new_declarator = cp_parser_new_declarator_opt (parser);
6319
6320   /* Determine the number of elements in the last array dimension, if
6321      any.  */
6322   *nelts = NULL_TREE;
6323   /* Skip down to the last array dimension.  */
6324   declarator = new_declarator;
6325   outer_declarator = NULL;
6326   while (declarator && (declarator->kind == cdk_pointer
6327                         || declarator->kind == cdk_ptrmem))
6328     {
6329       outer_declarator = declarator;
6330       declarator = declarator->declarator;
6331     }
6332   while (declarator
6333          && declarator->kind == cdk_array
6334          && declarator->declarator
6335          && declarator->declarator->kind == cdk_array)
6336     {
6337       outer_declarator = declarator;
6338       declarator = declarator->declarator;
6339     }
6340
6341   if (declarator && declarator->kind == cdk_array)
6342     {
6343       *nelts = declarator->u.array.bounds;
6344       if (*nelts == error_mark_node)
6345         *nelts = integer_one_node;
6346
6347       if (outer_declarator)
6348         outer_declarator->declarator = declarator->declarator;
6349       else
6350         new_declarator = NULL;
6351     }
6352
6353   type = groktypename (&type_specifier_seq, new_declarator, false);
6354   return type;
6355 }
6356
6357 /* Parse an (optional) new-declarator.
6358
6359    new-declarator:
6360      ptr-operator new-declarator [opt]
6361      direct-new-declarator
6362
6363    Returns the declarator.  */
6364
6365 static cp_declarator *
6366 cp_parser_new_declarator_opt (cp_parser* parser)
6367 {
6368   enum tree_code code;
6369   tree type;
6370   cp_cv_quals cv_quals;
6371
6372   /* We don't know if there's a ptr-operator next, or not.  */
6373   cp_parser_parse_tentatively (parser);
6374   /* Look for a ptr-operator.  */
6375   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6376   /* If that worked, look for more new-declarators.  */
6377   if (cp_parser_parse_definitely (parser))
6378     {
6379       cp_declarator *declarator;
6380
6381       /* Parse another optional declarator.  */
6382       declarator = cp_parser_new_declarator_opt (parser);
6383
6384       return cp_parser_make_indirect_declarator
6385         (code, type, cv_quals, declarator);
6386     }
6387
6388   /* If the next token is a `[', there is a direct-new-declarator.  */
6389   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6390     return cp_parser_direct_new_declarator (parser);
6391
6392   return NULL;
6393 }
6394
6395 /* Parse a direct-new-declarator.
6396
6397    direct-new-declarator:
6398      [ expression ]
6399      direct-new-declarator [constant-expression]
6400
6401    */
6402
6403 static cp_declarator *
6404 cp_parser_direct_new_declarator (cp_parser* parser)
6405 {
6406   cp_declarator *declarator = NULL;
6407
6408   while (true)
6409     {
6410       tree expression;
6411
6412       /* Look for the opening `['.  */
6413       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6414       /* The first expression is not required to be constant.  */
6415       if (!declarator)
6416         {
6417           cp_token *token = cp_lexer_peek_token (parser->lexer);
6418           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6419           /* The standard requires that the expression have integral
6420              type.  DR 74 adds enumeration types.  We believe that the
6421              real intent is that these expressions be handled like the
6422              expression in a `switch' condition, which also allows
6423              classes with a single conversion to integral or
6424              enumeration type.  */
6425           if (!processing_template_decl)
6426             {
6427               expression
6428                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6429                                               expression,
6430                                               /*complain=*/true);
6431               if (!expression)
6432                 {
6433                   error_at (token->location,
6434                             "expression in new-declarator must have integral "
6435                             "or enumeration type");
6436                   expression = error_mark_node;
6437                 }
6438             }
6439         }
6440       /* But all the other expressions must be.  */
6441       else
6442         expression
6443           = cp_parser_constant_expression (parser,
6444                                            /*allow_non_constant=*/false,
6445                                            NULL);
6446       /* Look for the closing `]'.  */
6447       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6448
6449       /* Add this bound to the declarator.  */
6450       declarator = make_array_declarator (declarator, expression);
6451
6452       /* If the next token is not a `[', then there are no more
6453          bounds.  */
6454       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6455         break;
6456     }
6457
6458   return declarator;
6459 }
6460
6461 /* Parse a new-initializer.
6462
6463    new-initializer:
6464      ( expression-list [opt] )
6465      braced-init-list
6466
6467    Returns a representation of the expression-list.  */
6468
6469 static VEC(tree,gc) *
6470 cp_parser_new_initializer (cp_parser* parser)
6471 {
6472   VEC(tree,gc) *expression_list;
6473
6474   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6475     {
6476       tree t;
6477       bool expr_non_constant_p;
6478       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6479       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6480       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6481       expression_list = make_tree_vector_single (t);
6482     }
6483   else
6484     expression_list = (cp_parser_parenthesized_expression_list
6485                        (parser, non_attr, /*cast_p=*/false,
6486                         /*allow_expansion_p=*/true,
6487                         /*non_constant_p=*/NULL));
6488
6489   return expression_list;
6490 }
6491
6492 /* Parse a delete-expression.
6493
6494    delete-expression:
6495      :: [opt] delete cast-expression
6496      :: [opt] delete [ ] cast-expression
6497
6498    Returns a representation of the expression.  */
6499
6500 static tree
6501 cp_parser_delete_expression (cp_parser* parser)
6502 {
6503   bool global_scope_p;
6504   bool array_p;
6505   tree expression;
6506
6507   /* Look for the optional `::' operator.  */
6508   global_scope_p
6509     = (cp_parser_global_scope_opt (parser,
6510                                    /*current_scope_valid_p=*/false)
6511        != NULL_TREE);
6512   /* Look for the `delete' keyword.  */
6513   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6514   /* See if the array syntax is in use.  */
6515   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6516     {
6517       /* Consume the `[' token.  */
6518       cp_lexer_consume_token (parser->lexer);
6519       /* Look for the `]' token.  */
6520       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6521       /* Remember that this is the `[]' construct.  */
6522       array_p = true;
6523     }
6524   else
6525     array_p = false;
6526
6527   /* Parse the cast-expression.  */
6528   expression = cp_parser_simple_cast_expression (parser);
6529
6530   /* A delete-expression may not appear in an integral constant
6531      expression.  */
6532   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6533     return error_mark_node;
6534
6535   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6536 }
6537
6538 /* Returns true if TOKEN may start a cast-expression and false
6539    otherwise.  */
6540
6541 static bool
6542 cp_parser_token_starts_cast_expression (cp_token *token)
6543 {
6544   switch (token->type)
6545     {
6546     case CPP_COMMA:
6547     case CPP_SEMICOLON:
6548     case CPP_QUERY:
6549     case CPP_COLON:
6550     case CPP_CLOSE_SQUARE:
6551     case CPP_CLOSE_PAREN:
6552     case CPP_CLOSE_BRACE:
6553     case CPP_DOT:
6554     case CPP_DOT_STAR:
6555     case CPP_DEREF:
6556     case CPP_DEREF_STAR:
6557     case CPP_DIV:
6558     case CPP_MOD:
6559     case CPP_LSHIFT:
6560     case CPP_RSHIFT:
6561     case CPP_LESS:
6562     case CPP_GREATER:
6563     case CPP_LESS_EQ:
6564     case CPP_GREATER_EQ:
6565     case CPP_EQ_EQ:
6566     case CPP_NOT_EQ:
6567     case CPP_EQ:
6568     case CPP_MULT_EQ:
6569     case CPP_DIV_EQ:
6570     case CPP_MOD_EQ:
6571     case CPP_PLUS_EQ:
6572     case CPP_MINUS_EQ:
6573     case CPP_RSHIFT_EQ:
6574     case CPP_LSHIFT_EQ:
6575     case CPP_AND_EQ:
6576     case CPP_XOR_EQ:
6577     case CPP_OR_EQ:
6578     case CPP_XOR:
6579     case CPP_OR:
6580     case CPP_OR_OR:
6581     case CPP_EOF:
6582       return false;
6583
6584       /* '[' may start a primary-expression in obj-c++.  */
6585     case CPP_OPEN_SQUARE:
6586       return c_dialect_objc ();
6587
6588     default:
6589       return true;
6590     }
6591 }
6592
6593 /* Parse a cast-expression.
6594
6595    cast-expression:
6596      unary-expression
6597      ( type-id ) cast-expression
6598
6599    ADDRESS_P is true iff the unary-expression is appearing as the
6600    operand of the `&' operator.   CAST_P is true if this expression is
6601    the target of a cast.
6602
6603    Returns a representation of the expression.  */
6604
6605 static tree
6606 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6607                            cp_id_kind * pidk)
6608 {
6609   /* If it's a `(', then we might be looking at a cast.  */
6610   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6611     {
6612       tree type = NULL_TREE;
6613       tree expr = NULL_TREE;
6614       bool compound_literal_p;
6615       const char *saved_message;
6616
6617       /* There's no way to know yet whether or not this is a cast.
6618          For example, `(int (3))' is a unary-expression, while `(int)
6619          3' is a cast.  So, we resort to parsing tentatively.  */
6620       cp_parser_parse_tentatively (parser);
6621       /* Types may not be defined in a cast.  */
6622       saved_message = parser->type_definition_forbidden_message;
6623       parser->type_definition_forbidden_message
6624         = G_("types may not be defined in casts");
6625       /* Consume the `('.  */
6626       cp_lexer_consume_token (parser->lexer);
6627       /* A very tricky bit is that `(struct S) { 3 }' is a
6628          compound-literal (which we permit in C++ as an extension).
6629          But, that construct is not a cast-expression -- it is a
6630          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6631          is legal; if the compound-literal were a cast-expression,
6632          you'd need an extra set of parentheses.)  But, if we parse
6633          the type-id, and it happens to be a class-specifier, then we
6634          will commit to the parse at that point, because we cannot
6635          undo the action that is done when creating a new class.  So,
6636          then we cannot back up and do a postfix-expression.
6637
6638          Therefore, we scan ahead to the closing `)', and check to see
6639          if the token after the `)' is a `{'.  If so, we are not
6640          looking at a cast-expression.
6641
6642          Save tokens so that we can put them back.  */
6643       cp_lexer_save_tokens (parser->lexer);
6644       /* Skip tokens until the next token is a closing parenthesis.
6645          If we find the closing `)', and the next token is a `{', then
6646          we are looking at a compound-literal.  */
6647       compound_literal_p
6648         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6649                                                   /*consume_paren=*/true)
6650            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6651       /* Roll back the tokens we skipped.  */
6652       cp_lexer_rollback_tokens (parser->lexer);
6653       /* If we were looking at a compound-literal, simulate an error
6654          so that the call to cp_parser_parse_definitely below will
6655          fail.  */
6656       if (compound_literal_p)
6657         cp_parser_simulate_error (parser);
6658       else
6659         {
6660           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6661           parser->in_type_id_in_expr_p = true;
6662           /* Look for the type-id.  */
6663           type = cp_parser_type_id (parser);
6664           /* Look for the closing `)'.  */
6665           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6666           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6667         }
6668
6669       /* Restore the saved message.  */
6670       parser->type_definition_forbidden_message = saved_message;
6671
6672       /* At this point this can only be either a cast or a
6673          parenthesized ctor such as `(T ())' that looks like a cast to
6674          function returning T.  */
6675       if (!cp_parser_error_occurred (parser)
6676           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6677                                                      (parser->lexer)))
6678         {
6679           cp_parser_parse_definitely (parser);
6680           expr = cp_parser_cast_expression (parser,
6681                                             /*address_p=*/false,
6682                                             /*cast_p=*/true, pidk);
6683
6684           /* Warn about old-style casts, if so requested.  */
6685           if (warn_old_style_cast
6686               && !in_system_header
6687               && !VOID_TYPE_P (type)
6688               && current_lang_name != lang_name_c)
6689             warning (OPT_Wold_style_cast, "use of old-style cast");
6690
6691           /* Only type conversions to integral or enumeration types
6692              can be used in constant-expressions.  */
6693           if (!cast_valid_in_integral_constant_expression_p (type)
6694               && cp_parser_non_integral_constant_expression (parser,
6695                                                              NIC_CAST))
6696             return error_mark_node;
6697
6698           /* Perform the cast.  */
6699           expr = build_c_cast (input_location, type, expr);
6700           return expr;
6701         }
6702       else 
6703         cp_parser_abort_tentative_parse (parser);
6704     }
6705
6706   /* If we get here, then it's not a cast, so it must be a
6707      unary-expression.  */
6708   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6709 }
6710
6711 /* Parse a binary expression of the general form:
6712
6713    pm-expression:
6714      cast-expression
6715      pm-expression .* cast-expression
6716      pm-expression ->* cast-expression
6717
6718    multiplicative-expression:
6719      pm-expression
6720      multiplicative-expression * pm-expression
6721      multiplicative-expression / pm-expression
6722      multiplicative-expression % pm-expression
6723
6724    additive-expression:
6725      multiplicative-expression
6726      additive-expression + multiplicative-expression
6727      additive-expression - multiplicative-expression
6728
6729    shift-expression:
6730      additive-expression
6731      shift-expression << additive-expression
6732      shift-expression >> additive-expression
6733
6734    relational-expression:
6735      shift-expression
6736      relational-expression < shift-expression
6737      relational-expression > shift-expression
6738      relational-expression <= shift-expression
6739      relational-expression >= shift-expression
6740
6741   GNU Extension:
6742
6743    relational-expression:
6744      relational-expression <? shift-expression
6745      relational-expression >? shift-expression
6746
6747    equality-expression:
6748      relational-expression
6749      equality-expression == relational-expression
6750      equality-expression != relational-expression
6751
6752    and-expression:
6753      equality-expression
6754      and-expression & equality-expression
6755
6756    exclusive-or-expression:
6757      and-expression
6758      exclusive-or-expression ^ and-expression
6759
6760    inclusive-or-expression:
6761      exclusive-or-expression
6762      inclusive-or-expression | exclusive-or-expression
6763
6764    logical-and-expression:
6765      inclusive-or-expression
6766      logical-and-expression && inclusive-or-expression
6767
6768    logical-or-expression:
6769      logical-and-expression
6770      logical-or-expression || logical-and-expression
6771
6772    All these are implemented with a single function like:
6773
6774    binary-expression:
6775      simple-cast-expression
6776      binary-expression <token> binary-expression
6777
6778    CAST_P is true if this expression is the target of a cast.
6779
6780    The binops_by_token map is used to get the tree codes for each <token> type.
6781    binary-expressions are associated according to a precedence table.  */
6782
6783 #define TOKEN_PRECEDENCE(token)                              \
6784 (((token->type == CPP_GREATER                                \
6785    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6786   && !parser->greater_than_is_operator_p)                    \
6787  ? PREC_NOT_OPERATOR                                         \
6788  : binops_by_token[token->type].prec)
6789
6790 static tree
6791 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6792                              bool no_toplevel_fold_p,
6793                              enum cp_parser_prec prec,
6794                              cp_id_kind * pidk)
6795 {
6796   cp_parser_expression_stack stack;
6797   cp_parser_expression_stack_entry *sp = &stack[0];
6798   tree lhs, rhs;
6799   cp_token *token;
6800   enum tree_code tree_type, lhs_type, rhs_type;
6801   enum cp_parser_prec new_prec, lookahead_prec;
6802   bool overloaded_p;
6803
6804   /* Parse the first expression.  */
6805   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6806   lhs_type = ERROR_MARK;
6807
6808   for (;;)
6809     {
6810       /* Get an operator token.  */
6811       token = cp_lexer_peek_token (parser->lexer);
6812
6813       if (warn_cxx0x_compat
6814           && token->type == CPP_RSHIFT
6815           && !parser->greater_than_is_operator_p)
6816         {
6817           if (warning_at (token->location, OPT_Wc__0x_compat, 
6818                           "%<>>%> operator will be treated as"
6819                           " two right angle brackets in C++0x"))
6820             inform (token->location,
6821                     "suggest parentheses around %<>>%> expression");
6822         }
6823
6824       new_prec = TOKEN_PRECEDENCE (token);
6825
6826       /* Popping an entry off the stack means we completed a subexpression:
6827          - either we found a token which is not an operator (`>' where it is not
6828            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6829            will happen repeatedly;
6830          - or, we found an operator which has lower priority.  This is the case
6831            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6832            parsing `3 * 4'.  */
6833       if (new_prec <= prec)
6834         {
6835           if (sp == stack)
6836             break;
6837           else
6838             goto pop;
6839         }
6840
6841      get_rhs:
6842       tree_type = binops_by_token[token->type].tree_type;
6843
6844       /* We used the operator token.  */
6845       cp_lexer_consume_token (parser->lexer);
6846
6847       /* For "false && x" or "true || x", x will never be executed;
6848          disable warnings while evaluating it.  */
6849       if (tree_type == TRUTH_ANDIF_EXPR)
6850         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6851       else if (tree_type == TRUTH_ORIF_EXPR)
6852         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6853
6854       /* Extract another operand.  It may be the RHS of this expression
6855          or the LHS of a new, higher priority expression.  */
6856       rhs = cp_parser_simple_cast_expression (parser);
6857       rhs_type = ERROR_MARK;
6858
6859       /* Get another operator token.  Look up its precedence to avoid
6860          building a useless (immediately popped) stack entry for common
6861          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6862       token = cp_lexer_peek_token (parser->lexer);
6863       lookahead_prec = TOKEN_PRECEDENCE (token);
6864       if (lookahead_prec > new_prec)
6865         {
6866           /* ... and prepare to parse the RHS of the new, higher priority
6867              expression.  Since precedence levels on the stack are
6868              monotonically increasing, we do not have to care about
6869              stack overflows.  */
6870           sp->prec = prec;
6871           sp->tree_type = tree_type;
6872           sp->lhs = lhs;
6873           sp->lhs_type = lhs_type;
6874           sp++;
6875           lhs = rhs;
6876           lhs_type = rhs_type;
6877           prec = new_prec;
6878           new_prec = lookahead_prec;
6879           goto get_rhs;
6880
6881          pop:
6882           lookahead_prec = new_prec;
6883           /* If the stack is not empty, we have parsed into LHS the right side
6884              (`4' in the example above) of an expression we had suspended.
6885              We can use the information on the stack to recover the LHS (`3')
6886              from the stack together with the tree code (`MULT_EXPR'), and
6887              the precedence of the higher level subexpression
6888              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6889              which will be used to actually build the additive expression.  */
6890           --sp;
6891           prec = sp->prec;
6892           tree_type = sp->tree_type;
6893           rhs = lhs;
6894           rhs_type = lhs_type;
6895           lhs = sp->lhs;
6896           lhs_type = sp->lhs_type;
6897         }
6898
6899       /* Undo the disabling of warnings done above.  */
6900       if (tree_type == TRUTH_ANDIF_EXPR)
6901         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6902       else if (tree_type == TRUTH_ORIF_EXPR)
6903         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6904
6905       overloaded_p = false;
6906       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6907          ERROR_MARK for everything that is not a binary expression.
6908          This makes warn_about_parentheses miss some warnings that
6909          involve unary operators.  For unary expressions we should
6910          pass the correct tree_code unless the unary expression was
6911          surrounded by parentheses.
6912       */
6913       if (no_toplevel_fold_p
6914           && lookahead_prec <= prec
6915           && sp == stack
6916           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6917         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6918       else
6919         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6920                                  &overloaded_p, tf_warning_or_error);
6921       lhs_type = tree_type;
6922
6923       /* If the binary operator required the use of an overloaded operator,
6924          then this expression cannot be an integral constant-expression.
6925          An overloaded operator can be used even if both operands are
6926          otherwise permissible in an integral constant-expression if at
6927          least one of the operands is of enumeration type.  */
6928
6929       if (overloaded_p
6930           && cp_parser_non_integral_constant_expression (parser,
6931                                                          NIC_OVERLOADED))
6932         return error_mark_node;
6933     }
6934
6935   return lhs;
6936 }
6937
6938
6939 /* Parse the `? expression : assignment-expression' part of a
6940    conditional-expression.  The LOGICAL_OR_EXPR is the
6941    logical-or-expression that started the conditional-expression.
6942    Returns a representation of the entire conditional-expression.
6943
6944    This routine is used by cp_parser_assignment_expression.
6945
6946      ? expression : assignment-expression
6947
6948    GNU Extensions:
6949
6950      ? : assignment-expression */
6951
6952 static tree
6953 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6954 {
6955   tree expr;
6956   tree assignment_expr;
6957   struct cp_token *token;
6958
6959   /* Consume the `?' token.  */
6960   cp_lexer_consume_token (parser->lexer);
6961   token = cp_lexer_peek_token (parser->lexer);
6962   if (cp_parser_allow_gnu_extensions_p (parser)
6963       && token->type == CPP_COLON)
6964     {
6965       pedwarn (token->location, OPT_pedantic, 
6966                "ISO C++ does not allow ?: with omitted middle operand");
6967       /* Implicit true clause.  */
6968       expr = NULL_TREE;
6969       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6970       warn_for_omitted_condop (token->location, logical_or_expr);
6971     }
6972   else
6973     {
6974       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6975       parser->colon_corrects_to_scope_p = false;
6976       /* Parse the expression.  */
6977       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6978       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6979       c_inhibit_evaluation_warnings +=
6980         ((logical_or_expr == truthvalue_true_node)
6981          - (logical_or_expr == truthvalue_false_node));
6982       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6983     }
6984
6985   /* The next token should be a `:'.  */
6986   cp_parser_require (parser, CPP_COLON, RT_COLON);
6987   /* Parse the assignment-expression.  */
6988   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6989   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6990
6991   /* Build the conditional-expression.  */
6992   return build_x_conditional_expr (logical_or_expr,
6993                                    expr,
6994                                    assignment_expr,
6995                                    tf_warning_or_error);
6996 }
6997
6998 /* Parse an assignment-expression.
6999
7000    assignment-expression:
7001      conditional-expression
7002      logical-or-expression assignment-operator assignment_expression
7003      throw-expression
7004
7005    CAST_P is true if this expression is the target of a cast.
7006
7007    Returns a representation for the expression.  */
7008
7009 static tree
7010 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7011                                  cp_id_kind * pidk)
7012 {
7013   tree expr;
7014
7015   /* If the next token is the `throw' keyword, then we're looking at
7016      a throw-expression.  */
7017   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7018     expr = cp_parser_throw_expression (parser);
7019   /* Otherwise, it must be that we are looking at a
7020      logical-or-expression.  */
7021   else
7022     {
7023       /* Parse the binary expressions (logical-or-expression).  */
7024       expr = cp_parser_binary_expression (parser, cast_p, false,
7025                                           PREC_NOT_OPERATOR, pidk);
7026       /* If the next token is a `?' then we're actually looking at a
7027          conditional-expression.  */
7028       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7029         return cp_parser_question_colon_clause (parser, expr);
7030       else
7031         {
7032           enum tree_code assignment_operator;
7033
7034           /* If it's an assignment-operator, we're using the second
7035              production.  */
7036           assignment_operator
7037             = cp_parser_assignment_operator_opt (parser);
7038           if (assignment_operator != ERROR_MARK)
7039             {
7040               bool non_constant_p;
7041
7042               /* Parse the right-hand side of the assignment.  */
7043               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7044
7045               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7046                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7047
7048               /* An assignment may not appear in a
7049                  constant-expression.  */
7050               if (cp_parser_non_integral_constant_expression (parser,
7051                                                               NIC_ASSIGNMENT))
7052                 return error_mark_node;
7053               /* Build the assignment expression.  */
7054               expr = build_x_modify_expr (expr,
7055                                           assignment_operator,
7056                                           rhs,
7057                                           tf_warning_or_error);
7058             }
7059         }
7060     }
7061
7062   return expr;
7063 }
7064
7065 /* Parse an (optional) assignment-operator.
7066
7067    assignment-operator: one of
7068      = *= /= %= += -= >>= <<= &= ^= |=
7069
7070    GNU Extension:
7071
7072    assignment-operator: one of
7073      <?= >?=
7074
7075    If the next token is an assignment operator, the corresponding tree
7076    code is returned, and the token is consumed.  For example, for
7077    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7078    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7079    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7080    operator, ERROR_MARK is returned.  */
7081
7082 static enum tree_code
7083 cp_parser_assignment_operator_opt (cp_parser* parser)
7084 {
7085   enum tree_code op;
7086   cp_token *token;
7087
7088   /* Peek at the next token.  */
7089   token = cp_lexer_peek_token (parser->lexer);
7090
7091   switch (token->type)
7092     {
7093     case CPP_EQ:
7094       op = NOP_EXPR;
7095       break;
7096
7097     case CPP_MULT_EQ:
7098       op = MULT_EXPR;
7099       break;
7100
7101     case CPP_DIV_EQ:
7102       op = TRUNC_DIV_EXPR;
7103       break;
7104
7105     case CPP_MOD_EQ:
7106       op = TRUNC_MOD_EXPR;
7107       break;
7108
7109     case CPP_PLUS_EQ:
7110       op = PLUS_EXPR;
7111       break;
7112
7113     case CPP_MINUS_EQ:
7114       op = MINUS_EXPR;
7115       break;
7116
7117     case CPP_RSHIFT_EQ:
7118       op = RSHIFT_EXPR;
7119       break;
7120
7121     case CPP_LSHIFT_EQ:
7122       op = LSHIFT_EXPR;
7123       break;
7124
7125     case CPP_AND_EQ:
7126       op = BIT_AND_EXPR;
7127       break;
7128
7129     case CPP_XOR_EQ:
7130       op = BIT_XOR_EXPR;
7131       break;
7132
7133     case CPP_OR_EQ:
7134       op = BIT_IOR_EXPR;
7135       break;
7136
7137     default:
7138       /* Nothing else is an assignment operator.  */
7139       op = ERROR_MARK;
7140     }
7141
7142   /* If it was an assignment operator, consume it.  */
7143   if (op != ERROR_MARK)
7144     cp_lexer_consume_token (parser->lexer);
7145
7146   return op;
7147 }
7148
7149 /* Parse an expression.
7150
7151    expression:
7152      assignment-expression
7153      expression , assignment-expression
7154
7155    CAST_P is true if this expression is the target of a cast.
7156
7157    Returns a representation of the expression.  */
7158
7159 static tree
7160 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7161 {
7162   tree expression = NULL_TREE;
7163
7164   while (true)
7165     {
7166       tree assignment_expression;
7167
7168       /* Parse the next assignment-expression.  */
7169       assignment_expression
7170         = cp_parser_assignment_expression (parser, cast_p, pidk);
7171       /* If this is the first assignment-expression, we can just
7172          save it away.  */
7173       if (!expression)
7174         expression = assignment_expression;
7175       else
7176         expression = build_x_compound_expr (expression,
7177                                             assignment_expression,
7178                                             tf_warning_or_error);
7179       /* If the next token is not a comma, then we are done with the
7180          expression.  */
7181       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7182         break;
7183       /* Consume the `,'.  */
7184       cp_lexer_consume_token (parser->lexer);
7185       /* A comma operator cannot appear in a constant-expression.  */
7186       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7187         expression = error_mark_node;
7188     }
7189
7190   return expression;
7191 }
7192
7193 /* Parse a constant-expression.
7194
7195    constant-expression:
7196      conditional-expression
7197
7198   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7199   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7200   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7201   is false, NON_CONSTANT_P should be NULL.  */
7202
7203 static tree
7204 cp_parser_constant_expression (cp_parser* parser,
7205                                bool allow_non_constant_p,
7206                                bool *non_constant_p)
7207 {
7208   bool saved_integral_constant_expression_p;
7209   bool saved_allow_non_integral_constant_expression_p;
7210   bool saved_non_integral_constant_expression_p;
7211   tree expression;
7212
7213   /* It might seem that we could simply parse the
7214      conditional-expression, and then check to see if it were
7215      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7216      one that the compiler can figure out is constant, possibly after
7217      doing some simplifications or optimizations.  The standard has a
7218      precise definition of constant-expression, and we must honor
7219      that, even though it is somewhat more restrictive.
7220
7221      For example:
7222
7223        int i[(2, 3)];
7224
7225      is not a legal declaration, because `(2, 3)' is not a
7226      constant-expression.  The `,' operator is forbidden in a
7227      constant-expression.  However, GCC's constant-folding machinery
7228      will fold this operation to an INTEGER_CST for `3'.  */
7229
7230   /* Save the old settings.  */
7231   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7232   saved_allow_non_integral_constant_expression_p
7233     = parser->allow_non_integral_constant_expression_p;
7234   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7235   /* We are now parsing a constant-expression.  */
7236   parser->integral_constant_expression_p = true;
7237   parser->allow_non_integral_constant_expression_p
7238     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7239   parser->non_integral_constant_expression_p = false;
7240   /* Although the grammar says "conditional-expression", we parse an
7241      "assignment-expression", which also permits "throw-expression"
7242      and the use of assignment operators.  In the case that
7243      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7244      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7245      actually essential that we look for an assignment-expression.
7246      For example, cp_parser_initializer_clauses uses this function to
7247      determine whether a particular assignment-expression is in fact
7248      constant.  */
7249   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7250   /* Restore the old settings.  */
7251   parser->integral_constant_expression_p
7252     = saved_integral_constant_expression_p;
7253   parser->allow_non_integral_constant_expression_p
7254     = saved_allow_non_integral_constant_expression_p;
7255   if (allow_non_constant_p)
7256     *non_constant_p = parser->non_integral_constant_expression_p;
7257   else if (parser->non_integral_constant_expression_p
7258            && cxx_dialect < cxx0x)
7259     expression = error_mark_node;
7260   parser->non_integral_constant_expression_p
7261     = saved_non_integral_constant_expression_p;
7262
7263   return expression;
7264 }
7265
7266 /* Parse __builtin_offsetof.
7267
7268    offsetof-expression:
7269      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7270
7271    offsetof-member-designator:
7272      id-expression
7273      | offsetof-member-designator "." id-expression
7274      | offsetof-member-designator "[" expression "]"
7275      | offsetof-member-designator "->" id-expression  */
7276
7277 static tree
7278 cp_parser_builtin_offsetof (cp_parser *parser)
7279 {
7280   int save_ice_p, save_non_ice_p;
7281   tree type, expr;
7282   cp_id_kind dummy;
7283   cp_token *token;
7284
7285   /* We're about to accept non-integral-constant things, but will
7286      definitely yield an integral constant expression.  Save and
7287      restore these values around our local parsing.  */
7288   save_ice_p = parser->integral_constant_expression_p;
7289   save_non_ice_p = parser->non_integral_constant_expression_p;
7290
7291   /* Consume the "__builtin_offsetof" token.  */
7292   cp_lexer_consume_token (parser->lexer);
7293   /* Consume the opening `('.  */
7294   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7295   /* Parse the type-id.  */
7296   type = cp_parser_type_id (parser);
7297   /* Look for the `,'.  */
7298   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7299   token = cp_lexer_peek_token (parser->lexer);
7300
7301   /* Build the (type *)null that begins the traditional offsetof macro.  */
7302   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7303                             tf_warning_or_error);
7304
7305   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7306   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7307                                                  true, &dummy, token->location);
7308   while (true)
7309     {
7310       token = cp_lexer_peek_token (parser->lexer);
7311       switch (token->type)
7312         {
7313         case CPP_OPEN_SQUARE:
7314           /* offsetof-member-designator "[" expression "]" */
7315           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7316           break;
7317
7318         case CPP_DEREF:
7319           /* offsetof-member-designator "->" identifier */
7320           expr = grok_array_decl (expr, integer_zero_node);
7321           /* FALLTHRU */
7322
7323         case CPP_DOT:
7324           /* offsetof-member-designator "." identifier */
7325           cp_lexer_consume_token (parser->lexer);
7326           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7327                                                          expr, true, &dummy,
7328                                                          token->location);
7329           break;
7330
7331         case CPP_CLOSE_PAREN:
7332           /* Consume the ")" token.  */
7333           cp_lexer_consume_token (parser->lexer);
7334           goto success;
7335
7336         default:
7337           /* Error.  We know the following require will fail, but
7338              that gives the proper error message.  */
7339           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7340           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7341           expr = error_mark_node;
7342           goto failure;
7343         }
7344     }
7345
7346  success:
7347   /* If we're processing a template, we can't finish the semantics yet.
7348      Otherwise we can fold the entire expression now.  */
7349   if (processing_template_decl)
7350     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7351   else
7352     expr = finish_offsetof (expr);
7353
7354  failure:
7355   parser->integral_constant_expression_p = save_ice_p;
7356   parser->non_integral_constant_expression_p = save_non_ice_p;
7357
7358   return expr;
7359 }
7360
7361 /* Parse a trait expression.  */
7362
7363 static tree
7364 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7365 {
7366   cp_trait_kind kind;
7367   tree type1, type2 = NULL_TREE;
7368   bool binary = false;
7369   cp_decl_specifier_seq decl_specs;
7370
7371   switch (keyword)
7372     {
7373     case RID_HAS_NOTHROW_ASSIGN:
7374       kind = CPTK_HAS_NOTHROW_ASSIGN;
7375       break;
7376     case RID_HAS_NOTHROW_CONSTRUCTOR:
7377       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7378       break;
7379     case RID_HAS_NOTHROW_COPY:
7380       kind = CPTK_HAS_NOTHROW_COPY;
7381       break;
7382     case RID_HAS_TRIVIAL_ASSIGN:
7383       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7384       break;
7385     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7386       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7387       break;
7388     case RID_HAS_TRIVIAL_COPY:
7389       kind = CPTK_HAS_TRIVIAL_COPY;
7390       break;
7391     case RID_HAS_TRIVIAL_DESTRUCTOR:
7392       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7393       break;
7394     case RID_HAS_VIRTUAL_DESTRUCTOR:
7395       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7396       break;
7397     case RID_IS_ABSTRACT:
7398       kind = CPTK_IS_ABSTRACT;
7399       break;
7400     case RID_IS_BASE_OF:
7401       kind = CPTK_IS_BASE_OF;
7402       binary = true;
7403       break;
7404     case RID_IS_CLASS:
7405       kind = CPTK_IS_CLASS;
7406       break;
7407     case RID_IS_CONVERTIBLE_TO:
7408       kind = CPTK_IS_CONVERTIBLE_TO;
7409       binary = true;
7410       break;
7411     case RID_IS_EMPTY:
7412       kind = CPTK_IS_EMPTY;
7413       break;
7414     case RID_IS_ENUM:
7415       kind = CPTK_IS_ENUM;
7416       break;
7417     case RID_IS_POD:
7418       kind = CPTK_IS_POD;
7419       break;
7420     case RID_IS_POLYMORPHIC:
7421       kind = CPTK_IS_POLYMORPHIC;
7422       break;
7423     case RID_IS_STD_LAYOUT:
7424       kind = CPTK_IS_STD_LAYOUT;
7425       break;
7426     case RID_IS_TRIVIAL:
7427       kind = CPTK_IS_TRIVIAL;
7428       break;
7429     case RID_IS_UNION:
7430       kind = CPTK_IS_UNION;
7431       break;
7432     case RID_IS_LITERAL_TYPE:
7433       kind = CPTK_IS_LITERAL_TYPE;
7434       break;
7435     default:
7436       gcc_unreachable ();
7437     }
7438
7439   /* Consume the token.  */
7440   cp_lexer_consume_token (parser->lexer);
7441
7442   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7443
7444   type1 = cp_parser_type_id (parser);
7445
7446   if (type1 == error_mark_node)
7447     return error_mark_node;
7448
7449   /* Build a trivial decl-specifier-seq.  */
7450   clear_decl_specs (&decl_specs);
7451   decl_specs.type = type1;
7452
7453   /* Call grokdeclarator to figure out what type this is.  */
7454   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7455                           /*initialized=*/0, /*attrlist=*/NULL);
7456
7457   if (binary)
7458     {
7459       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7460  
7461       type2 = cp_parser_type_id (parser);
7462
7463       if (type2 == error_mark_node)
7464         return error_mark_node;
7465
7466       /* Build a trivial decl-specifier-seq.  */
7467       clear_decl_specs (&decl_specs);
7468       decl_specs.type = type2;
7469
7470       /* Call grokdeclarator to figure out what type this is.  */
7471       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7472                               /*initialized=*/0, /*attrlist=*/NULL);
7473     }
7474
7475   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7476
7477   /* Complete the trait expression, which may mean either processing
7478      the trait expr now or saving it for template instantiation.  */
7479   return finish_trait_expr (kind, type1, type2);
7480 }
7481
7482 /* Lambdas that appear in variable initializer or default argument scope
7483    get that in their mangling, so we need to record it.  We might as well
7484    use the count for function and namespace scopes as well.  */
7485 static GTY(()) tree lambda_scope;
7486 static GTY(()) int lambda_count;
7487 typedef struct GTY(()) tree_int
7488 {
7489   tree t;
7490   int i;
7491 } tree_int;
7492 DEF_VEC_O(tree_int);
7493 DEF_VEC_ALLOC_O(tree_int,gc);
7494 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7495
7496 static void
7497 start_lambda_scope (tree decl)
7498 {
7499   tree_int ti;
7500   gcc_assert (decl);
7501   /* Once we're inside a function, we ignore other scopes and just push
7502      the function again so that popping works properly.  */
7503   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7504     decl = current_function_decl;
7505   ti.t = lambda_scope;
7506   ti.i = lambda_count;
7507   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7508   if (lambda_scope != decl)
7509     {
7510       /* Don't reset the count if we're still in the same function.  */
7511       lambda_scope = decl;
7512       lambda_count = 0;
7513     }
7514 }
7515
7516 static void
7517 record_lambda_scope (tree lambda)
7518 {
7519   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7520   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7521 }
7522
7523 static void
7524 finish_lambda_scope (void)
7525 {
7526   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7527   if (lambda_scope != p->t)
7528     {
7529       lambda_scope = p->t;
7530       lambda_count = p->i;
7531     }
7532   VEC_pop (tree_int, lambda_scope_stack);
7533 }
7534
7535 /* Parse a lambda expression.
7536
7537    lambda-expression:
7538      lambda-introducer lambda-declarator [opt] compound-statement
7539
7540    Returns a representation of the expression.  */
7541
7542 static tree
7543 cp_parser_lambda_expression (cp_parser* parser)
7544 {
7545   tree lambda_expr = build_lambda_expr ();
7546   tree type;
7547
7548   LAMBDA_EXPR_LOCATION (lambda_expr)
7549     = cp_lexer_peek_token (parser->lexer)->location;
7550
7551   if (cp_unevaluated_operand)
7552     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7553               "lambda-expression in unevaluated context");
7554
7555   /* We may be in the middle of deferred access check.  Disable
7556      it now.  */
7557   push_deferring_access_checks (dk_no_deferred);
7558
7559   cp_parser_lambda_introducer (parser, lambda_expr);
7560
7561   type = begin_lambda_type (lambda_expr);
7562
7563   record_lambda_scope (lambda_expr);
7564
7565   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7566   determine_visibility (TYPE_NAME (type));
7567
7568   /* Now that we've started the type, add the capture fields for any
7569      explicit captures.  */
7570   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7571
7572   {
7573     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7574     unsigned int saved_num_template_parameter_lists
7575         = parser->num_template_parameter_lists;
7576
7577     parser->num_template_parameter_lists = 0;
7578
7579     /* By virtue of defining a local class, a lambda expression has access to
7580        the private variables of enclosing classes.  */
7581
7582     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7583
7584     cp_parser_lambda_body (parser, lambda_expr);
7585
7586     /* The capture list was built up in reverse order; fix that now.  */
7587     {
7588       tree newlist = NULL_TREE;
7589       tree elt, next;
7590
7591       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7592            elt; elt = next)
7593         {
7594           tree field = TREE_PURPOSE (elt);
7595           char *buf;
7596
7597           next = TREE_CHAIN (elt);
7598           TREE_CHAIN (elt) = newlist;
7599           newlist = elt;
7600
7601           /* Also add __ to the beginning of the field name so that code
7602              outside the lambda body can't see the captured name.  We could
7603              just remove the name entirely, but this is more useful for
7604              debugging.  */
7605           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7606             /* The 'this' capture already starts with __.  */
7607             continue;
7608
7609           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7610           buf[1] = buf[0] = '_';
7611           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7612                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7613           DECL_NAME (field) = get_identifier (buf);
7614         }
7615       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7616     }
7617
7618     maybe_add_lambda_conv_op (type);
7619
7620     type = finish_struct (type, /*attributes=*/NULL_TREE);
7621
7622     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7623   }
7624
7625   pop_deferring_access_checks ();
7626
7627   return build_lambda_object (lambda_expr);
7628 }
7629
7630 /* Parse the beginning of a lambda expression.
7631
7632    lambda-introducer:
7633      [ lambda-capture [opt] ]
7634
7635    LAMBDA_EXPR is the current representation of the lambda expression.  */
7636
7637 static void
7638 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7639 {
7640   /* Need commas after the first capture.  */
7641   bool first = true;
7642
7643   /* Eat the leading `['.  */
7644   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7645
7646   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7647   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7648       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7649     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7650   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7651     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7652
7653   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7654     {
7655       cp_lexer_consume_token (parser->lexer);
7656       first = false;
7657     }
7658
7659   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7660     {
7661       cp_token* capture_token;
7662       tree capture_id;
7663       tree capture_init_expr;
7664       cp_id_kind idk = CP_ID_KIND_NONE;
7665       bool explicit_init_p = false;
7666
7667       enum capture_kind_type
7668       {
7669         BY_COPY,
7670         BY_REFERENCE
7671       };
7672       enum capture_kind_type capture_kind = BY_COPY;
7673
7674       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7675         {
7676           error ("expected end of capture-list");
7677           return;
7678         }
7679
7680       if (first)
7681         first = false;
7682       else
7683         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7684
7685       /* Possibly capture `this'.  */
7686       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7687         {
7688           cp_lexer_consume_token (parser->lexer);
7689           add_capture (lambda_expr,
7690                        /*id=*/get_identifier ("__this"),
7691                        /*initializer=*/finish_this_expr(),
7692                        /*by_reference_p=*/false,
7693                        explicit_init_p);
7694           continue;
7695         }
7696
7697       /* Remember whether we want to capture as a reference or not.  */
7698       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7699         {
7700           capture_kind = BY_REFERENCE;
7701           cp_lexer_consume_token (parser->lexer);
7702         }
7703
7704       /* Get the identifier.  */
7705       capture_token = cp_lexer_peek_token (parser->lexer);
7706       capture_id = cp_parser_identifier (parser);
7707
7708       if (capture_id == error_mark_node)
7709         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7710            delimiters, but I modified this to stop on unnested ']' as well.  It
7711            was already changed to stop on unnested '}', so the
7712            "closing_parenthesis" name is no more misleading with my change.  */
7713         {
7714           cp_parser_skip_to_closing_parenthesis (parser,
7715                                                  /*recovering=*/true,
7716                                                  /*or_comma=*/true,
7717                                                  /*consume_paren=*/true);
7718           break;
7719         }
7720
7721       /* Find the initializer for this capture.  */
7722       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7723         {
7724           /* An explicit expression exists.  */
7725           cp_lexer_consume_token (parser->lexer);
7726           pedwarn (input_location, OPT_pedantic,
7727                    "ISO C++ does not allow initializers "
7728                    "in lambda expression capture lists");
7729           capture_init_expr = cp_parser_assignment_expression (parser,
7730                                                                /*cast_p=*/true,
7731                                                                &idk);
7732           explicit_init_p = true;
7733         }
7734       else
7735         {
7736           const char* error_msg;
7737
7738           /* Turn the identifier into an id-expression.  */
7739           capture_init_expr
7740             = cp_parser_lookup_name
7741                 (parser,
7742                  capture_id,
7743                  none_type,
7744                  /*is_template=*/false,
7745                  /*is_namespace=*/false,
7746                  /*check_dependency=*/true,
7747                  /*ambiguous_decls=*/NULL,
7748                  capture_token->location);
7749
7750           capture_init_expr
7751             = finish_id_expression
7752                 (capture_id,
7753                  capture_init_expr,
7754                  parser->scope,
7755                  &idk,
7756                  /*integral_constant_expression_p=*/false,
7757                  /*allow_non_integral_constant_expression_p=*/false,
7758                  /*non_integral_constant_expression_p=*/NULL,
7759                  /*template_p=*/false,
7760                  /*done=*/true,
7761                  /*address_p=*/false,
7762                  /*template_arg_p=*/false,
7763                  &error_msg,
7764                  capture_token->location);
7765         }
7766
7767       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7768         capture_init_expr
7769           = unqualified_name_lookup_error (capture_init_expr);
7770
7771       add_capture (lambda_expr,
7772                    capture_id,
7773                    capture_init_expr,
7774                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7775                    explicit_init_p);
7776     }
7777
7778   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7779 }
7780
7781 /* Parse the (optional) middle of a lambda expression.
7782
7783    lambda-declarator:
7784      ( parameter-declaration-clause [opt] )
7785        attribute-specifier [opt]
7786        mutable [opt]
7787        exception-specification [opt]
7788        lambda-return-type-clause [opt]
7789
7790    LAMBDA_EXPR is the current representation of the lambda expression.  */
7791
7792 static void
7793 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7794 {
7795   /* 5.1.1.4 of the standard says:
7796        If a lambda-expression does not include a lambda-declarator, it is as if
7797        the lambda-declarator were ().
7798      This means an empty parameter list, no attributes, and no exception
7799      specification.  */
7800   tree param_list = void_list_node;
7801   tree attributes = NULL_TREE;
7802   tree exception_spec = NULL_TREE;
7803   tree t;
7804
7805   /* The lambda-declarator is optional, but must begin with an opening
7806      parenthesis if present.  */
7807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7808     {
7809       cp_lexer_consume_token (parser->lexer);
7810
7811       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7812
7813       /* Parse parameters.  */
7814       param_list = cp_parser_parameter_declaration_clause (parser);
7815
7816       /* Default arguments shall not be specified in the
7817          parameter-declaration-clause of a lambda-declarator.  */
7818       for (t = param_list; t; t = TREE_CHAIN (t))
7819         if (TREE_PURPOSE (t))
7820           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7821                    "default argument specified for lambda parameter");
7822
7823       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7824
7825       attributes = cp_parser_attributes_opt (parser);
7826
7827       /* Parse optional `mutable' keyword.  */
7828       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7829         {
7830           cp_lexer_consume_token (parser->lexer);
7831           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7832         }
7833
7834       /* Parse optional exception specification.  */
7835       exception_spec = cp_parser_exception_specification_opt (parser);
7836
7837       /* Parse optional trailing return type.  */
7838       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7839         {
7840           cp_lexer_consume_token (parser->lexer);
7841           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7842         }
7843
7844       /* The function parameters must be in scope all the way until after the
7845          trailing-return-type in case of decltype.  */
7846       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7847         pop_binding (DECL_NAME (t), t);
7848
7849       leave_scope ();
7850     }
7851
7852   /* Create the function call operator.
7853
7854      Messing with declarators like this is no uglier than building up the
7855      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7856      other code.  */
7857   {
7858     cp_decl_specifier_seq return_type_specs;
7859     cp_declarator* declarator;
7860     tree fco;
7861     int quals;
7862     void *p;
7863
7864     clear_decl_specs (&return_type_specs);
7865     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7866       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7867     else
7868       /* Maybe we will deduce the return type later, but we can use void
7869          as a placeholder return type anyways.  */
7870       return_type_specs.type = void_type_node;
7871
7872     p = obstack_alloc (&declarator_obstack, 0);
7873
7874     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7875                                      sfk_none);
7876
7877     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7878              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7879     declarator = make_call_declarator (declarator, param_list, quals,
7880                                        exception_spec,
7881                                        /*late_return_type=*/NULL_TREE);
7882     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7883
7884     fco = grokmethod (&return_type_specs,
7885                       declarator,
7886                       attributes);
7887     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7888     DECL_ARTIFICIAL (fco) = 1;
7889
7890     finish_member_declaration (fco);
7891
7892     obstack_free (&declarator_obstack, p);
7893   }
7894 }
7895
7896 /* Parse the body of a lambda expression, which is simply
7897
7898    compound-statement
7899
7900    but which requires special handling.
7901    LAMBDA_EXPR is the current representation of the lambda expression.  */
7902
7903 static void
7904 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7905 {
7906   bool nested = (current_function_decl != NULL_TREE);
7907   if (nested)
7908     push_function_context ();
7909
7910   /* Finish the function call operator
7911      - class_specifier
7912      + late_parsing_for_member
7913      + function_definition_after_declarator
7914      + ctor_initializer_opt_and_function_body  */
7915   {
7916     tree fco = lambda_function (lambda_expr);
7917     tree body;
7918     bool done = false;
7919
7920     /* Let the front end know that we are going to be defining this
7921        function.  */
7922     start_preparsed_function (fco,
7923                               NULL_TREE,
7924                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7925
7926     start_lambda_scope (fco);
7927     body = begin_function_body ();
7928
7929     /* 5.1.1.4 of the standard says:
7930          If a lambda-expression does not include a trailing-return-type, it
7931          is as if the trailing-return-type denotes the following type:
7932           * if the compound-statement is of the form
7933                { return attribute-specifier [opt] expression ; }
7934              the type of the returned expression after lvalue-to-rvalue
7935              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7936              (_conv.array_ 4.2), and function-to-pointer conversion
7937              (_conv.func_ 4.3);
7938           * otherwise, void.  */
7939
7940     /* In a lambda that has neither a lambda-return-type-clause
7941        nor a deducible form, errors should be reported for return statements
7942        in the body.  Since we used void as the placeholder return type, parsing
7943        the body as usual will give such desired behavior.  */
7944     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7945         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7946         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7947         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7948       {
7949         tree compound_stmt;
7950         tree expr = NULL_TREE;
7951         cp_id_kind idk = CP_ID_KIND_NONE;
7952
7953         /* Parse tentatively in case there's more after the initial return
7954            statement.  */
7955         cp_parser_parse_tentatively (parser);
7956
7957         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7958         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7959
7960         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7961
7962         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7963         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7964
7965         if (cp_parser_parse_definitely (parser))
7966           {
7967             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7968
7969             compound_stmt = begin_compound_stmt (0);
7970             /* Will get error here if type not deduced yet.  */
7971             finish_return_stmt (expr);
7972             finish_compound_stmt (compound_stmt);
7973
7974             done = true;
7975           }
7976       }
7977
7978     if (!done)
7979       {
7980         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7981           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7982         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7983            cp_parser_compound_stmt does not pass it.  */
7984         cp_parser_function_body (parser);
7985         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7986       }
7987
7988     finish_function_body (body);
7989     finish_lambda_scope ();
7990
7991     /* Finish the function and generate code for it if necessary.  */
7992     expand_or_defer_fn (finish_function (/*inline*/2));
7993   }
7994
7995   if (nested)
7996     pop_function_context();
7997 }
7998
7999 /* Statements [gram.stmt.stmt]  */
8000
8001 /* Parse a statement.
8002
8003    statement:
8004      labeled-statement
8005      expression-statement
8006      compound-statement
8007      selection-statement
8008      iteration-statement
8009      jump-statement
8010      declaration-statement
8011      try-block
8012
8013   IN_COMPOUND is true when the statement is nested inside a
8014   cp_parser_compound_statement; this matters for certain pragmas.
8015
8016   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8017   is a (possibly labeled) if statement which is not enclosed in braces
8018   and has an else clause.  This is used to implement -Wparentheses.  */
8019
8020 static void
8021 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8022                      bool in_compound, bool *if_p)
8023 {
8024   tree statement;
8025   cp_token *token;
8026   location_t statement_location;
8027
8028  restart:
8029   if (if_p != NULL)
8030     *if_p = false;
8031   /* There is no statement yet.  */
8032   statement = NULL_TREE;
8033   /* Peek at the next token.  */
8034   token = cp_lexer_peek_token (parser->lexer);
8035   /* Remember the location of the first token in the statement.  */
8036   statement_location = token->location;
8037   /* If this is a keyword, then that will often determine what kind of
8038      statement we have.  */
8039   if (token->type == CPP_KEYWORD)
8040     {
8041       enum rid keyword = token->keyword;
8042
8043       switch (keyword)
8044         {
8045         case RID_CASE:
8046         case RID_DEFAULT:
8047           /* Looks like a labeled-statement with a case label.
8048              Parse the label, and then use tail recursion to parse
8049              the statement.  */
8050           cp_parser_label_for_labeled_statement (parser);
8051           goto restart;
8052
8053         case RID_IF:
8054         case RID_SWITCH:
8055           statement = cp_parser_selection_statement (parser, if_p);
8056           break;
8057
8058         case RID_WHILE:
8059         case RID_DO:
8060         case RID_FOR:
8061           statement = cp_parser_iteration_statement (parser);
8062           break;
8063
8064         case RID_BREAK:
8065         case RID_CONTINUE:
8066         case RID_RETURN:
8067         case RID_GOTO:
8068           statement = cp_parser_jump_statement (parser);
8069           break;
8070
8071           /* Objective-C++ exception-handling constructs.  */
8072         case RID_AT_TRY:
8073         case RID_AT_CATCH:
8074         case RID_AT_FINALLY:
8075         case RID_AT_SYNCHRONIZED:
8076         case RID_AT_THROW:
8077           statement = cp_parser_objc_statement (parser);
8078           break;
8079
8080         case RID_TRY:
8081           statement = cp_parser_try_block (parser);
8082           break;
8083
8084         case RID_NAMESPACE:
8085           /* This must be a namespace alias definition.  */
8086           cp_parser_declaration_statement (parser);
8087           return;
8088           
8089         default:
8090           /* It might be a keyword like `int' that can start a
8091              declaration-statement.  */
8092           break;
8093         }
8094     }
8095   else if (token->type == CPP_NAME)
8096     {
8097       /* If the next token is a `:', then we are looking at a
8098          labeled-statement.  */
8099       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8100       if (token->type == CPP_COLON)
8101         {
8102           /* Looks like a labeled-statement with an ordinary label.
8103              Parse the label, and then use tail recursion to parse
8104              the statement.  */
8105           cp_parser_label_for_labeled_statement (parser);
8106           goto restart;
8107         }
8108     }
8109   /* Anything that starts with a `{' must be a compound-statement.  */
8110   else if (token->type == CPP_OPEN_BRACE)
8111     statement = cp_parser_compound_statement (parser, NULL, false);
8112   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8113      a statement all its own.  */
8114   else if (token->type == CPP_PRAGMA)
8115     {
8116       /* Only certain OpenMP pragmas are attached to statements, and thus
8117          are considered statements themselves.  All others are not.  In
8118          the context of a compound, accept the pragma as a "statement" and
8119          return so that we can check for a close brace.  Otherwise we
8120          require a real statement and must go back and read one.  */
8121       if (in_compound)
8122         cp_parser_pragma (parser, pragma_compound);
8123       else if (!cp_parser_pragma (parser, pragma_stmt))
8124         goto restart;
8125       return;
8126     }
8127   else if (token->type == CPP_EOF)
8128     {
8129       cp_parser_error (parser, "expected statement");
8130       return;
8131     }
8132
8133   /* Everything else must be a declaration-statement or an
8134      expression-statement.  Try for the declaration-statement
8135      first, unless we are looking at a `;', in which case we know that
8136      we have an expression-statement.  */
8137   if (!statement)
8138     {
8139       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8140         {
8141           cp_parser_parse_tentatively (parser);
8142           /* Try to parse the declaration-statement.  */
8143           cp_parser_declaration_statement (parser);
8144           /* If that worked, we're done.  */
8145           if (cp_parser_parse_definitely (parser))
8146             return;
8147         }
8148       /* Look for an expression-statement instead.  */
8149       statement = cp_parser_expression_statement (parser, in_statement_expr);
8150     }
8151
8152   /* Set the line number for the statement.  */
8153   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8154     SET_EXPR_LOCATION (statement, statement_location);
8155 }
8156
8157 /* Parse the label for a labeled-statement, i.e.
8158
8159    identifier :
8160    case constant-expression :
8161    default :
8162
8163    GNU Extension:
8164    case constant-expression ... constant-expression : statement
8165
8166    When a label is parsed without errors, the label is added to the
8167    parse tree by the finish_* functions, so this function doesn't
8168    have to return the label.  */
8169
8170 static void
8171 cp_parser_label_for_labeled_statement (cp_parser* parser)
8172 {
8173   cp_token *token;
8174   tree label = NULL_TREE;
8175   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8176
8177   /* The next token should be an identifier.  */
8178   token = cp_lexer_peek_token (parser->lexer);
8179   if (token->type != CPP_NAME
8180       && token->type != CPP_KEYWORD)
8181     {
8182       cp_parser_error (parser, "expected labeled-statement");
8183       return;
8184     }
8185
8186   parser->colon_corrects_to_scope_p = false;
8187   switch (token->keyword)
8188     {
8189     case RID_CASE:
8190       {
8191         tree expr, expr_hi;
8192         cp_token *ellipsis;
8193
8194         /* Consume the `case' token.  */
8195         cp_lexer_consume_token (parser->lexer);
8196         /* Parse the constant-expression.  */
8197         expr = cp_parser_constant_expression (parser,
8198                                               /*allow_non_constant_p=*/false,
8199                                               NULL);
8200
8201         ellipsis = cp_lexer_peek_token (parser->lexer);
8202         if (ellipsis->type == CPP_ELLIPSIS)
8203           {
8204             /* Consume the `...' token.  */
8205             cp_lexer_consume_token (parser->lexer);
8206             expr_hi =
8207               cp_parser_constant_expression (parser,
8208                                              /*allow_non_constant_p=*/false,
8209                                              NULL);
8210             /* We don't need to emit warnings here, as the common code
8211                will do this for us.  */
8212           }
8213         else
8214           expr_hi = NULL_TREE;
8215
8216         if (parser->in_switch_statement_p)
8217           finish_case_label (token->location, expr, expr_hi);
8218         else
8219           error_at (token->location,
8220                     "case label %qE not within a switch statement",
8221                     expr);
8222       }
8223       break;
8224
8225     case RID_DEFAULT:
8226       /* Consume the `default' token.  */
8227       cp_lexer_consume_token (parser->lexer);
8228
8229       if (parser->in_switch_statement_p)
8230         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8231       else
8232         error_at (token->location, "case label not within a switch statement");
8233       break;
8234
8235     default:
8236       /* Anything else must be an ordinary label.  */
8237       label = finish_label_stmt (cp_parser_identifier (parser));
8238       break;
8239     }
8240
8241   /* Require the `:' token.  */
8242   cp_parser_require (parser, CPP_COLON, RT_COLON);
8243
8244   /* An ordinary label may optionally be followed by attributes.
8245      However, this is only permitted if the attributes are then
8246      followed by a semicolon.  This is because, for backward
8247      compatibility, when parsing
8248        lab: __attribute__ ((unused)) int i;
8249      we want the attribute to attach to "i", not "lab".  */
8250   if (label != NULL_TREE
8251       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8252     {
8253       tree attrs;
8254
8255       cp_parser_parse_tentatively (parser);
8256       attrs = cp_parser_attributes_opt (parser);
8257       if (attrs == NULL_TREE
8258           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8259         cp_parser_abort_tentative_parse (parser);
8260       else if (!cp_parser_parse_definitely (parser))
8261         ;
8262       else
8263         cplus_decl_attributes (&label, attrs, 0);
8264     }
8265
8266   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8267 }
8268
8269 /* Parse an expression-statement.
8270
8271    expression-statement:
8272      expression [opt] ;
8273
8274    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8275    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8276    indicates whether this expression-statement is part of an
8277    expression statement.  */
8278
8279 static tree
8280 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8281 {
8282   tree statement = NULL_TREE;
8283   cp_token *token = cp_lexer_peek_token (parser->lexer);
8284
8285   /* If the next token is a ';', then there is no expression
8286      statement.  */
8287   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8288     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8289
8290   /* Give a helpful message for "A<T>::type t;" and the like.  */
8291   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8292       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8293     {
8294       if (TREE_CODE (statement) == SCOPE_REF)
8295         error_at (token->location, "need %<typename%> before %qE because "
8296                   "%qT is a dependent scope",
8297                   statement, TREE_OPERAND (statement, 0));
8298       else if (is_overloaded_fn (statement)
8299                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8300         {
8301           /* A::A a; */
8302           tree fn = get_first_fn (statement);
8303           error_at (token->location,
8304                     "%<%T::%D%> names the constructor, not the type",
8305                     DECL_CONTEXT (fn), DECL_NAME (fn));
8306         }
8307     }
8308
8309   /* Consume the final `;'.  */
8310   cp_parser_consume_semicolon_at_end_of_statement (parser);
8311
8312   if (in_statement_expr
8313       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8314     /* This is the final expression statement of a statement
8315        expression.  */
8316     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8317   else if (statement)
8318     statement = finish_expr_stmt (statement);
8319   else
8320     finish_stmt ();
8321
8322   return statement;
8323 }
8324
8325 /* Parse a compound-statement.
8326
8327    compound-statement:
8328      { statement-seq [opt] }
8329
8330    GNU extension:
8331
8332    compound-statement:
8333      { label-declaration-seq [opt] statement-seq [opt] }
8334
8335    label-declaration-seq:
8336      label-declaration
8337      label-declaration-seq label-declaration
8338
8339    Returns a tree representing the statement.  */
8340
8341 static tree
8342 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8343                               bool in_try)
8344 {
8345   tree compound_stmt;
8346
8347   /* Consume the `{'.  */
8348   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8349     return error_mark_node;
8350   /* Begin the compound-statement.  */
8351   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8352   /* If the next keyword is `__label__' we have a label declaration.  */
8353   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8354     cp_parser_label_declaration (parser);
8355   /* Parse an (optional) statement-seq.  */
8356   cp_parser_statement_seq_opt (parser, in_statement_expr);
8357   /* Finish the compound-statement.  */
8358   finish_compound_stmt (compound_stmt);
8359   /* Consume the `}'.  */
8360   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8361
8362   return compound_stmt;
8363 }
8364
8365 /* Parse an (optional) statement-seq.
8366
8367    statement-seq:
8368      statement
8369      statement-seq [opt] statement  */
8370
8371 static void
8372 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8373 {
8374   /* Scan statements until there aren't any more.  */
8375   while (true)
8376     {
8377       cp_token *token = cp_lexer_peek_token (parser->lexer);
8378
8379       /* If we are looking at a `}', then we have run out of
8380          statements; the same is true if we have reached the end
8381          of file, or have stumbled upon a stray '@end'.  */
8382       if (token->type == CPP_CLOSE_BRACE
8383           || token->type == CPP_EOF
8384           || token->type == CPP_PRAGMA_EOL
8385           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8386         break;
8387       
8388       /* If we are in a compound statement and find 'else' then
8389          something went wrong.  */
8390       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8391         {
8392           if (parser->in_statement & IN_IF_STMT) 
8393             break;
8394           else
8395             {
8396               token = cp_lexer_consume_token (parser->lexer);
8397               error_at (token->location, "%<else%> without a previous %<if%>");
8398             }
8399         }
8400
8401       /* Parse the statement.  */
8402       cp_parser_statement (parser, in_statement_expr, true, NULL);
8403     }
8404 }
8405
8406 /* Parse a selection-statement.
8407
8408    selection-statement:
8409      if ( condition ) statement
8410      if ( condition ) statement else statement
8411      switch ( condition ) statement
8412
8413    Returns the new IF_STMT or SWITCH_STMT.
8414
8415    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8416    is a (possibly labeled) if statement which is not enclosed in
8417    braces and has an else clause.  This is used to implement
8418    -Wparentheses.  */
8419
8420 static tree
8421 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8422 {
8423   cp_token *token;
8424   enum rid keyword;
8425
8426   if (if_p != NULL)
8427     *if_p = false;
8428
8429   /* Peek at the next token.  */
8430   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8431
8432   /* See what kind of keyword it is.  */
8433   keyword = token->keyword;
8434   switch (keyword)
8435     {
8436     case RID_IF:
8437     case RID_SWITCH:
8438       {
8439         tree statement;
8440         tree condition;
8441
8442         /* Look for the `('.  */
8443         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8444           {
8445             cp_parser_skip_to_end_of_statement (parser);
8446             return error_mark_node;
8447           }
8448
8449         /* Begin the selection-statement.  */
8450         if (keyword == RID_IF)
8451           statement = begin_if_stmt ();
8452         else
8453           statement = begin_switch_stmt ();
8454
8455         /* Parse the condition.  */
8456         condition = cp_parser_condition (parser);
8457         /* Look for the `)'.  */
8458         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8459           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8460                                                  /*consume_paren=*/true);
8461
8462         if (keyword == RID_IF)
8463           {
8464             bool nested_if;
8465             unsigned char in_statement;
8466
8467             /* Add the condition.  */
8468             finish_if_stmt_cond (condition, statement);
8469
8470             /* Parse the then-clause.  */
8471             in_statement = parser->in_statement;
8472             parser->in_statement |= IN_IF_STMT;
8473             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8474               {
8475                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8476                 add_stmt (build_empty_stmt (loc));
8477                 cp_lexer_consume_token (parser->lexer);
8478                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8479                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8480                               "empty body in an %<if%> statement");
8481                 nested_if = false;
8482               }
8483             else
8484               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8485             parser->in_statement = in_statement;
8486
8487             finish_then_clause (statement);
8488
8489             /* If the next token is `else', parse the else-clause.  */
8490             if (cp_lexer_next_token_is_keyword (parser->lexer,
8491                                                 RID_ELSE))
8492               {
8493                 /* Consume the `else' keyword.  */
8494                 cp_lexer_consume_token (parser->lexer);
8495                 begin_else_clause (statement);
8496                 /* Parse the else-clause.  */
8497                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8498                   {
8499                     location_t loc;
8500                     loc = cp_lexer_peek_token (parser->lexer)->location;
8501                     warning_at (loc,
8502                                 OPT_Wempty_body, "suggest braces around "
8503                                 "empty body in an %<else%> statement");
8504                     add_stmt (build_empty_stmt (loc));
8505                     cp_lexer_consume_token (parser->lexer);
8506                   }
8507                 else
8508                   cp_parser_implicitly_scoped_statement (parser, NULL);
8509
8510                 finish_else_clause (statement);
8511
8512                 /* If we are currently parsing a then-clause, then
8513                    IF_P will not be NULL.  We set it to true to
8514                    indicate that this if statement has an else clause.
8515                    This may trigger the Wparentheses warning below
8516                    when we get back up to the parent if statement.  */
8517                 if (if_p != NULL)
8518                   *if_p = true;
8519               }
8520             else
8521               {
8522                 /* This if statement does not have an else clause.  If
8523                    NESTED_IF is true, then the then-clause is an if
8524                    statement which does have an else clause.  We warn
8525                    about the potential ambiguity.  */
8526                 if (nested_if)
8527                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8528                               "suggest explicit braces to avoid ambiguous"
8529                               " %<else%>");
8530               }
8531
8532             /* Now we're all done with the if-statement.  */
8533             finish_if_stmt (statement);
8534           }
8535         else
8536           {
8537             bool in_switch_statement_p;
8538             unsigned char in_statement;
8539
8540             /* Add the condition.  */
8541             finish_switch_cond (condition, statement);
8542
8543             /* Parse the body of the switch-statement.  */
8544             in_switch_statement_p = parser->in_switch_statement_p;
8545             in_statement = parser->in_statement;
8546             parser->in_switch_statement_p = true;
8547             parser->in_statement |= IN_SWITCH_STMT;
8548             cp_parser_implicitly_scoped_statement (parser, NULL);
8549             parser->in_switch_statement_p = in_switch_statement_p;
8550             parser->in_statement = in_statement;
8551
8552             /* Now we're all done with the switch-statement.  */
8553             finish_switch_stmt (statement);
8554           }
8555
8556         return statement;
8557       }
8558       break;
8559
8560     default:
8561       cp_parser_error (parser, "expected selection-statement");
8562       return error_mark_node;
8563     }
8564 }
8565
8566 /* Parse a condition.
8567
8568    condition:
8569      expression
8570      type-specifier-seq declarator = initializer-clause
8571      type-specifier-seq declarator braced-init-list
8572
8573    GNU Extension:
8574
8575    condition:
8576      type-specifier-seq declarator asm-specification [opt]
8577        attributes [opt] = assignment-expression
8578
8579    Returns the expression that should be tested.  */
8580
8581 static tree
8582 cp_parser_condition (cp_parser* parser)
8583 {
8584   cp_decl_specifier_seq type_specifiers;
8585   const char *saved_message;
8586   int declares_class_or_enum;
8587
8588   /* Try the declaration first.  */
8589   cp_parser_parse_tentatively (parser);
8590   /* New types are not allowed in the type-specifier-seq for a
8591      condition.  */
8592   saved_message = parser->type_definition_forbidden_message;
8593   parser->type_definition_forbidden_message
8594     = G_("types may not be defined in conditions");
8595   /* Parse the type-specifier-seq.  */
8596   cp_parser_decl_specifier_seq (parser,
8597                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8598                                 &type_specifiers,
8599                                 &declares_class_or_enum);
8600   /* Restore the saved message.  */
8601   parser->type_definition_forbidden_message = saved_message;
8602   /* If all is well, we might be looking at a declaration.  */
8603   if (!cp_parser_error_occurred (parser))
8604     {
8605       tree decl;
8606       tree asm_specification;
8607       tree attributes;
8608       cp_declarator *declarator;
8609       tree initializer = NULL_TREE;
8610
8611       /* Parse the declarator.  */
8612       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8613                                          /*ctor_dtor_or_conv_p=*/NULL,
8614                                          /*parenthesized_p=*/NULL,
8615                                          /*member_p=*/false);
8616       /* Parse the attributes.  */
8617       attributes = cp_parser_attributes_opt (parser);
8618       /* Parse the asm-specification.  */
8619       asm_specification = cp_parser_asm_specification_opt (parser);
8620       /* If the next token is not an `=' or '{', then we might still be
8621          looking at an expression.  For example:
8622
8623            if (A(a).x)
8624
8625          looks like a decl-specifier-seq and a declarator -- but then
8626          there is no `=', so this is an expression.  */
8627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8628           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8629         cp_parser_simulate_error (parser);
8630         
8631       /* If we did see an `=' or '{', then we are looking at a declaration
8632          for sure.  */
8633       if (cp_parser_parse_definitely (parser))
8634         {
8635           tree pushed_scope;
8636           bool non_constant_p;
8637           bool flags = LOOKUP_ONLYCONVERTING;
8638
8639           /* Create the declaration.  */
8640           decl = start_decl (declarator, &type_specifiers,
8641                              /*initialized_p=*/true,
8642                              attributes, /*prefix_attributes=*/NULL_TREE,
8643                              &pushed_scope);
8644
8645           /* Parse the initializer.  */
8646           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8647             {
8648               initializer = cp_parser_braced_list (parser, &non_constant_p);
8649               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8650               flags = 0;
8651             }
8652           else
8653             {
8654               /* Consume the `='.  */
8655               cp_parser_require (parser, CPP_EQ, RT_EQ);
8656               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8657             }
8658           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8659             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8660
8661           if (!non_constant_p)
8662             initializer = fold_non_dependent_expr (initializer);
8663
8664           /* Process the initializer.  */
8665           cp_finish_decl (decl,
8666                           initializer, !non_constant_p,
8667                           asm_specification,
8668                           flags);
8669
8670           if (pushed_scope)
8671             pop_scope (pushed_scope);
8672
8673           return convert_from_reference (decl);
8674         }
8675     }
8676   /* If we didn't even get past the declarator successfully, we are
8677      definitely not looking at a declaration.  */
8678   else
8679     cp_parser_abort_tentative_parse (parser);
8680
8681   /* Otherwise, we are looking at an expression.  */
8682   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8683 }
8684
8685 /* Parses a traditional for-statement until the closing ')', not included. */
8686
8687 static tree
8688 cp_parser_c_for (cp_parser *parser)
8689 {
8690   /* Normal for loop */
8691   tree stmt;
8692   tree condition = NULL_TREE;
8693   tree expression = NULL_TREE;
8694
8695   /* Begin the for-statement.  */
8696   stmt = begin_for_stmt ();
8697
8698   /* Parse the initialization.  */
8699   cp_parser_for_init_statement (parser);
8700   finish_for_init_stmt (stmt);
8701
8702   /* If there's a condition, process it.  */
8703   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8704     condition = cp_parser_condition (parser);
8705   finish_for_cond (condition, stmt);
8706   /* Look for the `;'.  */
8707   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8708
8709   /* If there's an expression, process it.  */
8710   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8711     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8712   finish_for_expr (expression, stmt);
8713
8714   return stmt;
8715 }
8716
8717 /* Tries to parse a range-based for-statement:
8718
8719   range-based-for:
8720     type-specifier-seq declarator : expression
8721
8722   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8723   expression. Note that the *DECL is returned unfinished, so
8724   later you should call cp_finish_decl().
8725
8726   Returns TRUE iff a range-based for is parsed. */
8727
8728 static tree
8729 cp_parser_range_for (cp_parser *parser)
8730 {
8731   tree stmt, range_decl, range_expr;
8732   cp_decl_specifier_seq type_specifiers;
8733   cp_declarator *declarator;
8734   const char *saved_message;
8735   tree attributes, pushed_scope;
8736   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8737
8738   parser->colon_corrects_to_scope_p = false;
8739   cp_parser_parse_tentatively (parser);
8740   /* New types are not allowed in the type-specifier-seq for a
8741      range-based for loop.  */
8742   saved_message = parser->type_definition_forbidden_message;
8743   parser->type_definition_forbidden_message
8744     = G_("types may not be defined in range-based for loops");
8745   /* Parse the type-specifier-seq.  */
8746   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8747                                 /*is_trailing_return=*/false,
8748                                 &type_specifiers);
8749   /* Restore the saved message.  */
8750   parser->type_definition_forbidden_message = saved_message;
8751   /* If all is well, we might be looking at a declaration.  */
8752   if (cp_parser_error_occurred (parser))
8753     {
8754       cp_parser_abort_tentative_parse (parser);
8755       stmt = NULL_TREE;
8756       goto out;
8757     }
8758   /* Parse the declarator.  */
8759   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8760                                      /*ctor_dtor_or_conv_p=*/NULL,
8761                                      /*parenthesized_p=*/NULL,
8762                                      /*member_p=*/false);
8763   /* Parse the attributes.  */
8764   attributes = cp_parser_attributes_opt (parser);
8765   /* The next token should be `:'. */
8766   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8767     cp_parser_simulate_error (parser);
8768
8769   /* Check if it is a range-based for */
8770   if (!cp_parser_parse_definitely (parser))
8771     return NULL_TREE;
8772
8773   cp_parser_require (parser, CPP_COLON, RT_COLON);
8774   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8775     {
8776       bool expr_non_constant_p;
8777       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8778     }
8779   else
8780     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8781
8782   /* If in template, STMT is converted to a normal for-statements
8783      at instantiation. If not, it is done just ahead. */
8784   if (processing_template_decl)
8785     stmt = begin_range_for_stmt ();
8786   else
8787     stmt = begin_for_stmt ();
8788
8789   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8790   range_decl = start_decl (declarator, &type_specifiers,
8791                            /*initialized_p=*/SD_INITIALIZED,
8792                            attributes, /*prefix_attributes=*/NULL_TREE,
8793                            &pushed_scope);
8794   /* No scope allowed here */
8795   pop_scope (pushed_scope);
8796
8797   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8798     finish_range_for_decl (stmt, range_decl, range_expr);
8799   else
8800     /* Convert the range-based for loop into a normal for-statement. */
8801     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8802
8803  out:
8804   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8805   return stmt;
8806 }
8807
8808 /* Converts a range-based for-statement into a normal
8809    for-statement, as per the definition.
8810
8811       for (RANGE_DECL : RANGE_EXPR)
8812         BLOCK
8813
8814    should be equivalent to:
8815
8816       {
8817         auto &&__range = RANGE_EXPR;
8818         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8819               __begin != __end;
8820               ++__begin)
8821           {
8822               RANGE_DECL = *__begin;
8823               BLOCK
8824           }
8825       }
8826
8827    If RANGE_EXPR is an array:
8828        BEGIN_EXPR = __range
8829        END_EXPR = __range + ARRAY_SIZE(__range)
8830    Else:
8831         BEGIN_EXPR = begin(__range)
8832         END_EXPR = end(__range);
8833
8834    When calling begin()/end() we must use argument dependent
8835    lookup, but always considering 'std' as an associated namespace.  */
8836
8837 tree
8838 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8839 {
8840   tree range_type, range_temp;
8841   tree begin, end;
8842   tree iter_type, begin_expr, end_expr;
8843   tree condition, expression;
8844
8845   /* Find out the type deduced by the declaration
8846    * `auto &&__range = range_expr' */
8847   range_type = cp_build_reference_type (make_auto (), true);
8848   range_type = do_auto_deduction (range_type, range_expr,
8849                                   type_uses_auto (range_type));
8850
8851   /* Create the __range variable */
8852   range_temp = build_decl (input_location, VAR_DECL,
8853                            get_identifier ("__for_range"), range_type);
8854   TREE_USED (range_temp) = 1;
8855   DECL_ARTIFICIAL (range_temp) = 1;
8856   pushdecl (range_temp);
8857   cp_finish_decl (range_temp, range_expr,
8858                   /*is_constant_init*/false, NULL_TREE,
8859                   LOOKUP_ONLYCONVERTING);
8860
8861   range_temp = convert_from_reference (range_temp);
8862
8863   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8864     {
8865       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8866       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8867       begin_expr = range_temp;
8868       end_expr
8869         = build_binary_op (input_location, PLUS_EXPR,
8870                            range_temp,
8871                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8872     }
8873   else
8874     {
8875       /* If it is not an array, we must call begin(__range)/end__range() */
8876       VEC(tree,gc) *vec;
8877
8878       begin_expr = get_identifier ("begin");
8879       vec = make_tree_vector ();
8880       VEC_safe_push (tree, gc, vec, range_temp);
8881       begin_expr = perform_koenig_lookup (begin_expr, vec,
8882                                           /*include_std=*/true);
8883       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8884                                      tf_warning_or_error);
8885       release_tree_vector (vec);
8886
8887       end_expr = get_identifier ("end");
8888       vec = make_tree_vector ();
8889       VEC_safe_push (tree, gc, vec, range_temp);
8890       end_expr = perform_koenig_lookup (end_expr, vec,
8891                                         /*include_std=*/true);
8892       end_expr = finish_call_expr (end_expr, &vec, false, true,
8893                                    tf_warning_or_error);
8894       release_tree_vector (vec);
8895
8896       /* The unqualified type of the __begin and __end temporaries should
8897       * be the same as required by the multiple auto declaration */
8898       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8899       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8900         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8901                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8902     }
8903
8904   /* The new for initialization statement */
8905   begin = build_decl (input_location, VAR_DECL,
8906                       get_identifier ("__for_begin"), iter_type);
8907   TREE_USED (begin) = 1;
8908   DECL_ARTIFICIAL (begin) = 1;
8909   pushdecl (begin);
8910   cp_finish_decl (begin, begin_expr,
8911                   /*is_constant_init*/false, NULL_TREE,
8912                   LOOKUP_ONLYCONVERTING);
8913
8914   end = build_decl (input_location, VAR_DECL,
8915                     get_identifier ("__for_end"), iter_type);
8916   TREE_USED (end) = 1;
8917   DECL_ARTIFICIAL (end) = 1;
8918   pushdecl (end);
8919   cp_finish_decl (end, end_expr,
8920                   /*is_constant_init*/false, NULL_TREE,
8921                   LOOKUP_ONLYCONVERTING);
8922
8923   finish_for_init_stmt (statement);
8924
8925 /* The new for condition */
8926   condition = build_x_binary_op (NE_EXPR,
8927                                  begin, ERROR_MARK,
8928                                  end, ERROR_MARK,
8929                                  NULL, tf_warning_or_error);
8930   finish_for_cond (condition, statement);
8931
8932   /* The new increment expression */
8933   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8934   finish_for_expr (expression, statement);
8935
8936   /* The declaration is initialized with *__begin inside the loop body */
8937   cp_finish_decl (range_decl,
8938                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8939                   /*is_constant_init*/false, NULL_TREE,
8940                   LOOKUP_ONLYCONVERTING);
8941
8942   return statement;
8943 }
8944
8945
8946 /* Parse an iteration-statement.
8947
8948    iteration-statement:
8949      while ( condition ) statement
8950      do statement while ( expression ) ;
8951      for ( for-init-statement condition [opt] ; expression [opt] )
8952        statement
8953
8954    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8955
8956 static tree
8957 cp_parser_iteration_statement (cp_parser* parser)
8958 {
8959   cp_token *token;
8960   enum rid keyword;
8961   tree statement;
8962   unsigned char in_statement;
8963
8964   /* Peek at the next token.  */
8965   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8966   if (!token)
8967     return error_mark_node;
8968
8969   /* Remember whether or not we are already within an iteration
8970      statement.  */
8971   in_statement = parser->in_statement;
8972
8973   /* See what kind of keyword it is.  */
8974   keyword = token->keyword;
8975   switch (keyword)
8976     {
8977     case RID_WHILE:
8978       {
8979         tree condition;
8980
8981         /* Begin the while-statement.  */
8982         statement = begin_while_stmt ();
8983         /* Look for the `('.  */
8984         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8985         /* Parse the condition.  */
8986         condition = cp_parser_condition (parser);
8987         finish_while_stmt_cond (condition, statement);
8988         /* Look for the `)'.  */
8989         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8990         /* Parse the dependent statement.  */
8991         parser->in_statement = IN_ITERATION_STMT;
8992         cp_parser_already_scoped_statement (parser);
8993         parser->in_statement = in_statement;
8994         /* We're done with the while-statement.  */
8995         finish_while_stmt (statement);
8996       }
8997       break;
8998
8999     case RID_DO:
9000       {
9001         tree expression;
9002
9003         /* Begin the do-statement.  */
9004         statement = begin_do_stmt ();
9005         /* Parse the body of the do-statement.  */
9006         parser->in_statement = IN_ITERATION_STMT;
9007         cp_parser_implicitly_scoped_statement (parser, NULL);
9008         parser->in_statement = in_statement;
9009         finish_do_body (statement);
9010         /* Look for the `while' keyword.  */
9011         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9012         /* Look for the `('.  */
9013         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9014         /* Parse the expression.  */
9015         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9016         /* We're done with the do-statement.  */
9017         finish_do_stmt (expression, statement);
9018         /* Look for the `)'.  */
9019         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9020         /* Look for the `;'.  */
9021         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9022       }
9023       break;
9024
9025     case RID_FOR:
9026       {
9027         /* Look for the `('.  */
9028         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9029
9030         if (cxx_dialect == cxx0x)
9031           statement = cp_parser_range_for (parser);
9032         else
9033           statement = NULL_TREE;
9034         if (statement == NULL_TREE)
9035           statement = cp_parser_c_for (parser);
9036
9037         /* Look for the `)'.  */
9038         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9039
9040         /* Parse the body of the for-statement.  */
9041         parser->in_statement = IN_ITERATION_STMT;
9042         cp_parser_already_scoped_statement (parser);
9043         parser->in_statement = in_statement;
9044
9045         /* We're done with the for-statement.  */
9046         finish_for_stmt (statement);
9047       }
9048       break;
9049
9050     default:
9051       cp_parser_error (parser, "expected iteration-statement");
9052       statement = error_mark_node;
9053       break;
9054     }
9055
9056   return statement;
9057 }
9058
9059 /* Parse a for-init-statement.
9060
9061    for-init-statement:
9062      expression-statement
9063      simple-declaration  */
9064
9065 static void
9066 cp_parser_for_init_statement (cp_parser* parser)
9067 {
9068   /* If the next token is a `;', then we have an empty
9069      expression-statement.  Grammatically, this is also a
9070      simple-declaration, but an invalid one, because it does not
9071      declare anything.  Therefore, if we did not handle this case
9072      specially, we would issue an error message about an invalid
9073      declaration.  */
9074   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9075     {
9076       /* We're going to speculatively look for a declaration, falling back
9077          to an expression, if necessary.  */
9078       cp_parser_parse_tentatively (parser);
9079       /* Parse the declaration.  */
9080       cp_parser_simple_declaration (parser,
9081                                     /*function_definition_allowed_p=*/false);
9082       /* If the tentative parse failed, then we shall need to look for an
9083          expression-statement.  */
9084       if (cp_parser_parse_definitely (parser))
9085         return;
9086     }
9087
9088   cp_parser_expression_statement (parser, NULL_TREE);
9089 }
9090
9091 /* Parse a jump-statement.
9092
9093    jump-statement:
9094      break ;
9095      continue ;
9096      return expression [opt] ;
9097      return braced-init-list ;
9098      goto identifier ;
9099
9100    GNU extension:
9101
9102    jump-statement:
9103      goto * expression ;
9104
9105    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9106
9107 static tree
9108 cp_parser_jump_statement (cp_parser* parser)
9109 {
9110   tree statement = error_mark_node;
9111   cp_token *token;
9112   enum rid keyword;
9113   unsigned char in_statement;
9114
9115   /* Peek at the next token.  */
9116   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9117   if (!token)
9118     return error_mark_node;
9119
9120   /* See what kind of keyword it is.  */
9121   keyword = token->keyword;
9122   switch (keyword)
9123     {
9124     case RID_BREAK:
9125       in_statement = parser->in_statement & ~IN_IF_STMT;      
9126       switch (in_statement)
9127         {
9128         case 0:
9129           error_at (token->location, "break statement not within loop or switch");
9130           break;
9131         default:
9132           gcc_assert ((in_statement & IN_SWITCH_STMT)
9133                       || in_statement == IN_ITERATION_STMT);
9134           statement = finish_break_stmt ();
9135           break;
9136         case IN_OMP_BLOCK:
9137           error_at (token->location, "invalid exit from OpenMP structured block");
9138           break;
9139         case IN_OMP_FOR:
9140           error_at (token->location, "break statement used with OpenMP for loop");
9141           break;
9142         }
9143       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9144       break;
9145
9146     case RID_CONTINUE:
9147       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9148         {
9149         case 0:
9150           error_at (token->location, "continue statement not within a loop");
9151           break;
9152         case IN_ITERATION_STMT:
9153         case IN_OMP_FOR:
9154           statement = finish_continue_stmt ();
9155           break;
9156         case IN_OMP_BLOCK:
9157           error_at (token->location, "invalid exit from OpenMP structured block");
9158           break;
9159         default:
9160           gcc_unreachable ();
9161         }
9162       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9163       break;
9164
9165     case RID_RETURN:
9166       {
9167         tree expr;
9168         bool expr_non_constant_p;
9169
9170         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9171           {
9172             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9173             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9174           }
9175         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9176           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9177         else
9178           /* If the next token is a `;', then there is no
9179              expression.  */
9180           expr = NULL_TREE;
9181         /* Build the return-statement.  */
9182         statement = finish_return_stmt (expr);
9183         /* Look for the final `;'.  */
9184         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9185       }
9186       break;
9187
9188     case RID_GOTO:
9189       /* Create the goto-statement.  */
9190       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9191         {
9192           /* Issue a warning about this use of a GNU extension.  */
9193           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9194           /* Consume the '*' token.  */
9195           cp_lexer_consume_token (parser->lexer);
9196           /* Parse the dependent expression.  */
9197           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9198         }
9199       else
9200         finish_goto_stmt (cp_parser_identifier (parser));
9201       /* Look for the final `;'.  */
9202       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9203       break;
9204
9205     default:
9206       cp_parser_error (parser, "expected jump-statement");
9207       break;
9208     }
9209
9210   return statement;
9211 }
9212
9213 /* Parse a declaration-statement.
9214
9215    declaration-statement:
9216      block-declaration  */
9217
9218 static void
9219 cp_parser_declaration_statement (cp_parser* parser)
9220 {
9221   void *p;
9222
9223   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9224   p = obstack_alloc (&declarator_obstack, 0);
9225
9226  /* Parse the block-declaration.  */
9227   cp_parser_block_declaration (parser, /*statement_p=*/true);
9228
9229   /* Free any declarators allocated.  */
9230   obstack_free (&declarator_obstack, p);
9231
9232   /* Finish off the statement.  */
9233   finish_stmt ();
9234 }
9235
9236 /* Some dependent statements (like `if (cond) statement'), are
9237    implicitly in their own scope.  In other words, if the statement is
9238    a single statement (as opposed to a compound-statement), it is
9239    none-the-less treated as if it were enclosed in braces.  Any
9240    declarations appearing in the dependent statement are out of scope
9241    after control passes that point.  This function parses a statement,
9242    but ensures that is in its own scope, even if it is not a
9243    compound-statement.
9244
9245    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9246    is a (possibly labeled) if statement which is not enclosed in
9247    braces and has an else clause.  This is used to implement
9248    -Wparentheses.
9249
9250    Returns the new statement.  */
9251
9252 static tree
9253 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9254 {
9255   tree statement;
9256
9257   if (if_p != NULL)
9258     *if_p = false;
9259
9260   /* Mark if () ; with a special NOP_EXPR.  */
9261   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9262     {
9263       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9264       cp_lexer_consume_token (parser->lexer);
9265       statement = add_stmt (build_empty_stmt (loc));
9266     }
9267   /* if a compound is opened, we simply parse the statement directly.  */
9268   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9269     statement = cp_parser_compound_statement (parser, NULL, false);
9270   /* If the token is not a `{', then we must take special action.  */
9271   else
9272     {
9273       /* Create a compound-statement.  */
9274       statement = begin_compound_stmt (0);
9275       /* Parse the dependent-statement.  */
9276       cp_parser_statement (parser, NULL_TREE, false, if_p);
9277       /* Finish the dummy compound-statement.  */
9278       finish_compound_stmt (statement);
9279     }
9280
9281   /* Return the statement.  */
9282   return statement;
9283 }
9284
9285 /* For some dependent statements (like `while (cond) statement'), we
9286    have already created a scope.  Therefore, even if the dependent
9287    statement is a compound-statement, we do not want to create another
9288    scope.  */
9289
9290 static void
9291 cp_parser_already_scoped_statement (cp_parser* parser)
9292 {
9293   /* If the token is a `{', then we must take special action.  */
9294   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9295     cp_parser_statement (parser, NULL_TREE, false, NULL);
9296   else
9297     {
9298       /* Avoid calling cp_parser_compound_statement, so that we
9299          don't create a new scope.  Do everything else by hand.  */
9300       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9301       /* If the next keyword is `__label__' we have a label declaration.  */
9302       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9303         cp_parser_label_declaration (parser);
9304       /* Parse an (optional) statement-seq.  */
9305       cp_parser_statement_seq_opt (parser, NULL_TREE);
9306       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9307     }
9308 }
9309
9310 /* Declarations [gram.dcl.dcl] */
9311
9312 /* Parse an optional declaration-sequence.
9313
9314    declaration-seq:
9315      declaration
9316      declaration-seq declaration  */
9317
9318 static void
9319 cp_parser_declaration_seq_opt (cp_parser* parser)
9320 {
9321   while (true)
9322     {
9323       cp_token *token;
9324
9325       token = cp_lexer_peek_token (parser->lexer);
9326
9327       if (token->type == CPP_CLOSE_BRACE
9328           || token->type == CPP_EOF
9329           || token->type == CPP_PRAGMA_EOL)
9330         break;
9331
9332       if (token->type == CPP_SEMICOLON)
9333         {
9334           /* A declaration consisting of a single semicolon is
9335              invalid.  Allow it unless we're being pedantic.  */
9336           cp_lexer_consume_token (parser->lexer);
9337           if (!in_system_header)
9338             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9339           continue;
9340         }
9341
9342       /* If we're entering or exiting a region that's implicitly
9343          extern "C", modify the lang context appropriately.  */
9344       if (!parser->implicit_extern_c && token->implicit_extern_c)
9345         {
9346           push_lang_context (lang_name_c);
9347           parser->implicit_extern_c = true;
9348         }
9349       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9350         {
9351           pop_lang_context ();
9352           parser->implicit_extern_c = false;
9353         }
9354
9355       if (token->type == CPP_PRAGMA)
9356         {
9357           /* A top-level declaration can consist solely of a #pragma.
9358              A nested declaration cannot, so this is done here and not
9359              in cp_parser_declaration.  (A #pragma at block scope is
9360              handled in cp_parser_statement.)  */
9361           cp_parser_pragma (parser, pragma_external);
9362           continue;
9363         }
9364
9365       /* Parse the declaration itself.  */
9366       cp_parser_declaration (parser);
9367     }
9368 }
9369
9370 /* Parse a declaration.
9371
9372    declaration:
9373      block-declaration
9374      function-definition
9375      template-declaration
9376      explicit-instantiation
9377      explicit-specialization
9378      linkage-specification
9379      namespace-definition
9380
9381    GNU extension:
9382
9383    declaration:
9384       __extension__ declaration */
9385
9386 static void
9387 cp_parser_declaration (cp_parser* parser)
9388 {
9389   cp_token token1;
9390   cp_token token2;
9391   int saved_pedantic;
9392   void *p;
9393   tree attributes = NULL_TREE;
9394
9395   /* Check for the `__extension__' keyword.  */
9396   if (cp_parser_extension_opt (parser, &saved_pedantic))
9397     {
9398       /* Parse the qualified declaration.  */
9399       cp_parser_declaration (parser);
9400       /* Restore the PEDANTIC flag.  */
9401       pedantic = saved_pedantic;
9402
9403       return;
9404     }
9405
9406   /* Try to figure out what kind of declaration is present.  */
9407   token1 = *cp_lexer_peek_token (parser->lexer);
9408
9409   if (token1.type != CPP_EOF)
9410     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9411   else
9412     {
9413       token2.type = CPP_EOF;
9414       token2.keyword = RID_MAX;
9415     }
9416
9417   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9418   p = obstack_alloc (&declarator_obstack, 0);
9419
9420   /* If the next token is `extern' and the following token is a string
9421      literal, then we have a linkage specification.  */
9422   if (token1.keyword == RID_EXTERN
9423       && cp_parser_is_string_literal (&token2))
9424     cp_parser_linkage_specification (parser);
9425   /* If the next token is `template', then we have either a template
9426      declaration, an explicit instantiation, or an explicit
9427      specialization.  */
9428   else if (token1.keyword == RID_TEMPLATE)
9429     {
9430       /* `template <>' indicates a template specialization.  */
9431       if (token2.type == CPP_LESS
9432           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9433         cp_parser_explicit_specialization (parser);
9434       /* `template <' indicates a template declaration.  */
9435       else if (token2.type == CPP_LESS)
9436         cp_parser_template_declaration (parser, /*member_p=*/false);
9437       /* Anything else must be an explicit instantiation.  */
9438       else
9439         cp_parser_explicit_instantiation (parser);
9440     }
9441   /* If the next token is `export', then we have a template
9442      declaration.  */
9443   else if (token1.keyword == RID_EXPORT)
9444     cp_parser_template_declaration (parser, /*member_p=*/false);
9445   /* If the next token is `extern', 'static' or 'inline' and the one
9446      after that is `template', we have a GNU extended explicit
9447      instantiation directive.  */
9448   else if (cp_parser_allow_gnu_extensions_p (parser)
9449            && (token1.keyword == RID_EXTERN
9450                || token1.keyword == RID_STATIC
9451                || token1.keyword == RID_INLINE)
9452            && token2.keyword == RID_TEMPLATE)
9453     cp_parser_explicit_instantiation (parser);
9454   /* If the next token is `namespace', check for a named or unnamed
9455      namespace definition.  */
9456   else if (token1.keyword == RID_NAMESPACE
9457            && (/* A named namespace definition.  */
9458                (token2.type == CPP_NAME
9459                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9460                     != CPP_EQ))
9461                /* An unnamed namespace definition.  */
9462                || token2.type == CPP_OPEN_BRACE
9463                || token2.keyword == RID_ATTRIBUTE))
9464     cp_parser_namespace_definition (parser);
9465   /* An inline (associated) namespace definition.  */
9466   else if (token1.keyword == RID_INLINE
9467            && token2.keyword == RID_NAMESPACE)
9468     cp_parser_namespace_definition (parser);
9469   /* Objective-C++ declaration/definition.  */
9470   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9471     cp_parser_objc_declaration (parser, NULL_TREE);
9472   else if (c_dialect_objc ()
9473            && token1.keyword == RID_ATTRIBUTE
9474            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9475     cp_parser_objc_declaration (parser, attributes);
9476   /* We must have either a block declaration or a function
9477      definition.  */
9478   else
9479     /* Try to parse a block-declaration, or a function-definition.  */
9480     cp_parser_block_declaration (parser, /*statement_p=*/false);
9481
9482   /* Free any declarators allocated.  */
9483   obstack_free (&declarator_obstack, p);
9484 }
9485
9486 /* Parse a block-declaration.
9487
9488    block-declaration:
9489      simple-declaration
9490      asm-definition
9491      namespace-alias-definition
9492      using-declaration
9493      using-directive
9494
9495    GNU Extension:
9496
9497    block-declaration:
9498      __extension__ block-declaration
9499
9500    C++0x Extension:
9501
9502    block-declaration:
9503      static_assert-declaration
9504
9505    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9506    part of a declaration-statement.  */
9507
9508 static void
9509 cp_parser_block_declaration (cp_parser *parser,
9510                              bool      statement_p)
9511 {
9512   cp_token *token1;
9513   int saved_pedantic;
9514
9515   /* Check for the `__extension__' keyword.  */
9516   if (cp_parser_extension_opt (parser, &saved_pedantic))
9517     {
9518       /* Parse the qualified declaration.  */
9519       cp_parser_block_declaration (parser, statement_p);
9520       /* Restore the PEDANTIC flag.  */
9521       pedantic = saved_pedantic;
9522
9523       return;
9524     }
9525
9526   /* Peek at the next token to figure out which kind of declaration is
9527      present.  */
9528   token1 = cp_lexer_peek_token (parser->lexer);
9529
9530   /* If the next keyword is `asm', we have an asm-definition.  */
9531   if (token1->keyword == RID_ASM)
9532     {
9533       if (statement_p)
9534         cp_parser_commit_to_tentative_parse (parser);
9535       cp_parser_asm_definition (parser);
9536     }
9537   /* If the next keyword is `namespace', we have a
9538      namespace-alias-definition.  */
9539   else if (token1->keyword == RID_NAMESPACE)
9540     cp_parser_namespace_alias_definition (parser);
9541   /* If the next keyword is `using', we have either a
9542      using-declaration or a using-directive.  */
9543   else if (token1->keyword == RID_USING)
9544     {
9545       cp_token *token2;
9546
9547       if (statement_p)
9548         cp_parser_commit_to_tentative_parse (parser);
9549       /* If the token after `using' is `namespace', then we have a
9550          using-directive.  */
9551       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9552       if (token2->keyword == RID_NAMESPACE)
9553         cp_parser_using_directive (parser);
9554       /* Otherwise, it's a using-declaration.  */
9555       else
9556         cp_parser_using_declaration (parser,
9557                                      /*access_declaration_p=*/false);
9558     }
9559   /* If the next keyword is `__label__' we have a misplaced label
9560      declaration.  */
9561   else if (token1->keyword == RID_LABEL)
9562     {
9563       cp_lexer_consume_token (parser->lexer);
9564       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9565       cp_parser_skip_to_end_of_statement (parser);
9566       /* If the next token is now a `;', consume it.  */
9567       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9568         cp_lexer_consume_token (parser->lexer);
9569     }
9570   /* If the next token is `static_assert' we have a static assertion.  */
9571   else if (token1->keyword == RID_STATIC_ASSERT)
9572     cp_parser_static_assert (parser, /*member_p=*/false);
9573   /* Anything else must be a simple-declaration.  */
9574   else
9575     cp_parser_simple_declaration (parser, !statement_p);
9576 }
9577
9578 /* Parse a simple-declaration.
9579
9580    simple-declaration:
9581      decl-specifier-seq [opt] init-declarator-list [opt] ;
9582
9583    init-declarator-list:
9584      init-declarator
9585      init-declarator-list , init-declarator
9586
9587    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9588    function-definition as a simple-declaration.  */
9589
9590 static void
9591 cp_parser_simple_declaration (cp_parser* parser,
9592                               bool function_definition_allowed_p)
9593 {
9594   cp_decl_specifier_seq decl_specifiers;
9595   int declares_class_or_enum;
9596   bool saw_declarator;
9597
9598   /* Defer access checks until we know what is being declared; the
9599      checks for names appearing in the decl-specifier-seq should be
9600      done as if we were in the scope of the thing being declared.  */
9601   push_deferring_access_checks (dk_deferred);
9602
9603   /* Parse the decl-specifier-seq.  We have to keep track of whether
9604      or not the decl-specifier-seq declares a named class or
9605      enumeration type, since that is the only case in which the
9606      init-declarator-list is allowed to be empty.
9607
9608      [dcl.dcl]
9609
9610      In a simple-declaration, the optional init-declarator-list can be
9611      omitted only when declaring a class or enumeration, that is when
9612      the decl-specifier-seq contains either a class-specifier, an
9613      elaborated-type-specifier, or an enum-specifier.  */
9614   cp_parser_decl_specifier_seq (parser,
9615                                 CP_PARSER_FLAGS_OPTIONAL,
9616                                 &decl_specifiers,
9617                                 &declares_class_or_enum);
9618   /* We no longer need to defer access checks.  */
9619   stop_deferring_access_checks ();
9620
9621   /* In a block scope, a valid declaration must always have a
9622      decl-specifier-seq.  By not trying to parse declarators, we can
9623      resolve the declaration/expression ambiguity more quickly.  */
9624   if (!function_definition_allowed_p
9625       && !decl_specifiers.any_specifiers_p)
9626     {
9627       cp_parser_error (parser, "expected declaration");
9628       goto done;
9629     }
9630
9631   /* If the next two tokens are both identifiers, the code is
9632      erroneous. The usual cause of this situation is code like:
9633
9634        T t;
9635
9636      where "T" should name a type -- but does not.  */
9637   if (!decl_specifiers.any_type_specifiers_p
9638       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9639     {
9640       /* If parsing tentatively, we should commit; we really are
9641          looking at a declaration.  */
9642       cp_parser_commit_to_tentative_parse (parser);
9643       /* Give up.  */
9644       goto done;
9645     }
9646
9647   /* If we have seen at least one decl-specifier, and the next token
9648      is not a parenthesis, then we must be looking at a declaration.
9649      (After "int (" we might be looking at a functional cast.)  */
9650   if (decl_specifiers.any_specifiers_p
9651       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9652       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9653       && !cp_parser_error_occurred (parser))
9654     cp_parser_commit_to_tentative_parse (parser);
9655
9656   /* Keep going until we hit the `;' at the end of the simple
9657      declaration.  */
9658   saw_declarator = false;
9659   while (cp_lexer_next_token_is_not (parser->lexer,
9660                                      CPP_SEMICOLON))
9661     {
9662       cp_token *token;
9663       bool function_definition_p;
9664       tree decl;
9665
9666       if (saw_declarator)
9667         {
9668           /* If we are processing next declarator, coma is expected */
9669           token = cp_lexer_peek_token (parser->lexer);
9670           gcc_assert (token->type == CPP_COMMA);
9671           cp_lexer_consume_token (parser->lexer);
9672         }
9673       else
9674         saw_declarator = true;
9675
9676       /* Parse the init-declarator.  */
9677       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9678                                         /*checks=*/NULL,
9679                                         function_definition_allowed_p,
9680                                         /*member_p=*/false,
9681                                         declares_class_or_enum,
9682                                         &function_definition_p);
9683       /* If an error occurred while parsing tentatively, exit quickly.
9684          (That usually happens when in the body of a function; each
9685          statement is treated as a declaration-statement until proven
9686          otherwise.)  */
9687       if (cp_parser_error_occurred (parser))
9688         goto done;
9689       /* Handle function definitions specially.  */
9690       if (function_definition_p)
9691         {
9692           /* If the next token is a `,', then we are probably
9693              processing something like:
9694
9695                void f() {}, *p;
9696
9697              which is erroneous.  */
9698           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9699             {
9700               cp_token *token = cp_lexer_peek_token (parser->lexer);
9701               error_at (token->location,
9702                         "mixing"
9703                         " declarations and function-definitions is forbidden");
9704             }
9705           /* Otherwise, we're done with the list of declarators.  */
9706           else
9707             {
9708               pop_deferring_access_checks ();
9709               return;
9710             }
9711         }
9712       /* The next token should be either a `,' or a `;'.  */
9713       token = cp_lexer_peek_token (parser->lexer);
9714       /* If it's a `,', there are more declarators to come.  */
9715       if (token->type == CPP_COMMA)
9716         /* will be consumed next time around */;
9717       /* If it's a `;', we are done.  */
9718       else if (token->type == CPP_SEMICOLON)
9719         break;
9720       /* Anything else is an error.  */
9721       else
9722         {
9723           /* If we have already issued an error message we don't need
9724              to issue another one.  */
9725           if (decl != error_mark_node
9726               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9727             cp_parser_error (parser, "expected %<,%> or %<;%>");
9728           /* Skip tokens until we reach the end of the statement.  */
9729           cp_parser_skip_to_end_of_statement (parser);
9730           /* If the next token is now a `;', consume it.  */
9731           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9732             cp_lexer_consume_token (parser->lexer);
9733           goto done;
9734         }
9735       /* After the first time around, a function-definition is not
9736          allowed -- even if it was OK at first.  For example:
9737
9738            int i, f() {}
9739
9740          is not valid.  */
9741       function_definition_allowed_p = false;
9742     }
9743
9744   /* Issue an error message if no declarators are present, and the
9745      decl-specifier-seq does not itself declare a class or
9746      enumeration.  */
9747   if (!saw_declarator)
9748     {
9749       if (cp_parser_declares_only_class_p (parser))
9750         shadow_tag (&decl_specifiers);
9751       /* Perform any deferred access checks.  */
9752       perform_deferred_access_checks ();
9753     }
9754
9755   /* Consume the `;'.  */
9756   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9757
9758  done:
9759   pop_deferring_access_checks ();
9760 }
9761
9762 /* Parse a decl-specifier-seq.
9763
9764    decl-specifier-seq:
9765      decl-specifier-seq [opt] decl-specifier
9766
9767    decl-specifier:
9768      storage-class-specifier
9769      type-specifier
9770      function-specifier
9771      friend
9772      typedef
9773
9774    GNU Extension:
9775
9776    decl-specifier:
9777      attributes
9778
9779    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9780
9781    The parser flags FLAGS is used to control type-specifier parsing.
9782
9783    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9784    flags:
9785
9786      1: one of the decl-specifiers is an elaborated-type-specifier
9787         (i.e., a type declaration)
9788      2: one of the decl-specifiers is an enum-specifier or a
9789         class-specifier (i.e., a type definition)
9790
9791    */
9792
9793 static void
9794 cp_parser_decl_specifier_seq (cp_parser* parser,
9795                               cp_parser_flags flags,
9796                               cp_decl_specifier_seq *decl_specs,
9797                               int* declares_class_or_enum)
9798 {
9799   bool constructor_possible_p = !parser->in_declarator_p;
9800   cp_token *start_token = NULL;
9801
9802   /* Clear DECL_SPECS.  */
9803   clear_decl_specs (decl_specs);
9804
9805   /* Assume no class or enumeration type is declared.  */
9806   *declares_class_or_enum = 0;
9807
9808   /* Keep reading specifiers until there are no more to read.  */
9809   while (true)
9810     {
9811       bool constructor_p;
9812       bool found_decl_spec;
9813       cp_token *token;
9814
9815       /* Peek at the next token.  */
9816       token = cp_lexer_peek_token (parser->lexer);
9817
9818       /* Save the first token of the decl spec list for error
9819          reporting.  */
9820       if (!start_token)
9821         start_token = token;
9822       /* Handle attributes.  */
9823       if (token->keyword == RID_ATTRIBUTE)
9824         {
9825           /* Parse the attributes.  */
9826           decl_specs->attributes
9827             = chainon (decl_specs->attributes,
9828                        cp_parser_attributes_opt (parser));
9829           continue;
9830         }
9831       /* Assume we will find a decl-specifier keyword.  */
9832       found_decl_spec = true;
9833       /* If the next token is an appropriate keyword, we can simply
9834          add it to the list.  */
9835       switch (token->keyword)
9836         {
9837           /* decl-specifier:
9838                friend
9839                constexpr */
9840         case RID_FRIEND:
9841           if (!at_class_scope_p ())
9842             {
9843               error_at (token->location, "%<friend%> used outside of class");
9844               cp_lexer_purge_token (parser->lexer);
9845             }
9846           else
9847             {
9848               ++decl_specs->specs[(int) ds_friend];
9849               /* Consume the token.  */
9850               cp_lexer_consume_token (parser->lexer);
9851             }
9852           break;
9853
9854         case RID_CONSTEXPR:
9855           ++decl_specs->specs[(int) ds_constexpr];
9856           cp_lexer_consume_token (parser->lexer);
9857           break;
9858
9859           /* function-specifier:
9860                inline
9861                virtual
9862                explicit  */
9863         case RID_INLINE:
9864         case RID_VIRTUAL:
9865         case RID_EXPLICIT:
9866           cp_parser_function_specifier_opt (parser, decl_specs);
9867           break;
9868
9869           /* decl-specifier:
9870                typedef  */
9871         case RID_TYPEDEF:
9872           ++decl_specs->specs[(int) ds_typedef];
9873           /* Consume the token.  */
9874           cp_lexer_consume_token (parser->lexer);
9875           /* A constructor declarator cannot appear in a typedef.  */
9876           constructor_possible_p = false;
9877           /* The "typedef" keyword can only occur in a declaration; we
9878              may as well commit at this point.  */
9879           cp_parser_commit_to_tentative_parse (parser);
9880
9881           if (decl_specs->storage_class != sc_none)
9882             decl_specs->conflicting_specifiers_p = true;
9883           break;
9884
9885           /* storage-class-specifier:
9886                auto
9887                register
9888                static
9889                extern
9890                mutable
9891
9892              GNU Extension:
9893                thread  */
9894         case RID_AUTO:
9895           if (cxx_dialect == cxx98) 
9896             {
9897               /* Consume the token.  */
9898               cp_lexer_consume_token (parser->lexer);
9899
9900               /* Complain about `auto' as a storage specifier, if
9901                  we're complaining about C++0x compatibility.  */
9902               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9903                           " will change meaning in C++0x; please remove it");
9904
9905               /* Set the storage class anyway.  */
9906               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9907                                            token->location);
9908             }
9909           else
9910             /* C++0x auto type-specifier.  */
9911             found_decl_spec = false;
9912           break;
9913
9914         case RID_REGISTER:
9915         case RID_STATIC:
9916         case RID_EXTERN:
9917         case RID_MUTABLE:
9918           /* Consume the token.  */
9919           cp_lexer_consume_token (parser->lexer);
9920           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9921                                        token->location);
9922           break;
9923         case RID_THREAD:
9924           /* Consume the token.  */
9925           cp_lexer_consume_token (parser->lexer);
9926           ++decl_specs->specs[(int) ds_thread];
9927           break;
9928
9929         default:
9930           /* We did not yet find a decl-specifier yet.  */
9931           found_decl_spec = false;
9932           break;
9933         }
9934
9935       if (found_decl_spec
9936           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9937           && token->keyword != RID_CONSTEXPR)
9938         error ("decl-specifier invalid in condition");
9939
9940       /* Constructors are a special case.  The `S' in `S()' is not a
9941          decl-specifier; it is the beginning of the declarator.  */
9942       constructor_p
9943         = (!found_decl_spec
9944            && constructor_possible_p
9945            && (cp_parser_constructor_declarator_p
9946                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9947
9948       /* If we don't have a DECL_SPEC yet, then we must be looking at
9949          a type-specifier.  */
9950       if (!found_decl_spec && !constructor_p)
9951         {
9952           int decl_spec_declares_class_or_enum;
9953           bool is_cv_qualifier;
9954           tree type_spec;
9955
9956           type_spec
9957             = cp_parser_type_specifier (parser, flags,
9958                                         decl_specs,
9959                                         /*is_declaration=*/true,
9960                                         &decl_spec_declares_class_or_enum,
9961                                         &is_cv_qualifier);
9962           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9963
9964           /* If this type-specifier referenced a user-defined type
9965              (a typedef, class-name, etc.), then we can't allow any
9966              more such type-specifiers henceforth.
9967
9968              [dcl.spec]
9969
9970              The longest sequence of decl-specifiers that could
9971              possibly be a type name is taken as the
9972              decl-specifier-seq of a declaration.  The sequence shall
9973              be self-consistent as described below.
9974
9975              [dcl.type]
9976
9977              As a general rule, at most one type-specifier is allowed
9978              in the complete decl-specifier-seq of a declaration.  The
9979              only exceptions are the following:
9980
9981              -- const or volatile can be combined with any other
9982                 type-specifier.
9983
9984              -- signed or unsigned can be combined with char, long,
9985                 short, or int.
9986
9987              -- ..
9988
9989              Example:
9990
9991                typedef char* Pc;
9992                void g (const int Pc);
9993
9994              Here, Pc is *not* part of the decl-specifier seq; it's
9995              the declarator.  Therefore, once we see a type-specifier
9996              (other than a cv-qualifier), we forbid any additional
9997              user-defined types.  We *do* still allow things like `int
9998              int' to be considered a decl-specifier-seq, and issue the
9999              error message later.  */
10000           if (type_spec && !is_cv_qualifier)
10001             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10002           /* A constructor declarator cannot follow a type-specifier.  */
10003           if (type_spec)
10004             {
10005               constructor_possible_p = false;
10006               found_decl_spec = true;
10007               if (!is_cv_qualifier)
10008                 decl_specs->any_type_specifiers_p = true;
10009             }
10010         }
10011
10012       /* If we still do not have a DECL_SPEC, then there are no more
10013          decl-specifiers.  */
10014       if (!found_decl_spec)
10015         break;
10016
10017       decl_specs->any_specifiers_p = true;
10018       /* After we see one decl-specifier, further decl-specifiers are
10019          always optional.  */
10020       flags |= CP_PARSER_FLAGS_OPTIONAL;
10021     }
10022
10023   cp_parser_check_decl_spec (decl_specs, start_token->location);
10024
10025   /* Don't allow a friend specifier with a class definition.  */
10026   if (decl_specs->specs[(int) ds_friend] != 0
10027       && (*declares_class_or_enum & 2))
10028     error_at (start_token->location,
10029               "class definition may not be declared a friend");
10030 }
10031
10032 /* Parse an (optional) storage-class-specifier.
10033
10034    storage-class-specifier:
10035      auto
10036      register
10037      static
10038      extern
10039      mutable
10040
10041    GNU Extension:
10042
10043    storage-class-specifier:
10044      thread
10045
10046    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10047
10048 static tree
10049 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10050 {
10051   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10052     {
10053     case RID_AUTO:
10054       if (cxx_dialect != cxx98)
10055         return NULL_TREE;
10056       /* Fall through for C++98.  */
10057
10058     case RID_REGISTER:
10059     case RID_STATIC:
10060     case RID_EXTERN:
10061     case RID_MUTABLE:
10062     case RID_THREAD:
10063       /* Consume the token.  */
10064       return cp_lexer_consume_token (parser->lexer)->u.value;
10065
10066     default:
10067       return NULL_TREE;
10068     }
10069 }
10070
10071 /* Parse an (optional) function-specifier.
10072
10073    function-specifier:
10074      inline
10075      virtual
10076      explicit
10077
10078    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10079    Updates DECL_SPECS, if it is non-NULL.  */
10080
10081 static tree
10082 cp_parser_function_specifier_opt (cp_parser* parser,
10083                                   cp_decl_specifier_seq *decl_specs)
10084 {
10085   cp_token *token = cp_lexer_peek_token (parser->lexer);
10086   switch (token->keyword)
10087     {
10088     case RID_INLINE:
10089       if (decl_specs)
10090         ++decl_specs->specs[(int) ds_inline];
10091       break;
10092
10093     case RID_VIRTUAL:
10094       /* 14.5.2.3 [temp.mem]
10095
10096          A member function template shall not be virtual.  */
10097       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10098         error_at (token->location, "templates may not be %<virtual%>");
10099       else if (decl_specs)
10100         ++decl_specs->specs[(int) ds_virtual];
10101       break;
10102
10103     case RID_EXPLICIT:
10104       if (decl_specs)
10105         ++decl_specs->specs[(int) ds_explicit];
10106       break;
10107
10108     default:
10109       return NULL_TREE;
10110     }
10111
10112   /* Consume the token.  */
10113   return cp_lexer_consume_token (parser->lexer)->u.value;
10114 }
10115
10116 /* Parse a linkage-specification.
10117
10118    linkage-specification:
10119      extern string-literal { declaration-seq [opt] }
10120      extern string-literal declaration  */
10121
10122 static void
10123 cp_parser_linkage_specification (cp_parser* parser)
10124 {
10125   tree linkage;
10126
10127   /* Look for the `extern' keyword.  */
10128   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10129
10130   /* Look for the string-literal.  */
10131   linkage = cp_parser_string_literal (parser, false, false);
10132
10133   /* Transform the literal into an identifier.  If the literal is a
10134      wide-character string, or contains embedded NULs, then we can't
10135      handle it as the user wants.  */
10136   if (strlen (TREE_STRING_POINTER (linkage))
10137       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10138     {
10139       cp_parser_error (parser, "invalid linkage-specification");
10140       /* Assume C++ linkage.  */
10141       linkage = lang_name_cplusplus;
10142     }
10143   else
10144     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10145
10146   /* We're now using the new linkage.  */
10147   push_lang_context (linkage);
10148
10149   /* If the next token is a `{', then we're using the first
10150      production.  */
10151   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10152     {
10153       /* Consume the `{' token.  */
10154       cp_lexer_consume_token (parser->lexer);
10155       /* Parse the declarations.  */
10156       cp_parser_declaration_seq_opt (parser);
10157       /* Look for the closing `}'.  */
10158       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10159     }
10160   /* Otherwise, there's just one declaration.  */
10161   else
10162     {
10163       bool saved_in_unbraced_linkage_specification_p;
10164
10165       saved_in_unbraced_linkage_specification_p
10166         = parser->in_unbraced_linkage_specification_p;
10167       parser->in_unbraced_linkage_specification_p = true;
10168       cp_parser_declaration (parser);
10169       parser->in_unbraced_linkage_specification_p
10170         = saved_in_unbraced_linkage_specification_p;
10171     }
10172
10173   /* We're done with the linkage-specification.  */
10174   pop_lang_context ();
10175 }
10176
10177 /* Parse a static_assert-declaration.
10178
10179    static_assert-declaration:
10180      static_assert ( constant-expression , string-literal ) ; 
10181
10182    If MEMBER_P, this static_assert is a class member.  */
10183
10184 static void 
10185 cp_parser_static_assert(cp_parser *parser, bool member_p)
10186 {
10187   tree condition;
10188   tree message;
10189   cp_token *token;
10190   location_t saved_loc;
10191
10192   /* Peek at the `static_assert' token so we can keep track of exactly
10193      where the static assertion started.  */
10194   token = cp_lexer_peek_token (parser->lexer);
10195   saved_loc = token->location;
10196
10197   /* Look for the `static_assert' keyword.  */
10198   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10199                                   RT_STATIC_ASSERT))
10200     return;
10201
10202   /*  We know we are in a static assertion; commit to any tentative
10203       parse.  */
10204   if (cp_parser_parsing_tentatively (parser))
10205     cp_parser_commit_to_tentative_parse (parser);
10206
10207   /* Parse the `(' starting the static assertion condition.  */
10208   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10209
10210   /* Parse the constant-expression.  */
10211   condition = 
10212     cp_parser_constant_expression (parser,
10213                                    /*allow_non_constant_p=*/false,
10214                                    /*non_constant_p=*/NULL);
10215
10216   /* Parse the separating `,'.  */
10217   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10218
10219   /* Parse the string-literal message.  */
10220   message = cp_parser_string_literal (parser, 
10221                                       /*translate=*/false,
10222                                       /*wide_ok=*/true);
10223
10224   /* A `)' completes the static assertion.  */
10225   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10226     cp_parser_skip_to_closing_parenthesis (parser, 
10227                                            /*recovering=*/true, 
10228                                            /*or_comma=*/false,
10229                                            /*consume_paren=*/true);
10230
10231   /* A semicolon terminates the declaration.  */
10232   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10233
10234   /* Complete the static assertion, which may mean either processing 
10235      the static assert now or saving it for template instantiation.  */
10236   finish_static_assert (condition, message, saved_loc, member_p);
10237 }
10238
10239 /* Parse a `decltype' type. Returns the type. 
10240
10241    simple-type-specifier:
10242      decltype ( expression )  */
10243
10244 static tree
10245 cp_parser_decltype (cp_parser *parser)
10246 {
10247   tree expr;
10248   bool id_expression_or_member_access_p = false;
10249   const char *saved_message;
10250   bool saved_integral_constant_expression_p;
10251   bool saved_non_integral_constant_expression_p;
10252   cp_token *id_expr_start_token;
10253
10254   /* Look for the `decltype' token.  */
10255   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10256     return error_mark_node;
10257
10258   /* Types cannot be defined in a `decltype' expression.  Save away the
10259      old message.  */
10260   saved_message = parser->type_definition_forbidden_message;
10261
10262   /* And create the new one.  */
10263   parser->type_definition_forbidden_message
10264     = G_("types may not be defined in %<decltype%> expressions");
10265
10266   /* The restrictions on constant-expressions do not apply inside
10267      decltype expressions.  */
10268   saved_integral_constant_expression_p
10269     = parser->integral_constant_expression_p;
10270   saved_non_integral_constant_expression_p
10271     = parser->non_integral_constant_expression_p;
10272   parser->integral_constant_expression_p = false;
10273
10274   /* Do not actually evaluate the expression.  */
10275   ++cp_unevaluated_operand;
10276
10277   /* Do not warn about problems with the expression.  */
10278   ++c_inhibit_evaluation_warnings;
10279
10280   /* Parse the opening `('.  */
10281   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10282     return error_mark_node;
10283   
10284   /* First, try parsing an id-expression.  */
10285   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10286   cp_parser_parse_tentatively (parser);
10287   expr = cp_parser_id_expression (parser,
10288                                   /*template_keyword_p=*/false,
10289                                   /*check_dependency_p=*/true,
10290                                   /*template_p=*/NULL,
10291                                   /*declarator_p=*/false,
10292                                   /*optional_p=*/false);
10293
10294   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10295     {
10296       bool non_integral_constant_expression_p = false;
10297       tree id_expression = expr;
10298       cp_id_kind idk;
10299       const char *error_msg;
10300
10301       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10302         /* Lookup the name we got back from the id-expression.  */
10303         expr = cp_parser_lookup_name (parser, expr,
10304                                       none_type,
10305                                       /*is_template=*/false,
10306                                       /*is_namespace=*/false,
10307                                       /*check_dependency=*/true,
10308                                       /*ambiguous_decls=*/NULL,
10309                                       id_expr_start_token->location);
10310
10311       if (expr
10312           && expr != error_mark_node
10313           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10314           && TREE_CODE (expr) != TYPE_DECL
10315           && (TREE_CODE (expr) != BIT_NOT_EXPR
10316               || !TYPE_P (TREE_OPERAND (expr, 0)))
10317           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10318         {
10319           /* Complete lookup of the id-expression.  */
10320           expr = (finish_id_expression
10321                   (id_expression, expr, parser->scope, &idk,
10322                    /*integral_constant_expression_p=*/false,
10323                    /*allow_non_integral_constant_expression_p=*/true,
10324                    &non_integral_constant_expression_p,
10325                    /*template_p=*/false,
10326                    /*done=*/true,
10327                    /*address_p=*/false,
10328                    /*template_arg_p=*/false,
10329                    &error_msg,
10330                    id_expr_start_token->location));
10331
10332           if (expr == error_mark_node)
10333             /* We found an id-expression, but it was something that we
10334                should not have found. This is an error, not something
10335                we can recover from, so note that we found an
10336                id-expression and we'll recover as gracefully as
10337                possible.  */
10338             id_expression_or_member_access_p = true;
10339         }
10340
10341       if (expr 
10342           && expr != error_mark_node
10343           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10344         /* We have an id-expression.  */
10345         id_expression_or_member_access_p = true;
10346     }
10347
10348   if (!id_expression_or_member_access_p)
10349     {
10350       /* Abort the id-expression parse.  */
10351       cp_parser_abort_tentative_parse (parser);
10352
10353       /* Parsing tentatively, again.  */
10354       cp_parser_parse_tentatively (parser);
10355
10356       /* Parse a class member access.  */
10357       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10358                                            /*cast_p=*/false,
10359                                            /*member_access_only_p=*/true, NULL);
10360
10361       if (expr 
10362           && expr != error_mark_node
10363           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10364         /* We have an id-expression.  */
10365         id_expression_or_member_access_p = true;
10366     }
10367
10368   if (id_expression_or_member_access_p)
10369     /* We have parsed the complete id-expression or member access.  */
10370     cp_parser_parse_definitely (parser);
10371   else
10372     {
10373       bool saved_greater_than_is_operator_p;
10374
10375       /* Abort our attempt to parse an id-expression or member access
10376          expression.  */
10377       cp_parser_abort_tentative_parse (parser);
10378
10379       /* Within a parenthesized expression, a `>' token is always
10380          the greater-than operator.  */
10381       saved_greater_than_is_operator_p
10382         = parser->greater_than_is_operator_p;
10383       parser->greater_than_is_operator_p = true;
10384
10385       /* Parse a full expression.  */
10386       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10387
10388       /* The `>' token might be the end of a template-id or
10389          template-parameter-list now.  */
10390       parser->greater_than_is_operator_p
10391         = saved_greater_than_is_operator_p;
10392     }
10393
10394   /* Go back to evaluating expressions.  */
10395   --cp_unevaluated_operand;
10396   --c_inhibit_evaluation_warnings;
10397
10398   /* Restore the old message and the integral constant expression
10399      flags.  */
10400   parser->type_definition_forbidden_message = saved_message;
10401   parser->integral_constant_expression_p
10402     = saved_integral_constant_expression_p;
10403   parser->non_integral_constant_expression_p
10404     = saved_non_integral_constant_expression_p;
10405
10406   if (expr == error_mark_node)
10407     {
10408       /* Skip everything up to the closing `)'.  */
10409       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10410                                              /*consume_paren=*/true);
10411       return error_mark_node;
10412     }
10413   
10414   /* Parse to the closing `)'.  */
10415   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10416     {
10417       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10418                                              /*consume_paren=*/true);
10419       return error_mark_node;
10420     }
10421
10422   return finish_decltype_type (expr, id_expression_or_member_access_p);
10423 }
10424
10425 /* Special member functions [gram.special] */
10426
10427 /* Parse a conversion-function-id.
10428
10429    conversion-function-id:
10430      operator conversion-type-id
10431
10432    Returns an IDENTIFIER_NODE representing the operator.  */
10433
10434 static tree
10435 cp_parser_conversion_function_id (cp_parser* parser)
10436 {
10437   tree type;
10438   tree saved_scope;
10439   tree saved_qualifying_scope;
10440   tree saved_object_scope;
10441   tree pushed_scope = NULL_TREE;
10442
10443   /* Look for the `operator' token.  */
10444   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10445     return error_mark_node;
10446   /* When we parse the conversion-type-id, the current scope will be
10447      reset.  However, we need that information in able to look up the
10448      conversion function later, so we save it here.  */
10449   saved_scope = parser->scope;
10450   saved_qualifying_scope = parser->qualifying_scope;
10451   saved_object_scope = parser->object_scope;
10452   /* We must enter the scope of the class so that the names of
10453      entities declared within the class are available in the
10454      conversion-type-id.  For example, consider:
10455
10456        struct S {
10457          typedef int I;
10458          operator I();
10459        };
10460
10461        S::operator I() { ... }
10462
10463      In order to see that `I' is a type-name in the definition, we
10464      must be in the scope of `S'.  */
10465   if (saved_scope)
10466     pushed_scope = push_scope (saved_scope);
10467   /* Parse the conversion-type-id.  */
10468   type = cp_parser_conversion_type_id (parser);
10469   /* Leave the scope of the class, if any.  */
10470   if (pushed_scope)
10471     pop_scope (pushed_scope);
10472   /* Restore the saved scope.  */
10473   parser->scope = saved_scope;
10474   parser->qualifying_scope = saved_qualifying_scope;
10475   parser->object_scope = saved_object_scope;
10476   /* If the TYPE is invalid, indicate failure.  */
10477   if (type == error_mark_node)
10478     return error_mark_node;
10479   return mangle_conv_op_name_for_type (type);
10480 }
10481
10482 /* Parse a conversion-type-id:
10483
10484    conversion-type-id:
10485      type-specifier-seq conversion-declarator [opt]
10486
10487    Returns the TYPE specified.  */
10488
10489 static tree
10490 cp_parser_conversion_type_id (cp_parser* parser)
10491 {
10492   tree attributes;
10493   cp_decl_specifier_seq type_specifiers;
10494   cp_declarator *declarator;
10495   tree type_specified;
10496
10497   /* Parse the attributes.  */
10498   attributes = cp_parser_attributes_opt (parser);
10499   /* Parse the type-specifiers.  */
10500   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10501                                 /*is_trailing_return=*/false,
10502                                 &type_specifiers);
10503   /* If that didn't work, stop.  */
10504   if (type_specifiers.type == error_mark_node)
10505     return error_mark_node;
10506   /* Parse the conversion-declarator.  */
10507   declarator = cp_parser_conversion_declarator_opt (parser);
10508
10509   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10510                                     /*initialized=*/0, &attributes);
10511   if (attributes)
10512     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10513
10514   /* Don't give this error when parsing tentatively.  This happens to
10515      work because we always parse this definitively once.  */
10516   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10517       && type_uses_auto (type_specified))
10518     {
10519       error ("invalid use of %<auto%> in conversion operator");
10520       return error_mark_node;
10521     }
10522
10523   return type_specified;
10524 }
10525
10526 /* Parse an (optional) conversion-declarator.
10527
10528    conversion-declarator:
10529      ptr-operator conversion-declarator [opt]
10530
10531    */
10532
10533 static cp_declarator *
10534 cp_parser_conversion_declarator_opt (cp_parser* parser)
10535 {
10536   enum tree_code code;
10537   tree class_type;
10538   cp_cv_quals cv_quals;
10539
10540   /* We don't know if there's a ptr-operator next, or not.  */
10541   cp_parser_parse_tentatively (parser);
10542   /* Try the ptr-operator.  */
10543   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10544   /* If it worked, look for more conversion-declarators.  */
10545   if (cp_parser_parse_definitely (parser))
10546     {
10547       cp_declarator *declarator;
10548
10549       /* Parse another optional declarator.  */
10550       declarator = cp_parser_conversion_declarator_opt (parser);
10551
10552       return cp_parser_make_indirect_declarator
10553         (code, class_type, cv_quals, declarator);
10554    }
10555
10556   return NULL;
10557 }
10558
10559 /* Parse an (optional) ctor-initializer.
10560
10561    ctor-initializer:
10562      : mem-initializer-list
10563
10564    Returns TRUE iff the ctor-initializer was actually present.  */
10565
10566 static bool
10567 cp_parser_ctor_initializer_opt (cp_parser* parser)
10568 {
10569   /* If the next token is not a `:', then there is no
10570      ctor-initializer.  */
10571   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10572     {
10573       /* Do default initialization of any bases and members.  */
10574       if (DECL_CONSTRUCTOR_P (current_function_decl))
10575         finish_mem_initializers (NULL_TREE);
10576
10577       return false;
10578     }
10579
10580   /* Consume the `:' token.  */
10581   cp_lexer_consume_token (parser->lexer);
10582   /* And the mem-initializer-list.  */
10583   cp_parser_mem_initializer_list (parser);
10584
10585   return true;
10586 }
10587
10588 /* Parse a mem-initializer-list.
10589
10590    mem-initializer-list:
10591      mem-initializer ... [opt]
10592      mem-initializer ... [opt] , mem-initializer-list  */
10593
10594 static void
10595 cp_parser_mem_initializer_list (cp_parser* parser)
10596 {
10597   tree mem_initializer_list = NULL_TREE;
10598   cp_token *token = cp_lexer_peek_token (parser->lexer);
10599
10600   /* Let the semantic analysis code know that we are starting the
10601      mem-initializer-list.  */
10602   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10603     error_at (token->location,
10604               "only constructors take member initializers");
10605
10606   /* Loop through the list.  */
10607   while (true)
10608     {
10609       tree mem_initializer;
10610
10611       token = cp_lexer_peek_token (parser->lexer);
10612       /* Parse the mem-initializer.  */
10613       mem_initializer = cp_parser_mem_initializer (parser);
10614       /* If the next token is a `...', we're expanding member initializers. */
10615       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10616         {
10617           /* Consume the `...'. */
10618           cp_lexer_consume_token (parser->lexer);
10619
10620           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10621              can be expanded but members cannot. */
10622           if (mem_initializer != error_mark_node
10623               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10624             {
10625               error_at (token->location,
10626                         "cannot expand initializer for member %<%D%>",
10627                         TREE_PURPOSE (mem_initializer));
10628               mem_initializer = error_mark_node;
10629             }
10630
10631           /* Construct the pack expansion type. */
10632           if (mem_initializer != error_mark_node)
10633             mem_initializer = make_pack_expansion (mem_initializer);
10634         }
10635       /* Add it to the list, unless it was erroneous.  */
10636       if (mem_initializer != error_mark_node)
10637         {
10638           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10639           mem_initializer_list = mem_initializer;
10640         }
10641       /* If the next token is not a `,', we're done.  */
10642       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10643         break;
10644       /* Consume the `,' token.  */
10645       cp_lexer_consume_token (parser->lexer);
10646     }
10647
10648   /* Perform semantic analysis.  */
10649   if (DECL_CONSTRUCTOR_P (current_function_decl))
10650     finish_mem_initializers (mem_initializer_list);
10651 }
10652
10653 /* Parse a mem-initializer.
10654
10655    mem-initializer:
10656      mem-initializer-id ( expression-list [opt] )
10657      mem-initializer-id braced-init-list
10658
10659    GNU extension:
10660
10661    mem-initializer:
10662      ( expression-list [opt] )
10663
10664    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10665    class) or FIELD_DECL (for a non-static data member) to initialize;
10666    the TREE_VALUE is the expression-list.  An empty initialization
10667    list is represented by void_list_node.  */
10668
10669 static tree
10670 cp_parser_mem_initializer (cp_parser* parser)
10671 {
10672   tree mem_initializer_id;
10673   tree expression_list;
10674   tree member;
10675   cp_token *token = cp_lexer_peek_token (parser->lexer);
10676
10677   /* Find out what is being initialized.  */
10678   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10679     {
10680       permerror (token->location,
10681                  "anachronistic old-style base class initializer");
10682       mem_initializer_id = NULL_TREE;
10683     }
10684   else
10685     {
10686       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10687       if (mem_initializer_id == error_mark_node)
10688         return mem_initializer_id;
10689     }
10690   member = expand_member_init (mem_initializer_id);
10691   if (member && !DECL_P (member))
10692     in_base_initializer = 1;
10693
10694   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10695     {
10696       bool expr_non_constant_p;
10697       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10698       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10699       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10700       expression_list = build_tree_list (NULL_TREE, expression_list);
10701     }
10702   else
10703     {
10704       VEC(tree,gc)* vec;
10705       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10706                                                      /*cast_p=*/false,
10707                                                      /*allow_expansion_p=*/true,
10708                                                      /*non_constant_p=*/NULL);
10709       if (vec == NULL)
10710         return error_mark_node;
10711       expression_list = build_tree_list_vec (vec);
10712       release_tree_vector (vec);
10713     }
10714
10715   if (expression_list == error_mark_node)
10716     return error_mark_node;
10717   if (!expression_list)
10718     expression_list = void_type_node;
10719
10720   in_base_initializer = 0;
10721
10722   return member ? build_tree_list (member, expression_list) : error_mark_node;
10723 }
10724
10725 /* Parse a mem-initializer-id.
10726
10727    mem-initializer-id:
10728      :: [opt] nested-name-specifier [opt] class-name
10729      identifier
10730
10731    Returns a TYPE indicating the class to be initializer for the first
10732    production.  Returns an IDENTIFIER_NODE indicating the data member
10733    to be initialized for the second production.  */
10734
10735 static tree
10736 cp_parser_mem_initializer_id (cp_parser* parser)
10737 {
10738   bool global_scope_p;
10739   bool nested_name_specifier_p;
10740   bool template_p = false;
10741   tree id;
10742
10743   cp_token *token = cp_lexer_peek_token (parser->lexer);
10744
10745   /* `typename' is not allowed in this context ([temp.res]).  */
10746   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10747     {
10748       error_at (token->location, 
10749                 "keyword %<typename%> not allowed in this context (a qualified "
10750                 "member initializer is implicitly a type)");
10751       cp_lexer_consume_token (parser->lexer);
10752     }
10753   /* Look for the optional `::' operator.  */
10754   global_scope_p
10755     = (cp_parser_global_scope_opt (parser,
10756                                    /*current_scope_valid_p=*/false)
10757        != NULL_TREE);
10758   /* Look for the optional nested-name-specifier.  The simplest way to
10759      implement:
10760
10761        [temp.res]
10762
10763        The keyword `typename' is not permitted in a base-specifier or
10764        mem-initializer; in these contexts a qualified name that
10765        depends on a template-parameter is implicitly assumed to be a
10766        type name.
10767
10768      is to assume that we have seen the `typename' keyword at this
10769      point.  */
10770   nested_name_specifier_p
10771     = (cp_parser_nested_name_specifier_opt (parser,
10772                                             /*typename_keyword_p=*/true,
10773                                             /*check_dependency_p=*/true,
10774                                             /*type_p=*/true,
10775                                             /*is_declaration=*/true)
10776        != NULL_TREE);
10777   if (nested_name_specifier_p)
10778     template_p = cp_parser_optional_template_keyword (parser);
10779   /* If there is a `::' operator or a nested-name-specifier, then we
10780      are definitely looking for a class-name.  */
10781   if (global_scope_p || nested_name_specifier_p)
10782     return cp_parser_class_name (parser,
10783                                  /*typename_keyword_p=*/true,
10784                                  /*template_keyword_p=*/template_p,
10785                                  typename_type,
10786                                  /*check_dependency_p=*/true,
10787                                  /*class_head_p=*/false,
10788                                  /*is_declaration=*/true);
10789   /* Otherwise, we could also be looking for an ordinary identifier.  */
10790   cp_parser_parse_tentatively (parser);
10791   /* Try a class-name.  */
10792   id = cp_parser_class_name (parser,
10793                              /*typename_keyword_p=*/true,
10794                              /*template_keyword_p=*/false,
10795                              none_type,
10796                              /*check_dependency_p=*/true,
10797                              /*class_head_p=*/false,
10798                              /*is_declaration=*/true);
10799   /* If we found one, we're done.  */
10800   if (cp_parser_parse_definitely (parser))
10801     return id;
10802   /* Otherwise, look for an ordinary identifier.  */
10803   return cp_parser_identifier (parser);
10804 }
10805
10806 /* Overloading [gram.over] */
10807
10808 /* Parse an operator-function-id.
10809
10810    operator-function-id:
10811      operator operator
10812
10813    Returns an IDENTIFIER_NODE for the operator which is a
10814    human-readable spelling of the identifier, e.g., `operator +'.  */
10815
10816 static tree
10817 cp_parser_operator_function_id (cp_parser* parser)
10818 {
10819   /* Look for the `operator' keyword.  */
10820   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10821     return error_mark_node;
10822   /* And then the name of the operator itself.  */
10823   return cp_parser_operator (parser);
10824 }
10825
10826 /* Parse an operator.
10827
10828    operator:
10829      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10830      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10831      || ++ -- , ->* -> () []
10832
10833    GNU Extensions:
10834
10835    operator:
10836      <? >? <?= >?=
10837
10838    Returns an IDENTIFIER_NODE for the operator which is a
10839    human-readable spelling of the identifier, e.g., `operator +'.  */
10840
10841 static tree
10842 cp_parser_operator (cp_parser* parser)
10843 {
10844   tree id = NULL_TREE;
10845   cp_token *token;
10846
10847   /* Peek at the next token.  */
10848   token = cp_lexer_peek_token (parser->lexer);
10849   /* Figure out which operator we have.  */
10850   switch (token->type)
10851     {
10852     case CPP_KEYWORD:
10853       {
10854         enum tree_code op;
10855
10856         /* The keyword should be either `new' or `delete'.  */
10857         if (token->keyword == RID_NEW)
10858           op = NEW_EXPR;
10859         else if (token->keyword == RID_DELETE)
10860           op = DELETE_EXPR;
10861         else
10862           break;
10863
10864         /* Consume the `new' or `delete' token.  */
10865         cp_lexer_consume_token (parser->lexer);
10866
10867         /* Peek at the next token.  */
10868         token = cp_lexer_peek_token (parser->lexer);
10869         /* If it's a `[' token then this is the array variant of the
10870            operator.  */
10871         if (token->type == CPP_OPEN_SQUARE)
10872           {
10873             /* Consume the `[' token.  */
10874             cp_lexer_consume_token (parser->lexer);
10875             /* Look for the `]' token.  */
10876             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10877             id = ansi_opname (op == NEW_EXPR
10878                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10879           }
10880         /* Otherwise, we have the non-array variant.  */
10881         else
10882           id = ansi_opname (op);
10883
10884         return id;
10885       }
10886
10887     case CPP_PLUS:
10888       id = ansi_opname (PLUS_EXPR);
10889       break;
10890
10891     case CPP_MINUS:
10892       id = ansi_opname (MINUS_EXPR);
10893       break;
10894
10895     case CPP_MULT:
10896       id = ansi_opname (MULT_EXPR);
10897       break;
10898
10899     case CPP_DIV:
10900       id = ansi_opname (TRUNC_DIV_EXPR);
10901       break;
10902
10903     case CPP_MOD:
10904       id = ansi_opname (TRUNC_MOD_EXPR);
10905       break;
10906
10907     case CPP_XOR:
10908       id = ansi_opname (BIT_XOR_EXPR);
10909       break;
10910
10911     case CPP_AND:
10912       id = ansi_opname (BIT_AND_EXPR);
10913       break;
10914
10915     case CPP_OR:
10916       id = ansi_opname (BIT_IOR_EXPR);
10917       break;
10918
10919     case CPP_COMPL:
10920       id = ansi_opname (BIT_NOT_EXPR);
10921       break;
10922
10923     case CPP_NOT:
10924       id = ansi_opname (TRUTH_NOT_EXPR);
10925       break;
10926
10927     case CPP_EQ:
10928       id = ansi_assopname (NOP_EXPR);
10929       break;
10930
10931     case CPP_LESS:
10932       id = ansi_opname (LT_EXPR);
10933       break;
10934
10935     case CPP_GREATER:
10936       id = ansi_opname (GT_EXPR);
10937       break;
10938
10939     case CPP_PLUS_EQ:
10940       id = ansi_assopname (PLUS_EXPR);
10941       break;
10942
10943     case CPP_MINUS_EQ:
10944       id = ansi_assopname (MINUS_EXPR);
10945       break;
10946
10947     case CPP_MULT_EQ:
10948       id = ansi_assopname (MULT_EXPR);
10949       break;
10950
10951     case CPP_DIV_EQ:
10952       id = ansi_assopname (TRUNC_DIV_EXPR);
10953       break;
10954
10955     case CPP_MOD_EQ:
10956       id = ansi_assopname (TRUNC_MOD_EXPR);
10957       break;
10958
10959     case CPP_XOR_EQ:
10960       id = ansi_assopname (BIT_XOR_EXPR);
10961       break;
10962
10963     case CPP_AND_EQ:
10964       id = ansi_assopname (BIT_AND_EXPR);
10965       break;
10966
10967     case CPP_OR_EQ:
10968       id = ansi_assopname (BIT_IOR_EXPR);
10969       break;
10970
10971     case CPP_LSHIFT:
10972       id = ansi_opname (LSHIFT_EXPR);
10973       break;
10974
10975     case CPP_RSHIFT:
10976       id = ansi_opname (RSHIFT_EXPR);
10977       break;
10978
10979     case CPP_LSHIFT_EQ:
10980       id = ansi_assopname (LSHIFT_EXPR);
10981       break;
10982
10983     case CPP_RSHIFT_EQ:
10984       id = ansi_assopname (RSHIFT_EXPR);
10985       break;
10986
10987     case CPP_EQ_EQ:
10988       id = ansi_opname (EQ_EXPR);
10989       break;
10990
10991     case CPP_NOT_EQ:
10992       id = ansi_opname (NE_EXPR);
10993       break;
10994
10995     case CPP_LESS_EQ:
10996       id = ansi_opname (LE_EXPR);
10997       break;
10998
10999     case CPP_GREATER_EQ:
11000       id = ansi_opname (GE_EXPR);
11001       break;
11002
11003     case CPP_AND_AND:
11004       id = ansi_opname (TRUTH_ANDIF_EXPR);
11005       break;
11006
11007     case CPP_OR_OR:
11008       id = ansi_opname (TRUTH_ORIF_EXPR);
11009       break;
11010
11011     case CPP_PLUS_PLUS:
11012       id = ansi_opname (POSTINCREMENT_EXPR);
11013       break;
11014
11015     case CPP_MINUS_MINUS:
11016       id = ansi_opname (PREDECREMENT_EXPR);
11017       break;
11018
11019     case CPP_COMMA:
11020       id = ansi_opname (COMPOUND_EXPR);
11021       break;
11022
11023     case CPP_DEREF_STAR:
11024       id = ansi_opname (MEMBER_REF);
11025       break;
11026
11027     case CPP_DEREF:
11028       id = ansi_opname (COMPONENT_REF);
11029       break;
11030
11031     case CPP_OPEN_PAREN:
11032       /* Consume the `('.  */
11033       cp_lexer_consume_token (parser->lexer);
11034       /* Look for the matching `)'.  */
11035       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11036       return ansi_opname (CALL_EXPR);
11037
11038     case CPP_OPEN_SQUARE:
11039       /* Consume the `['.  */
11040       cp_lexer_consume_token (parser->lexer);
11041       /* Look for the matching `]'.  */
11042       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11043       return ansi_opname (ARRAY_REF);
11044
11045     default:
11046       /* Anything else is an error.  */
11047       break;
11048     }
11049
11050   /* If we have selected an identifier, we need to consume the
11051      operator token.  */
11052   if (id)
11053     cp_lexer_consume_token (parser->lexer);
11054   /* Otherwise, no valid operator name was present.  */
11055   else
11056     {
11057       cp_parser_error (parser, "expected operator");
11058       id = error_mark_node;
11059     }
11060
11061   return id;
11062 }
11063
11064 /* Parse a template-declaration.
11065
11066    template-declaration:
11067      export [opt] template < template-parameter-list > declaration
11068
11069    If MEMBER_P is TRUE, this template-declaration occurs within a
11070    class-specifier.
11071
11072    The grammar rule given by the standard isn't correct.  What
11073    is really meant is:
11074
11075    template-declaration:
11076      export [opt] template-parameter-list-seq
11077        decl-specifier-seq [opt] init-declarator [opt] ;
11078      export [opt] template-parameter-list-seq
11079        function-definition
11080
11081    template-parameter-list-seq:
11082      template-parameter-list-seq [opt]
11083      template < template-parameter-list >  */
11084
11085 static void
11086 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11087 {
11088   /* Check for `export'.  */
11089   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11090     {
11091       /* Consume the `export' token.  */
11092       cp_lexer_consume_token (parser->lexer);
11093       /* Warn that we do not support `export'.  */
11094       warning (0, "keyword %<export%> not implemented, and will be ignored");
11095     }
11096
11097   cp_parser_template_declaration_after_export (parser, member_p);
11098 }
11099
11100 /* Parse a template-parameter-list.
11101
11102    template-parameter-list:
11103      template-parameter
11104      template-parameter-list , template-parameter
11105
11106    Returns a TREE_LIST.  Each node represents a template parameter.
11107    The nodes are connected via their TREE_CHAINs.  */
11108
11109 static tree
11110 cp_parser_template_parameter_list (cp_parser* parser)
11111 {
11112   tree parameter_list = NULL_TREE;
11113
11114   begin_template_parm_list ();
11115
11116   /* The loop below parses the template parms.  We first need to know
11117      the total number of template parms to be able to compute proper
11118      canonical types of each dependent type. So after the loop, when
11119      we know the total number of template parms,
11120      end_template_parm_list computes the proper canonical types and
11121      fixes up the dependent types accordingly.  */
11122   while (true)
11123     {
11124       tree parameter;
11125       bool is_non_type;
11126       bool is_parameter_pack;
11127       location_t parm_loc;
11128
11129       /* Parse the template-parameter.  */
11130       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11131       parameter = cp_parser_template_parameter (parser, 
11132                                                 &is_non_type,
11133                                                 &is_parameter_pack);
11134       /* Add it to the list.  */
11135       if (parameter != error_mark_node)
11136         parameter_list = process_template_parm (parameter_list,
11137                                                 parm_loc,
11138                                                 parameter,
11139                                                 is_non_type,
11140                                                 is_parameter_pack,
11141                                                 0);
11142       else
11143        {
11144          tree err_parm = build_tree_list (parameter, parameter);
11145          parameter_list = chainon (parameter_list, err_parm);
11146        }
11147
11148       /* If the next token is not a `,', we're done.  */
11149       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11150         break;
11151       /* Otherwise, consume the `,' token.  */
11152       cp_lexer_consume_token (parser->lexer);
11153     }
11154
11155   return end_template_parm_list (parameter_list);
11156 }
11157
11158 /* Parse a template-parameter.
11159
11160    template-parameter:
11161      type-parameter
11162      parameter-declaration
11163
11164    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11165    the parameter.  The TREE_PURPOSE is the default value, if any.
11166    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11167    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11168    set to true iff this parameter is a parameter pack. */
11169
11170 static tree
11171 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11172                               bool *is_parameter_pack)
11173 {
11174   cp_token *token;
11175   cp_parameter_declarator *parameter_declarator;
11176   cp_declarator *id_declarator;
11177   tree parm;
11178
11179   /* Assume it is a type parameter or a template parameter.  */
11180   *is_non_type = false;
11181   /* Assume it not a parameter pack. */
11182   *is_parameter_pack = false;
11183   /* Peek at the next token.  */
11184   token = cp_lexer_peek_token (parser->lexer);
11185   /* If it is `class' or `template', we have a type-parameter.  */
11186   if (token->keyword == RID_TEMPLATE)
11187     return cp_parser_type_parameter (parser, is_parameter_pack);
11188   /* If it is `class' or `typename' we do not know yet whether it is a
11189      type parameter or a non-type parameter.  Consider:
11190
11191        template <typename T, typename T::X X> ...
11192
11193      or:
11194
11195        template <class C, class D*> ...
11196
11197      Here, the first parameter is a type parameter, and the second is
11198      a non-type parameter.  We can tell by looking at the token after
11199      the identifier -- if it is a `,', `=', or `>' then we have a type
11200      parameter.  */
11201   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11202     {
11203       /* Peek at the token after `class' or `typename'.  */
11204       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11205       /* If it's an ellipsis, we have a template type parameter
11206          pack. */
11207       if (token->type == CPP_ELLIPSIS)
11208         return cp_parser_type_parameter (parser, is_parameter_pack);
11209       /* If it's an identifier, skip it.  */
11210       if (token->type == CPP_NAME)
11211         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11212       /* Now, see if the token looks like the end of a template
11213          parameter.  */
11214       if (token->type == CPP_COMMA
11215           || token->type == CPP_EQ
11216           || token->type == CPP_GREATER)
11217         return cp_parser_type_parameter (parser, is_parameter_pack);
11218     }
11219
11220   /* Otherwise, it is a non-type parameter.
11221
11222      [temp.param]
11223
11224      When parsing a default template-argument for a non-type
11225      template-parameter, the first non-nested `>' is taken as the end
11226      of the template parameter-list rather than a greater-than
11227      operator.  */
11228   *is_non_type = true;
11229   parameter_declarator
11230      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11231                                         /*parenthesized_p=*/NULL);
11232
11233   /* If the parameter declaration is marked as a parameter pack, set
11234      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11235      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11236      grokdeclarator. */
11237   if (parameter_declarator
11238       && parameter_declarator->declarator
11239       && parameter_declarator->declarator->parameter_pack_p)
11240     {
11241       *is_parameter_pack = true;
11242       parameter_declarator->declarator->parameter_pack_p = false;
11243     }
11244
11245   /* If the next token is an ellipsis, and we don't already have it
11246      marked as a parameter pack, then we have a parameter pack (that
11247      has no declarator).  */
11248   if (!*is_parameter_pack
11249       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11250       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11251     {
11252       /* Consume the `...'.  */
11253       cp_lexer_consume_token (parser->lexer);
11254       maybe_warn_variadic_templates ();
11255       
11256       *is_parameter_pack = true;
11257     }
11258   /* We might end up with a pack expansion as the type of the non-type
11259      template parameter, in which case this is a non-type template
11260      parameter pack.  */
11261   else if (parameter_declarator
11262            && parameter_declarator->decl_specifiers.type
11263            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11264     {
11265       *is_parameter_pack = true;
11266       parameter_declarator->decl_specifiers.type = 
11267         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11268     }
11269
11270   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11271     {
11272       /* Parameter packs cannot have default arguments.  However, a
11273          user may try to do so, so we'll parse them and give an
11274          appropriate diagnostic here.  */
11275
11276       /* Consume the `='.  */
11277       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11278       cp_lexer_consume_token (parser->lexer);
11279       
11280       /* Find the name of the parameter pack.  */     
11281       id_declarator = parameter_declarator->declarator;
11282       while (id_declarator && id_declarator->kind != cdk_id)
11283         id_declarator = id_declarator->declarator;
11284       
11285       if (id_declarator && id_declarator->kind == cdk_id)
11286         error_at (start_token->location,
11287                   "template parameter pack %qD cannot have a default argument",
11288                   id_declarator->u.id.unqualified_name);
11289       else
11290         error_at (start_token->location,
11291                   "template parameter pack cannot have a default argument");
11292       
11293       /* Parse the default argument, but throw away the result.  */
11294       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11295     }
11296
11297   parm = grokdeclarator (parameter_declarator->declarator,
11298                          &parameter_declarator->decl_specifiers,
11299                          TPARM, /*initialized=*/0,
11300                          /*attrlist=*/NULL);
11301   if (parm == error_mark_node)
11302     return error_mark_node;
11303
11304   return build_tree_list (parameter_declarator->default_argument, parm);
11305 }
11306
11307 /* Parse a type-parameter.
11308
11309    type-parameter:
11310      class identifier [opt]
11311      class identifier [opt] = type-id
11312      typename identifier [opt]
11313      typename identifier [opt] = type-id
11314      template < template-parameter-list > class identifier [opt]
11315      template < template-parameter-list > class identifier [opt]
11316        = id-expression
11317
11318    GNU Extension (variadic templates):
11319
11320    type-parameter:
11321      class ... identifier [opt]
11322      typename ... identifier [opt]
11323
11324    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11325    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11326    the declaration of the parameter.
11327
11328    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11329
11330 static tree
11331 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11332 {
11333   cp_token *token;
11334   tree parameter;
11335
11336   /* Look for a keyword to tell us what kind of parameter this is.  */
11337   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11338   if (!token)
11339     return error_mark_node;
11340
11341   switch (token->keyword)
11342     {
11343     case RID_CLASS:
11344     case RID_TYPENAME:
11345       {
11346         tree identifier;
11347         tree default_argument;
11348
11349         /* If the next token is an ellipsis, we have a template
11350            argument pack. */
11351         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11352           {
11353             /* Consume the `...' token. */
11354             cp_lexer_consume_token (parser->lexer);
11355             maybe_warn_variadic_templates ();
11356
11357             *is_parameter_pack = true;
11358           }
11359
11360         /* If the next token is an identifier, then it names the
11361            parameter.  */
11362         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11363           identifier = cp_parser_identifier (parser);
11364         else
11365           identifier = NULL_TREE;
11366
11367         /* Create the parameter.  */
11368         parameter = finish_template_type_parm (class_type_node, identifier);
11369
11370         /* If the next token is an `=', we have a default argument.  */
11371         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11372           {
11373             /* Consume the `=' token.  */
11374             cp_lexer_consume_token (parser->lexer);
11375             /* Parse the default-argument.  */
11376             push_deferring_access_checks (dk_no_deferred);
11377             default_argument = cp_parser_type_id (parser);
11378
11379             /* Template parameter packs cannot have default
11380                arguments. */
11381             if (*is_parameter_pack)
11382               {
11383                 if (identifier)
11384                   error_at (token->location,
11385                             "template parameter pack %qD cannot have a "
11386                             "default argument", identifier);
11387                 else
11388                   error_at (token->location,
11389                             "template parameter packs cannot have "
11390                             "default arguments");
11391                 default_argument = NULL_TREE;
11392               }
11393             pop_deferring_access_checks ();
11394           }
11395         else
11396           default_argument = NULL_TREE;
11397
11398         /* Create the combined representation of the parameter and the
11399            default argument.  */
11400         parameter = build_tree_list (default_argument, parameter);
11401       }
11402       break;
11403
11404     case RID_TEMPLATE:
11405       {
11406         tree identifier;
11407         tree default_argument;
11408
11409         /* Look for the `<'.  */
11410         cp_parser_require (parser, CPP_LESS, RT_LESS);
11411         /* Parse the template-parameter-list.  */
11412         cp_parser_template_parameter_list (parser);
11413         /* Look for the `>'.  */
11414         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11415         /* Look for the `class' keyword.  */
11416         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11417         /* If the next token is an ellipsis, we have a template
11418            argument pack. */
11419         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11420           {
11421             /* Consume the `...' token. */
11422             cp_lexer_consume_token (parser->lexer);
11423             maybe_warn_variadic_templates ();
11424
11425             *is_parameter_pack = true;
11426           }
11427         /* If the next token is an `=', then there is a
11428            default-argument.  If the next token is a `>', we are at
11429            the end of the parameter-list.  If the next token is a `,',
11430            then we are at the end of this parameter.  */
11431         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11432             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11433             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11434           {
11435             identifier = cp_parser_identifier (parser);
11436             /* Treat invalid names as if the parameter were nameless.  */
11437             if (identifier == error_mark_node)
11438               identifier = NULL_TREE;
11439           }
11440         else
11441           identifier = NULL_TREE;
11442
11443         /* Create the template parameter.  */
11444         parameter = finish_template_template_parm (class_type_node,
11445                                                    identifier);
11446
11447         /* If the next token is an `=', then there is a
11448            default-argument.  */
11449         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11450           {
11451             bool is_template;
11452
11453             /* Consume the `='.  */
11454             cp_lexer_consume_token (parser->lexer);
11455             /* Parse the id-expression.  */
11456             push_deferring_access_checks (dk_no_deferred);
11457             /* save token before parsing the id-expression, for error
11458                reporting */
11459             token = cp_lexer_peek_token (parser->lexer);
11460             default_argument
11461               = cp_parser_id_expression (parser,
11462                                          /*template_keyword_p=*/false,
11463                                          /*check_dependency_p=*/true,
11464                                          /*template_p=*/&is_template,
11465                                          /*declarator_p=*/false,
11466                                          /*optional_p=*/false);
11467             if (TREE_CODE (default_argument) == TYPE_DECL)
11468               /* If the id-expression was a template-id that refers to
11469                  a template-class, we already have the declaration here,
11470                  so no further lookup is needed.  */
11471                  ;
11472             else
11473               /* Look up the name.  */
11474               default_argument
11475                 = cp_parser_lookup_name (parser, default_argument,
11476                                          none_type,
11477                                          /*is_template=*/is_template,
11478                                          /*is_namespace=*/false,
11479                                          /*check_dependency=*/true,
11480                                          /*ambiguous_decls=*/NULL,
11481                                          token->location);
11482             /* See if the default argument is valid.  */
11483             default_argument
11484               = check_template_template_default_arg (default_argument);
11485
11486             /* Template parameter packs cannot have default
11487                arguments. */
11488             if (*is_parameter_pack)
11489               {
11490                 if (identifier)
11491                   error_at (token->location,
11492                             "template parameter pack %qD cannot "
11493                             "have a default argument",
11494                             identifier);
11495                 else
11496                   error_at (token->location, "template parameter packs cannot "
11497                             "have default arguments");
11498                 default_argument = NULL_TREE;
11499               }
11500             pop_deferring_access_checks ();
11501           }
11502         else
11503           default_argument = NULL_TREE;
11504
11505         /* Create the combined representation of the parameter and the
11506            default argument.  */
11507         parameter = build_tree_list (default_argument, parameter);
11508       }
11509       break;
11510
11511     default:
11512       gcc_unreachable ();
11513       break;
11514     }
11515
11516   return parameter;
11517 }
11518
11519 /* Parse a template-id.
11520
11521    template-id:
11522      template-name < template-argument-list [opt] >
11523
11524    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11525    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11526    returned.  Otherwise, if the template-name names a function, or set
11527    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11528    names a class, returns a TYPE_DECL for the specialization.
11529
11530    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11531    uninstantiated templates.  */
11532
11533 static tree
11534 cp_parser_template_id (cp_parser *parser,
11535                        bool template_keyword_p,
11536                        bool check_dependency_p,
11537                        bool is_declaration)
11538 {
11539   int i;
11540   tree templ;
11541   tree arguments;
11542   tree template_id;
11543   cp_token_position start_of_id = 0;
11544   deferred_access_check *chk;
11545   VEC (deferred_access_check,gc) *access_check;
11546   cp_token *next_token = NULL, *next_token_2 = NULL;
11547   bool is_identifier;
11548
11549   /* If the next token corresponds to a template-id, there is no need
11550      to reparse it.  */
11551   next_token = cp_lexer_peek_token (parser->lexer);
11552   if (next_token->type == CPP_TEMPLATE_ID)
11553     {
11554       struct tree_check *check_value;
11555
11556       /* Get the stored value.  */
11557       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11558       /* Perform any access checks that were deferred.  */
11559       access_check = check_value->checks;
11560       if (access_check)
11561         {
11562           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11563             perform_or_defer_access_check (chk->binfo,
11564                                            chk->decl,
11565                                            chk->diag_decl);
11566         }
11567       /* Return the stored value.  */
11568       return check_value->value;
11569     }
11570
11571   /* Avoid performing name lookup if there is no possibility of
11572      finding a template-id.  */
11573   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11574       || (next_token->type == CPP_NAME
11575           && !cp_parser_nth_token_starts_template_argument_list_p
11576                (parser, 2)))
11577     {
11578       cp_parser_error (parser, "expected template-id");
11579       return error_mark_node;
11580     }
11581
11582   /* Remember where the template-id starts.  */
11583   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11584     start_of_id = cp_lexer_token_position (parser->lexer, false);
11585
11586   push_deferring_access_checks (dk_deferred);
11587
11588   /* Parse the template-name.  */
11589   is_identifier = false;
11590   templ = cp_parser_template_name (parser, template_keyword_p,
11591                                    check_dependency_p,
11592                                    is_declaration,
11593                                    &is_identifier);
11594   if (templ == error_mark_node || is_identifier)
11595     {
11596       pop_deferring_access_checks ();
11597       return templ;
11598     }
11599
11600   /* If we find the sequence `[:' after a template-name, it's probably
11601      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11602      parse correctly the argument list.  */
11603   next_token = cp_lexer_peek_token (parser->lexer);
11604   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11605   if (next_token->type == CPP_OPEN_SQUARE
11606       && next_token->flags & DIGRAPH
11607       && next_token_2->type == CPP_COLON
11608       && !(next_token_2->flags & PREV_WHITE))
11609     {
11610       cp_parser_parse_tentatively (parser);
11611       /* Change `:' into `::'.  */
11612       next_token_2->type = CPP_SCOPE;
11613       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11614          CPP_LESS.  */
11615       cp_lexer_consume_token (parser->lexer);
11616
11617       /* Parse the arguments.  */
11618       arguments = cp_parser_enclosed_template_argument_list (parser);
11619       if (!cp_parser_parse_definitely (parser))
11620         {
11621           /* If we couldn't parse an argument list, then we revert our changes
11622              and return simply an error. Maybe this is not a template-id
11623              after all.  */
11624           next_token_2->type = CPP_COLON;
11625           cp_parser_error (parser, "expected %<<%>");
11626           pop_deferring_access_checks ();
11627           return error_mark_node;
11628         }
11629       /* Otherwise, emit an error about the invalid digraph, but continue
11630          parsing because we got our argument list.  */
11631       if (permerror (next_token->location,
11632                      "%<<::%> cannot begin a template-argument list"))
11633         {
11634           static bool hint = false;
11635           inform (next_token->location,
11636                   "%<<:%> is an alternate spelling for %<[%>."
11637                   " Insert whitespace between %<<%> and %<::%>");
11638           if (!hint && !flag_permissive)
11639             {
11640               inform (next_token->location, "(if you use %<-fpermissive%>"
11641                       " G++ will accept your code)");
11642               hint = true;
11643             }
11644         }
11645     }
11646   else
11647     {
11648       /* Look for the `<' that starts the template-argument-list.  */
11649       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11650         {
11651           pop_deferring_access_checks ();
11652           return error_mark_node;
11653         }
11654       /* Parse the arguments.  */
11655       arguments = cp_parser_enclosed_template_argument_list (parser);
11656     }
11657
11658   /* Build a representation of the specialization.  */
11659   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11660     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11661   else if (DECL_CLASS_TEMPLATE_P (templ)
11662            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11663     {
11664       bool entering_scope;
11665       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11666          template (rather than some instantiation thereof) only if
11667          is not nested within some other construct.  For example, in
11668          "template <typename T> void f(T) { A<T>::", A<T> is just an
11669          instantiation of A.  */
11670       entering_scope = (template_parm_scope_p ()
11671                         && cp_lexer_next_token_is (parser->lexer,
11672                                                    CPP_SCOPE));
11673       template_id
11674         = finish_template_type (templ, arguments, entering_scope);
11675     }
11676   else
11677     {
11678       /* If it's not a class-template or a template-template, it should be
11679          a function-template.  */
11680       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11681                    || TREE_CODE (templ) == OVERLOAD
11682                    || BASELINK_P (templ)));
11683
11684       template_id = lookup_template_function (templ, arguments);
11685     }
11686
11687   /* If parsing tentatively, replace the sequence of tokens that makes
11688      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11689      should we re-parse the token stream, we will not have to repeat
11690      the effort required to do the parse, nor will we issue duplicate
11691      error messages about problems during instantiation of the
11692      template.  */
11693   if (start_of_id)
11694     {
11695       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11696
11697       /* Reset the contents of the START_OF_ID token.  */
11698       token->type = CPP_TEMPLATE_ID;
11699       /* Retrieve any deferred checks.  Do not pop this access checks yet
11700          so the memory will not be reclaimed during token replacing below.  */
11701       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11702       token->u.tree_check_value->value = template_id;
11703       token->u.tree_check_value->checks = get_deferred_access_checks ();
11704       token->keyword = RID_MAX;
11705
11706       /* Purge all subsequent tokens.  */
11707       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11708
11709       /* ??? Can we actually assume that, if template_id ==
11710          error_mark_node, we will have issued a diagnostic to the
11711          user, as opposed to simply marking the tentative parse as
11712          failed?  */
11713       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11714         error_at (token->location, "parse error in template argument list");
11715     }
11716
11717   pop_deferring_access_checks ();
11718   return template_id;
11719 }
11720
11721 /* Parse a template-name.
11722
11723    template-name:
11724      identifier
11725
11726    The standard should actually say:
11727
11728    template-name:
11729      identifier
11730      operator-function-id
11731
11732    A defect report has been filed about this issue.
11733
11734    A conversion-function-id cannot be a template name because they cannot
11735    be part of a template-id. In fact, looking at this code:
11736
11737    a.operator K<int>()
11738
11739    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11740    It is impossible to call a templated conversion-function-id with an
11741    explicit argument list, since the only allowed template parameter is
11742    the type to which it is converting.
11743
11744    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11745    `template' keyword, in a construction like:
11746
11747      T::template f<3>()
11748
11749    In that case `f' is taken to be a template-name, even though there
11750    is no way of knowing for sure.
11751
11752    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11753    name refers to a set of overloaded functions, at least one of which
11754    is a template, or an IDENTIFIER_NODE with the name of the template,
11755    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11756    names are looked up inside uninstantiated templates.  */
11757
11758 static tree
11759 cp_parser_template_name (cp_parser* parser,
11760                          bool template_keyword_p,
11761                          bool check_dependency_p,
11762                          bool is_declaration,
11763                          bool *is_identifier)
11764 {
11765   tree identifier;
11766   tree decl;
11767   tree fns;
11768   cp_token *token = cp_lexer_peek_token (parser->lexer);
11769
11770   /* If the next token is `operator', then we have either an
11771      operator-function-id or a conversion-function-id.  */
11772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11773     {
11774       /* We don't know whether we're looking at an
11775          operator-function-id or a conversion-function-id.  */
11776       cp_parser_parse_tentatively (parser);
11777       /* Try an operator-function-id.  */
11778       identifier = cp_parser_operator_function_id (parser);
11779       /* If that didn't work, try a conversion-function-id.  */
11780       if (!cp_parser_parse_definitely (parser))
11781         {
11782           cp_parser_error (parser, "expected template-name");
11783           return error_mark_node;
11784         }
11785     }
11786   /* Look for the identifier.  */
11787   else
11788     identifier = cp_parser_identifier (parser);
11789
11790   /* If we didn't find an identifier, we don't have a template-id.  */
11791   if (identifier == error_mark_node)
11792     return error_mark_node;
11793
11794   /* If the name immediately followed the `template' keyword, then it
11795      is a template-name.  However, if the next token is not `<', then
11796      we do not treat it as a template-name, since it is not being used
11797      as part of a template-id.  This enables us to handle constructs
11798      like:
11799
11800        template <typename T> struct S { S(); };
11801        template <typename T> S<T>::S();
11802
11803      correctly.  We would treat `S' as a template -- if it were `S<T>'
11804      -- but we do not if there is no `<'.  */
11805
11806   if (processing_template_decl
11807       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11808     {
11809       /* In a declaration, in a dependent context, we pretend that the
11810          "template" keyword was present in order to improve error
11811          recovery.  For example, given:
11812
11813            template <typename T> void f(T::X<int>);
11814
11815          we want to treat "X<int>" as a template-id.  */
11816       if (is_declaration
11817           && !template_keyword_p
11818           && parser->scope && TYPE_P (parser->scope)
11819           && check_dependency_p
11820           && dependent_scope_p (parser->scope)
11821           /* Do not do this for dtors (or ctors), since they never
11822              need the template keyword before their name.  */
11823           && !constructor_name_p (identifier, parser->scope))
11824         {
11825           cp_token_position start = 0;
11826
11827           /* Explain what went wrong.  */
11828           error_at (token->location, "non-template %qD used as template",
11829                     identifier);
11830           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11831                   parser->scope, identifier);
11832           /* If parsing tentatively, find the location of the "<" token.  */
11833           if (cp_parser_simulate_error (parser))
11834             start = cp_lexer_token_position (parser->lexer, true);
11835           /* Parse the template arguments so that we can issue error
11836              messages about them.  */
11837           cp_lexer_consume_token (parser->lexer);
11838           cp_parser_enclosed_template_argument_list (parser);
11839           /* Skip tokens until we find a good place from which to
11840              continue parsing.  */
11841           cp_parser_skip_to_closing_parenthesis (parser,
11842                                                  /*recovering=*/true,
11843                                                  /*or_comma=*/true,
11844                                                  /*consume_paren=*/false);
11845           /* If parsing tentatively, permanently remove the
11846              template argument list.  That will prevent duplicate
11847              error messages from being issued about the missing
11848              "template" keyword.  */
11849           if (start)
11850             cp_lexer_purge_tokens_after (parser->lexer, start);
11851           if (is_identifier)
11852             *is_identifier = true;
11853           return identifier;
11854         }
11855
11856       /* If the "template" keyword is present, then there is generally
11857          no point in doing name-lookup, so we just return IDENTIFIER.
11858          But, if the qualifying scope is non-dependent then we can
11859          (and must) do name-lookup normally.  */
11860       if (template_keyword_p
11861           && (!parser->scope
11862               || (TYPE_P (parser->scope)
11863                   && dependent_type_p (parser->scope))))
11864         return identifier;
11865     }
11866
11867   /* Look up the name.  */
11868   decl = cp_parser_lookup_name (parser, identifier,
11869                                 none_type,
11870                                 /*is_template=*/true,
11871                                 /*is_namespace=*/false,
11872                                 check_dependency_p,
11873                                 /*ambiguous_decls=*/NULL,
11874                                 token->location);
11875
11876   /* If DECL is a template, then the name was a template-name.  */
11877   if (TREE_CODE (decl) == TEMPLATE_DECL)
11878     ;
11879   else
11880     {
11881       tree fn = NULL_TREE;
11882
11883       /* The standard does not explicitly indicate whether a name that
11884          names a set of overloaded declarations, some of which are
11885          templates, is a template-name.  However, such a name should
11886          be a template-name; otherwise, there is no way to form a
11887          template-id for the overloaded templates.  */
11888       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11889       if (TREE_CODE (fns) == OVERLOAD)
11890         for (fn = fns; fn; fn = OVL_NEXT (fn))
11891           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11892             break;
11893
11894       if (!fn)
11895         {
11896           /* The name does not name a template.  */
11897           cp_parser_error (parser, "expected template-name");
11898           return error_mark_node;
11899         }
11900     }
11901
11902   /* If DECL is dependent, and refers to a function, then just return
11903      its name; we will look it up again during template instantiation.  */
11904   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11905     {
11906       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11907       if (TYPE_P (scope) && dependent_type_p (scope))
11908         return identifier;
11909     }
11910
11911   return decl;
11912 }
11913
11914 /* Parse a template-argument-list.
11915
11916    template-argument-list:
11917      template-argument ... [opt]
11918      template-argument-list , template-argument ... [opt]
11919
11920    Returns a TREE_VEC containing the arguments.  */
11921
11922 static tree
11923 cp_parser_template_argument_list (cp_parser* parser)
11924 {
11925   tree fixed_args[10];
11926   unsigned n_args = 0;
11927   unsigned alloced = 10;
11928   tree *arg_ary = fixed_args;
11929   tree vec;
11930   bool saved_in_template_argument_list_p;
11931   bool saved_ice_p;
11932   bool saved_non_ice_p;
11933
11934   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11935   parser->in_template_argument_list_p = true;
11936   /* Even if the template-id appears in an integral
11937      constant-expression, the contents of the argument list do
11938      not.  */
11939   saved_ice_p = parser->integral_constant_expression_p;
11940   parser->integral_constant_expression_p = false;
11941   saved_non_ice_p = parser->non_integral_constant_expression_p;
11942   parser->non_integral_constant_expression_p = false;
11943   /* Parse the arguments.  */
11944   do
11945     {
11946       tree argument;
11947
11948       if (n_args)
11949         /* Consume the comma.  */
11950         cp_lexer_consume_token (parser->lexer);
11951
11952       /* Parse the template-argument.  */
11953       argument = cp_parser_template_argument (parser);
11954
11955       /* If the next token is an ellipsis, we're expanding a template
11956          argument pack. */
11957       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11958         {
11959           if (argument == error_mark_node)
11960             {
11961               cp_token *token = cp_lexer_peek_token (parser->lexer);
11962               error_at (token->location,
11963                         "expected parameter pack before %<...%>");
11964             }
11965           /* Consume the `...' token. */
11966           cp_lexer_consume_token (parser->lexer);
11967
11968           /* Make the argument into a TYPE_PACK_EXPANSION or
11969              EXPR_PACK_EXPANSION. */
11970           argument = make_pack_expansion (argument);
11971         }
11972
11973       if (n_args == alloced)
11974         {
11975           alloced *= 2;
11976
11977           if (arg_ary == fixed_args)
11978             {
11979               arg_ary = XNEWVEC (tree, alloced);
11980               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11981             }
11982           else
11983             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11984         }
11985       arg_ary[n_args++] = argument;
11986     }
11987   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11988
11989   vec = make_tree_vec (n_args);
11990
11991   while (n_args--)
11992     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11993
11994   if (arg_ary != fixed_args)
11995     free (arg_ary);
11996   parser->non_integral_constant_expression_p = saved_non_ice_p;
11997   parser->integral_constant_expression_p = saved_ice_p;
11998   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11999 #ifdef ENABLE_CHECKING
12000   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12001 #endif
12002   return vec;
12003 }
12004
12005 /* Parse a template-argument.
12006
12007    template-argument:
12008      assignment-expression
12009      type-id
12010      id-expression
12011
12012    The representation is that of an assignment-expression, type-id, or
12013    id-expression -- except that the qualified id-expression is
12014    evaluated, so that the value returned is either a DECL or an
12015    OVERLOAD.
12016
12017    Although the standard says "assignment-expression", it forbids
12018    throw-expressions or assignments in the template argument.
12019    Therefore, we use "conditional-expression" instead.  */
12020
12021 static tree
12022 cp_parser_template_argument (cp_parser* parser)
12023 {
12024   tree argument;
12025   bool template_p;
12026   bool address_p;
12027   bool maybe_type_id = false;
12028   cp_token *token = NULL, *argument_start_token = NULL;
12029   cp_id_kind idk;
12030
12031   /* There's really no way to know what we're looking at, so we just
12032      try each alternative in order.
12033
12034        [temp.arg]
12035
12036        In a template-argument, an ambiguity between a type-id and an
12037        expression is resolved to a type-id, regardless of the form of
12038        the corresponding template-parameter.
12039
12040      Therefore, we try a type-id first.  */
12041   cp_parser_parse_tentatively (parser);
12042   argument = cp_parser_template_type_arg (parser);
12043   /* If there was no error parsing the type-id but the next token is a
12044      '>>', our behavior depends on which dialect of C++ we're
12045      parsing. In C++98, we probably found a typo for '> >'. But there
12046      are type-id which are also valid expressions. For instance:
12047
12048      struct X { int operator >> (int); };
12049      template <int V> struct Foo {};
12050      Foo<X () >> 5> r;
12051
12052      Here 'X()' is a valid type-id of a function type, but the user just
12053      wanted to write the expression "X() >> 5". Thus, we remember that we
12054      found a valid type-id, but we still try to parse the argument as an
12055      expression to see what happens. 
12056
12057      In C++0x, the '>>' will be considered two separate '>'
12058      tokens.  */
12059   if (!cp_parser_error_occurred (parser)
12060       && cxx_dialect == cxx98
12061       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12062     {
12063       maybe_type_id = true;
12064       cp_parser_abort_tentative_parse (parser);
12065     }
12066   else
12067     {
12068       /* If the next token isn't a `,' or a `>', then this argument wasn't
12069       really finished. This means that the argument is not a valid
12070       type-id.  */
12071       if (!cp_parser_next_token_ends_template_argument_p (parser))
12072         cp_parser_error (parser, "expected template-argument");
12073       /* If that worked, we're done.  */
12074       if (cp_parser_parse_definitely (parser))
12075         return argument;
12076     }
12077   /* We're still not sure what the argument will be.  */
12078   cp_parser_parse_tentatively (parser);
12079   /* Try a template.  */
12080   argument_start_token = cp_lexer_peek_token (parser->lexer);
12081   argument = cp_parser_id_expression (parser,
12082                                       /*template_keyword_p=*/false,
12083                                       /*check_dependency_p=*/true,
12084                                       &template_p,
12085                                       /*declarator_p=*/false,
12086                                       /*optional_p=*/false);
12087   /* If the next token isn't a `,' or a `>', then this argument wasn't
12088      really finished.  */
12089   if (!cp_parser_next_token_ends_template_argument_p (parser))
12090     cp_parser_error (parser, "expected template-argument");
12091   if (!cp_parser_error_occurred (parser))
12092     {
12093       /* Figure out what is being referred to.  If the id-expression
12094          was for a class template specialization, then we will have a
12095          TYPE_DECL at this point.  There is no need to do name lookup
12096          at this point in that case.  */
12097       if (TREE_CODE (argument) != TYPE_DECL)
12098         argument = cp_parser_lookup_name (parser, argument,
12099                                           none_type,
12100                                           /*is_template=*/template_p,
12101                                           /*is_namespace=*/false,
12102                                           /*check_dependency=*/true,
12103                                           /*ambiguous_decls=*/NULL,
12104                                           argument_start_token->location);
12105       if (TREE_CODE (argument) != TEMPLATE_DECL
12106           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12107         cp_parser_error (parser, "expected template-name");
12108     }
12109   if (cp_parser_parse_definitely (parser))
12110     return argument;
12111   /* It must be a non-type argument.  There permitted cases are given
12112      in [temp.arg.nontype]:
12113
12114      -- an integral constant-expression of integral or enumeration
12115         type; or
12116
12117      -- the name of a non-type template-parameter; or
12118
12119      -- the name of an object or function with external linkage...
12120
12121      -- the address of an object or function with external linkage...
12122
12123      -- a pointer to member...  */
12124   /* Look for a non-type template parameter.  */
12125   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12126     {
12127       cp_parser_parse_tentatively (parser);
12128       argument = cp_parser_primary_expression (parser,
12129                                                /*address_p=*/false,
12130                                                /*cast_p=*/false,
12131                                                /*template_arg_p=*/true,
12132                                                &idk);
12133       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12134           || !cp_parser_next_token_ends_template_argument_p (parser))
12135         cp_parser_simulate_error (parser);
12136       if (cp_parser_parse_definitely (parser))
12137         return argument;
12138     }
12139
12140   /* If the next token is "&", the argument must be the address of an
12141      object or function with external linkage.  */
12142   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12143   if (address_p)
12144     cp_lexer_consume_token (parser->lexer);
12145   /* See if we might have an id-expression.  */
12146   token = cp_lexer_peek_token (parser->lexer);
12147   if (token->type == CPP_NAME
12148       || token->keyword == RID_OPERATOR
12149       || token->type == CPP_SCOPE
12150       || token->type == CPP_TEMPLATE_ID
12151       || token->type == CPP_NESTED_NAME_SPECIFIER)
12152     {
12153       cp_parser_parse_tentatively (parser);
12154       argument = cp_parser_primary_expression (parser,
12155                                                address_p,
12156                                                /*cast_p=*/false,
12157                                                /*template_arg_p=*/true,
12158                                                &idk);
12159       if (cp_parser_error_occurred (parser)
12160           || !cp_parser_next_token_ends_template_argument_p (parser))
12161         cp_parser_abort_tentative_parse (parser);
12162       else
12163         {
12164           tree probe;
12165
12166           if (TREE_CODE (argument) == INDIRECT_REF)
12167             {
12168               gcc_assert (REFERENCE_REF_P (argument));
12169               argument = TREE_OPERAND (argument, 0);
12170             }
12171
12172           /* If we're in a template, we represent a qualified-id referring
12173              to a static data member as a SCOPE_REF even if the scope isn't
12174              dependent so that we can check access control later.  */
12175           probe = argument;
12176           if (TREE_CODE (probe) == SCOPE_REF)
12177             probe = TREE_OPERAND (probe, 1);
12178           if (TREE_CODE (probe) == VAR_DECL)
12179             {
12180               /* A variable without external linkage might still be a
12181                  valid constant-expression, so no error is issued here
12182                  if the external-linkage check fails.  */
12183               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12184                 cp_parser_simulate_error (parser);
12185             }
12186           else if (is_overloaded_fn (argument))
12187             /* All overloaded functions are allowed; if the external
12188                linkage test does not pass, an error will be issued
12189                later.  */
12190             ;
12191           else if (address_p
12192                    && (TREE_CODE (argument) == OFFSET_REF
12193                        || TREE_CODE (argument) == SCOPE_REF))
12194             /* A pointer-to-member.  */
12195             ;
12196           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12197             ;
12198           else
12199             cp_parser_simulate_error (parser);
12200
12201           if (cp_parser_parse_definitely (parser))
12202             {
12203               if (address_p)
12204                 argument = build_x_unary_op (ADDR_EXPR, argument,
12205                                              tf_warning_or_error);
12206               return argument;
12207             }
12208         }
12209     }
12210   /* If the argument started with "&", there are no other valid
12211      alternatives at this point.  */
12212   if (address_p)
12213     {
12214       cp_parser_error (parser, "invalid non-type template argument");
12215       return error_mark_node;
12216     }
12217
12218   /* If the argument wasn't successfully parsed as a type-id followed
12219      by '>>', the argument can only be a constant expression now.
12220      Otherwise, we try parsing the constant-expression tentatively,
12221      because the argument could really be a type-id.  */
12222   if (maybe_type_id)
12223     cp_parser_parse_tentatively (parser);
12224   argument = cp_parser_constant_expression (parser,
12225                                             /*allow_non_constant_p=*/false,
12226                                             /*non_constant_p=*/NULL);
12227   argument = fold_non_dependent_expr (argument);
12228   if (!maybe_type_id)
12229     return argument;
12230   if (!cp_parser_next_token_ends_template_argument_p (parser))
12231     cp_parser_error (parser, "expected template-argument");
12232   if (cp_parser_parse_definitely (parser))
12233     return argument;
12234   /* We did our best to parse the argument as a non type-id, but that
12235      was the only alternative that matched (albeit with a '>' after
12236      it). We can assume it's just a typo from the user, and a
12237      diagnostic will then be issued.  */
12238   return cp_parser_template_type_arg (parser);
12239 }
12240
12241 /* Parse an explicit-instantiation.
12242
12243    explicit-instantiation:
12244      template declaration
12245
12246    Although the standard says `declaration', what it really means is:
12247
12248    explicit-instantiation:
12249      template decl-specifier-seq [opt] declarator [opt] ;
12250
12251    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12252    supposed to be allowed.  A defect report has been filed about this
12253    issue.
12254
12255    GNU Extension:
12256
12257    explicit-instantiation:
12258      storage-class-specifier template
12259        decl-specifier-seq [opt] declarator [opt] ;
12260      function-specifier template
12261        decl-specifier-seq [opt] declarator [opt] ;  */
12262
12263 static void
12264 cp_parser_explicit_instantiation (cp_parser* parser)
12265 {
12266   int declares_class_or_enum;
12267   cp_decl_specifier_seq decl_specifiers;
12268   tree extension_specifier = NULL_TREE;
12269
12270   /* Look for an (optional) storage-class-specifier or
12271      function-specifier.  */
12272   if (cp_parser_allow_gnu_extensions_p (parser))
12273     {
12274       extension_specifier
12275         = cp_parser_storage_class_specifier_opt (parser);
12276       if (!extension_specifier)
12277         extension_specifier
12278           = cp_parser_function_specifier_opt (parser,
12279                                               /*decl_specs=*/NULL);
12280     }
12281
12282   /* Look for the `template' keyword.  */
12283   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12284   /* Let the front end know that we are processing an explicit
12285      instantiation.  */
12286   begin_explicit_instantiation ();
12287   /* [temp.explicit] says that we are supposed to ignore access
12288      control while processing explicit instantiation directives.  */
12289   push_deferring_access_checks (dk_no_check);
12290   /* Parse a decl-specifier-seq.  */
12291   cp_parser_decl_specifier_seq (parser,
12292                                 CP_PARSER_FLAGS_OPTIONAL,
12293                                 &decl_specifiers,
12294                                 &declares_class_or_enum);
12295   /* If there was exactly one decl-specifier, and it declared a class,
12296      and there's no declarator, then we have an explicit type
12297      instantiation.  */
12298   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12299     {
12300       tree type;
12301
12302       type = check_tag_decl (&decl_specifiers);
12303       /* Turn access control back on for names used during
12304          template instantiation.  */
12305       pop_deferring_access_checks ();
12306       if (type)
12307         do_type_instantiation (type, extension_specifier,
12308                                /*complain=*/tf_error);
12309     }
12310   else
12311     {
12312       cp_declarator *declarator;
12313       tree decl;
12314
12315       /* Parse the declarator.  */
12316       declarator
12317         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12318                                 /*ctor_dtor_or_conv_p=*/NULL,
12319                                 /*parenthesized_p=*/NULL,
12320                                 /*member_p=*/false);
12321       if (declares_class_or_enum & 2)
12322         cp_parser_check_for_definition_in_return_type (declarator,
12323                                                        decl_specifiers.type,
12324                                                        decl_specifiers.type_location);
12325       if (declarator != cp_error_declarator)
12326         {
12327           if (decl_specifiers.specs[(int)ds_inline])
12328             permerror (input_location, "explicit instantiation shall not use"
12329                        " %<inline%> specifier");
12330           if (decl_specifiers.specs[(int)ds_constexpr])
12331             permerror (input_location, "explicit instantiation shall not use"
12332                        " %<constexpr%> specifier");
12333
12334           decl = grokdeclarator (declarator, &decl_specifiers,
12335                                  NORMAL, 0, &decl_specifiers.attributes);
12336           /* Turn access control back on for names used during
12337              template instantiation.  */
12338           pop_deferring_access_checks ();
12339           /* Do the explicit instantiation.  */
12340           do_decl_instantiation (decl, extension_specifier);
12341         }
12342       else
12343         {
12344           pop_deferring_access_checks ();
12345           /* Skip the body of the explicit instantiation.  */
12346           cp_parser_skip_to_end_of_statement (parser);
12347         }
12348     }
12349   /* We're done with the instantiation.  */
12350   end_explicit_instantiation ();
12351
12352   cp_parser_consume_semicolon_at_end_of_statement (parser);
12353 }
12354
12355 /* Parse an explicit-specialization.
12356
12357    explicit-specialization:
12358      template < > declaration
12359
12360    Although the standard says `declaration', what it really means is:
12361
12362    explicit-specialization:
12363      template <> decl-specifier [opt] init-declarator [opt] ;
12364      template <> function-definition
12365      template <> explicit-specialization
12366      template <> template-declaration  */
12367
12368 static void
12369 cp_parser_explicit_specialization (cp_parser* parser)
12370 {
12371   bool need_lang_pop;
12372   cp_token *token = cp_lexer_peek_token (parser->lexer);
12373
12374   /* Look for the `template' keyword.  */
12375   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12376   /* Look for the `<'.  */
12377   cp_parser_require (parser, CPP_LESS, RT_LESS);
12378   /* Look for the `>'.  */
12379   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12380   /* We have processed another parameter list.  */
12381   ++parser->num_template_parameter_lists;
12382   /* [temp]
12383
12384      A template ... explicit specialization ... shall not have C
12385      linkage.  */
12386   if (current_lang_name == lang_name_c)
12387     {
12388       error_at (token->location, "template specialization with C linkage");
12389       /* Give it C++ linkage to avoid confusing other parts of the
12390          front end.  */
12391       push_lang_context (lang_name_cplusplus);
12392       need_lang_pop = true;
12393     }
12394   else
12395     need_lang_pop = false;
12396   /* Let the front end know that we are beginning a specialization.  */
12397   if (!begin_specialization ())
12398     {
12399       end_specialization ();
12400       return;
12401     }
12402
12403   /* If the next keyword is `template', we need to figure out whether
12404      or not we're looking a template-declaration.  */
12405   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12406     {
12407       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12408           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12409         cp_parser_template_declaration_after_export (parser,
12410                                                      /*member_p=*/false);
12411       else
12412         cp_parser_explicit_specialization (parser);
12413     }
12414   else
12415     /* Parse the dependent declaration.  */
12416     cp_parser_single_declaration (parser,
12417                                   /*checks=*/NULL,
12418                                   /*member_p=*/false,
12419                                   /*explicit_specialization_p=*/true,
12420                                   /*friend_p=*/NULL);
12421   /* We're done with the specialization.  */
12422   end_specialization ();
12423   /* For the erroneous case of a template with C linkage, we pushed an
12424      implicit C++ linkage scope; exit that scope now.  */
12425   if (need_lang_pop)
12426     pop_lang_context ();
12427   /* We're done with this parameter list.  */
12428   --parser->num_template_parameter_lists;
12429 }
12430
12431 /* Parse a type-specifier.
12432
12433    type-specifier:
12434      simple-type-specifier
12435      class-specifier
12436      enum-specifier
12437      elaborated-type-specifier
12438      cv-qualifier
12439
12440    GNU Extension:
12441
12442    type-specifier:
12443      __complex__
12444
12445    Returns a representation of the type-specifier.  For a
12446    class-specifier, enum-specifier, or elaborated-type-specifier, a
12447    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12448
12449    The parser flags FLAGS is used to control type-specifier parsing.
12450
12451    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12452    in a decl-specifier-seq.
12453
12454    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12455    class-specifier, enum-specifier, or elaborated-type-specifier, then
12456    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12457    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12458    zero.
12459
12460    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12461    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12462    is set to FALSE.  */
12463
12464 static tree
12465 cp_parser_type_specifier (cp_parser* parser,
12466                           cp_parser_flags flags,
12467                           cp_decl_specifier_seq *decl_specs,
12468                           bool is_declaration,
12469                           int* declares_class_or_enum,
12470                           bool* is_cv_qualifier)
12471 {
12472   tree type_spec = NULL_TREE;
12473   cp_token *token;
12474   enum rid keyword;
12475   cp_decl_spec ds = ds_last;
12476
12477   /* Assume this type-specifier does not declare a new type.  */
12478   if (declares_class_or_enum)
12479     *declares_class_or_enum = 0;
12480   /* And that it does not specify a cv-qualifier.  */
12481   if (is_cv_qualifier)
12482     *is_cv_qualifier = false;
12483   /* Peek at the next token.  */
12484   token = cp_lexer_peek_token (parser->lexer);
12485
12486   /* If we're looking at a keyword, we can use that to guide the
12487      production we choose.  */
12488   keyword = token->keyword;
12489   switch (keyword)
12490     {
12491     case RID_ENUM:
12492       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12493         goto elaborated_type_specifier;
12494
12495       /* Look for the enum-specifier.  */
12496       type_spec = cp_parser_enum_specifier (parser);
12497       /* If that worked, we're done.  */
12498       if (type_spec)
12499         {
12500           if (declares_class_or_enum)
12501             *declares_class_or_enum = 2;
12502           if (decl_specs)
12503             cp_parser_set_decl_spec_type (decl_specs,
12504                                           type_spec,
12505                                           token->location,
12506                                           /*user_defined_p=*/true);
12507           return type_spec;
12508         }
12509       else
12510         goto elaborated_type_specifier;
12511
12512       /* Any of these indicate either a class-specifier, or an
12513          elaborated-type-specifier.  */
12514     case RID_CLASS:
12515     case RID_STRUCT:
12516     case RID_UNION:
12517       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12518         goto elaborated_type_specifier;
12519
12520       /* Parse tentatively so that we can back up if we don't find a
12521          class-specifier.  */
12522       cp_parser_parse_tentatively (parser);
12523       /* Look for the class-specifier.  */
12524       type_spec = cp_parser_class_specifier (parser);
12525       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12526       /* If that worked, we're done.  */
12527       if (cp_parser_parse_definitely (parser))
12528         {
12529           if (declares_class_or_enum)
12530             *declares_class_or_enum = 2;
12531           if (decl_specs)
12532             cp_parser_set_decl_spec_type (decl_specs,
12533                                           type_spec,
12534                                           token->location,
12535                                           /*user_defined_p=*/true);
12536           return type_spec;
12537         }
12538
12539       /* Fall through.  */
12540     elaborated_type_specifier:
12541       /* We're declaring (not defining) a class or enum.  */
12542       if (declares_class_or_enum)
12543         *declares_class_or_enum = 1;
12544
12545       /* Fall through.  */
12546     case RID_TYPENAME:
12547       /* Look for an elaborated-type-specifier.  */
12548       type_spec
12549         = (cp_parser_elaborated_type_specifier
12550            (parser,
12551             decl_specs && decl_specs->specs[(int) ds_friend],
12552             is_declaration));
12553       if (decl_specs)
12554         cp_parser_set_decl_spec_type (decl_specs,
12555                                       type_spec,
12556                                       token->location,
12557                                       /*user_defined_p=*/true);
12558       return type_spec;
12559
12560     case RID_CONST:
12561       ds = ds_const;
12562       if (is_cv_qualifier)
12563         *is_cv_qualifier = true;
12564       break;
12565
12566     case RID_VOLATILE:
12567       ds = ds_volatile;
12568       if (is_cv_qualifier)
12569         *is_cv_qualifier = true;
12570       break;
12571
12572     case RID_RESTRICT:
12573       ds = ds_restrict;
12574       if (is_cv_qualifier)
12575         *is_cv_qualifier = true;
12576       break;
12577
12578     case RID_COMPLEX:
12579       /* The `__complex__' keyword is a GNU extension.  */
12580       ds = ds_complex;
12581       break;
12582
12583     default:
12584       break;
12585     }
12586
12587   /* Handle simple keywords.  */
12588   if (ds != ds_last)
12589     {
12590       if (decl_specs)
12591         {
12592           ++decl_specs->specs[(int)ds];
12593           decl_specs->any_specifiers_p = true;
12594         }
12595       return cp_lexer_consume_token (parser->lexer)->u.value;
12596     }
12597
12598   /* If we do not already have a type-specifier, assume we are looking
12599      at a simple-type-specifier.  */
12600   type_spec = cp_parser_simple_type_specifier (parser,
12601                                                decl_specs,
12602                                                flags);
12603
12604   /* If we didn't find a type-specifier, and a type-specifier was not
12605      optional in this context, issue an error message.  */
12606   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12607     {
12608       cp_parser_error (parser, "expected type specifier");
12609       return error_mark_node;
12610     }
12611
12612   return type_spec;
12613 }
12614
12615 /* Parse a simple-type-specifier.
12616
12617    simple-type-specifier:
12618      :: [opt] nested-name-specifier [opt] type-name
12619      :: [opt] nested-name-specifier template template-id
12620      char
12621      wchar_t
12622      bool
12623      short
12624      int
12625      long
12626      signed
12627      unsigned
12628      float
12629      double
12630      void
12631
12632    C++0x Extension:
12633
12634    simple-type-specifier:
12635      auto
12636      decltype ( expression )   
12637      char16_t
12638      char32_t
12639
12640    GNU Extension:
12641
12642    simple-type-specifier:
12643      __int128
12644      __typeof__ unary-expression
12645      __typeof__ ( type-id )
12646
12647    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12648    appropriately updated.  */
12649
12650 static tree
12651 cp_parser_simple_type_specifier (cp_parser* parser,
12652                                  cp_decl_specifier_seq *decl_specs,
12653                                  cp_parser_flags flags)
12654 {
12655   tree type = NULL_TREE;
12656   cp_token *token;
12657
12658   /* Peek at the next token.  */
12659   token = cp_lexer_peek_token (parser->lexer);
12660
12661   /* If we're looking at a keyword, things are easy.  */
12662   switch (token->keyword)
12663     {
12664     case RID_CHAR:
12665       if (decl_specs)
12666         decl_specs->explicit_char_p = true;
12667       type = char_type_node;
12668       break;
12669     case RID_CHAR16:
12670       type = char16_type_node;
12671       break;
12672     case RID_CHAR32:
12673       type = char32_type_node;
12674       break;
12675     case RID_WCHAR:
12676       type = wchar_type_node;
12677       break;
12678     case RID_BOOL:
12679       type = boolean_type_node;
12680       break;
12681     case RID_SHORT:
12682       if (decl_specs)
12683         ++decl_specs->specs[(int) ds_short];
12684       type = short_integer_type_node;
12685       break;
12686     case RID_INT:
12687       if (decl_specs)
12688         decl_specs->explicit_int_p = true;
12689       type = integer_type_node;
12690       break;
12691     case RID_INT128:
12692       if (!int128_integer_type_node)
12693         break;
12694       if (decl_specs)
12695         decl_specs->explicit_int128_p = true;
12696       type = int128_integer_type_node;
12697       break;
12698     case RID_LONG:
12699       if (decl_specs)
12700         ++decl_specs->specs[(int) ds_long];
12701       type = long_integer_type_node;
12702       break;
12703     case RID_SIGNED:
12704       if (decl_specs)
12705         ++decl_specs->specs[(int) ds_signed];
12706       type = integer_type_node;
12707       break;
12708     case RID_UNSIGNED:
12709       if (decl_specs)
12710         ++decl_specs->specs[(int) ds_unsigned];
12711       type = unsigned_type_node;
12712       break;
12713     case RID_FLOAT:
12714       type = float_type_node;
12715       break;
12716     case RID_DOUBLE:
12717       type = double_type_node;
12718       break;
12719     case RID_VOID:
12720       type = void_type_node;
12721       break;
12722       
12723     case RID_AUTO:
12724       maybe_warn_cpp0x (CPP0X_AUTO);
12725       type = make_auto ();
12726       break;
12727
12728     case RID_DECLTYPE:
12729       /* Parse the `decltype' type.  */
12730       type = cp_parser_decltype (parser);
12731
12732       if (decl_specs)
12733         cp_parser_set_decl_spec_type (decl_specs, type,
12734                                       token->location,
12735                                       /*user_defined_p=*/true);
12736
12737       return type;
12738
12739     case RID_TYPEOF:
12740       /* Consume the `typeof' token.  */
12741       cp_lexer_consume_token (parser->lexer);
12742       /* Parse the operand to `typeof'.  */
12743       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12744       /* If it is not already a TYPE, take its type.  */
12745       if (!TYPE_P (type))
12746         type = finish_typeof (type);
12747
12748       if (decl_specs)
12749         cp_parser_set_decl_spec_type (decl_specs, type,
12750                                       token->location,
12751                                       /*user_defined_p=*/true);
12752
12753       return type;
12754
12755     default:
12756       break;
12757     }
12758
12759   /* If the type-specifier was for a built-in type, we're done.  */
12760   if (type)
12761     {
12762       /* Record the type.  */
12763       if (decl_specs
12764           && (token->keyword != RID_SIGNED
12765               && token->keyword != RID_UNSIGNED
12766               && token->keyword != RID_SHORT
12767               && token->keyword != RID_LONG))
12768         cp_parser_set_decl_spec_type (decl_specs,
12769                                       type,
12770                                       token->location,
12771                                       /*user_defined=*/false);
12772       if (decl_specs)
12773         decl_specs->any_specifiers_p = true;
12774
12775       /* Consume the token.  */
12776       cp_lexer_consume_token (parser->lexer);
12777
12778       /* There is no valid C++ program where a non-template type is
12779          followed by a "<".  That usually indicates that the user thought
12780          that the type was a template.  */
12781       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12782
12783       return TYPE_NAME (type);
12784     }
12785
12786   /* The type-specifier must be a user-defined type.  */
12787   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12788     {
12789       bool qualified_p;
12790       bool global_p;
12791
12792       /* Don't gobble tokens or issue error messages if this is an
12793          optional type-specifier.  */
12794       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12795         cp_parser_parse_tentatively (parser);
12796
12797       /* Look for the optional `::' operator.  */
12798       global_p
12799         = (cp_parser_global_scope_opt (parser,
12800                                        /*current_scope_valid_p=*/false)
12801            != NULL_TREE);
12802       /* Look for the nested-name specifier.  */
12803       qualified_p
12804         = (cp_parser_nested_name_specifier_opt (parser,
12805                                                 /*typename_keyword_p=*/false,
12806                                                 /*check_dependency_p=*/true,
12807                                                 /*type_p=*/false,
12808                                                 /*is_declaration=*/false)
12809            != NULL_TREE);
12810       token = cp_lexer_peek_token (parser->lexer);
12811       /* If we have seen a nested-name-specifier, and the next token
12812          is `template', then we are using the template-id production.  */
12813       if (parser->scope
12814           && cp_parser_optional_template_keyword (parser))
12815         {
12816           /* Look for the template-id.  */
12817           type = cp_parser_template_id (parser,
12818                                         /*template_keyword_p=*/true,
12819                                         /*check_dependency_p=*/true,
12820                                         /*is_declaration=*/false);
12821           /* If the template-id did not name a type, we are out of
12822              luck.  */
12823           if (TREE_CODE (type) != TYPE_DECL)
12824             {
12825               cp_parser_error (parser, "expected template-id for type");
12826               type = NULL_TREE;
12827             }
12828         }
12829       /* Otherwise, look for a type-name.  */
12830       else
12831         type = cp_parser_type_name (parser);
12832       /* Keep track of all name-lookups performed in class scopes.  */
12833       if (type
12834           && !global_p
12835           && !qualified_p
12836           && TREE_CODE (type) == TYPE_DECL
12837           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12838         maybe_note_name_used_in_class (DECL_NAME (type), type);
12839       /* If it didn't work out, we don't have a TYPE.  */
12840       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12841           && !cp_parser_parse_definitely (parser))
12842         type = NULL_TREE;
12843       if (type && decl_specs)
12844         cp_parser_set_decl_spec_type (decl_specs, type,
12845                                       token->location,
12846                                       /*user_defined=*/true);
12847     }
12848
12849   /* If we didn't get a type-name, issue an error message.  */
12850   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12851     {
12852       cp_parser_error (parser, "expected type-name");
12853       return error_mark_node;
12854     }
12855
12856   if (type && type != error_mark_node)
12857     {
12858       /* See if TYPE is an Objective-C type, and if so, parse and
12859          accept any protocol references following it.  Do this before
12860          the cp_parser_check_for_invalid_template_id() call, because
12861          Objective-C types can be followed by '<...>' which would
12862          enclose protocol names rather than template arguments, and so
12863          everything is fine.  */
12864       if (c_dialect_objc () && !parser->scope
12865           && (objc_is_id (type) || objc_is_class_name (type)))
12866         {
12867           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12868           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12869
12870           /* Clobber the "unqualified" type previously entered into
12871              DECL_SPECS with the new, improved protocol-qualified version.  */
12872           if (decl_specs)
12873             decl_specs->type = qual_type;
12874
12875           return qual_type;
12876         }
12877
12878       /* There is no valid C++ program where a non-template type is
12879          followed by a "<".  That usually indicates that the user
12880          thought that the type was a template.  */
12881       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12882                                                token->location);
12883     }
12884
12885   return type;
12886 }
12887
12888 /* Parse a type-name.
12889
12890    type-name:
12891      class-name
12892      enum-name
12893      typedef-name
12894
12895    enum-name:
12896      identifier
12897
12898    typedef-name:
12899      identifier
12900
12901    Returns a TYPE_DECL for the type.  */
12902
12903 static tree
12904 cp_parser_type_name (cp_parser* parser)
12905 {
12906   tree type_decl;
12907
12908   /* We can't know yet whether it is a class-name or not.  */
12909   cp_parser_parse_tentatively (parser);
12910   /* Try a class-name.  */
12911   type_decl = cp_parser_class_name (parser,
12912                                     /*typename_keyword_p=*/false,
12913                                     /*template_keyword_p=*/false,
12914                                     none_type,
12915                                     /*check_dependency_p=*/true,
12916                                     /*class_head_p=*/false,
12917                                     /*is_declaration=*/false);
12918   /* If it's not a class-name, keep looking.  */
12919   if (!cp_parser_parse_definitely (parser))
12920     {
12921       /* It must be a typedef-name or an enum-name.  */
12922       return cp_parser_nonclass_name (parser);
12923     }
12924
12925   return type_decl;
12926 }
12927
12928 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12929
12930    enum-name:
12931      identifier
12932
12933    typedef-name:
12934      identifier
12935
12936    Returns a TYPE_DECL for the type.  */
12937
12938 static tree
12939 cp_parser_nonclass_name (cp_parser* parser)
12940 {
12941   tree type_decl;
12942   tree identifier;
12943
12944   cp_token *token = cp_lexer_peek_token (parser->lexer);
12945   identifier = cp_parser_identifier (parser);
12946   if (identifier == error_mark_node)
12947     return error_mark_node;
12948
12949   /* Look up the type-name.  */
12950   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12951
12952   if (TREE_CODE (type_decl) != TYPE_DECL
12953       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12954     {
12955       /* See if this is an Objective-C type.  */
12956       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12957       tree type = objc_get_protocol_qualified_type (identifier, protos);
12958       if (type)
12959         type_decl = TYPE_NAME (type);
12960     }
12961
12962   /* Issue an error if we did not find a type-name.  */
12963   if (TREE_CODE (type_decl) != TYPE_DECL
12964       /* In Objective-C, we have the complication that class names are
12965          normally type names and start declarations (eg, the
12966          "NSObject" in "NSObject *object;"), but can be used in an
12967          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12968          is an expression.  So, a classname followed by a dot is not a
12969          valid type-name.  */
12970       || (objc_is_class_name (TREE_TYPE (type_decl))
12971           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12972     {
12973       if (!cp_parser_simulate_error (parser))
12974         cp_parser_name_lookup_error (parser, identifier, type_decl,
12975                                      NLE_TYPE, token->location);
12976       return error_mark_node;
12977     }
12978   /* Remember that the name was used in the definition of the
12979      current class so that we can check later to see if the
12980      meaning would have been different after the class was
12981      entirely defined.  */
12982   else if (type_decl != error_mark_node
12983            && !parser->scope)
12984     maybe_note_name_used_in_class (identifier, type_decl);
12985   
12986   return type_decl;
12987 }
12988
12989 /* Parse an elaborated-type-specifier.  Note that the grammar given
12990    here incorporates the resolution to DR68.
12991
12992    elaborated-type-specifier:
12993      class-key :: [opt] nested-name-specifier [opt] identifier
12994      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12995      enum-key :: [opt] nested-name-specifier [opt] identifier
12996      typename :: [opt] nested-name-specifier identifier
12997      typename :: [opt] nested-name-specifier template [opt]
12998        template-id
12999
13000    GNU extension:
13001
13002    elaborated-type-specifier:
13003      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13004      class-key attributes :: [opt] nested-name-specifier [opt]
13005                template [opt] template-id
13006      enum attributes :: [opt] nested-name-specifier [opt] identifier
13007
13008    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13009    declared `friend'.  If IS_DECLARATION is TRUE, then this
13010    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13011    something is being declared.
13012
13013    Returns the TYPE specified.  */
13014
13015 static tree
13016 cp_parser_elaborated_type_specifier (cp_parser* parser,
13017                                      bool is_friend,
13018                                      bool is_declaration)
13019 {
13020   enum tag_types tag_type;
13021   tree identifier;
13022   tree type = NULL_TREE;
13023   tree attributes = NULL_TREE;
13024   tree globalscope;
13025   cp_token *token = NULL;
13026
13027   /* See if we're looking at the `enum' keyword.  */
13028   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13029     {
13030       /* Consume the `enum' token.  */
13031       cp_lexer_consume_token (parser->lexer);
13032       /* Remember that it's an enumeration type.  */
13033       tag_type = enum_type;
13034       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13035          enums) is used here.  */
13036       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13037           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13038         {
13039             pedwarn (input_location, 0, "elaborated-type-specifier "
13040                       "for a scoped enum must not use the %<%D%> keyword",
13041                       cp_lexer_peek_token (parser->lexer)->u.value);
13042           /* Consume the `struct' or `class' and parse it anyway.  */
13043           cp_lexer_consume_token (parser->lexer);
13044         }
13045       /* Parse the attributes.  */
13046       attributes = cp_parser_attributes_opt (parser);
13047     }
13048   /* Or, it might be `typename'.  */
13049   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13050                                            RID_TYPENAME))
13051     {
13052       /* Consume the `typename' token.  */
13053       cp_lexer_consume_token (parser->lexer);
13054       /* Remember that it's a `typename' type.  */
13055       tag_type = typename_type;
13056     }
13057   /* Otherwise it must be a class-key.  */
13058   else
13059     {
13060       tag_type = cp_parser_class_key (parser);
13061       if (tag_type == none_type)
13062         return error_mark_node;
13063       /* Parse the attributes.  */
13064       attributes = cp_parser_attributes_opt (parser);
13065     }
13066
13067   /* Look for the `::' operator.  */
13068   globalscope =  cp_parser_global_scope_opt (parser,
13069                                              /*current_scope_valid_p=*/false);
13070   /* Look for the nested-name-specifier.  */
13071   if (tag_type == typename_type && !globalscope)
13072     {
13073       if (!cp_parser_nested_name_specifier (parser,
13074                                            /*typename_keyword_p=*/true,
13075                                            /*check_dependency_p=*/true,
13076                                            /*type_p=*/true,
13077                                             is_declaration))
13078         return error_mark_node;
13079     }
13080   else
13081     /* Even though `typename' is not present, the proposed resolution
13082        to Core Issue 180 says that in `class A<T>::B', `B' should be
13083        considered a type-name, even if `A<T>' is dependent.  */
13084     cp_parser_nested_name_specifier_opt (parser,
13085                                          /*typename_keyword_p=*/true,
13086                                          /*check_dependency_p=*/true,
13087                                          /*type_p=*/true,
13088                                          is_declaration);
13089  /* For everything but enumeration types, consider a template-id.
13090     For an enumeration type, consider only a plain identifier.  */
13091   if (tag_type != enum_type)
13092     {
13093       bool template_p = false;
13094       tree decl;
13095
13096       /* Allow the `template' keyword.  */
13097       template_p = cp_parser_optional_template_keyword (parser);
13098       /* If we didn't see `template', we don't know if there's a
13099          template-id or not.  */
13100       if (!template_p)
13101         cp_parser_parse_tentatively (parser);
13102       /* Parse the template-id.  */
13103       token = cp_lexer_peek_token (parser->lexer);
13104       decl = cp_parser_template_id (parser, template_p,
13105                                     /*check_dependency_p=*/true,
13106                                     is_declaration);
13107       /* If we didn't find a template-id, look for an ordinary
13108          identifier.  */
13109       if (!template_p && !cp_parser_parse_definitely (parser))
13110         ;
13111       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13112          in effect, then we must assume that, upon instantiation, the
13113          template will correspond to a class.  */
13114       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13115                && tag_type == typename_type)
13116         type = make_typename_type (parser->scope, decl,
13117                                    typename_type,
13118                                    /*complain=*/tf_error);
13119       /* If the `typename' keyword is in effect and DECL is not a type
13120          decl. Then type is non existant.   */
13121       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13122         type = NULL_TREE; 
13123       else 
13124         type = TREE_TYPE (decl);
13125     }
13126
13127   if (!type)
13128     {
13129       token = cp_lexer_peek_token (parser->lexer);
13130       identifier = cp_parser_identifier (parser);
13131
13132       if (identifier == error_mark_node)
13133         {
13134           parser->scope = NULL_TREE;
13135           return error_mark_node;
13136         }
13137
13138       /* For a `typename', we needn't call xref_tag.  */
13139       if (tag_type == typename_type
13140           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13141         return cp_parser_make_typename_type (parser, parser->scope,
13142                                              identifier,
13143                                              token->location);
13144       /* Look up a qualified name in the usual way.  */
13145       if (parser->scope)
13146         {
13147           tree decl;
13148           tree ambiguous_decls;
13149
13150           decl = cp_parser_lookup_name (parser, identifier,
13151                                         tag_type,
13152                                         /*is_template=*/false,
13153                                         /*is_namespace=*/false,
13154                                         /*check_dependency=*/true,
13155                                         &ambiguous_decls,
13156                                         token->location);
13157
13158           /* If the lookup was ambiguous, an error will already have been
13159              issued.  */
13160           if (ambiguous_decls)
13161             return error_mark_node;
13162
13163           /* If we are parsing friend declaration, DECL may be a
13164              TEMPLATE_DECL tree node here.  However, we need to check
13165              whether this TEMPLATE_DECL results in valid code.  Consider
13166              the following example:
13167
13168                namespace N {
13169                  template <class T> class C {};
13170                }
13171                class X {
13172                  template <class T> friend class N::C; // #1, valid code
13173                };
13174                template <class T> class Y {
13175                  friend class N::C;                    // #2, invalid code
13176                };
13177
13178              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13179              name lookup of `N::C'.  We see that friend declaration must
13180              be template for the code to be valid.  Note that
13181              processing_template_decl does not work here since it is
13182              always 1 for the above two cases.  */
13183
13184           decl = (cp_parser_maybe_treat_template_as_class
13185                   (decl, /*tag_name_p=*/is_friend
13186                          && parser->num_template_parameter_lists));
13187
13188           if (TREE_CODE (decl) != TYPE_DECL)
13189             {
13190               cp_parser_diagnose_invalid_type_name (parser,
13191                                                     parser->scope,
13192                                                     identifier,
13193                                                     token->location);
13194               return error_mark_node;
13195             }
13196
13197           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13198             {
13199               bool allow_template = (parser->num_template_parameter_lists
13200                                       || DECL_SELF_REFERENCE_P (decl));
13201               type = check_elaborated_type_specifier (tag_type, decl, 
13202                                                       allow_template);
13203
13204               if (type == error_mark_node)
13205                 return error_mark_node;
13206             }
13207
13208           /* Forward declarations of nested types, such as
13209
13210                class C1::C2;
13211                class C1::C2::C3;
13212
13213              are invalid unless all components preceding the final '::'
13214              are complete.  If all enclosing types are complete, these
13215              declarations become merely pointless.
13216
13217              Invalid forward declarations of nested types are errors
13218              caught elsewhere in parsing.  Those that are pointless arrive
13219              here.  */
13220
13221           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13222               && !is_friend && !processing_explicit_instantiation)
13223             warning (0, "declaration %qD does not declare anything", decl);
13224
13225           type = TREE_TYPE (decl);
13226         }
13227       else
13228         {
13229           /* An elaborated-type-specifier sometimes introduces a new type and
13230              sometimes names an existing type.  Normally, the rule is that it
13231              introduces a new type only if there is not an existing type of
13232              the same name already in scope.  For example, given:
13233
13234                struct S {};
13235                void f() { struct S s; }
13236
13237              the `struct S' in the body of `f' is the same `struct S' as in
13238              the global scope; the existing definition is used.  However, if
13239              there were no global declaration, this would introduce a new
13240              local class named `S'.
13241
13242              An exception to this rule applies to the following code:
13243
13244                namespace N { struct S; }
13245
13246              Here, the elaborated-type-specifier names a new type
13247              unconditionally; even if there is already an `S' in the
13248              containing scope this declaration names a new type.
13249              This exception only applies if the elaborated-type-specifier
13250              forms the complete declaration:
13251
13252                [class.name]
13253
13254                A declaration consisting solely of `class-key identifier ;' is
13255                either a redeclaration of the name in the current scope or a
13256                forward declaration of the identifier as a class name.  It
13257                introduces the name into the current scope.
13258
13259              We are in this situation precisely when the next token is a `;'.
13260
13261              An exception to the exception is that a `friend' declaration does
13262              *not* name a new type; i.e., given:
13263
13264                struct S { friend struct T; };
13265
13266              `T' is not a new type in the scope of `S'.
13267
13268              Also, `new struct S' or `sizeof (struct S)' never results in the
13269              definition of a new type; a new type can only be declared in a
13270              declaration context.  */
13271
13272           tag_scope ts;
13273           bool template_p;
13274
13275           if (is_friend)
13276             /* Friends have special name lookup rules.  */
13277             ts = ts_within_enclosing_non_class;
13278           else if (is_declaration
13279                    && cp_lexer_next_token_is (parser->lexer,
13280                                               CPP_SEMICOLON))
13281             /* This is a `class-key identifier ;' */
13282             ts = ts_current;
13283           else
13284             ts = ts_global;
13285
13286           template_p =
13287             (parser->num_template_parameter_lists
13288              && (cp_parser_next_token_starts_class_definition_p (parser)
13289                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13290           /* An unqualified name was used to reference this type, so
13291              there were no qualifying templates.  */
13292           if (!cp_parser_check_template_parameters (parser,
13293                                                     /*num_templates=*/0,
13294                                                     token->location,
13295                                                     /*declarator=*/NULL))
13296             return error_mark_node;
13297           type = xref_tag (tag_type, identifier, ts, template_p);
13298         }
13299     }
13300
13301   if (type == error_mark_node)
13302     return error_mark_node;
13303
13304   /* Allow attributes on forward declarations of classes.  */
13305   if (attributes)
13306     {
13307       if (TREE_CODE (type) == TYPENAME_TYPE)
13308         warning (OPT_Wattributes,
13309                  "attributes ignored on uninstantiated type");
13310       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13311                && ! processing_explicit_instantiation)
13312         warning (OPT_Wattributes,
13313                  "attributes ignored on template instantiation");
13314       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13315         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13316       else
13317         warning (OPT_Wattributes,
13318                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13319     }
13320
13321   if (tag_type != enum_type)
13322     cp_parser_check_class_key (tag_type, type);
13323
13324   /* A "<" cannot follow an elaborated type specifier.  If that
13325      happens, the user was probably trying to form a template-id.  */
13326   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13327
13328   return type;
13329 }
13330
13331 /* Parse an enum-specifier.
13332
13333    enum-specifier:
13334      enum-head { enumerator-list [opt] }
13335
13336    enum-head:
13337      enum-key identifier [opt] enum-base [opt]
13338      enum-key nested-name-specifier identifier enum-base [opt]
13339
13340    enum-key:
13341      enum
13342      enum class   [C++0x]
13343      enum struct  [C++0x]
13344
13345    enum-base:   [C++0x]
13346      : type-specifier-seq
13347
13348    opaque-enum-specifier:
13349      enum-key identifier enum-base [opt] ;
13350
13351    GNU Extensions:
13352      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13353        { enumerator-list [opt] }attributes[opt]
13354
13355    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13356    if the token stream isn't an enum-specifier after all.  */
13357
13358 static tree
13359 cp_parser_enum_specifier (cp_parser* parser)
13360 {
13361   tree identifier;
13362   tree type = NULL_TREE;
13363   tree prev_scope;
13364   tree nested_name_specifier = NULL_TREE;
13365   tree attributes;
13366   bool scoped_enum_p = false;
13367   bool has_underlying_type = false;
13368   bool nested_being_defined = false;
13369   bool new_value_list = false;
13370   bool is_new_type = false;
13371   bool is_anonymous = false;
13372   tree underlying_type = NULL_TREE;
13373   cp_token *type_start_token = NULL;
13374   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13375
13376   parser->colon_corrects_to_scope_p = false;
13377
13378   /* Parse tentatively so that we can back up if we don't find a
13379      enum-specifier.  */
13380   cp_parser_parse_tentatively (parser);
13381
13382   /* Caller guarantees that the current token is 'enum', an identifier
13383      possibly follows, and the token after that is an opening brace.
13384      If we don't have an identifier, fabricate an anonymous name for
13385      the enumeration being defined.  */
13386   cp_lexer_consume_token (parser->lexer);
13387
13388   /* Parse the "class" or "struct", which indicates a scoped
13389      enumeration type in C++0x.  */
13390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13391       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13392     {
13393       if (cxx_dialect < cxx0x)
13394         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13395
13396       /* Consume the `struct' or `class' token.  */
13397       cp_lexer_consume_token (parser->lexer);
13398
13399       scoped_enum_p = true;
13400     }
13401
13402   attributes = cp_parser_attributes_opt (parser);
13403
13404   /* Clear the qualification.  */
13405   parser->scope = NULL_TREE;
13406   parser->qualifying_scope = NULL_TREE;
13407   parser->object_scope = NULL_TREE;
13408
13409   /* Figure out in what scope the declaration is being placed.  */
13410   prev_scope = current_scope ();
13411
13412   type_start_token = cp_lexer_peek_token (parser->lexer);
13413
13414   push_deferring_access_checks (dk_no_check);
13415   nested_name_specifier
13416       = cp_parser_nested_name_specifier_opt (parser,
13417                                              /*typename_keyword_p=*/true,
13418                                              /*check_dependency_p=*/false,
13419                                              /*type_p=*/false,
13420                                              /*is_declaration=*/false);
13421
13422   if (nested_name_specifier)
13423     {
13424       tree name;
13425
13426       identifier = cp_parser_identifier (parser);
13427       name =  cp_parser_lookup_name (parser, identifier,
13428                                      enum_type,
13429                                      /*is_template=*/false,
13430                                      /*is_namespace=*/false,
13431                                      /*check_dependency=*/true,
13432                                      /*ambiguous_decls=*/NULL,
13433                                      input_location);
13434       if (name)
13435         {
13436           type = TREE_TYPE (name);
13437           if (TREE_CODE (type) == TYPENAME_TYPE)
13438             {
13439               /* Are template enums allowed in ISO? */
13440               if (template_parm_scope_p ())
13441                 pedwarn (type_start_token->location, OPT_pedantic,
13442                          "%qD is an enumeration template", name);
13443               /* ignore a typename reference, for it will be solved by name
13444                  in start_enum.  */
13445               type = NULL_TREE;
13446             }
13447         }
13448       else
13449         error_at (type_start_token->location,
13450                   "%qD is not an enumerator-name", identifier);
13451     }
13452   else
13453     {
13454       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13455         identifier = cp_parser_identifier (parser);
13456       else
13457         {
13458           identifier = make_anon_name ();
13459           is_anonymous = true;
13460         }
13461     }
13462   pop_deferring_access_checks ();
13463
13464   /* Check for the `:' that denotes a specified underlying type in C++0x.
13465      Note that a ':' could also indicate a bitfield width, however.  */
13466   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13467     {
13468       cp_decl_specifier_seq type_specifiers;
13469
13470       /* Consume the `:'.  */
13471       cp_lexer_consume_token (parser->lexer);
13472
13473       /* Parse the type-specifier-seq.  */
13474       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13475                                     /*is_trailing_return=*/false,
13476                                     &type_specifiers);
13477
13478       /* At this point this is surely not elaborated type specifier.  */
13479       if (!cp_parser_parse_definitely (parser))
13480         return NULL_TREE;
13481
13482       if (cxx_dialect < cxx0x)
13483         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13484
13485       has_underlying_type = true;
13486
13487       /* If that didn't work, stop.  */
13488       if (type_specifiers.type != error_mark_node)
13489         {
13490           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13491                                             /*initialized=*/0, NULL);
13492           if (underlying_type == error_mark_node)
13493             underlying_type = NULL_TREE;
13494         }
13495     }
13496
13497   /* Look for the `{' but don't consume it yet.  */
13498   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13499     {
13500       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13501         {
13502           cp_parser_error (parser, "expected %<{%>");
13503           if (has_underlying_type)
13504             {
13505               type = NULL_TREE;
13506               goto out;
13507             }
13508         }
13509       /* An opaque-enum-specifier must have a ';' here.  */
13510       if ((scoped_enum_p || underlying_type)
13511           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13512         {
13513           cp_parser_error (parser, "expected %<;%> or %<{%>");
13514           if (has_underlying_type)
13515             {
13516               type = NULL_TREE;
13517               goto out;
13518             }
13519         }
13520     }
13521
13522   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13523     return NULL_TREE;
13524
13525   if (nested_name_specifier)
13526     {
13527       if (CLASS_TYPE_P (nested_name_specifier))
13528         {
13529           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13530           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13531           push_scope (nested_name_specifier);
13532         }
13533       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13534         {
13535           push_nested_namespace (nested_name_specifier);
13536         }
13537     }
13538
13539   /* Issue an error message if type-definitions are forbidden here.  */
13540   if (!cp_parser_check_type_definition (parser))
13541     type = error_mark_node;
13542   else
13543     /* Create the new type.  We do this before consuming the opening
13544        brace so the enum will be recorded as being on the line of its
13545        tag (or the 'enum' keyword, if there is no tag).  */
13546     type = start_enum (identifier, type, underlying_type,
13547                        scoped_enum_p, &is_new_type);
13548
13549   /* If the next token is not '{' it is an opaque-enum-specifier or an
13550      elaborated-type-specifier.  */
13551   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13552     {
13553       if (nested_name_specifier)
13554         {
13555           /* The following catches invalid code such as:
13556              enum class S<int>::E { A, B, C }; */
13557           if (!processing_specialization
13558               && CLASS_TYPE_P (nested_name_specifier)
13559               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13560             error_at (type_start_token->location, "cannot add an enumerator "
13561                       "list to a template instantiation");
13562
13563           /* If that scope does not contain the scope in which the
13564              class was originally declared, the program is invalid.  */
13565           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13566             {
13567               if (at_namespace_scope_p ())
13568                 error_at (type_start_token->location,
13569                           "declaration of %qD in namespace %qD which does not "
13570                           "enclose %qD",
13571                           type, prev_scope, nested_name_specifier);
13572               else
13573                 error_at (type_start_token->location,
13574                           "declaration of %qD in %qD which does not enclose %qD",
13575                           type, prev_scope, nested_name_specifier);
13576               type = error_mark_node;
13577             }
13578         }
13579
13580       if (scoped_enum_p)
13581         begin_scope (sk_scoped_enum, type);
13582
13583       /* Consume the opening brace.  */
13584       cp_lexer_consume_token (parser->lexer);
13585
13586       if (type == error_mark_node)
13587         ; /* Nothing to add */
13588       else if (OPAQUE_ENUM_P (type)
13589                || (cxx_dialect > cxx98 && processing_specialization))
13590         {
13591           new_value_list = true;
13592           SET_OPAQUE_ENUM_P (type, false);
13593           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13594         }
13595       else
13596         {
13597           error_at (type_start_token->location, "multiple definition of %q#T", type);
13598           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13599                     "previous definition here");
13600           type = error_mark_node;
13601         }
13602
13603       if (type == error_mark_node)
13604         cp_parser_skip_to_end_of_block_or_statement (parser);
13605       /* If the next token is not '}', then there are some enumerators.  */
13606       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13607         cp_parser_enumerator_list (parser, type);
13608
13609       /* Consume the final '}'.  */
13610       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13611
13612       if (scoped_enum_p)
13613         finish_scope ();
13614     }
13615   else
13616     {
13617       /* If a ';' follows, then it is an opaque-enum-specifier
13618         and additional restrictions apply.  */
13619       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13620         {
13621           if (is_anonymous)
13622             error_at (type_start_token->location,
13623                       "opaque-enum-specifier without name");
13624           else if (nested_name_specifier)
13625             error_at (type_start_token->location,
13626                       "opaque-enum-specifier must use a simple identifier");
13627         }
13628     }
13629
13630   /* Look for trailing attributes to apply to this enumeration, and
13631      apply them if appropriate.  */
13632   if (cp_parser_allow_gnu_extensions_p (parser))
13633     {
13634       tree trailing_attr = cp_parser_attributes_opt (parser);
13635       trailing_attr = chainon (trailing_attr, attributes);
13636       cplus_decl_attributes (&type,
13637                              trailing_attr,
13638                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13639     }
13640
13641   /* Finish up the enumeration.  */
13642   if (type != error_mark_node)
13643     {
13644       if (new_value_list)
13645         finish_enum_value_list (type);
13646       if (is_new_type)
13647         finish_enum (type);
13648     }
13649
13650   if (nested_name_specifier)
13651     {
13652       if (CLASS_TYPE_P (nested_name_specifier))
13653         {
13654           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13655           pop_scope (nested_name_specifier);
13656         }
13657       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13658         {
13659           pop_nested_namespace (nested_name_specifier);
13660         }
13661     }
13662  out:
13663   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13664   return type;
13665 }
13666
13667 /* Parse an enumerator-list.  The enumerators all have the indicated
13668    TYPE.
13669
13670    enumerator-list:
13671      enumerator-definition
13672      enumerator-list , enumerator-definition  */
13673
13674 static void
13675 cp_parser_enumerator_list (cp_parser* parser, tree type)
13676 {
13677   while (true)
13678     {
13679       /* Parse an enumerator-definition.  */
13680       cp_parser_enumerator_definition (parser, type);
13681
13682       /* If the next token is not a ',', we've reached the end of
13683          the list.  */
13684       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13685         break;
13686       /* Otherwise, consume the `,' and keep going.  */
13687       cp_lexer_consume_token (parser->lexer);
13688       /* If the next token is a `}', there is a trailing comma.  */
13689       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13690         {
13691           if (!in_system_header)
13692             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13693           break;
13694         }
13695     }
13696 }
13697
13698 /* Parse an enumerator-definition.  The enumerator has the indicated
13699    TYPE.
13700
13701    enumerator-definition:
13702      enumerator
13703      enumerator = constant-expression
13704
13705    enumerator:
13706      identifier  */
13707
13708 static void
13709 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13710 {
13711   tree identifier;
13712   tree value;
13713   location_t loc;
13714
13715   /* Save the input location because we are interested in the location
13716      of the identifier and not the location of the explicit value.  */
13717   loc = cp_lexer_peek_token (parser->lexer)->location;
13718
13719   /* Look for the identifier.  */
13720   identifier = cp_parser_identifier (parser);
13721   if (identifier == error_mark_node)
13722     return;
13723
13724   /* If the next token is an '=', then there is an explicit value.  */
13725   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13726     {
13727       /* Consume the `=' token.  */
13728       cp_lexer_consume_token (parser->lexer);
13729       /* Parse the value.  */
13730       value = cp_parser_constant_expression (parser,
13731                                              /*allow_non_constant_p=*/false,
13732                                              NULL);
13733     }
13734   else
13735     value = NULL_TREE;
13736
13737   /* If we are processing a template, make sure the initializer of the
13738      enumerator doesn't contain any bare template parameter pack.  */
13739   if (check_for_bare_parameter_packs (value))
13740     value = error_mark_node;
13741
13742   /* Create the enumerator.  */
13743   build_enumerator (identifier, value, type, loc);
13744 }
13745
13746 /* Parse a namespace-name.
13747
13748    namespace-name:
13749      original-namespace-name
13750      namespace-alias
13751
13752    Returns the NAMESPACE_DECL for the namespace.  */
13753
13754 static tree
13755 cp_parser_namespace_name (cp_parser* parser)
13756 {
13757   tree identifier;
13758   tree namespace_decl;
13759
13760   cp_token *token = cp_lexer_peek_token (parser->lexer);
13761
13762   /* Get the name of the namespace.  */
13763   identifier = cp_parser_identifier (parser);
13764   if (identifier == error_mark_node)
13765     return error_mark_node;
13766
13767   /* Look up the identifier in the currently active scope.  Look only
13768      for namespaces, due to:
13769
13770        [basic.lookup.udir]
13771
13772        When looking up a namespace-name in a using-directive or alias
13773        definition, only namespace names are considered.
13774
13775      And:
13776
13777        [basic.lookup.qual]
13778
13779        During the lookup of a name preceding the :: scope resolution
13780        operator, object, function, and enumerator names are ignored.
13781
13782      (Note that cp_parser_qualifying_entity only calls this
13783      function if the token after the name is the scope resolution
13784      operator.)  */
13785   namespace_decl = cp_parser_lookup_name (parser, identifier,
13786                                           none_type,
13787                                           /*is_template=*/false,
13788                                           /*is_namespace=*/true,
13789                                           /*check_dependency=*/true,
13790                                           /*ambiguous_decls=*/NULL,
13791                                           token->location);
13792   /* If it's not a namespace, issue an error.  */
13793   if (namespace_decl == error_mark_node
13794       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13795     {
13796       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13797         error_at (token->location, "%qD is not a namespace-name", identifier);
13798       cp_parser_error (parser, "expected namespace-name");
13799       namespace_decl = error_mark_node;
13800     }
13801
13802   return namespace_decl;
13803 }
13804
13805 /* Parse a namespace-definition.
13806
13807    namespace-definition:
13808      named-namespace-definition
13809      unnamed-namespace-definition
13810
13811    named-namespace-definition:
13812      original-namespace-definition
13813      extension-namespace-definition
13814
13815    original-namespace-definition:
13816      namespace identifier { namespace-body }
13817
13818    extension-namespace-definition:
13819      namespace original-namespace-name { namespace-body }
13820
13821    unnamed-namespace-definition:
13822      namespace { namespace-body } */
13823
13824 static void
13825 cp_parser_namespace_definition (cp_parser* parser)
13826 {
13827   tree identifier, attribs;
13828   bool has_visibility;
13829   bool is_inline;
13830
13831   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13832     {
13833       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13834       is_inline = true;
13835       cp_lexer_consume_token (parser->lexer);
13836     }
13837   else
13838     is_inline = false;
13839
13840   /* Look for the `namespace' keyword.  */
13841   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13842
13843   /* Get the name of the namespace.  We do not attempt to distinguish
13844      between an original-namespace-definition and an
13845      extension-namespace-definition at this point.  The semantic
13846      analysis routines are responsible for that.  */
13847   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13848     identifier = cp_parser_identifier (parser);
13849   else
13850     identifier = NULL_TREE;
13851
13852   /* Parse any specified attributes.  */
13853   attribs = cp_parser_attributes_opt (parser);
13854
13855   /* Look for the `{' to start the namespace.  */
13856   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13857   /* Start the namespace.  */
13858   push_namespace (identifier);
13859
13860   /* "inline namespace" is equivalent to a stub namespace definition
13861      followed by a strong using directive.  */
13862   if (is_inline)
13863     {
13864       tree name_space = current_namespace;
13865       /* Set up namespace association.  */
13866       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13867         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13868                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13869       /* Import the contents of the inline namespace.  */
13870       pop_namespace ();
13871       do_using_directive (name_space);
13872       push_namespace (identifier);
13873     }
13874
13875   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13876
13877   /* Parse the body of the namespace.  */
13878   cp_parser_namespace_body (parser);
13879
13880   if (has_visibility)
13881     pop_visibility (1);
13882
13883   /* Finish the namespace.  */
13884   pop_namespace ();
13885   /* Look for the final `}'.  */
13886   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13887 }
13888
13889 /* Parse a namespace-body.
13890
13891    namespace-body:
13892      declaration-seq [opt]  */
13893
13894 static void
13895 cp_parser_namespace_body (cp_parser* parser)
13896 {
13897   cp_parser_declaration_seq_opt (parser);
13898 }
13899
13900 /* Parse a namespace-alias-definition.
13901
13902    namespace-alias-definition:
13903      namespace identifier = qualified-namespace-specifier ;  */
13904
13905 static void
13906 cp_parser_namespace_alias_definition (cp_parser* parser)
13907 {
13908   tree identifier;
13909   tree namespace_specifier;
13910
13911   cp_token *token = cp_lexer_peek_token (parser->lexer);
13912
13913   /* Look for the `namespace' keyword.  */
13914   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13915   /* Look for the identifier.  */
13916   identifier = cp_parser_identifier (parser);
13917   if (identifier == error_mark_node)
13918     return;
13919   /* Look for the `=' token.  */
13920   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13921       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13922     {
13923       error_at (token->location, "%<namespace%> definition is not allowed here");
13924       /* Skip the definition.  */
13925       cp_lexer_consume_token (parser->lexer);
13926       if (cp_parser_skip_to_closing_brace (parser))
13927         cp_lexer_consume_token (parser->lexer);
13928       return;
13929     }
13930   cp_parser_require (parser, CPP_EQ, RT_EQ);
13931   /* Look for the qualified-namespace-specifier.  */
13932   namespace_specifier
13933     = cp_parser_qualified_namespace_specifier (parser);
13934   /* Look for the `;' token.  */
13935   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13936
13937   /* Register the alias in the symbol table.  */
13938   do_namespace_alias (identifier, namespace_specifier);
13939 }
13940
13941 /* Parse a qualified-namespace-specifier.
13942
13943    qualified-namespace-specifier:
13944      :: [opt] nested-name-specifier [opt] namespace-name
13945
13946    Returns a NAMESPACE_DECL corresponding to the specified
13947    namespace.  */
13948
13949 static tree
13950 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13951 {
13952   /* Look for the optional `::'.  */
13953   cp_parser_global_scope_opt (parser,
13954                               /*current_scope_valid_p=*/false);
13955
13956   /* Look for the optional nested-name-specifier.  */
13957   cp_parser_nested_name_specifier_opt (parser,
13958                                        /*typename_keyword_p=*/false,
13959                                        /*check_dependency_p=*/true,
13960                                        /*type_p=*/false,
13961                                        /*is_declaration=*/true);
13962
13963   return cp_parser_namespace_name (parser);
13964 }
13965
13966 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13967    access declaration.
13968
13969    using-declaration:
13970      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13971      using :: unqualified-id ;  
13972
13973    access-declaration:
13974      qualified-id ;  
13975
13976    */
13977
13978 static bool
13979 cp_parser_using_declaration (cp_parser* parser, 
13980                              bool access_declaration_p)
13981 {
13982   cp_token *token;
13983   bool typename_p = false;
13984   bool global_scope_p;
13985   tree decl;
13986   tree identifier;
13987   tree qscope;
13988
13989   if (access_declaration_p)
13990     cp_parser_parse_tentatively (parser);
13991   else
13992     {
13993       /* Look for the `using' keyword.  */
13994       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13995       
13996       /* Peek at the next token.  */
13997       token = cp_lexer_peek_token (parser->lexer);
13998       /* See if it's `typename'.  */
13999       if (token->keyword == RID_TYPENAME)
14000         {
14001           /* Remember that we've seen it.  */
14002           typename_p = true;
14003           /* Consume the `typename' token.  */
14004           cp_lexer_consume_token (parser->lexer);
14005         }
14006     }
14007
14008   /* Look for the optional global scope qualification.  */
14009   global_scope_p
14010     = (cp_parser_global_scope_opt (parser,
14011                                    /*current_scope_valid_p=*/false)
14012        != NULL_TREE);
14013
14014   /* If we saw `typename', or didn't see `::', then there must be a
14015      nested-name-specifier present.  */
14016   if (typename_p || !global_scope_p)
14017     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14018                                               /*check_dependency_p=*/true,
14019                                               /*type_p=*/false,
14020                                               /*is_declaration=*/true);
14021   /* Otherwise, we could be in either of the two productions.  In that
14022      case, treat the nested-name-specifier as optional.  */
14023   else
14024     qscope = cp_parser_nested_name_specifier_opt (parser,
14025                                                   /*typename_keyword_p=*/false,
14026                                                   /*check_dependency_p=*/true,
14027                                                   /*type_p=*/false,
14028                                                   /*is_declaration=*/true);
14029   if (!qscope)
14030     qscope = global_namespace;
14031
14032   if (access_declaration_p && cp_parser_error_occurred (parser))
14033     /* Something has already gone wrong; there's no need to parse
14034        further.  Since an error has occurred, the return value of
14035        cp_parser_parse_definitely will be false, as required.  */
14036     return cp_parser_parse_definitely (parser);
14037
14038   token = cp_lexer_peek_token (parser->lexer);
14039   /* Parse the unqualified-id.  */
14040   identifier = cp_parser_unqualified_id (parser,
14041                                          /*template_keyword_p=*/false,
14042                                          /*check_dependency_p=*/true,
14043                                          /*declarator_p=*/true,
14044                                          /*optional_p=*/false);
14045
14046   if (access_declaration_p)
14047     {
14048       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14049         cp_parser_simulate_error (parser);
14050       if (!cp_parser_parse_definitely (parser))
14051         return false;
14052     }
14053
14054   /* The function we call to handle a using-declaration is different
14055      depending on what scope we are in.  */
14056   if (qscope == error_mark_node || identifier == error_mark_node)
14057     ;
14058   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14059            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14060     /* [namespace.udecl]
14061
14062        A using declaration shall not name a template-id.  */
14063     error_at (token->location,
14064               "a template-id may not appear in a using-declaration");
14065   else
14066     {
14067       if (at_class_scope_p ())
14068         {
14069           /* Create the USING_DECL.  */
14070           decl = do_class_using_decl (parser->scope, identifier);
14071
14072           if (check_for_bare_parameter_packs (decl))
14073             return false;
14074           else
14075             /* Add it to the list of members in this class.  */
14076             finish_member_declaration (decl);
14077         }
14078       else
14079         {
14080           decl = cp_parser_lookup_name_simple (parser,
14081                                                identifier,
14082                                                token->location);
14083           if (decl == error_mark_node)
14084             cp_parser_name_lookup_error (parser, identifier,
14085                                          decl, NLE_NULL,
14086                                          token->location);
14087           else if (check_for_bare_parameter_packs (decl))
14088             return false;
14089           else if (!at_namespace_scope_p ())
14090             do_local_using_decl (decl, qscope, identifier);
14091           else
14092             do_toplevel_using_decl (decl, qscope, identifier);
14093         }
14094     }
14095
14096   /* Look for the final `;'.  */
14097   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14098   
14099   return true;
14100 }
14101
14102 /* Parse a using-directive.
14103
14104    using-directive:
14105      using namespace :: [opt] nested-name-specifier [opt]
14106        namespace-name ;  */
14107
14108 static void
14109 cp_parser_using_directive (cp_parser* parser)
14110 {
14111   tree namespace_decl;
14112   tree attribs;
14113
14114   /* Look for the `using' keyword.  */
14115   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14116   /* And the `namespace' keyword.  */
14117   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14118   /* Look for the optional `::' operator.  */
14119   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14120   /* And the optional nested-name-specifier.  */
14121   cp_parser_nested_name_specifier_opt (parser,
14122                                        /*typename_keyword_p=*/false,
14123                                        /*check_dependency_p=*/true,
14124                                        /*type_p=*/false,
14125                                        /*is_declaration=*/true);
14126   /* Get the namespace being used.  */
14127   namespace_decl = cp_parser_namespace_name (parser);
14128   /* And any specified attributes.  */
14129   attribs = cp_parser_attributes_opt (parser);
14130   /* Update the symbol table.  */
14131   parse_using_directive (namespace_decl, attribs);
14132   /* Look for the final `;'.  */
14133   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14134 }
14135
14136 /* Parse an asm-definition.
14137
14138    asm-definition:
14139      asm ( string-literal ) ;
14140
14141    GNU Extension:
14142
14143    asm-definition:
14144      asm volatile [opt] ( string-literal ) ;
14145      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14146      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14147                           : asm-operand-list [opt] ) ;
14148      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14149                           : asm-operand-list [opt]
14150                           : asm-clobber-list [opt] ) ;
14151      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14152                                : asm-clobber-list [opt]
14153                                : asm-goto-list ) ;  */
14154
14155 static void
14156 cp_parser_asm_definition (cp_parser* parser)
14157 {
14158   tree string;
14159   tree outputs = NULL_TREE;
14160   tree inputs = NULL_TREE;
14161   tree clobbers = NULL_TREE;
14162   tree labels = NULL_TREE;
14163   tree asm_stmt;
14164   bool volatile_p = false;
14165   bool extended_p = false;
14166   bool invalid_inputs_p = false;
14167   bool invalid_outputs_p = false;
14168   bool goto_p = false;
14169   required_token missing = RT_NONE;
14170
14171   /* Look for the `asm' keyword.  */
14172   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14173   /* See if the next token is `volatile'.  */
14174   if (cp_parser_allow_gnu_extensions_p (parser)
14175       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14176     {
14177       /* Remember that we saw the `volatile' keyword.  */
14178       volatile_p = true;
14179       /* Consume the token.  */
14180       cp_lexer_consume_token (parser->lexer);
14181     }
14182   if (cp_parser_allow_gnu_extensions_p (parser)
14183       && parser->in_function_body
14184       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14185     {
14186       /* Remember that we saw the `goto' keyword.  */
14187       goto_p = true;
14188       /* Consume the token.  */
14189       cp_lexer_consume_token (parser->lexer);
14190     }
14191   /* Look for the opening `('.  */
14192   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14193     return;
14194   /* Look for the string.  */
14195   string = cp_parser_string_literal (parser, false, false);
14196   if (string == error_mark_node)
14197     {
14198       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14199                                              /*consume_paren=*/true);
14200       return;
14201     }
14202
14203   /* If we're allowing GNU extensions, check for the extended assembly
14204      syntax.  Unfortunately, the `:' tokens need not be separated by
14205      a space in C, and so, for compatibility, we tolerate that here
14206      too.  Doing that means that we have to treat the `::' operator as
14207      two `:' tokens.  */
14208   if (cp_parser_allow_gnu_extensions_p (parser)
14209       && parser->in_function_body
14210       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14211           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14212     {
14213       bool inputs_p = false;
14214       bool clobbers_p = false;
14215       bool labels_p = false;
14216
14217       /* The extended syntax was used.  */
14218       extended_p = true;
14219
14220       /* Look for outputs.  */
14221       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14222         {
14223           /* Consume the `:'.  */
14224           cp_lexer_consume_token (parser->lexer);
14225           /* Parse the output-operands.  */
14226           if (cp_lexer_next_token_is_not (parser->lexer,
14227                                           CPP_COLON)
14228               && cp_lexer_next_token_is_not (parser->lexer,
14229                                              CPP_SCOPE)
14230               && cp_lexer_next_token_is_not (parser->lexer,
14231                                              CPP_CLOSE_PAREN)
14232               && !goto_p)
14233             outputs = cp_parser_asm_operand_list (parser);
14234
14235             if (outputs == error_mark_node)
14236               invalid_outputs_p = true;
14237         }
14238       /* If the next token is `::', there are no outputs, and the
14239          next token is the beginning of the inputs.  */
14240       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14241         /* The inputs are coming next.  */
14242         inputs_p = true;
14243
14244       /* Look for inputs.  */
14245       if (inputs_p
14246           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14247         {
14248           /* Consume the `:' or `::'.  */
14249           cp_lexer_consume_token (parser->lexer);
14250           /* Parse the output-operands.  */
14251           if (cp_lexer_next_token_is_not (parser->lexer,
14252                                           CPP_COLON)
14253               && cp_lexer_next_token_is_not (parser->lexer,
14254                                              CPP_SCOPE)
14255               && cp_lexer_next_token_is_not (parser->lexer,
14256                                              CPP_CLOSE_PAREN))
14257             inputs = cp_parser_asm_operand_list (parser);
14258
14259             if (inputs == error_mark_node)
14260               invalid_inputs_p = true;
14261         }
14262       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14263         /* The clobbers are coming next.  */
14264         clobbers_p = true;
14265
14266       /* Look for clobbers.  */
14267       if (clobbers_p
14268           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14269         {
14270           clobbers_p = true;
14271           /* Consume the `:' or `::'.  */
14272           cp_lexer_consume_token (parser->lexer);
14273           /* Parse the clobbers.  */
14274           if (cp_lexer_next_token_is_not (parser->lexer,
14275                                           CPP_COLON)
14276               && cp_lexer_next_token_is_not (parser->lexer,
14277                                              CPP_CLOSE_PAREN))
14278             clobbers = cp_parser_asm_clobber_list (parser);
14279         }
14280       else if (goto_p
14281                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14282         /* The labels are coming next.  */
14283         labels_p = true;
14284
14285       /* Look for labels.  */
14286       if (labels_p
14287           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14288         {
14289           labels_p = true;
14290           /* Consume the `:' or `::'.  */
14291           cp_lexer_consume_token (parser->lexer);
14292           /* Parse the labels.  */
14293           labels = cp_parser_asm_label_list (parser);
14294         }
14295
14296       if (goto_p && !labels_p)
14297         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14298     }
14299   else if (goto_p)
14300     missing = RT_COLON_SCOPE;
14301
14302   /* Look for the closing `)'.  */
14303   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14304                           missing ? missing : RT_CLOSE_PAREN))
14305     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14306                                            /*consume_paren=*/true);
14307   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14308
14309   if (!invalid_inputs_p && !invalid_outputs_p)
14310     {
14311       /* Create the ASM_EXPR.  */
14312       if (parser->in_function_body)
14313         {
14314           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14315                                       inputs, clobbers, labels);
14316           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14317           if (!extended_p)
14318             {
14319               tree temp = asm_stmt;
14320               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14321                 temp = TREE_OPERAND (temp, 0);
14322
14323               ASM_INPUT_P (temp) = 1;
14324             }
14325         }
14326       else
14327         cgraph_add_asm_node (string);
14328     }
14329 }
14330
14331 /* Declarators [gram.dcl.decl] */
14332
14333 /* Parse an init-declarator.
14334
14335    init-declarator:
14336      declarator initializer [opt]
14337
14338    GNU Extension:
14339
14340    init-declarator:
14341      declarator asm-specification [opt] attributes [opt] initializer [opt]
14342
14343    function-definition:
14344      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14345        function-body
14346      decl-specifier-seq [opt] declarator function-try-block
14347
14348    GNU Extension:
14349
14350    function-definition:
14351      __extension__ function-definition
14352
14353    The DECL_SPECIFIERS apply to this declarator.  Returns a
14354    representation of the entity declared.  If MEMBER_P is TRUE, then
14355    this declarator appears in a class scope.  The new DECL created by
14356    this declarator is returned.
14357
14358    The CHECKS are access checks that should be performed once we know
14359    what entity is being declared (and, therefore, what classes have
14360    befriended it).
14361
14362    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14363    for a function-definition here as well.  If the declarator is a
14364    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14365    be TRUE upon return.  By that point, the function-definition will
14366    have been completely parsed.
14367
14368    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14369    is FALSE.  */
14370
14371 static tree
14372 cp_parser_init_declarator (cp_parser* parser,
14373                            cp_decl_specifier_seq *decl_specifiers,
14374                            VEC (deferred_access_check,gc)* checks,
14375                            bool function_definition_allowed_p,
14376                            bool member_p,
14377                            int declares_class_or_enum,
14378                            bool* function_definition_p)
14379 {
14380   cp_token *token = NULL, *asm_spec_start_token = NULL,
14381            *attributes_start_token = NULL;
14382   cp_declarator *declarator;
14383   tree prefix_attributes;
14384   tree attributes;
14385   tree asm_specification;
14386   tree initializer;
14387   tree decl = NULL_TREE;
14388   tree scope;
14389   int is_initialized;
14390   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14391      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14392      "(...)".  */
14393   enum cpp_ttype initialization_kind;
14394   bool is_direct_init = false;
14395   bool is_non_constant_init;
14396   int ctor_dtor_or_conv_p;
14397   bool friend_p;
14398   tree pushed_scope = NULL;
14399
14400   /* Gather the attributes that were provided with the
14401      decl-specifiers.  */
14402   prefix_attributes = decl_specifiers->attributes;
14403
14404   /* Assume that this is not the declarator for a function
14405      definition.  */
14406   if (function_definition_p)
14407     *function_definition_p = false;
14408
14409   /* Defer access checks while parsing the declarator; we cannot know
14410      what names are accessible until we know what is being
14411      declared.  */
14412   resume_deferring_access_checks ();
14413
14414   /* Parse the declarator.  */
14415   token = cp_lexer_peek_token (parser->lexer);
14416   declarator
14417     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14418                             &ctor_dtor_or_conv_p,
14419                             /*parenthesized_p=*/NULL,
14420                             /*member_p=*/false);
14421   /* Gather up the deferred checks.  */
14422   stop_deferring_access_checks ();
14423
14424   /* If the DECLARATOR was erroneous, there's no need to go
14425      further.  */
14426   if (declarator == cp_error_declarator)
14427     return error_mark_node;
14428
14429   /* Check that the number of template-parameter-lists is OK.  */
14430   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14431                                                        token->location))
14432     return error_mark_node;
14433
14434   if (declares_class_or_enum & 2)
14435     cp_parser_check_for_definition_in_return_type (declarator,
14436                                                    decl_specifiers->type,
14437                                                    decl_specifiers->type_location);
14438
14439   /* Figure out what scope the entity declared by the DECLARATOR is
14440      located in.  `grokdeclarator' sometimes changes the scope, so
14441      we compute it now.  */
14442   scope = get_scope_of_declarator (declarator);
14443
14444   /* Perform any lookups in the declared type which were thought to be
14445      dependent, but are not in the scope of the declarator.  */
14446   decl_specifiers->type
14447     = maybe_update_decl_type (decl_specifiers->type, scope);
14448
14449   /* If we're allowing GNU extensions, look for an asm-specification
14450      and attributes.  */
14451   if (cp_parser_allow_gnu_extensions_p (parser))
14452     {
14453       /* Look for an asm-specification.  */
14454       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14455       asm_specification = cp_parser_asm_specification_opt (parser);
14456       /* And attributes.  */
14457       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14458       attributes = cp_parser_attributes_opt (parser);
14459     }
14460   else
14461     {
14462       asm_specification = NULL_TREE;
14463       attributes = NULL_TREE;
14464     }
14465
14466   /* Peek at the next token.  */
14467   token = cp_lexer_peek_token (parser->lexer);
14468   /* Check to see if the token indicates the start of a
14469      function-definition.  */
14470   if (function_declarator_p (declarator)
14471       && cp_parser_token_starts_function_definition_p (token))
14472     {
14473       if (!function_definition_allowed_p)
14474         {
14475           /* If a function-definition should not appear here, issue an
14476              error message.  */
14477           cp_parser_error (parser,
14478                            "a function-definition is not allowed here");
14479           return error_mark_node;
14480         }
14481       else
14482         {
14483           location_t func_brace_location
14484             = cp_lexer_peek_token (parser->lexer)->location;
14485
14486           /* Neither attributes nor an asm-specification are allowed
14487              on a function-definition.  */
14488           if (asm_specification)
14489             error_at (asm_spec_start_token->location,
14490                       "an asm-specification is not allowed "
14491                       "on a function-definition");
14492           if (attributes)
14493             error_at (attributes_start_token->location,
14494                       "attributes are not allowed on a function-definition");
14495           /* This is a function-definition.  */
14496           *function_definition_p = true;
14497
14498           /* Parse the function definition.  */
14499           if (member_p)
14500             decl = cp_parser_save_member_function_body (parser,
14501                                                         decl_specifiers,
14502                                                         declarator,
14503                                                         prefix_attributes);
14504           else
14505             decl
14506               = (cp_parser_function_definition_from_specifiers_and_declarator
14507                  (parser, decl_specifiers, prefix_attributes, declarator));
14508
14509           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14510             {
14511               /* This is where the prologue starts...  */
14512               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14513                 = func_brace_location;
14514             }
14515
14516           return decl;
14517         }
14518     }
14519
14520   /* [dcl.dcl]
14521
14522      Only in function declarations for constructors, destructors, and
14523      type conversions can the decl-specifier-seq be omitted.
14524
14525      We explicitly postpone this check past the point where we handle
14526      function-definitions because we tolerate function-definitions
14527      that are missing their return types in some modes.  */
14528   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14529     {
14530       cp_parser_error (parser,
14531                        "expected constructor, destructor, or type conversion");
14532       return error_mark_node;
14533     }
14534
14535   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14536   if (token->type == CPP_EQ
14537       || token->type == CPP_OPEN_PAREN
14538       || token->type == CPP_OPEN_BRACE)
14539     {
14540       is_initialized = SD_INITIALIZED;
14541       initialization_kind = token->type;
14542
14543       if (token->type == CPP_EQ
14544           && function_declarator_p (declarator))
14545         {
14546           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14547           if (t2->keyword == RID_DEFAULT)
14548             is_initialized = SD_DEFAULTED;
14549           else if (t2->keyword == RID_DELETE)
14550             is_initialized = SD_DELETED;
14551         }
14552     }
14553   else
14554     {
14555       /* If the init-declarator isn't initialized and isn't followed by a
14556          `,' or `;', it's not a valid init-declarator.  */
14557       if (token->type != CPP_COMMA
14558           && token->type != CPP_SEMICOLON)
14559         {
14560           cp_parser_error (parser, "expected initializer");
14561           return error_mark_node;
14562         }
14563       is_initialized = SD_UNINITIALIZED;
14564       initialization_kind = CPP_EOF;
14565     }
14566
14567   /* Because start_decl has side-effects, we should only call it if we
14568      know we're going ahead.  By this point, we know that we cannot
14569      possibly be looking at any other construct.  */
14570   cp_parser_commit_to_tentative_parse (parser);
14571
14572   /* If the decl specifiers were bad, issue an error now that we're
14573      sure this was intended to be a declarator.  Then continue
14574      declaring the variable(s), as int, to try to cut down on further
14575      errors.  */
14576   if (decl_specifiers->any_specifiers_p
14577       && decl_specifiers->type == error_mark_node)
14578     {
14579       cp_parser_error (parser, "invalid type in declaration");
14580       decl_specifiers->type = integer_type_node;
14581     }
14582
14583   /* Check to see whether or not this declaration is a friend.  */
14584   friend_p = cp_parser_friend_p (decl_specifiers);
14585
14586   /* Enter the newly declared entry in the symbol table.  If we're
14587      processing a declaration in a class-specifier, we wait until
14588      after processing the initializer.  */
14589   if (!member_p)
14590     {
14591       if (parser->in_unbraced_linkage_specification_p)
14592         decl_specifiers->storage_class = sc_extern;
14593       decl = start_decl (declarator, decl_specifiers,
14594                          is_initialized, attributes, prefix_attributes,
14595                          &pushed_scope);
14596       /* Adjust location of decl if declarator->id_loc is more appropriate:
14597          set, and decl wasn't merged with another decl, in which case its
14598          location would be different from input_location, and more accurate.  */
14599       if (DECL_P (decl)
14600           && declarator->id_loc != UNKNOWN_LOCATION
14601           && DECL_SOURCE_LOCATION (decl) == input_location)
14602         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14603     }
14604   else if (scope)
14605     /* Enter the SCOPE.  That way unqualified names appearing in the
14606        initializer will be looked up in SCOPE.  */
14607     pushed_scope = push_scope (scope);
14608
14609   /* Perform deferred access control checks, now that we know in which
14610      SCOPE the declared entity resides.  */
14611   if (!member_p && decl)
14612     {
14613       tree saved_current_function_decl = NULL_TREE;
14614
14615       /* If the entity being declared is a function, pretend that we
14616          are in its scope.  If it is a `friend', it may have access to
14617          things that would not otherwise be accessible.  */
14618       if (TREE_CODE (decl) == FUNCTION_DECL)
14619         {
14620           saved_current_function_decl = current_function_decl;
14621           current_function_decl = decl;
14622         }
14623
14624       /* Perform access checks for template parameters.  */
14625       cp_parser_perform_template_parameter_access_checks (checks);
14626
14627       /* Perform the access control checks for the declarator and the
14628          decl-specifiers.  */
14629       perform_deferred_access_checks ();
14630
14631       /* Restore the saved value.  */
14632       if (TREE_CODE (decl) == FUNCTION_DECL)
14633         current_function_decl = saved_current_function_decl;
14634     }
14635
14636   /* Parse the initializer.  */
14637   initializer = NULL_TREE;
14638   is_direct_init = false;
14639   is_non_constant_init = true;
14640   if (is_initialized)
14641     {
14642       if (function_declarator_p (declarator))
14643         {
14644           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14645            if (initialization_kind == CPP_EQ)
14646              initializer = cp_parser_pure_specifier (parser);
14647            else
14648              {
14649                /* If the declaration was erroneous, we don't really
14650                   know what the user intended, so just silently
14651                   consume the initializer.  */
14652                if (decl != error_mark_node)
14653                  error_at (initializer_start_token->location,
14654                            "initializer provided for function");
14655                cp_parser_skip_to_closing_parenthesis (parser,
14656                                                       /*recovering=*/true,
14657                                                       /*or_comma=*/false,
14658                                                       /*consume_paren=*/true);
14659              }
14660         }
14661       else
14662         {
14663           /* We want to record the extra mangling scope for in-class
14664              initializers of class members and initializers of static data
14665              member templates.  The former is a C++0x feature which isn't
14666              implemented yet, and I expect it will involve deferring
14667              parsing of the initializer until end of class as with default
14668              arguments.  So right here we only handle the latter.  */
14669           if (!member_p && processing_template_decl)
14670             start_lambda_scope (decl);
14671           initializer = cp_parser_initializer (parser,
14672                                                &is_direct_init,
14673                                                &is_non_constant_init);
14674           if (!member_p && processing_template_decl)
14675             finish_lambda_scope ();
14676         }
14677     }
14678
14679   /* The old parser allows attributes to appear after a parenthesized
14680      initializer.  Mark Mitchell proposed removing this functionality
14681      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14682      attributes -- but ignores them.  */
14683   if (cp_parser_allow_gnu_extensions_p (parser)
14684       && initialization_kind == CPP_OPEN_PAREN)
14685     if (cp_parser_attributes_opt (parser))
14686       warning (OPT_Wattributes,
14687                "attributes after parenthesized initializer ignored");
14688
14689   /* For an in-class declaration, use `grokfield' to create the
14690      declaration.  */
14691   if (member_p)
14692     {
14693       if (pushed_scope)
14694         {
14695           pop_scope (pushed_scope);
14696           pushed_scope = false;
14697         }
14698       decl = grokfield (declarator, decl_specifiers,
14699                         initializer, !is_non_constant_init,
14700                         /*asmspec=*/NULL_TREE,
14701                         prefix_attributes);
14702       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14703         cp_parser_save_default_args (parser, decl);
14704     }
14705
14706   /* Finish processing the declaration.  But, skip friend
14707      declarations.  */
14708   if (!friend_p && decl && decl != error_mark_node)
14709     {
14710       cp_finish_decl (decl,
14711                       initializer, !is_non_constant_init,
14712                       asm_specification,
14713                       /* If the initializer is in parentheses, then this is
14714                          a direct-initialization, which means that an
14715                          `explicit' constructor is OK.  Otherwise, an
14716                          `explicit' constructor cannot be used.  */
14717                       ((is_direct_init || !is_initialized)
14718                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14719     }
14720   else if ((cxx_dialect != cxx98) && friend_p
14721            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14722     /* Core issue #226 (C++0x only): A default template-argument
14723        shall not be specified in a friend class template
14724        declaration. */
14725     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14726                              /*is_partial=*/0, /*is_friend_decl=*/1);
14727
14728   if (!friend_p && pushed_scope)
14729     pop_scope (pushed_scope);
14730
14731   return decl;
14732 }
14733
14734 /* Parse a declarator.
14735
14736    declarator:
14737      direct-declarator
14738      ptr-operator declarator
14739
14740    abstract-declarator:
14741      ptr-operator abstract-declarator [opt]
14742      direct-abstract-declarator
14743
14744    GNU Extensions:
14745
14746    declarator:
14747      attributes [opt] direct-declarator
14748      attributes [opt] ptr-operator declarator
14749
14750    abstract-declarator:
14751      attributes [opt] ptr-operator abstract-declarator [opt]
14752      attributes [opt] direct-abstract-declarator
14753
14754    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14755    detect constructor, destructor or conversion operators. It is set
14756    to -1 if the declarator is a name, and +1 if it is a
14757    function. Otherwise it is set to zero. Usually you just want to
14758    test for >0, but internally the negative value is used.
14759
14760    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14761    a decl-specifier-seq unless it declares a constructor, destructor,
14762    or conversion.  It might seem that we could check this condition in
14763    semantic analysis, rather than parsing, but that makes it difficult
14764    to handle something like `f()'.  We want to notice that there are
14765    no decl-specifiers, and therefore realize that this is an
14766    expression, not a declaration.)
14767
14768    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14769    the declarator is a direct-declarator of the form "(...)".
14770
14771    MEMBER_P is true iff this declarator is a member-declarator.  */
14772
14773 static cp_declarator *
14774 cp_parser_declarator (cp_parser* parser,
14775                       cp_parser_declarator_kind dcl_kind,
14776                       int* ctor_dtor_or_conv_p,
14777                       bool* parenthesized_p,
14778                       bool member_p)
14779 {
14780   cp_declarator *declarator;
14781   enum tree_code code;
14782   cp_cv_quals cv_quals;
14783   tree class_type;
14784   tree attributes = NULL_TREE;
14785
14786   /* Assume this is not a constructor, destructor, or type-conversion
14787      operator.  */
14788   if (ctor_dtor_or_conv_p)
14789     *ctor_dtor_or_conv_p = 0;
14790
14791   if (cp_parser_allow_gnu_extensions_p (parser))
14792     attributes = cp_parser_attributes_opt (parser);
14793
14794   /* Check for the ptr-operator production.  */
14795   cp_parser_parse_tentatively (parser);
14796   /* Parse the ptr-operator.  */
14797   code = cp_parser_ptr_operator (parser,
14798                                  &class_type,
14799                                  &cv_quals);
14800   /* If that worked, then we have a ptr-operator.  */
14801   if (cp_parser_parse_definitely (parser))
14802     {
14803       /* If a ptr-operator was found, then this declarator was not
14804          parenthesized.  */
14805       if (parenthesized_p)
14806         *parenthesized_p = true;
14807       /* The dependent declarator is optional if we are parsing an
14808          abstract-declarator.  */
14809       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14810         cp_parser_parse_tentatively (parser);
14811
14812       /* Parse the dependent declarator.  */
14813       declarator = cp_parser_declarator (parser, dcl_kind,
14814                                          /*ctor_dtor_or_conv_p=*/NULL,
14815                                          /*parenthesized_p=*/NULL,
14816                                          /*member_p=*/false);
14817
14818       /* If we are parsing an abstract-declarator, we must handle the
14819          case where the dependent declarator is absent.  */
14820       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14821           && !cp_parser_parse_definitely (parser))
14822         declarator = NULL;
14823
14824       declarator = cp_parser_make_indirect_declarator
14825         (code, class_type, cv_quals, declarator);
14826     }
14827   /* Everything else is a direct-declarator.  */
14828   else
14829     {
14830       if (parenthesized_p)
14831         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14832                                                    CPP_OPEN_PAREN);
14833       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14834                                                 ctor_dtor_or_conv_p,
14835                                                 member_p);
14836     }
14837
14838   if (attributes && declarator && declarator != cp_error_declarator)
14839     declarator->attributes = attributes;
14840
14841   return declarator;
14842 }
14843
14844 /* Parse a direct-declarator or direct-abstract-declarator.
14845
14846    direct-declarator:
14847      declarator-id
14848      direct-declarator ( parameter-declaration-clause )
14849        cv-qualifier-seq [opt]
14850        exception-specification [opt]
14851      direct-declarator [ constant-expression [opt] ]
14852      ( declarator )
14853
14854    direct-abstract-declarator:
14855      direct-abstract-declarator [opt]
14856        ( parameter-declaration-clause )
14857        cv-qualifier-seq [opt]
14858        exception-specification [opt]
14859      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14860      ( abstract-declarator )
14861
14862    Returns a representation of the declarator.  DCL_KIND is
14863    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14864    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14865    we are parsing a direct-declarator.  It is
14866    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14867    of ambiguity we prefer an abstract declarator, as per
14868    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14869    cp_parser_declarator.  */
14870
14871 static cp_declarator *
14872 cp_parser_direct_declarator (cp_parser* parser,
14873                              cp_parser_declarator_kind dcl_kind,
14874                              int* ctor_dtor_or_conv_p,
14875                              bool member_p)
14876 {
14877   cp_token *token;
14878   cp_declarator *declarator = NULL;
14879   tree scope = NULL_TREE;
14880   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14881   bool saved_in_declarator_p = parser->in_declarator_p;
14882   bool first = true;
14883   tree pushed_scope = NULL_TREE;
14884
14885   while (true)
14886     {
14887       /* Peek at the next token.  */
14888       token = cp_lexer_peek_token (parser->lexer);
14889       if (token->type == CPP_OPEN_PAREN)
14890         {
14891           /* This is either a parameter-declaration-clause, or a
14892              parenthesized declarator. When we know we are parsing a
14893              named declarator, it must be a parenthesized declarator
14894              if FIRST is true. For instance, `(int)' is a
14895              parameter-declaration-clause, with an omitted
14896              direct-abstract-declarator. But `((*))', is a
14897              parenthesized abstract declarator. Finally, when T is a
14898              template parameter `(T)' is a
14899              parameter-declaration-clause, and not a parenthesized
14900              named declarator.
14901
14902              We first try and parse a parameter-declaration-clause,
14903              and then try a nested declarator (if FIRST is true).
14904
14905              It is not an error for it not to be a
14906              parameter-declaration-clause, even when FIRST is
14907              false. Consider,
14908
14909                int i (int);
14910                int i (3);
14911
14912              The first is the declaration of a function while the
14913              second is the definition of a variable, including its
14914              initializer.
14915
14916              Having seen only the parenthesis, we cannot know which of
14917              these two alternatives should be selected.  Even more
14918              complex are examples like:
14919
14920                int i (int (a));
14921                int i (int (3));
14922
14923              The former is a function-declaration; the latter is a
14924              variable initialization.
14925
14926              Thus again, we try a parameter-declaration-clause, and if
14927              that fails, we back out and return.  */
14928
14929           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14930             {
14931               tree params;
14932               unsigned saved_num_template_parameter_lists;
14933               bool is_declarator = false;
14934               tree t;
14935
14936               /* In a member-declarator, the only valid interpretation
14937                  of a parenthesis is the start of a
14938                  parameter-declaration-clause.  (It is invalid to
14939                  initialize a static data member with a parenthesized
14940                  initializer; only the "=" form of initialization is
14941                  permitted.)  */
14942               if (!member_p)
14943                 cp_parser_parse_tentatively (parser);
14944
14945               /* Consume the `('.  */
14946               cp_lexer_consume_token (parser->lexer);
14947               if (first)
14948                 {
14949                   /* If this is going to be an abstract declarator, we're
14950                      in a declarator and we can't have default args.  */
14951                   parser->default_arg_ok_p = false;
14952                   parser->in_declarator_p = true;
14953                 }
14954
14955               /* Inside the function parameter list, surrounding
14956                  template-parameter-lists do not apply.  */
14957               saved_num_template_parameter_lists
14958                 = parser->num_template_parameter_lists;
14959               parser->num_template_parameter_lists = 0;
14960
14961               begin_scope (sk_function_parms, NULL_TREE);
14962
14963               /* Parse the parameter-declaration-clause.  */
14964               params = cp_parser_parameter_declaration_clause (parser);
14965
14966               parser->num_template_parameter_lists
14967                 = saved_num_template_parameter_lists;
14968
14969               /* If all went well, parse the cv-qualifier-seq and the
14970                  exception-specification.  */
14971               if (member_p || cp_parser_parse_definitely (parser))
14972                 {
14973                   cp_cv_quals cv_quals;
14974                   tree exception_specification;
14975                   tree late_return;
14976
14977                   is_declarator = true;
14978
14979                   if (ctor_dtor_or_conv_p)
14980                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14981                   first = false;
14982                   /* Consume the `)'.  */
14983                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14984
14985                   /* Parse the cv-qualifier-seq.  */
14986                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14987                   /* And the exception-specification.  */
14988                   exception_specification
14989                     = cp_parser_exception_specification_opt (parser);
14990
14991                   late_return
14992                     = cp_parser_late_return_type_opt (parser);
14993
14994                   /* Create the function-declarator.  */
14995                   declarator = make_call_declarator (declarator,
14996                                                      params,
14997                                                      cv_quals,
14998                                                      exception_specification,
14999                                                      late_return);
15000                   /* Any subsequent parameter lists are to do with
15001                      return type, so are not those of the declared
15002                      function.  */
15003                   parser->default_arg_ok_p = false;
15004                 }
15005
15006               /* Remove the function parms from scope.  */
15007               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15008                 pop_binding (DECL_NAME (t), t);
15009               leave_scope();
15010
15011               if (is_declarator)
15012                 /* Repeat the main loop.  */
15013                 continue;
15014             }
15015
15016           /* If this is the first, we can try a parenthesized
15017              declarator.  */
15018           if (first)
15019             {
15020               bool saved_in_type_id_in_expr_p;
15021
15022               parser->default_arg_ok_p = saved_default_arg_ok_p;
15023               parser->in_declarator_p = saved_in_declarator_p;
15024
15025               /* Consume the `('.  */
15026               cp_lexer_consume_token (parser->lexer);
15027               /* Parse the nested declarator.  */
15028               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15029               parser->in_type_id_in_expr_p = true;
15030               declarator
15031                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15032                                         /*parenthesized_p=*/NULL,
15033                                         member_p);
15034               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15035               first = false;
15036               /* Expect a `)'.  */
15037               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15038                 declarator = cp_error_declarator;
15039               if (declarator == cp_error_declarator)
15040                 break;
15041
15042               goto handle_declarator;
15043             }
15044           /* Otherwise, we must be done.  */
15045           else
15046             break;
15047         }
15048       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15049                && token->type == CPP_OPEN_SQUARE)
15050         {
15051           /* Parse an array-declarator.  */
15052           tree bounds;
15053
15054           if (ctor_dtor_or_conv_p)
15055             *ctor_dtor_or_conv_p = 0;
15056
15057           first = false;
15058           parser->default_arg_ok_p = false;
15059           parser->in_declarator_p = true;
15060           /* Consume the `['.  */
15061           cp_lexer_consume_token (parser->lexer);
15062           /* Peek at the next token.  */
15063           token = cp_lexer_peek_token (parser->lexer);
15064           /* If the next token is `]', then there is no
15065              constant-expression.  */
15066           if (token->type != CPP_CLOSE_SQUARE)
15067             {
15068               bool non_constant_p;
15069
15070               bounds
15071                 = cp_parser_constant_expression (parser,
15072                                                  /*allow_non_constant=*/true,
15073                                                  &non_constant_p);
15074               if (!non_constant_p || cxx_dialect >= cxx0x)
15075                 /* OK */;
15076               /* Normally, the array bound must be an integral constant
15077                  expression.  However, as an extension, we allow VLAs
15078                  in function scopes as long as they aren't part of a
15079                  parameter declaration.  */
15080               else if (!parser->in_function_body
15081                        || current_binding_level->kind == sk_function_parms)
15082                 {
15083                   cp_parser_error (parser,
15084                                    "array bound is not an integer constant");
15085                   bounds = error_mark_node;
15086                 }
15087               else if (processing_template_decl && !error_operand_p (bounds))
15088                 {
15089                   /* Remember this wasn't a constant-expression.  */
15090                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15091                   TREE_SIDE_EFFECTS (bounds) = 1;
15092                 }
15093             }
15094           else
15095             bounds = NULL_TREE;
15096           /* Look for the closing `]'.  */
15097           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15098             {
15099               declarator = cp_error_declarator;
15100               break;
15101             }
15102
15103           declarator = make_array_declarator (declarator, bounds);
15104         }
15105       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15106         {
15107           {
15108             tree qualifying_scope;
15109             tree unqualified_name;
15110             special_function_kind sfk;
15111             bool abstract_ok;
15112             bool pack_expansion_p = false;
15113             cp_token *declarator_id_start_token;
15114
15115             /* Parse a declarator-id */
15116             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15117             if (abstract_ok)
15118               {
15119                 cp_parser_parse_tentatively (parser);
15120
15121                 /* If we see an ellipsis, we should be looking at a
15122                    parameter pack. */
15123                 if (token->type == CPP_ELLIPSIS)
15124                   {
15125                     /* Consume the `...' */
15126                     cp_lexer_consume_token (parser->lexer);
15127
15128                     pack_expansion_p = true;
15129                   }
15130               }
15131
15132             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15133             unqualified_name
15134               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15135             qualifying_scope = parser->scope;
15136             if (abstract_ok)
15137               {
15138                 bool okay = false;
15139
15140                 if (!unqualified_name && pack_expansion_p)
15141                   {
15142                     /* Check whether an error occurred. */
15143                     okay = !cp_parser_error_occurred (parser);
15144
15145                     /* We already consumed the ellipsis to mark a
15146                        parameter pack, but we have no way to report it,
15147                        so abort the tentative parse. We will be exiting
15148                        immediately anyway. */
15149                     cp_parser_abort_tentative_parse (parser);
15150                   }
15151                 else
15152                   okay = cp_parser_parse_definitely (parser);
15153
15154                 if (!okay)
15155                   unqualified_name = error_mark_node;
15156                 else if (unqualified_name
15157                          && (qualifying_scope
15158                              || (TREE_CODE (unqualified_name)
15159                                  != IDENTIFIER_NODE)))
15160                   {
15161                     cp_parser_error (parser, "expected unqualified-id");
15162                     unqualified_name = error_mark_node;
15163                   }
15164               }
15165
15166             if (!unqualified_name)
15167               return NULL;
15168             if (unqualified_name == error_mark_node)
15169               {
15170                 declarator = cp_error_declarator;
15171                 pack_expansion_p = false;
15172                 declarator->parameter_pack_p = false;
15173                 break;
15174               }
15175
15176             if (qualifying_scope && at_namespace_scope_p ()
15177                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15178               {
15179                 /* In the declaration of a member of a template class
15180                    outside of the class itself, the SCOPE will sometimes
15181                    be a TYPENAME_TYPE.  For example, given:
15182
15183                    template <typename T>
15184                    int S<T>::R::i = 3;
15185
15186                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15187                    this context, we must resolve S<T>::R to an ordinary
15188                    type, rather than a typename type.
15189
15190                    The reason we normally avoid resolving TYPENAME_TYPEs
15191                    is that a specialization of `S' might render
15192                    `S<T>::R' not a type.  However, if `S' is
15193                    specialized, then this `i' will not be used, so there
15194                    is no harm in resolving the types here.  */
15195                 tree type;
15196
15197                 /* Resolve the TYPENAME_TYPE.  */
15198                 type = resolve_typename_type (qualifying_scope,
15199                                               /*only_current_p=*/false);
15200                 /* If that failed, the declarator is invalid.  */
15201                 if (TREE_CODE (type) == TYPENAME_TYPE)
15202                   {
15203                     if (typedef_variant_p (type))
15204                       error_at (declarator_id_start_token->location,
15205                                 "cannot define member of dependent typedef "
15206                                 "%qT", type);
15207                     else
15208                       error_at (declarator_id_start_token->location,
15209                                 "%<%T::%E%> is not a type",
15210                                 TYPE_CONTEXT (qualifying_scope),
15211                                 TYPE_IDENTIFIER (qualifying_scope));
15212                   }
15213                 qualifying_scope = type;
15214               }
15215
15216             sfk = sfk_none;
15217
15218             if (unqualified_name)
15219               {
15220                 tree class_type;
15221
15222                 if (qualifying_scope
15223                     && CLASS_TYPE_P (qualifying_scope))
15224                   class_type = qualifying_scope;
15225                 else
15226                   class_type = current_class_type;
15227
15228                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15229                   {
15230                     tree name_type = TREE_TYPE (unqualified_name);
15231                     if (class_type && same_type_p (name_type, class_type))
15232                       {
15233                         if (qualifying_scope
15234                             && CLASSTYPE_USE_TEMPLATE (name_type))
15235                           {
15236                             error_at (declarator_id_start_token->location,
15237                                       "invalid use of constructor as a template");
15238                             inform (declarator_id_start_token->location,
15239                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15240                                     "name the constructor in a qualified name",
15241                                     class_type,
15242                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15243                                     class_type, name_type);
15244                             declarator = cp_error_declarator;
15245                             break;
15246                           }
15247                         else
15248                           unqualified_name = constructor_name (class_type);
15249                       }
15250                     else
15251                       {
15252                         /* We do not attempt to print the declarator
15253                            here because we do not have enough
15254                            information about its original syntactic
15255                            form.  */
15256                         cp_parser_error (parser, "invalid declarator");
15257                         declarator = cp_error_declarator;
15258                         break;
15259                       }
15260                   }
15261
15262                 if (class_type)
15263                   {
15264                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15265                       sfk = sfk_destructor;
15266                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15267                       sfk = sfk_conversion;
15268                     else if (/* There's no way to declare a constructor
15269                                 for an anonymous type, even if the type
15270                                 got a name for linkage purposes.  */
15271                              !TYPE_WAS_ANONYMOUS (class_type)
15272                              && constructor_name_p (unqualified_name,
15273                                                     class_type))
15274                       {
15275                         unqualified_name = constructor_name (class_type);
15276                         sfk = sfk_constructor;
15277                       }
15278                     else if (is_overloaded_fn (unqualified_name)
15279                              && DECL_CONSTRUCTOR_P (get_first_fn
15280                                                     (unqualified_name)))
15281                       sfk = sfk_constructor;
15282
15283                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15284                       *ctor_dtor_or_conv_p = -1;
15285                   }
15286               }
15287             declarator = make_id_declarator (qualifying_scope,
15288                                              unqualified_name,
15289                                              sfk);
15290             declarator->id_loc = token->location;
15291             declarator->parameter_pack_p = pack_expansion_p;
15292
15293             if (pack_expansion_p)
15294               maybe_warn_variadic_templates ();
15295           }
15296
15297         handle_declarator:;
15298           scope = get_scope_of_declarator (declarator);
15299           if (scope)
15300             /* Any names that appear after the declarator-id for a
15301                member are looked up in the containing scope.  */
15302             pushed_scope = push_scope (scope);
15303           parser->in_declarator_p = true;
15304           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15305               || (declarator && declarator->kind == cdk_id))
15306             /* Default args are only allowed on function
15307                declarations.  */
15308             parser->default_arg_ok_p = saved_default_arg_ok_p;
15309           else
15310             parser->default_arg_ok_p = false;
15311
15312           first = false;
15313         }
15314       /* We're done.  */
15315       else
15316         break;
15317     }
15318
15319   /* For an abstract declarator, we might wind up with nothing at this
15320      point.  That's an error; the declarator is not optional.  */
15321   if (!declarator)
15322     cp_parser_error (parser, "expected declarator");
15323
15324   /* If we entered a scope, we must exit it now.  */
15325   if (pushed_scope)
15326     pop_scope (pushed_scope);
15327
15328   parser->default_arg_ok_p = saved_default_arg_ok_p;
15329   parser->in_declarator_p = saved_in_declarator_p;
15330
15331   return declarator;
15332 }
15333
15334 /* Parse a ptr-operator.
15335
15336    ptr-operator:
15337      * cv-qualifier-seq [opt]
15338      &
15339      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15340
15341    GNU Extension:
15342
15343    ptr-operator:
15344      & cv-qualifier-seq [opt]
15345
15346    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15347    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15348    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15349    filled in with the TYPE containing the member.  *CV_QUALS is
15350    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15351    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15352    Note that the tree codes returned by this function have nothing
15353    to do with the types of trees that will be eventually be created
15354    to represent the pointer or reference type being parsed. They are
15355    just constants with suggestive names. */
15356 static enum tree_code
15357 cp_parser_ptr_operator (cp_parser* parser,
15358                         tree* type,
15359                         cp_cv_quals *cv_quals)
15360 {
15361   enum tree_code code = ERROR_MARK;
15362   cp_token *token;
15363
15364   /* Assume that it's not a pointer-to-member.  */
15365   *type = NULL_TREE;
15366   /* And that there are no cv-qualifiers.  */
15367   *cv_quals = TYPE_UNQUALIFIED;
15368
15369   /* Peek at the next token.  */
15370   token = cp_lexer_peek_token (parser->lexer);
15371
15372   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15373   if (token->type == CPP_MULT)
15374     code = INDIRECT_REF;
15375   else if (token->type == CPP_AND)
15376     code = ADDR_EXPR;
15377   else if ((cxx_dialect != cxx98) &&
15378            token->type == CPP_AND_AND) /* C++0x only */
15379     code = NON_LVALUE_EXPR;
15380
15381   if (code != ERROR_MARK)
15382     {
15383       /* Consume the `*', `&' or `&&'.  */
15384       cp_lexer_consume_token (parser->lexer);
15385
15386       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15387          `&', if we are allowing GNU extensions.  (The only qualifier
15388          that can legally appear after `&' is `restrict', but that is
15389          enforced during semantic analysis.  */
15390       if (code == INDIRECT_REF
15391           || cp_parser_allow_gnu_extensions_p (parser))
15392         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15393     }
15394   else
15395     {
15396       /* Try the pointer-to-member case.  */
15397       cp_parser_parse_tentatively (parser);
15398       /* Look for the optional `::' operator.  */
15399       cp_parser_global_scope_opt (parser,
15400                                   /*current_scope_valid_p=*/false);
15401       /* Look for the nested-name specifier.  */
15402       token = cp_lexer_peek_token (parser->lexer);
15403       cp_parser_nested_name_specifier (parser,
15404                                        /*typename_keyword_p=*/false,
15405                                        /*check_dependency_p=*/true,
15406                                        /*type_p=*/false,
15407                                        /*is_declaration=*/false);
15408       /* If we found it, and the next token is a `*', then we are
15409          indeed looking at a pointer-to-member operator.  */
15410       if (!cp_parser_error_occurred (parser)
15411           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15412         {
15413           /* Indicate that the `*' operator was used.  */
15414           code = INDIRECT_REF;
15415
15416           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15417             error_at (token->location, "%qD is a namespace", parser->scope);
15418           else
15419             {
15420               /* The type of which the member is a member is given by the
15421                  current SCOPE.  */
15422               *type = parser->scope;
15423               /* The next name will not be qualified.  */
15424               parser->scope = NULL_TREE;
15425               parser->qualifying_scope = NULL_TREE;
15426               parser->object_scope = NULL_TREE;
15427               /* Look for the optional cv-qualifier-seq.  */
15428               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15429             }
15430         }
15431       /* If that didn't work we don't have a ptr-operator.  */
15432       if (!cp_parser_parse_definitely (parser))
15433         cp_parser_error (parser, "expected ptr-operator");
15434     }
15435
15436   return code;
15437 }
15438
15439 /* Parse an (optional) cv-qualifier-seq.
15440
15441    cv-qualifier-seq:
15442      cv-qualifier cv-qualifier-seq [opt]
15443
15444    cv-qualifier:
15445      const
15446      volatile
15447
15448    GNU Extension:
15449
15450    cv-qualifier:
15451      __restrict__
15452
15453    Returns a bitmask representing the cv-qualifiers.  */
15454
15455 static cp_cv_quals
15456 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15457 {
15458   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15459
15460   while (true)
15461     {
15462       cp_token *token;
15463       cp_cv_quals cv_qualifier;
15464
15465       /* Peek at the next token.  */
15466       token = cp_lexer_peek_token (parser->lexer);
15467       /* See if it's a cv-qualifier.  */
15468       switch (token->keyword)
15469         {
15470         case RID_CONST:
15471           cv_qualifier = TYPE_QUAL_CONST;
15472           break;
15473
15474         case RID_VOLATILE:
15475           cv_qualifier = TYPE_QUAL_VOLATILE;
15476           break;
15477
15478         case RID_RESTRICT:
15479           cv_qualifier = TYPE_QUAL_RESTRICT;
15480           break;
15481
15482         default:
15483           cv_qualifier = TYPE_UNQUALIFIED;
15484           break;
15485         }
15486
15487       if (!cv_qualifier)
15488         break;
15489
15490       if (cv_quals & cv_qualifier)
15491         {
15492           error_at (token->location, "duplicate cv-qualifier");
15493           cp_lexer_purge_token (parser->lexer);
15494         }
15495       else
15496         {
15497           cp_lexer_consume_token (parser->lexer);
15498           cv_quals |= cv_qualifier;
15499         }
15500     }
15501
15502   return cv_quals;
15503 }
15504
15505 /* Parse a late-specified return type, if any.  This is not a separate
15506    non-terminal, but part of a function declarator, which looks like
15507
15508    -> trailing-type-specifier-seq abstract-declarator(opt)
15509
15510    Returns the type indicated by the type-id.  */
15511
15512 static tree
15513 cp_parser_late_return_type_opt (cp_parser* parser)
15514 {
15515   cp_token *token;
15516
15517   /* Peek at the next token.  */
15518   token = cp_lexer_peek_token (parser->lexer);
15519   /* A late-specified return type is indicated by an initial '->'. */
15520   if (token->type != CPP_DEREF)
15521     return NULL_TREE;
15522
15523   /* Consume the ->.  */
15524   cp_lexer_consume_token (parser->lexer);
15525
15526   return cp_parser_trailing_type_id (parser);
15527 }
15528
15529 /* Parse a declarator-id.
15530
15531    declarator-id:
15532      id-expression
15533      :: [opt] nested-name-specifier [opt] type-name
15534
15535    In the `id-expression' case, the value returned is as for
15536    cp_parser_id_expression if the id-expression was an unqualified-id.
15537    If the id-expression was a qualified-id, then a SCOPE_REF is
15538    returned.  The first operand is the scope (either a NAMESPACE_DECL
15539    or TREE_TYPE), but the second is still just a representation of an
15540    unqualified-id.  */
15541
15542 static tree
15543 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15544 {
15545   tree id;
15546   /* The expression must be an id-expression.  Assume that qualified
15547      names are the names of types so that:
15548
15549        template <class T>
15550        int S<T>::R::i = 3;
15551
15552      will work; we must treat `S<T>::R' as the name of a type.
15553      Similarly, assume that qualified names are templates, where
15554      required, so that:
15555
15556        template <class T>
15557        int S<T>::R<T>::i = 3;
15558
15559      will work, too.  */
15560   id = cp_parser_id_expression (parser,
15561                                 /*template_keyword_p=*/false,
15562                                 /*check_dependency_p=*/false,
15563                                 /*template_p=*/NULL,
15564                                 /*declarator_p=*/true,
15565                                 optional_p);
15566   if (id && BASELINK_P (id))
15567     id = BASELINK_FUNCTIONS (id);
15568   return id;
15569 }
15570
15571 /* Parse a type-id.
15572
15573    type-id:
15574      type-specifier-seq abstract-declarator [opt]
15575
15576    Returns the TYPE specified.  */
15577
15578 static tree
15579 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15580                      bool is_trailing_return)
15581 {
15582   cp_decl_specifier_seq type_specifier_seq;
15583   cp_declarator *abstract_declarator;
15584
15585   /* Parse the type-specifier-seq.  */
15586   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15587                                 is_trailing_return,
15588                                 &type_specifier_seq);
15589   if (type_specifier_seq.type == error_mark_node)
15590     return error_mark_node;
15591
15592   /* There might or might not be an abstract declarator.  */
15593   cp_parser_parse_tentatively (parser);
15594   /* Look for the declarator.  */
15595   abstract_declarator
15596     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15597                             /*parenthesized_p=*/NULL,
15598                             /*member_p=*/false);
15599   /* Check to see if there really was a declarator.  */
15600   if (!cp_parser_parse_definitely (parser))
15601     abstract_declarator = NULL;
15602
15603   if (type_specifier_seq.type
15604       && type_uses_auto (type_specifier_seq.type))
15605     {
15606       /* A type-id with type 'auto' is only ok if the abstract declarator
15607          is a function declarator with a late-specified return type.  */
15608       if (abstract_declarator
15609           && abstract_declarator->kind == cdk_function
15610           && abstract_declarator->u.function.late_return_type)
15611         /* OK */;
15612       else
15613         {
15614           error ("invalid use of %<auto%>");
15615           return error_mark_node;
15616         }
15617     }
15618   
15619   return groktypename (&type_specifier_seq, abstract_declarator,
15620                        is_template_arg);
15621 }
15622
15623 static tree cp_parser_type_id (cp_parser *parser)
15624 {
15625   return cp_parser_type_id_1 (parser, false, false);
15626 }
15627
15628 static tree cp_parser_template_type_arg (cp_parser *parser)
15629 {
15630   return cp_parser_type_id_1 (parser, true, false);
15631 }
15632
15633 static tree cp_parser_trailing_type_id (cp_parser *parser)
15634 {
15635   return cp_parser_type_id_1 (parser, false, true);
15636 }
15637
15638 /* Parse a type-specifier-seq.
15639
15640    type-specifier-seq:
15641      type-specifier type-specifier-seq [opt]
15642
15643    GNU extension:
15644
15645    type-specifier-seq:
15646      attributes type-specifier-seq [opt]
15647
15648    If IS_DECLARATION is true, we are at the start of a "condition" or
15649    exception-declaration, so we might be followed by a declarator-id.
15650
15651    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15652    i.e. we've just seen "->".
15653
15654    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15655
15656 static void
15657 cp_parser_type_specifier_seq (cp_parser* parser,
15658                               bool is_declaration,
15659                               bool is_trailing_return,
15660                               cp_decl_specifier_seq *type_specifier_seq)
15661 {
15662   bool seen_type_specifier = false;
15663   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15664   cp_token *start_token = NULL;
15665
15666   /* Clear the TYPE_SPECIFIER_SEQ.  */
15667   clear_decl_specs (type_specifier_seq);
15668
15669   /* In the context of a trailing return type, enum E { } is an
15670      elaborated-type-specifier followed by a function-body, not an
15671      enum-specifier.  */
15672   if (is_trailing_return)
15673     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15674
15675   /* Parse the type-specifiers and attributes.  */
15676   while (true)
15677     {
15678       tree type_specifier;
15679       bool is_cv_qualifier;
15680
15681       /* Check for attributes first.  */
15682       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15683         {
15684           type_specifier_seq->attributes =
15685             chainon (type_specifier_seq->attributes,
15686                      cp_parser_attributes_opt (parser));
15687           continue;
15688         }
15689
15690       /* record the token of the beginning of the type specifier seq,
15691          for error reporting purposes*/
15692      if (!start_token)
15693        start_token = cp_lexer_peek_token (parser->lexer);
15694
15695       /* Look for the type-specifier.  */
15696       type_specifier = cp_parser_type_specifier (parser,
15697                                                  flags,
15698                                                  type_specifier_seq,
15699                                                  /*is_declaration=*/false,
15700                                                  NULL,
15701                                                  &is_cv_qualifier);
15702       if (!type_specifier)
15703         {
15704           /* If the first type-specifier could not be found, this is not a
15705              type-specifier-seq at all.  */
15706           if (!seen_type_specifier)
15707             {
15708               cp_parser_error (parser, "expected type-specifier");
15709               type_specifier_seq->type = error_mark_node;
15710               return;
15711             }
15712           /* If subsequent type-specifiers could not be found, the
15713              type-specifier-seq is complete.  */
15714           break;
15715         }
15716
15717       seen_type_specifier = true;
15718       /* The standard says that a condition can be:
15719
15720             type-specifier-seq declarator = assignment-expression
15721
15722          However, given:
15723
15724            struct S {};
15725            if (int S = ...)
15726
15727          we should treat the "S" as a declarator, not as a
15728          type-specifier.  The standard doesn't say that explicitly for
15729          type-specifier-seq, but it does say that for
15730          decl-specifier-seq in an ordinary declaration.  Perhaps it
15731          would be clearer just to allow a decl-specifier-seq here, and
15732          then add a semantic restriction that if any decl-specifiers
15733          that are not type-specifiers appear, the program is invalid.  */
15734       if (is_declaration && !is_cv_qualifier)
15735         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15736     }
15737
15738   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15739 }
15740
15741 /* Parse a parameter-declaration-clause.
15742
15743    parameter-declaration-clause:
15744      parameter-declaration-list [opt] ... [opt]
15745      parameter-declaration-list , ...
15746
15747    Returns a representation for the parameter declarations.  A return
15748    value of NULL indicates a parameter-declaration-clause consisting
15749    only of an ellipsis.  */
15750
15751 static tree
15752 cp_parser_parameter_declaration_clause (cp_parser* parser)
15753 {
15754   tree parameters;
15755   cp_token *token;
15756   bool ellipsis_p;
15757   bool is_error;
15758
15759   /* Peek at the next token.  */
15760   token = cp_lexer_peek_token (parser->lexer);
15761   /* Check for trivial parameter-declaration-clauses.  */
15762   if (token->type == CPP_ELLIPSIS)
15763     {
15764       /* Consume the `...' token.  */
15765       cp_lexer_consume_token (parser->lexer);
15766       return NULL_TREE;
15767     }
15768   else if (token->type == CPP_CLOSE_PAREN)
15769     /* There are no parameters.  */
15770     {
15771 #ifndef NO_IMPLICIT_EXTERN_C
15772       if (in_system_header && current_class_type == NULL
15773           && current_lang_name == lang_name_c)
15774         return NULL_TREE;
15775       else
15776 #endif
15777         return void_list_node;
15778     }
15779   /* Check for `(void)', too, which is a special case.  */
15780   else if (token->keyword == RID_VOID
15781            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15782                == CPP_CLOSE_PAREN))
15783     {
15784       /* Consume the `void' token.  */
15785       cp_lexer_consume_token (parser->lexer);
15786       /* There are no parameters.  */
15787       return void_list_node;
15788     }
15789
15790   /* Parse the parameter-declaration-list.  */
15791   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15792   /* If a parse error occurred while parsing the
15793      parameter-declaration-list, then the entire
15794      parameter-declaration-clause is erroneous.  */
15795   if (is_error)
15796     return NULL;
15797
15798   /* Peek at the next token.  */
15799   token = cp_lexer_peek_token (parser->lexer);
15800   /* If it's a `,', the clause should terminate with an ellipsis.  */
15801   if (token->type == CPP_COMMA)
15802     {
15803       /* Consume the `,'.  */
15804       cp_lexer_consume_token (parser->lexer);
15805       /* Expect an ellipsis.  */
15806       ellipsis_p
15807         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15808     }
15809   /* It might also be `...' if the optional trailing `,' was
15810      omitted.  */
15811   else if (token->type == CPP_ELLIPSIS)
15812     {
15813       /* Consume the `...' token.  */
15814       cp_lexer_consume_token (parser->lexer);
15815       /* And remember that we saw it.  */
15816       ellipsis_p = true;
15817     }
15818   else
15819     ellipsis_p = false;
15820
15821   /* Finish the parameter list.  */
15822   if (!ellipsis_p)
15823     parameters = chainon (parameters, void_list_node);
15824
15825   return parameters;
15826 }
15827
15828 /* Parse a parameter-declaration-list.
15829
15830    parameter-declaration-list:
15831      parameter-declaration
15832      parameter-declaration-list , parameter-declaration
15833
15834    Returns a representation of the parameter-declaration-list, as for
15835    cp_parser_parameter_declaration_clause.  However, the
15836    `void_list_node' is never appended to the list.  Upon return,
15837    *IS_ERROR will be true iff an error occurred.  */
15838
15839 static tree
15840 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15841 {
15842   tree parameters = NULL_TREE;
15843   tree *tail = &parameters; 
15844   bool saved_in_unbraced_linkage_specification_p;
15845   int index = 0;
15846
15847   /* Assume all will go well.  */
15848   *is_error = false;
15849   /* The special considerations that apply to a function within an
15850      unbraced linkage specifications do not apply to the parameters
15851      to the function.  */
15852   saved_in_unbraced_linkage_specification_p 
15853     = parser->in_unbraced_linkage_specification_p;
15854   parser->in_unbraced_linkage_specification_p = false;
15855
15856   /* Look for more parameters.  */
15857   while (true)
15858     {
15859       cp_parameter_declarator *parameter;
15860       tree decl = error_mark_node;
15861       bool parenthesized_p;
15862       /* Parse the parameter.  */
15863       parameter
15864         = cp_parser_parameter_declaration (parser,
15865                                            /*template_parm_p=*/false,
15866                                            &parenthesized_p);
15867
15868       /* We don't know yet if the enclosing context is deprecated, so wait
15869          and warn in grokparms if appropriate.  */
15870       deprecated_state = DEPRECATED_SUPPRESS;
15871
15872       if (parameter)
15873         decl = grokdeclarator (parameter->declarator,
15874                                &parameter->decl_specifiers,
15875                                PARM,
15876                                parameter->default_argument != NULL_TREE,
15877                                &parameter->decl_specifiers.attributes);
15878
15879       deprecated_state = DEPRECATED_NORMAL;
15880
15881       /* If a parse error occurred parsing the parameter declaration,
15882          then the entire parameter-declaration-list is erroneous.  */
15883       if (decl == error_mark_node)
15884         {
15885           *is_error = true;
15886           parameters = error_mark_node;
15887           break;
15888         }
15889
15890       if (parameter->decl_specifiers.attributes)
15891         cplus_decl_attributes (&decl,
15892                                parameter->decl_specifiers.attributes,
15893                                0);
15894       if (DECL_NAME (decl))
15895         decl = pushdecl (decl);
15896
15897       if (decl != error_mark_node)
15898         {
15899           retrofit_lang_decl (decl);
15900           DECL_PARM_INDEX (decl) = ++index;
15901         }
15902
15903       /* Add the new parameter to the list.  */
15904       *tail = build_tree_list (parameter->default_argument, decl);
15905       tail = &TREE_CHAIN (*tail);
15906
15907       /* Peek at the next token.  */
15908       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15909           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15910           /* These are for Objective-C++ */
15911           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15912           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15913         /* The parameter-declaration-list is complete.  */
15914         break;
15915       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15916         {
15917           cp_token *token;
15918
15919           /* Peek at the next token.  */
15920           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15921           /* If it's an ellipsis, then the list is complete.  */
15922           if (token->type == CPP_ELLIPSIS)
15923             break;
15924           /* Otherwise, there must be more parameters.  Consume the
15925              `,'.  */
15926           cp_lexer_consume_token (parser->lexer);
15927           /* When parsing something like:
15928
15929                 int i(float f, double d)
15930
15931              we can tell after seeing the declaration for "f" that we
15932              are not looking at an initialization of a variable "i",
15933              but rather at the declaration of a function "i".
15934
15935              Due to the fact that the parsing of template arguments
15936              (as specified to a template-id) requires backtracking we
15937              cannot use this technique when inside a template argument
15938              list.  */
15939           if (!parser->in_template_argument_list_p
15940               && !parser->in_type_id_in_expr_p
15941               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15942               /* However, a parameter-declaration of the form
15943                  "foat(f)" (which is a valid declaration of a
15944                  parameter "f") can also be interpreted as an
15945                  expression (the conversion of "f" to "float").  */
15946               && !parenthesized_p)
15947             cp_parser_commit_to_tentative_parse (parser);
15948         }
15949       else
15950         {
15951           cp_parser_error (parser, "expected %<,%> or %<...%>");
15952           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15953             cp_parser_skip_to_closing_parenthesis (parser,
15954                                                    /*recovering=*/true,
15955                                                    /*or_comma=*/false,
15956                                                    /*consume_paren=*/false);
15957           break;
15958         }
15959     }
15960
15961   parser->in_unbraced_linkage_specification_p
15962     = saved_in_unbraced_linkage_specification_p;
15963
15964   return parameters;
15965 }
15966
15967 /* Parse a parameter declaration.
15968
15969    parameter-declaration:
15970      decl-specifier-seq ... [opt] declarator
15971      decl-specifier-seq declarator = assignment-expression
15972      decl-specifier-seq ... [opt] abstract-declarator [opt]
15973      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15974
15975    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15976    declares a template parameter.  (In that case, a non-nested `>'
15977    token encountered during the parsing of the assignment-expression
15978    is not interpreted as a greater-than operator.)
15979
15980    Returns a representation of the parameter, or NULL if an error
15981    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15982    true iff the declarator is of the form "(p)".  */
15983
15984 static cp_parameter_declarator *
15985 cp_parser_parameter_declaration (cp_parser *parser,
15986                                  bool template_parm_p,
15987                                  bool *parenthesized_p)
15988 {
15989   int declares_class_or_enum;
15990   cp_decl_specifier_seq decl_specifiers;
15991   cp_declarator *declarator;
15992   tree default_argument;
15993   cp_token *token = NULL, *declarator_token_start = NULL;
15994   const char *saved_message;
15995
15996   /* In a template parameter, `>' is not an operator.
15997
15998      [temp.param]
15999
16000      When parsing a default template-argument for a non-type
16001      template-parameter, the first non-nested `>' is taken as the end
16002      of the template parameter-list rather than a greater-than
16003      operator.  */
16004
16005   /* Type definitions may not appear in parameter types.  */
16006   saved_message = parser->type_definition_forbidden_message;
16007   parser->type_definition_forbidden_message
16008     = G_("types may not be defined in parameter types");
16009
16010   /* Parse the declaration-specifiers.  */
16011   cp_parser_decl_specifier_seq (parser,
16012                                 CP_PARSER_FLAGS_NONE,
16013                                 &decl_specifiers,
16014                                 &declares_class_or_enum);
16015
16016   /* Complain about missing 'typename' or other invalid type names.  */
16017   if (!decl_specifiers.any_type_specifiers_p)
16018     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16019
16020   /* If an error occurred, there's no reason to attempt to parse the
16021      rest of the declaration.  */
16022   if (cp_parser_error_occurred (parser))
16023     {
16024       parser->type_definition_forbidden_message = saved_message;
16025       return NULL;
16026     }
16027
16028   /* Peek at the next token.  */
16029   token = cp_lexer_peek_token (parser->lexer);
16030
16031   /* If the next token is a `)', `,', `=', `>', or `...', then there
16032      is no declarator. However, when variadic templates are enabled,
16033      there may be a declarator following `...'.  */
16034   if (token->type == CPP_CLOSE_PAREN
16035       || token->type == CPP_COMMA
16036       || token->type == CPP_EQ
16037       || token->type == CPP_GREATER)
16038     {
16039       declarator = NULL;
16040       if (parenthesized_p)
16041         *parenthesized_p = false;
16042     }
16043   /* Otherwise, there should be a declarator.  */
16044   else
16045     {
16046       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16047       parser->default_arg_ok_p = false;
16048
16049       /* After seeing a decl-specifier-seq, if the next token is not a
16050          "(", there is no possibility that the code is a valid
16051          expression.  Therefore, if parsing tentatively, we commit at
16052          this point.  */
16053       if (!parser->in_template_argument_list_p
16054           /* In an expression context, having seen:
16055
16056                (int((char ...
16057
16058              we cannot be sure whether we are looking at a
16059              function-type (taking a "char" as a parameter) or a cast
16060              of some object of type "char" to "int".  */
16061           && !parser->in_type_id_in_expr_p
16062           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16063           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16064         cp_parser_commit_to_tentative_parse (parser);
16065       /* Parse the declarator.  */
16066       declarator_token_start = token;
16067       declarator = cp_parser_declarator (parser,
16068                                          CP_PARSER_DECLARATOR_EITHER,
16069                                          /*ctor_dtor_or_conv_p=*/NULL,
16070                                          parenthesized_p,
16071                                          /*member_p=*/false);
16072       parser->default_arg_ok_p = saved_default_arg_ok_p;
16073       /* After the declarator, allow more attributes.  */
16074       decl_specifiers.attributes
16075         = chainon (decl_specifiers.attributes,
16076                    cp_parser_attributes_opt (parser));
16077     }
16078
16079   /* If the next token is an ellipsis, and we have not seen a
16080      declarator name, and the type of the declarator contains parameter
16081      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16082      a parameter pack expansion expression. Otherwise, leave the
16083      ellipsis for a C-style variadic function. */
16084   token = cp_lexer_peek_token (parser->lexer);
16085   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16086     {
16087       tree type = decl_specifiers.type;
16088
16089       if (type && DECL_P (type))
16090         type = TREE_TYPE (type);
16091
16092       if (type
16093           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16094           && declarator_can_be_parameter_pack (declarator)
16095           && (!declarator || !declarator->parameter_pack_p)
16096           && uses_parameter_packs (type))
16097         {
16098           /* Consume the `...'. */
16099           cp_lexer_consume_token (parser->lexer);
16100           maybe_warn_variadic_templates ();
16101           
16102           /* Build a pack expansion type */
16103           if (declarator)
16104             declarator->parameter_pack_p = true;
16105           else
16106             decl_specifiers.type = make_pack_expansion (type);
16107         }
16108     }
16109
16110   /* The restriction on defining new types applies only to the type
16111      of the parameter, not to the default argument.  */
16112   parser->type_definition_forbidden_message = saved_message;
16113
16114   /* If the next token is `=', then process a default argument.  */
16115   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16116     {
16117       /* Consume the `='.  */
16118       cp_lexer_consume_token (parser->lexer);
16119
16120       /* If we are defining a class, then the tokens that make up the
16121          default argument must be saved and processed later.  */
16122       if (!template_parm_p && at_class_scope_p ()
16123           && TYPE_BEING_DEFINED (current_class_type)
16124           && !LAMBDA_TYPE_P (current_class_type))
16125         {
16126           unsigned depth = 0;
16127           int maybe_template_id = 0;
16128           cp_token *first_token;
16129           cp_token *token;
16130
16131           /* Add tokens until we have processed the entire default
16132              argument.  We add the range [first_token, token).  */
16133           first_token = cp_lexer_peek_token (parser->lexer);
16134           while (true)
16135             {
16136               bool done = false;
16137
16138               /* Peek at the next token.  */
16139               token = cp_lexer_peek_token (parser->lexer);
16140               /* What we do depends on what token we have.  */
16141               switch (token->type)
16142                 {
16143                   /* In valid code, a default argument must be
16144                      immediately followed by a `,' `)', or `...'.  */
16145                 case CPP_COMMA:
16146                   if (depth == 0 && maybe_template_id)
16147                     {
16148                       /* If we've seen a '<', we might be in a
16149                          template-argument-list.  Until Core issue 325 is
16150                          resolved, we don't know how this situation ought
16151                          to be handled, so try to DTRT.  We check whether
16152                          what comes after the comma is a valid parameter
16153                          declaration list.  If it is, then the comma ends
16154                          the default argument; otherwise the default
16155                          argument continues.  */
16156                       bool error = false;
16157                       tree t;
16158
16159                       /* Set ITALP so cp_parser_parameter_declaration_list
16160                          doesn't decide to commit to this parse.  */
16161                       bool saved_italp = parser->in_template_argument_list_p;
16162                       parser->in_template_argument_list_p = true;
16163
16164                       cp_parser_parse_tentatively (parser);
16165                       cp_lexer_consume_token (parser->lexer);
16166                       begin_scope (sk_function_parms, NULL_TREE);
16167                       cp_parser_parameter_declaration_list (parser, &error);
16168                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16169                         pop_binding (DECL_NAME (t), t);
16170                       leave_scope ();
16171                       if (!cp_parser_error_occurred (parser) && !error)
16172                         done = true;
16173                       cp_parser_abort_tentative_parse (parser);
16174
16175                       parser->in_template_argument_list_p = saved_italp;
16176                       break;
16177                     }
16178                 case CPP_CLOSE_PAREN:
16179                 case CPP_ELLIPSIS:
16180                   /* If we run into a non-nested `;', `}', or `]',
16181                      then the code is invalid -- but the default
16182                      argument is certainly over.  */
16183                 case CPP_SEMICOLON:
16184                 case CPP_CLOSE_BRACE:
16185                 case CPP_CLOSE_SQUARE:
16186                   if (depth == 0)
16187                     done = true;
16188                   /* Update DEPTH, if necessary.  */
16189                   else if (token->type == CPP_CLOSE_PAREN
16190                            || token->type == CPP_CLOSE_BRACE
16191                            || token->type == CPP_CLOSE_SQUARE)
16192                     --depth;
16193                   break;
16194
16195                 case CPP_OPEN_PAREN:
16196                 case CPP_OPEN_SQUARE:
16197                 case CPP_OPEN_BRACE:
16198                   ++depth;
16199                   break;
16200
16201                 case CPP_LESS:
16202                   if (depth == 0)
16203                     /* This might be the comparison operator, or it might
16204                        start a template argument list.  */
16205                     ++maybe_template_id;
16206                   break;
16207
16208                 case CPP_RSHIFT:
16209                   if (cxx_dialect == cxx98)
16210                     break;
16211                   /* Fall through for C++0x, which treats the `>>'
16212                      operator like two `>' tokens in certain
16213                      cases.  */
16214
16215                 case CPP_GREATER:
16216                   if (depth == 0)
16217                     {
16218                       /* This might be an operator, or it might close a
16219                          template argument list.  But if a previous '<'
16220                          started a template argument list, this will have
16221                          closed it, so we can't be in one anymore.  */
16222                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16223                       if (maybe_template_id < 0)
16224                         maybe_template_id = 0;
16225                     }
16226                   break;
16227
16228                   /* If we run out of tokens, issue an error message.  */
16229                 case CPP_EOF:
16230                 case CPP_PRAGMA_EOL:
16231                   error_at (token->location, "file ends in default argument");
16232                   done = true;
16233                   break;
16234
16235                 case CPP_NAME:
16236                 case CPP_SCOPE:
16237                   /* In these cases, we should look for template-ids.
16238                      For example, if the default argument is
16239                      `X<int, double>()', we need to do name lookup to
16240                      figure out whether or not `X' is a template; if
16241                      so, the `,' does not end the default argument.
16242
16243                      That is not yet done.  */
16244                   break;
16245
16246                 default:
16247                   break;
16248                 }
16249
16250               /* If we've reached the end, stop.  */
16251               if (done)
16252                 break;
16253
16254               /* Add the token to the token block.  */
16255               token = cp_lexer_consume_token (parser->lexer);
16256             }
16257
16258           /* Create a DEFAULT_ARG to represent the unparsed default
16259              argument.  */
16260           default_argument = make_node (DEFAULT_ARG);
16261           DEFARG_TOKENS (default_argument)
16262             = cp_token_cache_new (first_token, token);
16263           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16264         }
16265       /* Outside of a class definition, we can just parse the
16266          assignment-expression.  */
16267       else
16268         {
16269           token = cp_lexer_peek_token (parser->lexer);
16270           default_argument 
16271             = cp_parser_default_argument (parser, template_parm_p);
16272         }
16273
16274       if (!parser->default_arg_ok_p)
16275         {
16276           if (flag_permissive)
16277             warning (0, "deprecated use of default argument for parameter of non-function");
16278           else
16279             {
16280               error_at (token->location,
16281                         "default arguments are only "
16282                         "permitted for function parameters");
16283               default_argument = NULL_TREE;
16284             }
16285         }
16286       else if ((declarator && declarator->parameter_pack_p)
16287                || (decl_specifiers.type
16288                    && PACK_EXPANSION_P (decl_specifiers.type)))
16289         {
16290           /* Find the name of the parameter pack.  */     
16291           cp_declarator *id_declarator = declarator;
16292           while (id_declarator && id_declarator->kind != cdk_id)
16293             id_declarator = id_declarator->declarator;
16294           
16295           if (id_declarator && id_declarator->kind == cdk_id)
16296             error_at (declarator_token_start->location,
16297                       template_parm_p 
16298                       ? "template parameter pack %qD"
16299                       " cannot have a default argument"
16300                       : "parameter pack %qD cannot have a default argument",
16301                       id_declarator->u.id.unqualified_name);
16302           else
16303             error_at (declarator_token_start->location,
16304                       template_parm_p 
16305                       ? "template parameter pack cannot have a default argument"
16306                       : "parameter pack cannot have a default argument");
16307           
16308           default_argument = NULL_TREE;
16309         }
16310     }
16311   else
16312     default_argument = NULL_TREE;
16313
16314   return make_parameter_declarator (&decl_specifiers,
16315                                     declarator,
16316                                     default_argument);
16317 }
16318
16319 /* Parse a default argument and return it.
16320
16321    TEMPLATE_PARM_P is true if this is a default argument for a
16322    non-type template parameter.  */
16323 static tree
16324 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16325 {
16326   tree default_argument = NULL_TREE;
16327   bool saved_greater_than_is_operator_p;
16328   bool saved_local_variables_forbidden_p;
16329
16330   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16331      set correctly.  */
16332   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16333   parser->greater_than_is_operator_p = !template_parm_p;
16334   /* Local variable names (and the `this' keyword) may not
16335      appear in a default argument.  */
16336   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16337   parser->local_variables_forbidden_p = true;
16338   /* Parse the assignment-expression.  */
16339   if (template_parm_p)
16340     push_deferring_access_checks (dk_no_deferred);
16341   default_argument
16342     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16343   if (template_parm_p)
16344     pop_deferring_access_checks ();
16345   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16346   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16347
16348   return default_argument;
16349 }
16350
16351 /* Parse a function-body.
16352
16353    function-body:
16354      compound_statement  */
16355
16356 static void
16357 cp_parser_function_body (cp_parser *parser)
16358 {
16359   cp_parser_compound_statement (parser, NULL, false);
16360 }
16361
16362 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16363    true if a ctor-initializer was present.  */
16364
16365 static bool
16366 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16367 {
16368   tree body, list;
16369   bool ctor_initializer_p;
16370   const bool check_body_p =
16371      DECL_CONSTRUCTOR_P (current_function_decl)
16372      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16373   tree last = NULL;
16374
16375   /* Begin the function body.  */
16376   body = begin_function_body ();
16377   /* Parse the optional ctor-initializer.  */
16378   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16379
16380   /* If we're parsing a constexpr constructor definition, we need
16381      to check that the constructor body is indeed empty.  However,
16382      before we get to cp_parser_function_body lot of junk has been
16383      generated, so we can't just check that we have an empty block.
16384      Rather we take a snapshot of the outermost block, and check whether
16385      cp_parser_function_body changed its state.  */
16386   if (check_body_p)
16387     {
16388       list = body;
16389       if (TREE_CODE (list) == BIND_EXPR)
16390         list = BIND_EXPR_BODY (list);
16391       if (TREE_CODE (list) == STATEMENT_LIST
16392           && STATEMENT_LIST_TAIL (list) != NULL)
16393         last = STATEMENT_LIST_TAIL (list)->stmt;
16394     }
16395   /* Parse the function-body.  */
16396   cp_parser_function_body (parser);
16397   if (check_body_p)
16398     check_constexpr_ctor_body (last, list);
16399   /* Finish the function body.  */
16400   finish_function_body (body);
16401
16402   return ctor_initializer_p;
16403 }
16404
16405 /* Parse an initializer.
16406
16407    initializer:
16408      = initializer-clause
16409      ( expression-list )
16410
16411    Returns an expression representing the initializer.  If no
16412    initializer is present, NULL_TREE is returned.
16413
16414    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16415    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16416    set to TRUE if there is no initializer present.  If there is an
16417    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16418    is set to true; otherwise it is set to false.  */
16419
16420 static tree
16421 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16422                        bool* non_constant_p)
16423 {
16424   cp_token *token;
16425   tree init;
16426
16427   /* Peek at the next token.  */
16428   token = cp_lexer_peek_token (parser->lexer);
16429
16430   /* Let our caller know whether or not this initializer was
16431      parenthesized.  */
16432   *is_direct_init = (token->type != CPP_EQ);
16433   /* Assume that the initializer is constant.  */
16434   *non_constant_p = false;
16435
16436   if (token->type == CPP_EQ)
16437     {
16438       /* Consume the `='.  */
16439       cp_lexer_consume_token (parser->lexer);
16440       /* Parse the initializer-clause.  */
16441       init = cp_parser_initializer_clause (parser, non_constant_p);
16442     }
16443   else if (token->type == CPP_OPEN_PAREN)
16444     {
16445       VEC(tree,gc) *vec;
16446       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16447                                                      /*cast_p=*/false,
16448                                                      /*allow_expansion_p=*/true,
16449                                                      non_constant_p);
16450       if (vec == NULL)
16451         return error_mark_node;
16452       init = build_tree_list_vec (vec);
16453       release_tree_vector (vec);
16454     }
16455   else if (token->type == CPP_OPEN_BRACE)
16456     {
16457       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16458       init = cp_parser_braced_list (parser, non_constant_p);
16459       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16460     }
16461   else
16462     {
16463       /* Anything else is an error.  */
16464       cp_parser_error (parser, "expected initializer");
16465       init = error_mark_node;
16466     }
16467
16468   return init;
16469 }
16470
16471 /* Parse an initializer-clause.
16472
16473    initializer-clause:
16474      assignment-expression
16475      braced-init-list
16476
16477    Returns an expression representing the initializer.
16478
16479    If the `assignment-expression' production is used the value
16480    returned is simply a representation for the expression.
16481
16482    Otherwise, calls cp_parser_braced_list.  */
16483
16484 static tree
16485 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16486 {
16487   tree initializer;
16488
16489   /* Assume the expression is constant.  */
16490   *non_constant_p = false;
16491
16492   /* If it is not a `{', then we are looking at an
16493      assignment-expression.  */
16494   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16495     {
16496       initializer
16497         = cp_parser_constant_expression (parser,
16498                                         /*allow_non_constant_p=*/true,
16499                                         non_constant_p);
16500       if (!*non_constant_p)
16501         {
16502           /* We only want to fold if this is really a constant
16503              expression.  FIXME Actually, we don't want to fold here, but in
16504              cp_finish_decl.  */
16505           tree folded = fold_non_dependent_expr (initializer);
16506           folded = maybe_constant_value (folded);
16507           if (TREE_CONSTANT (folded))
16508             initializer = folded;
16509         }
16510     }
16511   else
16512     initializer = cp_parser_braced_list (parser, non_constant_p);
16513
16514   return initializer;
16515 }
16516
16517 /* Parse a brace-enclosed initializer list.
16518
16519    braced-init-list:
16520      { initializer-list , [opt] }
16521      { }
16522
16523    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16524    the elements of the initializer-list (or NULL, if the last
16525    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16526    NULL_TREE.  There is no way to detect whether or not the optional
16527    trailing `,' was provided.  NON_CONSTANT_P is as for
16528    cp_parser_initializer.  */     
16529
16530 static tree
16531 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16532 {
16533   tree initializer;
16534
16535   /* Consume the `{' token.  */
16536   cp_lexer_consume_token (parser->lexer);
16537   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16538   initializer = make_node (CONSTRUCTOR);
16539   /* If it's not a `}', then there is a non-trivial initializer.  */
16540   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16541     {
16542       /* Parse the initializer list.  */
16543       CONSTRUCTOR_ELTS (initializer)
16544         = cp_parser_initializer_list (parser, non_constant_p);
16545       /* A trailing `,' token is allowed.  */
16546       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16547         cp_lexer_consume_token (parser->lexer);
16548     }
16549   /* Now, there should be a trailing `}'.  */
16550   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16551   TREE_TYPE (initializer) = init_list_type_node;
16552   return initializer;
16553 }
16554
16555 /* Parse an initializer-list.
16556
16557    initializer-list:
16558      initializer-clause ... [opt]
16559      initializer-list , initializer-clause ... [opt]
16560
16561    GNU Extension:
16562
16563    initializer-list:
16564      identifier : initializer-clause
16565      initializer-list, identifier : initializer-clause
16566
16567    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16568    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16569    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16570    as for cp_parser_initializer.  */
16571
16572 static VEC(constructor_elt,gc) *
16573 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16574 {
16575   VEC(constructor_elt,gc) *v = NULL;
16576
16577   /* Assume all of the expressions are constant.  */
16578   *non_constant_p = false;
16579
16580   /* Parse the rest of the list.  */
16581   while (true)
16582     {
16583       cp_token *token;
16584       tree identifier;
16585       tree initializer;
16586       bool clause_non_constant_p;
16587
16588       /* If the next token is an identifier and the following one is a
16589          colon, we are looking at the GNU designated-initializer
16590          syntax.  */
16591       if (cp_parser_allow_gnu_extensions_p (parser)
16592           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16593           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16594         {
16595           /* Warn the user that they are using an extension.  */
16596           pedwarn (input_location, OPT_pedantic, 
16597                    "ISO C++ does not allow designated initializers");
16598           /* Consume the identifier.  */
16599           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16600           /* Consume the `:'.  */
16601           cp_lexer_consume_token (parser->lexer);
16602         }
16603       else
16604         identifier = NULL_TREE;
16605
16606       /* Parse the initializer.  */
16607       initializer = cp_parser_initializer_clause (parser,
16608                                                   &clause_non_constant_p);
16609       /* If any clause is non-constant, so is the entire initializer.  */
16610       if (clause_non_constant_p)
16611         *non_constant_p = true;
16612
16613       /* If we have an ellipsis, this is an initializer pack
16614          expansion.  */
16615       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16616         {
16617           /* Consume the `...'.  */
16618           cp_lexer_consume_token (parser->lexer);
16619
16620           /* Turn the initializer into an initializer expansion.  */
16621           initializer = make_pack_expansion (initializer);
16622         }
16623
16624       /* Add it to the vector.  */
16625       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16626
16627       /* If the next token is not a comma, we have reached the end of
16628          the list.  */
16629       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16630         break;
16631
16632       /* Peek at the next token.  */
16633       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16634       /* If the next token is a `}', then we're still done.  An
16635          initializer-clause can have a trailing `,' after the
16636          initializer-list and before the closing `}'.  */
16637       if (token->type == CPP_CLOSE_BRACE)
16638         break;
16639
16640       /* Consume the `,' token.  */
16641       cp_lexer_consume_token (parser->lexer);
16642     }
16643
16644   return v;
16645 }
16646
16647 /* Classes [gram.class] */
16648
16649 /* Parse a class-name.
16650
16651    class-name:
16652      identifier
16653      template-id
16654
16655    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16656    to indicate that names looked up in dependent types should be
16657    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16658    keyword has been used to indicate that the name that appears next
16659    is a template.  TAG_TYPE indicates the explicit tag given before
16660    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16661    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16662    is the class being defined in a class-head.
16663
16664    Returns the TYPE_DECL representing the class.  */
16665
16666 static tree
16667 cp_parser_class_name (cp_parser *parser,
16668                       bool typename_keyword_p,
16669                       bool template_keyword_p,
16670                       enum tag_types tag_type,
16671                       bool check_dependency_p,
16672                       bool class_head_p,
16673                       bool is_declaration)
16674 {
16675   tree decl;
16676   tree scope;
16677   bool typename_p;
16678   cp_token *token;
16679   tree identifier = NULL_TREE;
16680
16681   /* All class-names start with an identifier.  */
16682   token = cp_lexer_peek_token (parser->lexer);
16683   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16684     {
16685       cp_parser_error (parser, "expected class-name");
16686       return error_mark_node;
16687     }
16688
16689   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16690      to a template-id, so we save it here.  */
16691   scope = parser->scope;
16692   if (scope == error_mark_node)
16693     return error_mark_node;
16694
16695   /* Any name names a type if we're following the `typename' keyword
16696      in a qualified name where the enclosing scope is type-dependent.  */
16697   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16698                 && dependent_type_p (scope));
16699   /* Handle the common case (an identifier, but not a template-id)
16700      efficiently.  */
16701   if (token->type == CPP_NAME
16702       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16703     {
16704       cp_token *identifier_token;
16705       bool ambiguous_p;
16706
16707       /* Look for the identifier.  */
16708       identifier_token = cp_lexer_peek_token (parser->lexer);
16709       ambiguous_p = identifier_token->ambiguous_p;
16710       identifier = cp_parser_identifier (parser);
16711       /* If the next token isn't an identifier, we are certainly not
16712          looking at a class-name.  */
16713       if (identifier == error_mark_node)
16714         decl = error_mark_node;
16715       /* If we know this is a type-name, there's no need to look it
16716          up.  */
16717       else if (typename_p)
16718         decl = identifier;
16719       else
16720         {
16721           tree ambiguous_decls;
16722           /* If we already know that this lookup is ambiguous, then
16723              we've already issued an error message; there's no reason
16724              to check again.  */
16725           if (ambiguous_p)
16726             {
16727               cp_parser_simulate_error (parser);
16728               return error_mark_node;
16729             }
16730           /* If the next token is a `::', then the name must be a type
16731              name.
16732
16733              [basic.lookup.qual]
16734
16735              During the lookup for a name preceding the :: scope
16736              resolution operator, object, function, and enumerator
16737              names are ignored.  */
16738           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16739             tag_type = typename_type;
16740           /* Look up the name.  */
16741           decl = cp_parser_lookup_name (parser, identifier,
16742                                         tag_type,
16743                                         /*is_template=*/false,
16744                                         /*is_namespace=*/false,
16745                                         check_dependency_p,
16746                                         &ambiguous_decls,
16747                                         identifier_token->location);
16748           if (ambiguous_decls)
16749             {
16750               if (cp_parser_parsing_tentatively (parser))
16751                 cp_parser_simulate_error (parser);
16752               return error_mark_node;
16753             }
16754         }
16755     }
16756   else
16757     {
16758       /* Try a template-id.  */
16759       decl = cp_parser_template_id (parser, template_keyword_p,
16760                                     check_dependency_p,
16761                                     is_declaration);
16762       if (decl == error_mark_node)
16763         return error_mark_node;
16764     }
16765
16766   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16767
16768   /* If this is a typename, create a TYPENAME_TYPE.  */
16769   if (typename_p && decl != error_mark_node)
16770     {
16771       decl = make_typename_type (scope, decl, typename_type,
16772                                  /*complain=*/tf_error);
16773       if (decl != error_mark_node)
16774         decl = TYPE_NAME (decl);
16775     }
16776
16777   /* Check to see that it is really the name of a class.  */
16778   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16779       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16780       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16781     /* Situations like this:
16782
16783          template <typename T> struct A {
16784            typename T::template X<int>::I i;
16785          };
16786
16787        are problematic.  Is `T::template X<int>' a class-name?  The
16788        standard does not seem to be definitive, but there is no other
16789        valid interpretation of the following `::'.  Therefore, those
16790        names are considered class-names.  */
16791     {
16792       decl = make_typename_type (scope, decl, tag_type, tf_error);
16793       if (decl != error_mark_node)
16794         decl = TYPE_NAME (decl);
16795     }
16796   else if (TREE_CODE (decl) != TYPE_DECL
16797            || TREE_TYPE (decl) == error_mark_node
16798            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16799            /* In Objective-C 2.0, a classname followed by '.' starts a
16800               dot-syntax expression, and it's not a type-name.  */
16801            || (c_dialect_objc ()
16802                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16803                && objc_is_class_name (decl)))
16804     decl = error_mark_node;
16805
16806   if (decl == error_mark_node)
16807     cp_parser_error (parser, "expected class-name");
16808   else if (identifier && !parser->scope)
16809     maybe_note_name_used_in_class (identifier, decl);
16810
16811   return decl;
16812 }
16813
16814 /* Parse a class-specifier.
16815
16816    class-specifier:
16817      class-head { member-specification [opt] }
16818
16819    Returns the TREE_TYPE representing the class.  */
16820
16821 static tree
16822 cp_parser_class_specifier (cp_parser* parser)
16823 {
16824   tree type;
16825   tree attributes = NULL_TREE;
16826   bool nested_name_specifier_p;
16827   unsigned saved_num_template_parameter_lists;
16828   bool saved_in_function_body;
16829   bool saved_in_unbraced_linkage_specification_p;
16830   tree old_scope = NULL_TREE;
16831   tree scope = NULL_TREE;
16832   tree bases;
16833
16834   push_deferring_access_checks (dk_no_deferred);
16835
16836   /* Parse the class-head.  */
16837   type = cp_parser_class_head (parser,
16838                                &nested_name_specifier_p,
16839                                &attributes,
16840                                &bases);
16841   /* If the class-head was a semantic disaster, skip the entire body
16842      of the class.  */
16843   if (!type)
16844     {
16845       cp_parser_skip_to_end_of_block_or_statement (parser);
16846       pop_deferring_access_checks ();
16847       return error_mark_node;
16848     }
16849
16850   /* Look for the `{'.  */
16851   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16852     {
16853       pop_deferring_access_checks ();
16854       return error_mark_node;
16855     }
16856
16857   /* Process the base classes. If they're invalid, skip the 
16858      entire class body.  */
16859   if (!xref_basetypes (type, bases))
16860     {
16861       /* Consuming the closing brace yields better error messages
16862          later on.  */
16863       if (cp_parser_skip_to_closing_brace (parser))
16864         cp_lexer_consume_token (parser->lexer);
16865       pop_deferring_access_checks ();
16866       return error_mark_node;
16867     }
16868
16869   /* Issue an error message if type-definitions are forbidden here.  */
16870   cp_parser_check_type_definition (parser);
16871   /* Remember that we are defining one more class.  */
16872   ++parser->num_classes_being_defined;
16873   /* Inside the class, surrounding template-parameter-lists do not
16874      apply.  */
16875   saved_num_template_parameter_lists
16876     = parser->num_template_parameter_lists;
16877   parser->num_template_parameter_lists = 0;
16878   /* We are not in a function body.  */
16879   saved_in_function_body = parser->in_function_body;
16880   parser->in_function_body = false;
16881   /* We are not immediately inside an extern "lang" block.  */
16882   saved_in_unbraced_linkage_specification_p
16883     = parser->in_unbraced_linkage_specification_p;
16884   parser->in_unbraced_linkage_specification_p = false;
16885
16886   /* Start the class.  */
16887   if (nested_name_specifier_p)
16888     {
16889       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16890       old_scope = push_inner_scope (scope);
16891     }
16892   type = begin_class_definition (type, attributes);
16893
16894   if (type == error_mark_node)
16895     /* If the type is erroneous, skip the entire body of the class.  */
16896     cp_parser_skip_to_closing_brace (parser);
16897   else
16898     /* Parse the member-specification.  */
16899     cp_parser_member_specification_opt (parser);
16900
16901   /* Look for the trailing `}'.  */
16902   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16903   /* Look for trailing attributes to apply to this class.  */
16904   if (cp_parser_allow_gnu_extensions_p (parser))
16905     attributes = cp_parser_attributes_opt (parser);
16906   if (type != error_mark_node)
16907     type = finish_struct (type, attributes);
16908   if (nested_name_specifier_p)
16909     pop_inner_scope (old_scope, scope);
16910
16911   /* We've finished a type definition.  Check for the common syntax
16912      error of forgetting a semicolon after the definition.  We need to
16913      be careful, as we can't just check for not-a-semicolon and be done
16914      with it; the user might have typed:
16915
16916      class X { } c = ...;
16917      class X { } *p = ...;
16918
16919      and so forth.  Instead, enumerate all the possible tokens that
16920      might follow this production; if we don't see one of them, then
16921      complain and silently insert the semicolon.  */
16922   {
16923     cp_token *token = cp_lexer_peek_token (parser->lexer);
16924     bool want_semicolon = true;
16925
16926     switch (token->type)
16927       {
16928       case CPP_NAME:
16929       case CPP_SEMICOLON:
16930       case CPP_MULT:
16931       case CPP_AND:
16932       case CPP_OPEN_PAREN:
16933       case CPP_CLOSE_PAREN:
16934       case CPP_COMMA:
16935         want_semicolon = false;
16936         break;
16937
16938         /* While it's legal for type qualifiers and storage class
16939            specifiers to follow type definitions in the grammar, only
16940            compiler testsuites contain code like that.  Assume that if
16941            we see such code, then what we're really seeing is a case
16942            like:
16943
16944            class X { }
16945            const <type> var = ...;
16946
16947            or
16948
16949            class Y { }
16950            static <type> func (...) ...
16951
16952            i.e. the qualifier or specifier applies to the next
16953            declaration.  To do so, however, we need to look ahead one
16954            more token to see if *that* token is a type specifier.
16955
16956            This code could be improved to handle:
16957
16958            class Z { }
16959            static const <type> var = ...;  */
16960       case CPP_KEYWORD:
16961         if (keyword_is_storage_class_specifier (token->keyword)
16962             || keyword_is_type_qualifier (token->keyword))
16963           {
16964             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16965
16966             if (lookahead->type == CPP_KEYWORD
16967                 && !keyword_begins_type_specifier (lookahead->keyword))
16968               want_semicolon = false;
16969             else if (lookahead->type == CPP_NAME)
16970               /* Handling user-defined types here would be nice, but
16971                  very tricky.  */
16972               want_semicolon = false;
16973           }
16974         break;
16975       default:
16976         break;
16977       }
16978
16979     /* If we don't have a type, then something is very wrong and we
16980        shouldn't try to do anything clever.  */
16981     if (TYPE_P (type) && want_semicolon)
16982       {
16983         cp_token_position prev
16984           = cp_lexer_previous_token_position (parser->lexer);
16985         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16986         location_t loc = prev_token->location;
16987
16988         if (CLASSTYPE_DECLARED_CLASS (type))
16989           error_at (loc, "expected %<;%> after class definition");
16990         else if (TREE_CODE (type) == RECORD_TYPE)
16991           error_at (loc, "expected %<;%> after struct definition");
16992         else if (TREE_CODE (type) == UNION_TYPE)
16993           error_at (loc, "expected %<;%> after union definition");
16994         else
16995           gcc_unreachable ();
16996
16997         /* Unget one token and smash it to look as though we encountered
16998            a semicolon in the input stream.  */
16999         cp_lexer_set_token_position (parser->lexer, prev);
17000         token = cp_lexer_peek_token (parser->lexer);
17001         token->type = CPP_SEMICOLON;
17002         token->keyword = RID_MAX;
17003       }
17004   }
17005
17006   /* If this class is not itself within the scope of another class,
17007      then we need to parse the bodies of all of the queued function
17008      definitions.  Note that the queued functions defined in a class
17009      are not always processed immediately following the
17010      class-specifier for that class.  Consider:
17011
17012        struct A {
17013          struct B { void f() { sizeof (A); } };
17014        };
17015
17016      If `f' were processed before the processing of `A' were
17017      completed, there would be no way to compute the size of `A'.
17018      Note that the nesting we are interested in here is lexical --
17019      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17020      for:
17021
17022        struct A { struct B; };
17023        struct A::B { void f() { } };
17024
17025      there is no need to delay the parsing of `A::B::f'.  */
17026   if (--parser->num_classes_being_defined == 0)
17027     {
17028       tree fn;
17029       tree class_type = NULL_TREE;
17030       tree pushed_scope = NULL_TREE;
17031       unsigned ix;
17032       cp_default_arg_entry *e;
17033
17034       /* In a first pass, parse default arguments to the functions.
17035          Then, in a second pass, parse the bodies of the functions.
17036          This two-phased approach handles cases like:
17037
17038             struct S {
17039               void f() { g(); }
17040               void g(int i = 3);
17041             };
17042
17043          */
17044       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17045                         ix, e)
17046         {
17047           fn = e->decl;
17048           /* If there are default arguments that have not yet been processed,
17049              take care of them now.  */
17050           if (class_type != e->class_type)
17051             {
17052               if (pushed_scope)
17053                 pop_scope (pushed_scope);
17054               class_type = e->class_type;
17055               pushed_scope = push_scope (class_type);
17056             }
17057           /* Make sure that any template parameters are in scope.  */
17058           maybe_begin_member_template_processing (fn);
17059           /* Parse the default argument expressions.  */
17060           cp_parser_late_parsing_default_args (parser, fn);
17061           /* Remove any template parameters from the symbol table.  */
17062           maybe_end_member_template_processing ();
17063         }
17064       if (pushed_scope)
17065         pop_scope (pushed_scope);
17066       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17067       /* Now parse the body of the functions.  */
17068       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17069         cp_parser_late_parsing_for_member (parser, fn);
17070       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17071     }
17072
17073   /* Put back any saved access checks.  */
17074   pop_deferring_access_checks ();
17075
17076   /* Restore saved state.  */
17077   parser->in_function_body = saved_in_function_body;
17078   parser->num_template_parameter_lists
17079     = saved_num_template_parameter_lists;
17080   parser->in_unbraced_linkage_specification_p
17081     = saved_in_unbraced_linkage_specification_p;
17082
17083   return type;
17084 }
17085
17086 /* Parse a class-head.
17087
17088    class-head:
17089      class-key identifier [opt] base-clause [opt]
17090      class-key nested-name-specifier identifier base-clause [opt]
17091      class-key nested-name-specifier [opt] template-id
17092        base-clause [opt]
17093
17094    GNU Extensions:
17095      class-key attributes identifier [opt] base-clause [opt]
17096      class-key attributes nested-name-specifier identifier base-clause [opt]
17097      class-key attributes nested-name-specifier [opt] template-id
17098        base-clause [opt]
17099
17100    Upon return BASES is initialized to the list of base classes (or
17101    NULL, if there are none) in the same form returned by
17102    cp_parser_base_clause.
17103
17104    Returns the TYPE of the indicated class.  Sets
17105    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17106    involving a nested-name-specifier was used, and FALSE otherwise.
17107
17108    Returns error_mark_node if this is not a class-head.
17109
17110    Returns NULL_TREE if the class-head is syntactically valid, but
17111    semantically invalid in a way that means we should skip the entire
17112    body of the class.  */
17113
17114 static tree
17115 cp_parser_class_head (cp_parser* parser,
17116                       bool* nested_name_specifier_p,
17117                       tree *attributes_p,
17118                       tree *bases)
17119 {
17120   tree nested_name_specifier;
17121   enum tag_types class_key;
17122   tree id = NULL_TREE;
17123   tree type = NULL_TREE;
17124   tree attributes;
17125   bool template_id_p = false;
17126   bool qualified_p = false;
17127   bool invalid_nested_name_p = false;
17128   bool invalid_explicit_specialization_p = false;
17129   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17130   tree pushed_scope = NULL_TREE;
17131   unsigned num_templates;
17132   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17133   /* Assume no nested-name-specifier will be present.  */
17134   *nested_name_specifier_p = false;
17135   /* Assume no template parameter lists will be used in defining the
17136      type.  */
17137   num_templates = 0;
17138   parser->colon_corrects_to_scope_p = false;
17139
17140   *bases = NULL_TREE;
17141
17142   /* Look for the class-key.  */
17143   class_key = cp_parser_class_key (parser);
17144   if (class_key == none_type)
17145     return error_mark_node;
17146
17147   /* Parse the attributes.  */
17148   attributes = cp_parser_attributes_opt (parser);
17149
17150   /* If the next token is `::', that is invalid -- but sometimes
17151      people do try to write:
17152
17153        struct ::S {};
17154
17155      Handle this gracefully by accepting the extra qualifier, and then
17156      issuing an error about it later if this really is a
17157      class-head.  If it turns out just to be an elaborated type
17158      specifier, remain silent.  */
17159   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17160     qualified_p = true;
17161
17162   push_deferring_access_checks (dk_no_check);
17163
17164   /* Determine the name of the class.  Begin by looking for an
17165      optional nested-name-specifier.  */
17166   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17167   nested_name_specifier
17168     = cp_parser_nested_name_specifier_opt (parser,
17169                                            /*typename_keyword_p=*/false,
17170                                            /*check_dependency_p=*/false,
17171                                            /*type_p=*/false,
17172                                            /*is_declaration=*/false);
17173   /* If there was a nested-name-specifier, then there *must* be an
17174      identifier.  */
17175   if (nested_name_specifier)
17176     {
17177       type_start_token = cp_lexer_peek_token (parser->lexer);
17178       /* Although the grammar says `identifier', it really means
17179          `class-name' or `template-name'.  You are only allowed to
17180          define a class that has already been declared with this
17181          syntax.
17182
17183          The proposed resolution for Core Issue 180 says that wherever
17184          you see `class T::X' you should treat `X' as a type-name.
17185
17186          It is OK to define an inaccessible class; for example:
17187
17188            class A { class B; };
17189            class A::B {};
17190
17191          We do not know if we will see a class-name, or a
17192          template-name.  We look for a class-name first, in case the
17193          class-name is a template-id; if we looked for the
17194          template-name first we would stop after the template-name.  */
17195       cp_parser_parse_tentatively (parser);
17196       type = cp_parser_class_name (parser,
17197                                    /*typename_keyword_p=*/false,
17198                                    /*template_keyword_p=*/false,
17199                                    class_type,
17200                                    /*check_dependency_p=*/false,
17201                                    /*class_head_p=*/true,
17202                                    /*is_declaration=*/false);
17203       /* If that didn't work, ignore the nested-name-specifier.  */
17204       if (!cp_parser_parse_definitely (parser))
17205         {
17206           invalid_nested_name_p = true;
17207           type_start_token = cp_lexer_peek_token (parser->lexer);
17208           id = cp_parser_identifier (parser);
17209           if (id == error_mark_node)
17210             id = NULL_TREE;
17211         }
17212       /* If we could not find a corresponding TYPE, treat this
17213          declaration like an unqualified declaration.  */
17214       if (type == error_mark_node)
17215         nested_name_specifier = NULL_TREE;
17216       /* Otherwise, count the number of templates used in TYPE and its
17217          containing scopes.  */
17218       else
17219         {
17220           tree scope;
17221
17222           for (scope = TREE_TYPE (type);
17223                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17224                scope = (TYPE_P (scope)
17225                         ? TYPE_CONTEXT (scope)
17226                         : DECL_CONTEXT (scope)))
17227             if (TYPE_P (scope)
17228                 && CLASS_TYPE_P (scope)
17229                 && CLASSTYPE_TEMPLATE_INFO (scope)
17230                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17231                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17232               ++num_templates;
17233         }
17234     }
17235   /* Otherwise, the identifier is optional.  */
17236   else
17237     {
17238       /* We don't know whether what comes next is a template-id,
17239          an identifier, or nothing at all.  */
17240       cp_parser_parse_tentatively (parser);
17241       /* Check for a template-id.  */
17242       type_start_token = cp_lexer_peek_token (parser->lexer);
17243       id = cp_parser_template_id (parser,
17244                                   /*template_keyword_p=*/false,
17245                                   /*check_dependency_p=*/true,
17246                                   /*is_declaration=*/true);
17247       /* If that didn't work, it could still be an identifier.  */
17248       if (!cp_parser_parse_definitely (parser))
17249         {
17250           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17251             {
17252               type_start_token = cp_lexer_peek_token (parser->lexer);
17253               id = cp_parser_identifier (parser);
17254             }
17255           else
17256             id = NULL_TREE;
17257         }
17258       else
17259         {
17260           template_id_p = true;
17261           ++num_templates;
17262         }
17263     }
17264
17265   pop_deferring_access_checks ();
17266
17267   if (id)
17268     cp_parser_check_for_invalid_template_id (parser, id,
17269                                              type_start_token->location);
17270
17271   /* If it's not a `:' or a `{' then we can't really be looking at a
17272      class-head, since a class-head only appears as part of a
17273      class-specifier.  We have to detect this situation before calling
17274      xref_tag, since that has irreversible side-effects.  */
17275   if (!cp_parser_next_token_starts_class_definition_p (parser))
17276     {
17277       cp_parser_error (parser, "expected %<{%> or %<:%>");
17278       type = error_mark_node;
17279       goto out;
17280     }
17281
17282   /* At this point, we're going ahead with the class-specifier, even
17283      if some other problem occurs.  */
17284   cp_parser_commit_to_tentative_parse (parser);
17285   /* Issue the error about the overly-qualified name now.  */
17286   if (qualified_p)
17287     {
17288       cp_parser_error (parser,
17289                        "global qualification of class name is invalid");
17290       type = error_mark_node;
17291       goto out;
17292     }
17293   else if (invalid_nested_name_p)
17294     {
17295       cp_parser_error (parser,
17296                        "qualified name does not name a class");
17297       type = error_mark_node;
17298       goto out;
17299     }
17300   else if (nested_name_specifier)
17301     {
17302       tree scope;
17303
17304       /* Reject typedef-names in class heads.  */
17305       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17306         {
17307           error_at (type_start_token->location,
17308                     "invalid class name in declaration of %qD",
17309                     type);
17310           type = NULL_TREE;
17311           goto done;
17312         }
17313
17314       /* Figure out in what scope the declaration is being placed.  */
17315       scope = current_scope ();
17316       /* If that scope does not contain the scope in which the
17317          class was originally declared, the program is invalid.  */
17318       if (scope && !is_ancestor (scope, nested_name_specifier))
17319         {
17320           if (at_namespace_scope_p ())
17321             error_at (type_start_token->location,
17322                       "declaration of %qD in namespace %qD which does not "
17323                       "enclose %qD",
17324                       type, scope, nested_name_specifier);
17325           else
17326             error_at (type_start_token->location,
17327                       "declaration of %qD in %qD which does not enclose %qD",
17328                       type, scope, nested_name_specifier);
17329           type = NULL_TREE;
17330           goto done;
17331         }
17332       /* [dcl.meaning]
17333
17334          A declarator-id shall not be qualified except for the
17335          definition of a ... nested class outside of its class
17336          ... [or] the definition or explicit instantiation of a
17337          class member of a namespace outside of its namespace.  */
17338       if (scope == nested_name_specifier)
17339         {
17340           permerror (nested_name_specifier_token_start->location,
17341                      "extra qualification not allowed");
17342           nested_name_specifier = NULL_TREE;
17343           num_templates = 0;
17344         }
17345     }
17346   /* An explicit-specialization must be preceded by "template <>".  If
17347      it is not, try to recover gracefully.  */
17348   if (at_namespace_scope_p ()
17349       && parser->num_template_parameter_lists == 0
17350       && template_id_p)
17351     {
17352       error_at (type_start_token->location,
17353                 "an explicit specialization must be preceded by %<template <>%>");
17354       invalid_explicit_specialization_p = true;
17355       /* Take the same action that would have been taken by
17356          cp_parser_explicit_specialization.  */
17357       ++parser->num_template_parameter_lists;
17358       begin_specialization ();
17359     }
17360   /* There must be no "return" statements between this point and the
17361      end of this function; set "type "to the correct return value and
17362      use "goto done;" to return.  */
17363   /* Make sure that the right number of template parameters were
17364      present.  */
17365   if (!cp_parser_check_template_parameters (parser, num_templates,
17366                                             type_start_token->location,
17367                                             /*declarator=*/NULL))
17368     {
17369       /* If something went wrong, there is no point in even trying to
17370          process the class-definition.  */
17371       type = NULL_TREE;
17372       goto done;
17373     }
17374
17375   /* Look up the type.  */
17376   if (template_id_p)
17377     {
17378       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17379           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17380               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17381         {
17382           error_at (type_start_token->location,
17383                     "function template %qD redeclared as a class template", id);
17384           type = error_mark_node;
17385         }
17386       else
17387         {
17388           type = TREE_TYPE (id);
17389           type = maybe_process_partial_specialization (type);
17390         }
17391       if (nested_name_specifier)
17392         pushed_scope = push_scope (nested_name_specifier);
17393     }
17394   else if (nested_name_specifier)
17395     {
17396       tree class_type;
17397
17398       /* Given:
17399
17400             template <typename T> struct S { struct T };
17401             template <typename T> struct S<T>::T { };
17402
17403          we will get a TYPENAME_TYPE when processing the definition of
17404          `S::T'.  We need to resolve it to the actual type before we
17405          try to define it.  */
17406       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17407         {
17408           class_type = resolve_typename_type (TREE_TYPE (type),
17409                                               /*only_current_p=*/false);
17410           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17411             type = TYPE_NAME (class_type);
17412           else
17413             {
17414               cp_parser_error (parser, "could not resolve typename type");
17415               type = error_mark_node;
17416             }
17417         }
17418
17419       if (maybe_process_partial_specialization (TREE_TYPE (type))
17420           == error_mark_node)
17421         {
17422           type = NULL_TREE;
17423           goto done;
17424         }
17425
17426       class_type = current_class_type;
17427       /* Enter the scope indicated by the nested-name-specifier.  */
17428       pushed_scope = push_scope (nested_name_specifier);
17429       /* Get the canonical version of this type.  */
17430       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17431       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17432           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17433         {
17434           type = push_template_decl (type);
17435           if (type == error_mark_node)
17436             {
17437               type = NULL_TREE;
17438               goto done;
17439             }
17440         }
17441
17442       type = TREE_TYPE (type);
17443       *nested_name_specifier_p = true;
17444     }
17445   else      /* The name is not a nested name.  */
17446     {
17447       /* If the class was unnamed, create a dummy name.  */
17448       if (!id)
17449         id = make_anon_name ();
17450       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17451                        parser->num_template_parameter_lists);
17452     }
17453
17454   /* Indicate whether this class was declared as a `class' or as a
17455      `struct'.  */
17456   if (TREE_CODE (type) == RECORD_TYPE)
17457     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17458   cp_parser_check_class_key (class_key, type);
17459
17460   /* If this type was already complete, and we see another definition,
17461      that's an error.  */
17462   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17463     {
17464       error_at (type_start_token->location, "redefinition of %q#T",
17465                 type);
17466       error_at (type_start_token->location, "previous definition of %q+#T",
17467                 type);
17468       type = NULL_TREE;
17469       goto done;
17470     }
17471   else if (type == error_mark_node)
17472     type = NULL_TREE;
17473
17474   /* We will have entered the scope containing the class; the names of
17475      base classes should be looked up in that context.  For example:
17476
17477        struct A { struct B {}; struct C; };
17478        struct A::C : B {};
17479
17480      is valid.  */
17481
17482   /* Get the list of base-classes, if there is one.  */
17483   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17484     *bases = cp_parser_base_clause (parser);
17485
17486  done:
17487   /* Leave the scope given by the nested-name-specifier.  We will
17488      enter the class scope itself while processing the members.  */
17489   if (pushed_scope)
17490     pop_scope (pushed_scope);
17491
17492   if (invalid_explicit_specialization_p)
17493     {
17494       end_specialization ();
17495       --parser->num_template_parameter_lists;
17496     }
17497
17498   if (type)
17499     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17500   *attributes_p = attributes;
17501  out:
17502   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17503   return type;
17504 }
17505
17506 /* Parse a class-key.
17507
17508    class-key:
17509      class
17510      struct
17511      union
17512
17513    Returns the kind of class-key specified, or none_type to indicate
17514    error.  */
17515
17516 static enum tag_types
17517 cp_parser_class_key (cp_parser* parser)
17518 {
17519   cp_token *token;
17520   enum tag_types tag_type;
17521
17522   /* Look for the class-key.  */
17523   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17524   if (!token)
17525     return none_type;
17526
17527   /* Check to see if the TOKEN is a class-key.  */
17528   tag_type = cp_parser_token_is_class_key (token);
17529   if (!tag_type)
17530     cp_parser_error (parser, "expected class-key");
17531   return tag_type;
17532 }
17533
17534 /* Parse an (optional) member-specification.
17535
17536    member-specification:
17537      member-declaration member-specification [opt]
17538      access-specifier : member-specification [opt]  */
17539
17540 static void
17541 cp_parser_member_specification_opt (cp_parser* parser)
17542 {
17543   while (true)
17544     {
17545       cp_token *token;
17546       enum rid keyword;
17547
17548       /* Peek at the next token.  */
17549       token = cp_lexer_peek_token (parser->lexer);
17550       /* If it's a `}', or EOF then we've seen all the members.  */
17551       if (token->type == CPP_CLOSE_BRACE
17552           || token->type == CPP_EOF
17553           || token->type == CPP_PRAGMA_EOL)
17554         break;
17555
17556       /* See if this token is a keyword.  */
17557       keyword = token->keyword;
17558       switch (keyword)
17559         {
17560         case RID_PUBLIC:
17561         case RID_PROTECTED:
17562         case RID_PRIVATE:
17563           /* Consume the access-specifier.  */
17564           cp_lexer_consume_token (parser->lexer);
17565           /* Remember which access-specifier is active.  */
17566           current_access_specifier = token->u.value;
17567           /* Look for the `:'.  */
17568           cp_parser_require (parser, CPP_COLON, RT_COLON);
17569           break;
17570
17571         default:
17572           /* Accept #pragmas at class scope.  */
17573           if (token->type == CPP_PRAGMA)
17574             {
17575               cp_parser_pragma (parser, pragma_external);
17576               break;
17577             }
17578
17579           /* Otherwise, the next construction must be a
17580              member-declaration.  */
17581           cp_parser_member_declaration (parser);
17582         }
17583     }
17584 }
17585
17586 /* Parse a member-declaration.
17587
17588    member-declaration:
17589      decl-specifier-seq [opt] member-declarator-list [opt] ;
17590      function-definition ; [opt]
17591      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17592      using-declaration
17593      template-declaration
17594
17595    member-declarator-list:
17596      member-declarator
17597      member-declarator-list , member-declarator
17598
17599    member-declarator:
17600      declarator pure-specifier [opt]
17601      declarator constant-initializer [opt]
17602      identifier [opt] : constant-expression
17603
17604    GNU Extensions:
17605
17606    member-declaration:
17607      __extension__ member-declaration
17608
17609    member-declarator:
17610      declarator attributes [opt] pure-specifier [opt]
17611      declarator attributes [opt] constant-initializer [opt]
17612      identifier [opt] attributes [opt] : constant-expression  
17613
17614    C++0x Extensions:
17615
17616    member-declaration:
17617      static_assert-declaration  */
17618
17619 static void
17620 cp_parser_member_declaration (cp_parser* parser)
17621 {
17622   cp_decl_specifier_seq decl_specifiers;
17623   tree prefix_attributes;
17624   tree decl;
17625   int declares_class_or_enum;
17626   bool friend_p;
17627   cp_token *token = NULL;
17628   cp_token *decl_spec_token_start = NULL;
17629   cp_token *initializer_token_start = NULL;
17630   int saved_pedantic;
17631   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17632
17633   /* Check for the `__extension__' keyword.  */
17634   if (cp_parser_extension_opt (parser, &saved_pedantic))
17635     {
17636       /* Recurse.  */
17637       cp_parser_member_declaration (parser);
17638       /* Restore the old value of the PEDANTIC flag.  */
17639       pedantic = saved_pedantic;
17640
17641       return;
17642     }
17643
17644   /* Check for a template-declaration.  */
17645   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17646     {
17647       /* An explicit specialization here is an error condition, and we
17648          expect the specialization handler to detect and report this.  */
17649       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17650           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17651         cp_parser_explicit_specialization (parser);
17652       else
17653         cp_parser_template_declaration (parser, /*member_p=*/true);
17654
17655       return;
17656     }
17657
17658   /* Check for a using-declaration.  */
17659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17660     {
17661       /* Parse the using-declaration.  */
17662       cp_parser_using_declaration (parser,
17663                                    /*access_declaration_p=*/false);
17664       return;
17665     }
17666
17667   /* Check for @defs.  */
17668   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17669     {
17670       tree ivar, member;
17671       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17672       ivar = ivar_chains;
17673       while (ivar)
17674         {
17675           member = ivar;
17676           ivar = TREE_CHAIN (member);
17677           TREE_CHAIN (member) = NULL_TREE;
17678           finish_member_declaration (member);
17679         }
17680       return;
17681     }
17682
17683   /* If the next token is `static_assert' we have a static assertion.  */
17684   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17685     {
17686       cp_parser_static_assert (parser, /*member_p=*/true);
17687       return;
17688     }
17689
17690   parser->colon_corrects_to_scope_p = false;
17691
17692   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17693     goto out;
17694
17695   /* Parse the decl-specifier-seq.  */
17696   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17697   cp_parser_decl_specifier_seq (parser,
17698                                 CP_PARSER_FLAGS_OPTIONAL,
17699                                 &decl_specifiers,
17700                                 &declares_class_or_enum);
17701   prefix_attributes = decl_specifiers.attributes;
17702   decl_specifiers.attributes = NULL_TREE;
17703   /* Check for an invalid type-name.  */
17704   if (!decl_specifiers.any_type_specifiers_p
17705       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17706     goto out;
17707   /* If there is no declarator, then the decl-specifier-seq should
17708      specify a type.  */
17709   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17710     {
17711       /* If there was no decl-specifier-seq, and the next token is a
17712          `;', then we have something like:
17713
17714            struct S { ; };
17715
17716          [class.mem]
17717
17718          Each member-declaration shall declare at least one member
17719          name of the class.  */
17720       if (!decl_specifiers.any_specifiers_p)
17721         {
17722           cp_token *token = cp_lexer_peek_token (parser->lexer);
17723           if (!in_system_header_at (token->location))
17724             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17725         }
17726       else
17727         {
17728           tree type;
17729
17730           /* See if this declaration is a friend.  */
17731           friend_p = cp_parser_friend_p (&decl_specifiers);
17732           /* If there were decl-specifiers, check to see if there was
17733              a class-declaration.  */
17734           type = check_tag_decl (&decl_specifiers);
17735           /* Nested classes have already been added to the class, but
17736              a `friend' needs to be explicitly registered.  */
17737           if (friend_p)
17738             {
17739               /* If the `friend' keyword was present, the friend must
17740                  be introduced with a class-key.  */
17741                if (!declares_class_or_enum)
17742                  error_at (decl_spec_token_start->location,
17743                            "a class-key must be used when declaring a friend");
17744                /* In this case:
17745
17746                     template <typename T> struct A {
17747                       friend struct A<T>::B;
17748                     };
17749
17750                   A<T>::B will be represented by a TYPENAME_TYPE, and
17751                   therefore not recognized by check_tag_decl.  */
17752                if (!type
17753                    && decl_specifiers.type
17754                    && TYPE_P (decl_specifiers.type))
17755                  type = decl_specifiers.type;
17756                if (!type || !TYPE_P (type))
17757                  error_at (decl_spec_token_start->location,
17758                            "friend declaration does not name a class or "
17759                            "function");
17760                else
17761                  make_friend_class (current_class_type, type,
17762                                     /*complain=*/true);
17763             }
17764           /* If there is no TYPE, an error message will already have
17765              been issued.  */
17766           else if (!type || type == error_mark_node)
17767             ;
17768           /* An anonymous aggregate has to be handled specially; such
17769              a declaration really declares a data member (with a
17770              particular type), as opposed to a nested class.  */
17771           else if (ANON_AGGR_TYPE_P (type))
17772             {
17773               /* Remove constructors and such from TYPE, now that we
17774                  know it is an anonymous aggregate.  */
17775               fixup_anonymous_aggr (type);
17776               /* And make the corresponding data member.  */
17777               decl = build_decl (decl_spec_token_start->location,
17778                                  FIELD_DECL, NULL_TREE, type);
17779               /* Add it to the class.  */
17780               finish_member_declaration (decl);
17781             }
17782           else
17783             cp_parser_check_access_in_redeclaration
17784                                               (TYPE_NAME (type),
17785                                                decl_spec_token_start->location);
17786         }
17787     }
17788   else
17789     {
17790       bool assume_semicolon = false;
17791
17792       /* See if these declarations will be friends.  */
17793       friend_p = cp_parser_friend_p (&decl_specifiers);
17794
17795       /* Keep going until we hit the `;' at the end of the
17796          declaration.  */
17797       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17798         {
17799           tree attributes = NULL_TREE;
17800           tree first_attribute;
17801
17802           /* Peek at the next token.  */
17803           token = cp_lexer_peek_token (parser->lexer);
17804
17805           /* Check for a bitfield declaration.  */
17806           if (token->type == CPP_COLON
17807               || (token->type == CPP_NAME
17808                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17809                   == CPP_COLON))
17810             {
17811               tree identifier;
17812               tree width;
17813
17814               /* Get the name of the bitfield.  Note that we cannot just
17815                  check TOKEN here because it may have been invalidated by
17816                  the call to cp_lexer_peek_nth_token above.  */
17817               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17818                 identifier = cp_parser_identifier (parser);
17819               else
17820                 identifier = NULL_TREE;
17821
17822               /* Consume the `:' token.  */
17823               cp_lexer_consume_token (parser->lexer);
17824               /* Get the width of the bitfield.  */
17825               width
17826                 = cp_parser_constant_expression (parser,
17827                                                  /*allow_non_constant=*/false,
17828                                                  NULL);
17829
17830               /* Look for attributes that apply to the bitfield.  */
17831               attributes = cp_parser_attributes_opt (parser);
17832               /* Remember which attributes are prefix attributes and
17833                  which are not.  */
17834               first_attribute = attributes;
17835               /* Combine the attributes.  */
17836               attributes = chainon (prefix_attributes, attributes);
17837
17838               /* Create the bitfield declaration.  */
17839               decl = grokbitfield (identifier
17840                                    ? make_id_declarator (NULL_TREE,
17841                                                          identifier,
17842                                                          sfk_none)
17843                                    : NULL,
17844                                    &decl_specifiers,
17845                                    width,
17846                                    attributes);
17847             }
17848           else
17849             {
17850               cp_declarator *declarator;
17851               tree initializer;
17852               tree asm_specification;
17853               int ctor_dtor_or_conv_p;
17854
17855               /* Parse the declarator.  */
17856               declarator
17857                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17858                                         &ctor_dtor_or_conv_p,
17859                                         /*parenthesized_p=*/NULL,
17860                                         /*member_p=*/true);
17861
17862               /* If something went wrong parsing the declarator, make sure
17863                  that we at least consume some tokens.  */
17864               if (declarator == cp_error_declarator)
17865                 {
17866                   /* Skip to the end of the statement.  */
17867                   cp_parser_skip_to_end_of_statement (parser);
17868                   /* If the next token is not a semicolon, that is
17869                      probably because we just skipped over the body of
17870                      a function.  So, we consume a semicolon if
17871                      present, but do not issue an error message if it
17872                      is not present.  */
17873                   if (cp_lexer_next_token_is (parser->lexer,
17874                                               CPP_SEMICOLON))
17875                     cp_lexer_consume_token (parser->lexer);
17876                   goto out;
17877                 }
17878
17879               if (declares_class_or_enum & 2)
17880                 cp_parser_check_for_definition_in_return_type
17881                                             (declarator, decl_specifiers.type,
17882                                              decl_specifiers.type_location);
17883
17884               /* Look for an asm-specification.  */
17885               asm_specification = cp_parser_asm_specification_opt (parser);
17886               /* Look for attributes that apply to the declaration.  */
17887               attributes = cp_parser_attributes_opt (parser);
17888               /* Remember which attributes are prefix attributes and
17889                  which are not.  */
17890               first_attribute = attributes;
17891               /* Combine the attributes.  */
17892               attributes = chainon (prefix_attributes, attributes);
17893
17894               /* If it's an `=', then we have a constant-initializer or a
17895                  pure-specifier.  It is not correct to parse the
17896                  initializer before registering the member declaration
17897                  since the member declaration should be in scope while
17898                  its initializer is processed.  However, the rest of the
17899                  front end does not yet provide an interface that allows
17900                  us to handle this correctly.  */
17901               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17902                 {
17903                   /* In [class.mem]:
17904
17905                      A pure-specifier shall be used only in the declaration of
17906                      a virtual function.
17907
17908                      A member-declarator can contain a constant-initializer
17909                      only if it declares a static member of integral or
17910                      enumeration type.
17911
17912                      Therefore, if the DECLARATOR is for a function, we look
17913                      for a pure-specifier; otherwise, we look for a
17914                      constant-initializer.  When we call `grokfield', it will
17915                      perform more stringent semantics checks.  */
17916                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17917                   if (function_declarator_p (declarator))
17918                     initializer = cp_parser_pure_specifier (parser);
17919                   else
17920                     /* Parse the initializer.  */
17921                     initializer = cp_parser_constant_initializer (parser);
17922                 }
17923               /* Otherwise, there is no initializer.  */
17924               else
17925                 initializer = NULL_TREE;
17926
17927               /* See if we are probably looking at a function
17928                  definition.  We are certainly not looking at a
17929                  member-declarator.  Calling `grokfield' has
17930                  side-effects, so we must not do it unless we are sure
17931                  that we are looking at a member-declarator.  */
17932               if (cp_parser_token_starts_function_definition_p
17933                   (cp_lexer_peek_token (parser->lexer)))
17934                 {
17935                   /* The grammar does not allow a pure-specifier to be
17936                      used when a member function is defined.  (It is
17937                      possible that this fact is an oversight in the
17938                      standard, since a pure function may be defined
17939                      outside of the class-specifier.  */
17940                   if (initializer)
17941                     error_at (initializer_token_start->location,
17942                               "pure-specifier on function-definition");
17943                   decl = cp_parser_save_member_function_body (parser,
17944                                                               &decl_specifiers,
17945                                                               declarator,
17946                                                               attributes);
17947                   /* If the member was not a friend, declare it here.  */
17948                   if (!friend_p)
17949                     finish_member_declaration (decl);
17950                   /* Peek at the next token.  */
17951                   token = cp_lexer_peek_token (parser->lexer);
17952                   /* If the next token is a semicolon, consume it.  */
17953                   if (token->type == CPP_SEMICOLON)
17954                     cp_lexer_consume_token (parser->lexer);
17955                   goto out;
17956                 }
17957               else
17958                 if (declarator->kind == cdk_function)
17959                   declarator->id_loc = token->location;
17960                 /* Create the declaration.  */
17961                 decl = grokfield (declarator, &decl_specifiers,
17962                                   initializer, /*init_const_expr_p=*/true,
17963                                   asm_specification,
17964                                   attributes);
17965             }
17966
17967           /* Reset PREFIX_ATTRIBUTES.  */
17968           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17969             attributes = TREE_CHAIN (attributes);
17970           if (attributes)
17971             TREE_CHAIN (attributes) = NULL_TREE;
17972
17973           /* If there is any qualification still in effect, clear it
17974              now; we will be starting fresh with the next declarator.  */
17975           parser->scope = NULL_TREE;
17976           parser->qualifying_scope = NULL_TREE;
17977           parser->object_scope = NULL_TREE;
17978           /* If it's a `,', then there are more declarators.  */
17979           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17980             cp_lexer_consume_token (parser->lexer);
17981           /* If the next token isn't a `;', then we have a parse error.  */
17982           else if (cp_lexer_next_token_is_not (parser->lexer,
17983                                                CPP_SEMICOLON))
17984             {
17985               /* The next token might be a ways away from where the
17986                  actual semicolon is missing.  Find the previous token
17987                  and use that for our error position.  */
17988               cp_token *token = cp_lexer_previous_token (parser->lexer);
17989               error_at (token->location,
17990                         "expected %<;%> at end of member declaration");
17991
17992               /* Assume that the user meant to provide a semicolon.  If
17993                  we were to cp_parser_skip_to_end_of_statement, we might
17994                  skip to a semicolon inside a member function definition
17995                  and issue nonsensical error messages.  */
17996               assume_semicolon = true;
17997             }
17998
17999           if (decl)
18000             {
18001               /* Add DECL to the list of members.  */
18002               if (!friend_p)
18003                 finish_member_declaration (decl);
18004
18005               if (TREE_CODE (decl) == FUNCTION_DECL)
18006                 cp_parser_save_default_args (parser, decl);
18007             }
18008
18009           if (assume_semicolon)
18010             goto out;
18011         }
18012     }
18013
18014   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18015  out:
18016   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18017 }
18018
18019 /* Parse a pure-specifier.
18020
18021    pure-specifier:
18022      = 0
18023
18024    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18025    Otherwise, ERROR_MARK_NODE is returned.  */
18026
18027 static tree
18028 cp_parser_pure_specifier (cp_parser* parser)
18029 {
18030   cp_token *token;
18031
18032   /* Look for the `=' token.  */
18033   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18034     return error_mark_node;
18035   /* Look for the `0' token.  */
18036   token = cp_lexer_peek_token (parser->lexer);
18037
18038   if (token->type == CPP_EOF
18039       || token->type == CPP_PRAGMA_EOL)
18040     return error_mark_node;
18041
18042   cp_lexer_consume_token (parser->lexer);
18043
18044   /* Accept = default or = delete in c++0x mode.  */
18045   if (token->keyword == RID_DEFAULT
18046       || token->keyword == RID_DELETE)
18047     {
18048       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18049       return token->u.value;
18050     }
18051
18052   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18053   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18054     {
18055       cp_parser_error (parser,
18056                        "invalid pure specifier (only %<= 0%> is allowed)");
18057       cp_parser_skip_to_end_of_statement (parser);
18058       return error_mark_node;
18059     }
18060   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18061     {
18062       error_at (token->location, "templates may not be %<virtual%>");
18063       return error_mark_node;
18064     }
18065
18066   return integer_zero_node;
18067 }
18068
18069 /* Parse a constant-initializer.
18070
18071    constant-initializer:
18072      = constant-expression
18073
18074    Returns a representation of the constant-expression.  */
18075
18076 static tree
18077 cp_parser_constant_initializer (cp_parser* parser)
18078 {
18079   /* Look for the `=' token.  */
18080   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18081     return error_mark_node;
18082
18083   /* It is invalid to write:
18084
18085        struct S { static const int i = { 7 }; };
18086
18087      */
18088   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18089     {
18090       cp_parser_error (parser,
18091                        "a brace-enclosed initializer is not allowed here");
18092       /* Consume the opening brace.  */
18093       cp_lexer_consume_token (parser->lexer);
18094       /* Skip the initializer.  */
18095       cp_parser_skip_to_closing_brace (parser);
18096       /* Look for the trailing `}'.  */
18097       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18098
18099       return error_mark_node;
18100     }
18101
18102   return cp_parser_constant_expression (parser,
18103                                         /*allow_non_constant=*/false,
18104                                         NULL);
18105 }
18106
18107 /* Derived classes [gram.class.derived] */
18108
18109 /* Parse a base-clause.
18110
18111    base-clause:
18112      : base-specifier-list
18113
18114    base-specifier-list:
18115      base-specifier ... [opt]
18116      base-specifier-list , base-specifier ... [opt]
18117
18118    Returns a TREE_LIST representing the base-classes, in the order in
18119    which they were declared.  The representation of each node is as
18120    described by cp_parser_base_specifier.
18121
18122    In the case that no bases are specified, this function will return
18123    NULL_TREE, not ERROR_MARK_NODE.  */
18124
18125 static tree
18126 cp_parser_base_clause (cp_parser* parser)
18127 {
18128   tree bases = NULL_TREE;
18129
18130   /* Look for the `:' that begins the list.  */
18131   cp_parser_require (parser, CPP_COLON, RT_COLON);
18132
18133   /* Scan the base-specifier-list.  */
18134   while (true)
18135     {
18136       cp_token *token;
18137       tree base;
18138       bool pack_expansion_p = false;
18139
18140       /* Look for the base-specifier.  */
18141       base = cp_parser_base_specifier (parser);
18142       /* Look for the (optional) ellipsis. */
18143       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18144         {
18145           /* Consume the `...'. */
18146           cp_lexer_consume_token (parser->lexer);
18147
18148           pack_expansion_p = true;
18149         }
18150
18151       /* Add BASE to the front of the list.  */
18152       if (base != error_mark_node)
18153         {
18154           if (pack_expansion_p)
18155             /* Make this a pack expansion type. */
18156             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18157           
18158
18159           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18160             {
18161               TREE_CHAIN (base) = bases;
18162               bases = base;
18163             }
18164         }
18165       /* Peek at the next token.  */
18166       token = cp_lexer_peek_token (parser->lexer);
18167       /* If it's not a comma, then the list is complete.  */
18168       if (token->type != CPP_COMMA)
18169         break;
18170       /* Consume the `,'.  */
18171       cp_lexer_consume_token (parser->lexer);
18172     }
18173
18174   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18175      base class had a qualified name.  However, the next name that
18176      appears is certainly not qualified.  */
18177   parser->scope = NULL_TREE;
18178   parser->qualifying_scope = NULL_TREE;
18179   parser->object_scope = NULL_TREE;
18180
18181   return nreverse (bases);
18182 }
18183
18184 /* Parse a base-specifier.
18185
18186    base-specifier:
18187      :: [opt] nested-name-specifier [opt] class-name
18188      virtual access-specifier [opt] :: [opt] nested-name-specifier
18189        [opt] class-name
18190      access-specifier virtual [opt] :: [opt] nested-name-specifier
18191        [opt] class-name
18192
18193    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18194    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18195    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18196    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18197
18198 static tree
18199 cp_parser_base_specifier (cp_parser* parser)
18200 {
18201   cp_token *token;
18202   bool done = false;
18203   bool virtual_p = false;
18204   bool duplicate_virtual_error_issued_p = false;
18205   bool duplicate_access_error_issued_p = false;
18206   bool class_scope_p, template_p;
18207   tree access = access_default_node;
18208   tree type;
18209
18210   /* Process the optional `virtual' and `access-specifier'.  */
18211   while (!done)
18212     {
18213       /* Peek at the next token.  */
18214       token = cp_lexer_peek_token (parser->lexer);
18215       /* Process `virtual'.  */
18216       switch (token->keyword)
18217         {
18218         case RID_VIRTUAL:
18219           /* If `virtual' appears more than once, issue an error.  */
18220           if (virtual_p && !duplicate_virtual_error_issued_p)
18221             {
18222               cp_parser_error (parser,
18223                                "%<virtual%> specified more than once in base-specified");
18224               duplicate_virtual_error_issued_p = true;
18225             }
18226
18227           virtual_p = true;
18228
18229           /* Consume the `virtual' token.  */
18230           cp_lexer_consume_token (parser->lexer);
18231
18232           break;
18233
18234         case RID_PUBLIC:
18235         case RID_PROTECTED:
18236         case RID_PRIVATE:
18237           /* If more than one access specifier appears, issue an
18238              error.  */
18239           if (access != access_default_node
18240               && !duplicate_access_error_issued_p)
18241             {
18242               cp_parser_error (parser,
18243                                "more than one access specifier in base-specified");
18244               duplicate_access_error_issued_p = true;
18245             }
18246
18247           access = ridpointers[(int) token->keyword];
18248
18249           /* Consume the access-specifier.  */
18250           cp_lexer_consume_token (parser->lexer);
18251
18252           break;
18253
18254         default:
18255           done = true;
18256           break;
18257         }
18258     }
18259   /* It is not uncommon to see programs mechanically, erroneously, use
18260      the 'typename' keyword to denote (dependent) qualified types
18261      as base classes.  */
18262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18263     {
18264       token = cp_lexer_peek_token (parser->lexer);
18265       if (!processing_template_decl)
18266         error_at (token->location,
18267                   "keyword %<typename%> not allowed outside of templates");
18268       else
18269         error_at (token->location,
18270                   "keyword %<typename%> not allowed in this context "
18271                   "(the base class is implicitly a type)");
18272       cp_lexer_consume_token (parser->lexer);
18273     }
18274
18275   /* Look for the optional `::' operator.  */
18276   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18277   /* Look for the nested-name-specifier.  The simplest way to
18278      implement:
18279
18280        [temp.res]
18281
18282        The keyword `typename' is not permitted in a base-specifier or
18283        mem-initializer; in these contexts a qualified name that
18284        depends on a template-parameter is implicitly assumed to be a
18285        type name.
18286
18287      is to pretend that we have seen the `typename' keyword at this
18288      point.  */
18289   cp_parser_nested_name_specifier_opt (parser,
18290                                        /*typename_keyword_p=*/true,
18291                                        /*check_dependency_p=*/true,
18292                                        typename_type,
18293                                        /*is_declaration=*/true);
18294   /* If the base class is given by a qualified name, assume that names
18295      we see are type names or templates, as appropriate.  */
18296   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18297   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18298
18299   /* Finally, look for the class-name.  */
18300   type = cp_parser_class_name (parser,
18301                                class_scope_p,
18302                                template_p,
18303                                typename_type,
18304                                /*check_dependency_p=*/true,
18305                                /*class_head_p=*/false,
18306                                /*is_declaration=*/true);
18307
18308   if (type == error_mark_node)
18309     return error_mark_node;
18310
18311   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18312 }
18313
18314 /* Exception handling [gram.exception] */
18315
18316 /* Parse an (optional) exception-specification.
18317
18318    exception-specification:
18319      throw ( type-id-list [opt] )
18320
18321    Returns a TREE_LIST representing the exception-specification.  The
18322    TREE_VALUE of each node is a type.  */
18323
18324 static tree
18325 cp_parser_exception_specification_opt (cp_parser* parser)
18326 {
18327   cp_token *token;
18328   tree type_id_list;
18329   const char *saved_message;
18330
18331   /* Peek at the next token.  */
18332   token = cp_lexer_peek_token (parser->lexer);
18333
18334   /* Is it a noexcept-specification?  */
18335   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18336     {
18337       tree expr;
18338       cp_lexer_consume_token (parser->lexer);
18339
18340       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18341         {
18342           cp_lexer_consume_token (parser->lexer);
18343
18344           /* Types may not be defined in an exception-specification.  */
18345           saved_message = parser->type_definition_forbidden_message;
18346           parser->type_definition_forbidden_message
18347             = G_("types may not be defined in an exception-specification");
18348
18349           expr = cp_parser_constant_expression (parser, false, NULL);
18350
18351           /* Restore the saved message.  */
18352           parser->type_definition_forbidden_message = saved_message;
18353
18354           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18355         }
18356       else
18357         expr = boolean_true_node;
18358
18359       return build_noexcept_spec (expr, tf_warning_or_error);
18360     }
18361
18362   /* If it's not `throw', then there's no exception-specification.  */
18363   if (!cp_parser_is_keyword (token, RID_THROW))
18364     return NULL_TREE;
18365
18366 #if 0
18367   /* Enable this once a lot of code has transitioned to noexcept?  */
18368   if (cxx_dialect == cxx0x && !in_system_header)
18369     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18370              "deprecated in C++0x; use %<noexcept%> instead");
18371 #endif
18372
18373   /* Consume the `throw'.  */
18374   cp_lexer_consume_token (parser->lexer);
18375
18376   /* Look for the `('.  */
18377   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18378
18379   /* Peek at the next token.  */
18380   token = cp_lexer_peek_token (parser->lexer);
18381   /* If it's not a `)', then there is a type-id-list.  */
18382   if (token->type != CPP_CLOSE_PAREN)
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       /* Parse the type-id-list.  */
18389       type_id_list = cp_parser_type_id_list (parser);
18390       /* Restore the saved message.  */
18391       parser->type_definition_forbidden_message = saved_message;
18392     }
18393   else
18394     type_id_list = empty_except_spec;
18395
18396   /* Look for the `)'.  */
18397   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18398
18399   return type_id_list;
18400 }
18401
18402 /* Parse an (optional) type-id-list.
18403
18404    type-id-list:
18405      type-id ... [opt]
18406      type-id-list , type-id ... [opt]
18407
18408    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18409    in the order that the types were presented.  */
18410
18411 static tree
18412 cp_parser_type_id_list (cp_parser* parser)
18413 {
18414   tree types = NULL_TREE;
18415
18416   while (true)
18417     {
18418       cp_token *token;
18419       tree type;
18420
18421       /* Get the next type-id.  */
18422       type = cp_parser_type_id (parser);
18423       /* Parse the optional ellipsis. */
18424       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18425         {
18426           /* Consume the `...'. */
18427           cp_lexer_consume_token (parser->lexer);
18428
18429           /* Turn the type into a pack expansion expression. */
18430           type = make_pack_expansion (type);
18431         }
18432       /* Add it to the list.  */
18433       types = add_exception_specifier (types, type, /*complain=*/1);
18434       /* Peek at the next token.  */
18435       token = cp_lexer_peek_token (parser->lexer);
18436       /* If it is not a `,', we are done.  */
18437       if (token->type != CPP_COMMA)
18438         break;
18439       /* Consume the `,'.  */
18440       cp_lexer_consume_token (parser->lexer);
18441     }
18442
18443   return nreverse (types);
18444 }
18445
18446 /* Parse a try-block.
18447
18448    try-block:
18449      try compound-statement handler-seq  */
18450
18451 static tree
18452 cp_parser_try_block (cp_parser* parser)
18453 {
18454   tree try_block;
18455
18456   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18457   try_block = begin_try_block ();
18458   cp_parser_compound_statement (parser, NULL, true);
18459   finish_try_block (try_block);
18460   cp_parser_handler_seq (parser);
18461   finish_handler_sequence (try_block);
18462
18463   return try_block;
18464 }
18465
18466 /* Parse a function-try-block.
18467
18468    function-try-block:
18469      try ctor-initializer [opt] function-body handler-seq  */
18470
18471 static bool
18472 cp_parser_function_try_block (cp_parser* parser)
18473 {
18474   tree compound_stmt;
18475   tree try_block;
18476   bool ctor_initializer_p;
18477
18478   /* Look for the `try' keyword.  */
18479   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18480     return false;
18481   /* Let the rest of the front end know where we are.  */
18482   try_block = begin_function_try_block (&compound_stmt);
18483   /* Parse the function-body.  */
18484   ctor_initializer_p
18485     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18486   /* We're done with the `try' part.  */
18487   finish_function_try_block (try_block);
18488   /* Parse the handlers.  */
18489   cp_parser_handler_seq (parser);
18490   /* We're done with the handlers.  */
18491   finish_function_handler_sequence (try_block, compound_stmt);
18492
18493   return ctor_initializer_p;
18494 }
18495
18496 /* Parse a handler-seq.
18497
18498    handler-seq:
18499      handler handler-seq [opt]  */
18500
18501 static void
18502 cp_parser_handler_seq (cp_parser* parser)
18503 {
18504   while (true)
18505     {
18506       cp_token *token;
18507
18508       /* Parse the handler.  */
18509       cp_parser_handler (parser);
18510       /* Peek at the next token.  */
18511       token = cp_lexer_peek_token (parser->lexer);
18512       /* If it's not `catch' then there are no more handlers.  */
18513       if (!cp_parser_is_keyword (token, RID_CATCH))
18514         break;
18515     }
18516 }
18517
18518 /* Parse a handler.
18519
18520    handler:
18521      catch ( exception-declaration ) compound-statement  */
18522
18523 static void
18524 cp_parser_handler (cp_parser* parser)
18525 {
18526   tree handler;
18527   tree declaration;
18528
18529   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18530   handler = begin_handler ();
18531   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18532   declaration = cp_parser_exception_declaration (parser);
18533   finish_handler_parms (declaration, handler);
18534   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18535   cp_parser_compound_statement (parser, NULL, false);
18536   finish_handler (handler);
18537 }
18538
18539 /* Parse an exception-declaration.
18540
18541    exception-declaration:
18542      type-specifier-seq declarator
18543      type-specifier-seq abstract-declarator
18544      type-specifier-seq
18545      ...
18546
18547    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18548    ellipsis variant is used.  */
18549
18550 static tree
18551 cp_parser_exception_declaration (cp_parser* parser)
18552 {
18553   cp_decl_specifier_seq type_specifiers;
18554   cp_declarator *declarator;
18555   const char *saved_message;
18556
18557   /* If it's an ellipsis, it's easy to handle.  */
18558   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18559     {
18560       /* Consume the `...' token.  */
18561       cp_lexer_consume_token (parser->lexer);
18562       return NULL_TREE;
18563     }
18564
18565   /* Types may not be defined in exception-declarations.  */
18566   saved_message = parser->type_definition_forbidden_message;
18567   parser->type_definition_forbidden_message
18568     = G_("types may not be defined in exception-declarations");
18569
18570   /* Parse the type-specifier-seq.  */
18571   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18572                                 /*is_trailing_return=*/false,
18573                                 &type_specifiers);
18574   /* If it's a `)', then there is no declarator.  */
18575   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18576     declarator = NULL;
18577   else
18578     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18579                                        /*ctor_dtor_or_conv_p=*/NULL,
18580                                        /*parenthesized_p=*/NULL,
18581                                        /*member_p=*/false);
18582
18583   /* Restore the saved message.  */
18584   parser->type_definition_forbidden_message = saved_message;
18585
18586   if (!type_specifiers.any_specifiers_p)
18587     return error_mark_node;
18588
18589   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18590 }
18591
18592 /* Parse a throw-expression.
18593
18594    throw-expression:
18595      throw assignment-expression [opt]
18596
18597    Returns a THROW_EXPR representing the throw-expression.  */
18598
18599 static tree
18600 cp_parser_throw_expression (cp_parser* parser)
18601 {
18602   tree expression;
18603   cp_token* token;
18604
18605   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18606   token = cp_lexer_peek_token (parser->lexer);
18607   /* Figure out whether or not there is an assignment-expression
18608      following the "throw" keyword.  */
18609   if (token->type == CPP_COMMA
18610       || token->type == CPP_SEMICOLON
18611       || token->type == CPP_CLOSE_PAREN
18612       || token->type == CPP_CLOSE_SQUARE
18613       || token->type == CPP_CLOSE_BRACE
18614       || token->type == CPP_COLON)
18615     expression = NULL_TREE;
18616   else
18617     expression = cp_parser_assignment_expression (parser,
18618                                                   /*cast_p=*/false, NULL);
18619
18620   return build_throw (expression);
18621 }
18622
18623 /* GNU Extensions */
18624
18625 /* Parse an (optional) asm-specification.
18626
18627    asm-specification:
18628      asm ( string-literal )
18629
18630    If the asm-specification is present, returns a STRING_CST
18631    corresponding to the string-literal.  Otherwise, returns
18632    NULL_TREE.  */
18633
18634 static tree
18635 cp_parser_asm_specification_opt (cp_parser* parser)
18636 {
18637   cp_token *token;
18638   tree asm_specification;
18639
18640   /* Peek at the next token.  */
18641   token = cp_lexer_peek_token (parser->lexer);
18642   /* If the next token isn't the `asm' keyword, then there's no
18643      asm-specification.  */
18644   if (!cp_parser_is_keyword (token, RID_ASM))
18645     return NULL_TREE;
18646
18647   /* Consume the `asm' token.  */
18648   cp_lexer_consume_token (parser->lexer);
18649   /* Look for the `('.  */
18650   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18651
18652   /* Look for the string-literal.  */
18653   asm_specification = cp_parser_string_literal (parser, false, false);
18654
18655   /* Look for the `)'.  */
18656   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18657
18658   return asm_specification;
18659 }
18660
18661 /* Parse an asm-operand-list.
18662
18663    asm-operand-list:
18664      asm-operand
18665      asm-operand-list , asm-operand
18666
18667    asm-operand:
18668      string-literal ( expression )
18669      [ string-literal ] string-literal ( expression )
18670
18671    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18672    each node is the expression.  The TREE_PURPOSE is itself a
18673    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18674    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18675    is a STRING_CST for the string literal before the parenthesis. Returns
18676    ERROR_MARK_NODE if any of the operands are invalid.  */
18677
18678 static tree
18679 cp_parser_asm_operand_list (cp_parser* parser)
18680 {
18681   tree asm_operands = NULL_TREE;
18682   bool invalid_operands = false;
18683
18684   while (true)
18685     {
18686       tree string_literal;
18687       tree expression;
18688       tree name;
18689
18690       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18691         {
18692           /* Consume the `[' token.  */
18693           cp_lexer_consume_token (parser->lexer);
18694           /* Read the operand name.  */
18695           name = cp_parser_identifier (parser);
18696           if (name != error_mark_node)
18697             name = build_string (IDENTIFIER_LENGTH (name),
18698                                  IDENTIFIER_POINTER (name));
18699           /* Look for the closing `]'.  */
18700           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18701         }
18702       else
18703         name = NULL_TREE;
18704       /* Look for the string-literal.  */
18705       string_literal = cp_parser_string_literal (parser, false, false);
18706
18707       /* Look for the `('.  */
18708       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18709       /* Parse the expression.  */
18710       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18711       /* Look for the `)'.  */
18712       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18713
18714       if (name == error_mark_node 
18715           || string_literal == error_mark_node 
18716           || expression == error_mark_node)
18717         invalid_operands = true;
18718
18719       /* Add this operand to the list.  */
18720       asm_operands = tree_cons (build_tree_list (name, string_literal),
18721                                 expression,
18722                                 asm_operands);
18723       /* If the next token is not a `,', there are no more
18724          operands.  */
18725       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18726         break;
18727       /* Consume the `,'.  */
18728       cp_lexer_consume_token (parser->lexer);
18729     }
18730
18731   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18732 }
18733
18734 /* Parse an asm-clobber-list.
18735
18736    asm-clobber-list:
18737      string-literal
18738      asm-clobber-list , string-literal
18739
18740    Returns a TREE_LIST, indicating the clobbers in the order that they
18741    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18742
18743 static tree
18744 cp_parser_asm_clobber_list (cp_parser* parser)
18745 {
18746   tree clobbers = NULL_TREE;
18747
18748   while (true)
18749     {
18750       tree string_literal;
18751
18752       /* Look for the string literal.  */
18753       string_literal = cp_parser_string_literal (parser, false, false);
18754       /* Add it to the list.  */
18755       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18756       /* If the next token is not a `,', then the list is
18757          complete.  */
18758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18759         break;
18760       /* Consume the `,' token.  */
18761       cp_lexer_consume_token (parser->lexer);
18762     }
18763
18764   return clobbers;
18765 }
18766
18767 /* Parse an asm-label-list.
18768
18769    asm-label-list:
18770      identifier
18771      asm-label-list , identifier
18772
18773    Returns a TREE_LIST, indicating the labels in the order that they
18774    appeared.  The TREE_VALUE of each node is a label.  */
18775
18776 static tree
18777 cp_parser_asm_label_list (cp_parser* parser)
18778 {
18779   tree labels = NULL_TREE;
18780
18781   while (true)
18782     {
18783       tree identifier, label, name;
18784
18785       /* Look for the identifier.  */
18786       identifier = cp_parser_identifier (parser);
18787       if (!error_operand_p (identifier))
18788         {
18789           label = lookup_label (identifier);
18790           if (TREE_CODE (label) == LABEL_DECL)
18791             {
18792               TREE_USED (label) = 1;
18793               check_goto (label);
18794               name = build_string (IDENTIFIER_LENGTH (identifier),
18795                                    IDENTIFIER_POINTER (identifier));
18796               labels = tree_cons (name, label, labels);
18797             }
18798         }
18799       /* If the next token is not a `,', then the list is
18800          complete.  */
18801       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18802         break;
18803       /* Consume the `,' token.  */
18804       cp_lexer_consume_token (parser->lexer);
18805     }
18806
18807   return nreverse (labels);
18808 }
18809
18810 /* Parse an (optional) series of attributes.
18811
18812    attributes:
18813      attributes attribute
18814
18815    attribute:
18816      __attribute__ (( attribute-list [opt] ))
18817
18818    The return value is as for cp_parser_attribute_list.  */
18819
18820 static tree
18821 cp_parser_attributes_opt (cp_parser* parser)
18822 {
18823   tree attributes = NULL_TREE;
18824
18825   while (true)
18826     {
18827       cp_token *token;
18828       tree attribute_list;
18829
18830       /* Peek at the next token.  */
18831       token = cp_lexer_peek_token (parser->lexer);
18832       /* If it's not `__attribute__', then we're done.  */
18833       if (token->keyword != RID_ATTRIBUTE)
18834         break;
18835
18836       /* Consume the `__attribute__' keyword.  */
18837       cp_lexer_consume_token (parser->lexer);
18838       /* Look for the two `(' tokens.  */
18839       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18840       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18841
18842       /* Peek at the next token.  */
18843       token = cp_lexer_peek_token (parser->lexer);
18844       if (token->type != CPP_CLOSE_PAREN)
18845         /* Parse the attribute-list.  */
18846         attribute_list = cp_parser_attribute_list (parser);
18847       else
18848         /* If the next token is a `)', then there is no attribute
18849            list.  */
18850         attribute_list = NULL;
18851
18852       /* Look for the two `)' tokens.  */
18853       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18854       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18855
18856       /* Add these new attributes to the list.  */
18857       attributes = chainon (attributes, attribute_list);
18858     }
18859
18860   return attributes;
18861 }
18862
18863 /* Parse an attribute-list.
18864
18865    attribute-list:
18866      attribute
18867      attribute-list , attribute
18868
18869    attribute:
18870      identifier
18871      identifier ( identifier )
18872      identifier ( identifier , expression-list )
18873      identifier ( expression-list )
18874
18875    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18876    to an attribute.  The TREE_PURPOSE of each node is the identifier
18877    indicating which attribute is in use.  The TREE_VALUE represents
18878    the arguments, if any.  */
18879
18880 static tree
18881 cp_parser_attribute_list (cp_parser* parser)
18882 {
18883   tree attribute_list = NULL_TREE;
18884   bool save_translate_strings_p = parser->translate_strings_p;
18885
18886   parser->translate_strings_p = false;
18887   while (true)
18888     {
18889       cp_token *token;
18890       tree identifier;
18891       tree attribute;
18892
18893       /* Look for the identifier.  We also allow keywords here; for
18894          example `__attribute__ ((const))' is legal.  */
18895       token = cp_lexer_peek_token (parser->lexer);
18896       if (token->type == CPP_NAME
18897           || token->type == CPP_KEYWORD)
18898         {
18899           tree arguments = NULL_TREE;
18900
18901           /* Consume the token.  */
18902           token = cp_lexer_consume_token (parser->lexer);
18903
18904           /* Save away the identifier that indicates which attribute
18905              this is.  */
18906           identifier = (token->type == CPP_KEYWORD) 
18907             /* For keywords, use the canonical spelling, not the
18908                parsed identifier.  */
18909             ? ridpointers[(int) token->keyword]
18910             : token->u.value;
18911           
18912           attribute = build_tree_list (identifier, NULL_TREE);
18913
18914           /* Peek at the next token.  */
18915           token = cp_lexer_peek_token (parser->lexer);
18916           /* If it's an `(', then parse the attribute arguments.  */
18917           if (token->type == CPP_OPEN_PAREN)
18918             {
18919               VEC(tree,gc) *vec;
18920               int attr_flag = (attribute_takes_identifier_p (identifier)
18921                                ? id_attr : normal_attr);
18922               vec = cp_parser_parenthesized_expression_list
18923                     (parser, attr_flag, /*cast_p=*/false,
18924                      /*allow_expansion_p=*/false,
18925                      /*non_constant_p=*/NULL);
18926               if (vec == NULL)
18927                 arguments = error_mark_node;
18928               else
18929                 {
18930                   arguments = build_tree_list_vec (vec);
18931                   release_tree_vector (vec);
18932                 }
18933               /* Save the arguments away.  */
18934               TREE_VALUE (attribute) = arguments;
18935             }
18936
18937           if (arguments != error_mark_node)
18938             {
18939               /* Add this attribute to the list.  */
18940               TREE_CHAIN (attribute) = attribute_list;
18941               attribute_list = attribute;
18942             }
18943
18944           token = cp_lexer_peek_token (parser->lexer);
18945         }
18946       /* Now, look for more attributes.  If the next token isn't a
18947          `,', we're done.  */
18948       if (token->type != CPP_COMMA)
18949         break;
18950
18951       /* Consume the comma and keep going.  */
18952       cp_lexer_consume_token (parser->lexer);
18953     }
18954   parser->translate_strings_p = save_translate_strings_p;
18955
18956   /* We built up the list in reverse order.  */
18957   return nreverse (attribute_list);
18958 }
18959
18960 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18961    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18962    current value of the PEDANTIC flag, regardless of whether or not
18963    the `__extension__' keyword is present.  The caller is responsible
18964    for restoring the value of the PEDANTIC flag.  */
18965
18966 static bool
18967 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18968 {
18969   /* Save the old value of the PEDANTIC flag.  */
18970   *saved_pedantic = pedantic;
18971
18972   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18973     {
18974       /* Consume the `__extension__' token.  */
18975       cp_lexer_consume_token (parser->lexer);
18976       /* We're not being pedantic while the `__extension__' keyword is
18977          in effect.  */
18978       pedantic = 0;
18979
18980       return true;
18981     }
18982
18983   return false;
18984 }
18985
18986 /* Parse a label declaration.
18987
18988    label-declaration:
18989      __label__ label-declarator-seq ;
18990
18991    label-declarator-seq:
18992      identifier , label-declarator-seq
18993      identifier  */
18994
18995 static void
18996 cp_parser_label_declaration (cp_parser* parser)
18997 {
18998   /* Look for the `__label__' keyword.  */
18999   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19000
19001   while (true)
19002     {
19003       tree identifier;
19004
19005       /* Look for an identifier.  */
19006       identifier = cp_parser_identifier (parser);
19007       /* If we failed, stop.  */
19008       if (identifier == error_mark_node)
19009         break;
19010       /* Declare it as a label.  */
19011       finish_label_decl (identifier);
19012       /* If the next token is a `;', stop.  */
19013       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19014         break;
19015       /* Look for the `,' separating the label declarations.  */
19016       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19017     }
19018
19019   /* Look for the final `;'.  */
19020   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19021 }
19022
19023 /* Support Functions */
19024
19025 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19026    NAME should have one of the representations used for an
19027    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19028    is returned.  If PARSER->SCOPE is a dependent type, then a
19029    SCOPE_REF is returned.
19030
19031    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19032    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19033    was formed.  Abstractly, such entities should not be passed to this
19034    function, because they do not need to be looked up, but it is
19035    simpler to check for this special case here, rather than at the
19036    call-sites.
19037
19038    In cases not explicitly covered above, this function returns a
19039    DECL, OVERLOAD, or baselink representing the result of the lookup.
19040    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19041    is returned.
19042
19043    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19044    (e.g., "struct") that was used.  In that case bindings that do not
19045    refer to types are ignored.
19046
19047    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19048    ignored.
19049
19050    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19051    are ignored.
19052
19053    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19054    types.
19055
19056    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19057    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19058    NULL_TREE otherwise.  */
19059
19060 static tree
19061 cp_parser_lookup_name (cp_parser *parser, tree name,
19062                        enum tag_types tag_type,
19063                        bool is_template,
19064                        bool is_namespace,
19065                        bool check_dependency,
19066                        tree *ambiguous_decls,
19067                        location_t name_location)
19068 {
19069   int flags = 0;
19070   tree decl;
19071   tree object_type = parser->context->object_type;
19072
19073   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19074     flags |= LOOKUP_COMPLAIN;
19075
19076   /* Assume that the lookup will be unambiguous.  */
19077   if (ambiguous_decls)
19078     *ambiguous_decls = NULL_TREE;
19079
19080   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19081      no longer valid.  Note that if we are parsing tentatively, and
19082      the parse fails, OBJECT_TYPE will be automatically restored.  */
19083   parser->context->object_type = NULL_TREE;
19084
19085   if (name == error_mark_node)
19086     return error_mark_node;
19087
19088   /* A template-id has already been resolved; there is no lookup to
19089      do.  */
19090   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19091     return name;
19092   if (BASELINK_P (name))
19093     {
19094       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19095                   == TEMPLATE_ID_EXPR);
19096       return name;
19097     }
19098
19099   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19100      it should already have been checked to make sure that the name
19101      used matches the type being destroyed.  */
19102   if (TREE_CODE (name) == BIT_NOT_EXPR)
19103     {
19104       tree type;
19105
19106       /* Figure out to which type this destructor applies.  */
19107       if (parser->scope)
19108         type = parser->scope;
19109       else if (object_type)
19110         type = object_type;
19111       else
19112         type = current_class_type;
19113       /* If that's not a class type, there is no destructor.  */
19114       if (!type || !CLASS_TYPE_P (type))
19115         return error_mark_node;
19116       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19117         lazily_declare_fn (sfk_destructor, type);
19118       if (!CLASSTYPE_DESTRUCTORS (type))
19119           return error_mark_node;
19120       /* If it was a class type, return the destructor.  */
19121       return CLASSTYPE_DESTRUCTORS (type);
19122     }
19123
19124   /* By this point, the NAME should be an ordinary identifier.  If
19125      the id-expression was a qualified name, the qualifying scope is
19126      stored in PARSER->SCOPE at this point.  */
19127   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19128
19129   /* Perform the lookup.  */
19130   if (parser->scope)
19131     {
19132       bool dependent_p;
19133
19134       if (parser->scope == error_mark_node)
19135         return error_mark_node;
19136
19137       /* If the SCOPE is dependent, the lookup must be deferred until
19138          the template is instantiated -- unless we are explicitly
19139          looking up names in uninstantiated templates.  Even then, we
19140          cannot look up the name if the scope is not a class type; it
19141          might, for example, be a template type parameter.  */
19142       dependent_p = (TYPE_P (parser->scope)
19143                      && dependent_scope_p (parser->scope));
19144       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19145           && dependent_p)
19146         /* Defer lookup.  */
19147         decl = error_mark_node;
19148       else
19149         {
19150           tree pushed_scope = NULL_TREE;
19151
19152           /* If PARSER->SCOPE is a dependent type, then it must be a
19153              class type, and we must not be checking dependencies;
19154              otherwise, we would have processed this lookup above.  So
19155              that PARSER->SCOPE is not considered a dependent base by
19156              lookup_member, we must enter the scope here.  */
19157           if (dependent_p)
19158             pushed_scope = push_scope (parser->scope);
19159
19160           /* If the PARSER->SCOPE is a template specialization, it
19161              may be instantiated during name lookup.  In that case,
19162              errors may be issued.  Even if we rollback the current
19163              tentative parse, those errors are valid.  */
19164           decl = lookup_qualified_name (parser->scope, name,
19165                                         tag_type != none_type,
19166                                         /*complain=*/true);
19167
19168           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19169              lookup result and the nested-name-specifier nominates a class C:
19170                * if the name specified after the nested-name-specifier, when
19171                looked up in C, is the injected-class-name of C (Clause 9), or
19172                * if the name specified after the nested-name-specifier is the
19173                same as the identifier or the simple-template-id's template-
19174                name in the last component of the nested-name-specifier,
19175              the name is instead considered to name the constructor of
19176              class C. [ Note: for example, the constructor is not an
19177              acceptable lookup result in an elaborated-type-specifier so
19178              the constructor would not be used in place of the
19179              injected-class-name. --end note ] Such a constructor name
19180              shall be used only in the declarator-id of a declaration that
19181              names a constructor or in a using-declaration.  */
19182           if (tag_type == none_type
19183               && DECL_SELF_REFERENCE_P (decl)
19184               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19185             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19186                                           tag_type != none_type,
19187                                           /*complain=*/true);
19188
19189           /* If we have a single function from a using decl, pull it out.  */
19190           if (TREE_CODE (decl) == OVERLOAD
19191               && !really_overloaded_fn (decl))
19192             decl = OVL_FUNCTION (decl);
19193
19194           if (pushed_scope)
19195             pop_scope (pushed_scope);
19196         }
19197
19198       /* If the scope is a dependent type and either we deferred lookup or
19199          we did lookup but didn't find the name, rememeber the name.  */
19200       if (decl == error_mark_node && TYPE_P (parser->scope)
19201           && dependent_type_p (parser->scope))
19202         {
19203           if (tag_type)
19204             {
19205               tree type;
19206
19207               /* The resolution to Core Issue 180 says that `struct
19208                  A::B' should be considered a type-name, even if `A'
19209                  is dependent.  */
19210               type = make_typename_type (parser->scope, name, tag_type,
19211                                          /*complain=*/tf_error);
19212               decl = TYPE_NAME (type);
19213             }
19214           else if (is_template
19215                    && (cp_parser_next_token_ends_template_argument_p (parser)
19216                        || cp_lexer_next_token_is (parser->lexer,
19217                                                   CPP_CLOSE_PAREN)))
19218             decl = make_unbound_class_template (parser->scope,
19219                                                 name, NULL_TREE,
19220                                                 /*complain=*/tf_error);
19221           else
19222             decl = build_qualified_name (/*type=*/NULL_TREE,
19223                                          parser->scope, name,
19224                                          is_template);
19225         }
19226       parser->qualifying_scope = parser->scope;
19227       parser->object_scope = NULL_TREE;
19228     }
19229   else if (object_type)
19230     {
19231       tree object_decl = NULL_TREE;
19232       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19233          OBJECT_TYPE is not a class.  */
19234       if (CLASS_TYPE_P (object_type))
19235         /* If the OBJECT_TYPE is a template specialization, it may
19236            be instantiated during name lookup.  In that case, errors
19237            may be issued.  Even if we rollback the current tentative
19238            parse, those errors are valid.  */
19239         object_decl = lookup_member (object_type,
19240                                      name,
19241                                      /*protect=*/0,
19242                                      tag_type != none_type);
19243       /* Look it up in the enclosing context, too.  */
19244       decl = lookup_name_real (name, tag_type != none_type,
19245                                /*nonclass=*/0,
19246                                /*block_p=*/true, is_namespace, flags);
19247       parser->object_scope = object_type;
19248       parser->qualifying_scope = NULL_TREE;
19249       if (object_decl)
19250         decl = object_decl;
19251     }
19252   else
19253     {
19254       decl = lookup_name_real (name, tag_type != none_type,
19255                                /*nonclass=*/0,
19256                                /*block_p=*/true, is_namespace, flags);
19257       parser->qualifying_scope = NULL_TREE;
19258       parser->object_scope = NULL_TREE;
19259     }
19260
19261   /* If the lookup failed, let our caller know.  */
19262   if (!decl || decl == error_mark_node)
19263     return error_mark_node;
19264
19265   /* Pull out the template from an injected-class-name (or multiple).  */
19266   if (is_template)
19267     decl = maybe_get_template_decl_from_type_decl (decl);
19268
19269   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19270   if (TREE_CODE (decl) == TREE_LIST)
19271     {
19272       if (ambiguous_decls)
19273         *ambiguous_decls = decl;
19274       /* The error message we have to print is too complicated for
19275          cp_parser_error, so we incorporate its actions directly.  */
19276       if (!cp_parser_simulate_error (parser))
19277         {
19278           error_at (name_location, "reference to %qD is ambiguous",
19279                     name);
19280           print_candidates (decl);
19281         }
19282       return error_mark_node;
19283     }
19284
19285   gcc_assert (DECL_P (decl)
19286               || TREE_CODE (decl) == OVERLOAD
19287               || TREE_CODE (decl) == SCOPE_REF
19288               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19289               || BASELINK_P (decl));
19290
19291   /* If we have resolved the name of a member declaration, check to
19292      see if the declaration is accessible.  When the name resolves to
19293      set of overloaded functions, accessibility is checked when
19294      overload resolution is done.
19295
19296      During an explicit instantiation, access is not checked at all,
19297      as per [temp.explicit].  */
19298   if (DECL_P (decl))
19299     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19300
19301   return decl;
19302 }
19303
19304 /* Like cp_parser_lookup_name, but for use in the typical case where
19305    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19306    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19307
19308 static tree
19309 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19310 {
19311   return cp_parser_lookup_name (parser, name,
19312                                 none_type,
19313                                 /*is_template=*/false,
19314                                 /*is_namespace=*/false,
19315                                 /*check_dependency=*/true,
19316                                 /*ambiguous_decls=*/NULL,
19317                                 location);
19318 }
19319
19320 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19321    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19322    true, the DECL indicates the class being defined in a class-head,
19323    or declared in an elaborated-type-specifier.
19324
19325    Otherwise, return DECL.  */
19326
19327 static tree
19328 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19329 {
19330   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19331      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19332
19333        struct A {
19334          template <typename T> struct B;
19335        };
19336
19337        template <typename T> struct A::B {};
19338
19339      Similarly, in an elaborated-type-specifier:
19340
19341        namespace N { struct X{}; }
19342
19343        struct A {
19344          template <typename T> friend struct N::X;
19345        };
19346
19347      However, if the DECL refers to a class type, and we are in
19348      the scope of the class, then the name lookup automatically
19349      finds the TYPE_DECL created by build_self_reference rather
19350      than a TEMPLATE_DECL.  For example, in:
19351
19352        template <class T> struct S {
19353          S s;
19354        };
19355
19356      there is no need to handle such case.  */
19357
19358   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19359     return DECL_TEMPLATE_RESULT (decl);
19360
19361   return decl;
19362 }
19363
19364 /* If too many, or too few, template-parameter lists apply to the
19365    declarator, issue an error message.  Returns TRUE if all went well,
19366    and FALSE otherwise.  */
19367
19368 static bool
19369 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19370                                                 cp_declarator *declarator,
19371                                                 location_t declarator_location)
19372 {
19373   unsigned num_templates;
19374
19375   /* We haven't seen any classes that involve template parameters yet.  */
19376   num_templates = 0;
19377
19378   switch (declarator->kind)
19379     {
19380     case cdk_id:
19381       if (declarator->u.id.qualifying_scope)
19382         {
19383           tree scope;
19384
19385           scope = declarator->u.id.qualifying_scope;
19386
19387           while (scope && CLASS_TYPE_P (scope))
19388             {
19389               /* You're supposed to have one `template <...>'
19390                  for every template class, but you don't need one
19391                  for a full specialization.  For example:
19392
19393                  template <class T> struct S{};
19394                  template <> struct S<int> { void f(); };
19395                  void S<int>::f () {}
19396
19397                  is correct; there shouldn't be a `template <>' for
19398                  the definition of `S<int>::f'.  */
19399               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19400                 /* If SCOPE does not have template information of any
19401                    kind, then it is not a template, nor is it nested
19402                    within a template.  */
19403                 break;
19404               if (explicit_class_specialization_p (scope))
19405                 break;
19406               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19407                 ++num_templates;
19408
19409               scope = TYPE_CONTEXT (scope);
19410             }
19411         }
19412       else if (TREE_CODE (declarator->u.id.unqualified_name)
19413                == TEMPLATE_ID_EXPR)
19414         /* If the DECLARATOR has the form `X<y>' then it uses one
19415            additional level of template parameters.  */
19416         ++num_templates;
19417
19418       return cp_parser_check_template_parameters 
19419         (parser, num_templates, declarator_location, declarator);
19420
19421
19422     case cdk_function:
19423     case cdk_array:
19424     case cdk_pointer:
19425     case cdk_reference:
19426     case cdk_ptrmem:
19427       return (cp_parser_check_declarator_template_parameters
19428               (parser, declarator->declarator, declarator_location));
19429
19430     case cdk_error:
19431       return true;
19432
19433     default:
19434       gcc_unreachable ();
19435     }
19436   return false;
19437 }
19438
19439 /* NUM_TEMPLATES were used in the current declaration.  If that is
19440    invalid, return FALSE and issue an error messages.  Otherwise,
19441    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19442    declarator and we can print more accurate diagnostics.  */
19443
19444 static bool
19445 cp_parser_check_template_parameters (cp_parser* parser,
19446                                      unsigned num_templates,
19447                                      location_t location,
19448                                      cp_declarator *declarator)
19449 {
19450   /* If there are the same number of template classes and parameter
19451      lists, that's OK.  */
19452   if (parser->num_template_parameter_lists == num_templates)
19453     return true;
19454   /* If there are more, but only one more, then we are referring to a
19455      member template.  That's OK too.  */
19456   if (parser->num_template_parameter_lists == num_templates + 1)
19457     return true;
19458   /* If there are more template classes than parameter lists, we have
19459      something like:
19460
19461        template <class T> void S<T>::R<T>::f ();  */
19462   if (parser->num_template_parameter_lists < num_templates)
19463     {
19464       if (declarator && !current_function_decl)
19465         error_at (location, "specializing member %<%T::%E%> "
19466                   "requires %<template<>%> syntax", 
19467                   declarator->u.id.qualifying_scope,
19468                   declarator->u.id.unqualified_name);
19469       else if (declarator)
19470         error_at (location, "invalid declaration of %<%T::%E%>",
19471                   declarator->u.id.qualifying_scope,
19472                   declarator->u.id.unqualified_name);
19473       else 
19474         error_at (location, "too few template-parameter-lists");
19475       return false;
19476     }
19477   /* Otherwise, there are too many template parameter lists.  We have
19478      something like:
19479
19480      template <class T> template <class U> void S::f();  */
19481   error_at (location, "too many template-parameter-lists");
19482   return false;
19483 }
19484
19485 /* Parse an optional `::' token indicating that the following name is
19486    from the global namespace.  If so, PARSER->SCOPE is set to the
19487    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19488    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19489    Returns the new value of PARSER->SCOPE, if the `::' token is
19490    present, and NULL_TREE otherwise.  */
19491
19492 static tree
19493 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19494 {
19495   cp_token *token;
19496
19497   /* Peek at the next token.  */
19498   token = cp_lexer_peek_token (parser->lexer);
19499   /* If we're looking at a `::' token then we're starting from the
19500      global namespace, not our current location.  */
19501   if (token->type == CPP_SCOPE)
19502     {
19503       /* Consume the `::' token.  */
19504       cp_lexer_consume_token (parser->lexer);
19505       /* Set the SCOPE so that we know where to start the lookup.  */
19506       parser->scope = global_namespace;
19507       parser->qualifying_scope = global_namespace;
19508       parser->object_scope = NULL_TREE;
19509
19510       return parser->scope;
19511     }
19512   else if (!current_scope_valid_p)
19513     {
19514       parser->scope = NULL_TREE;
19515       parser->qualifying_scope = NULL_TREE;
19516       parser->object_scope = NULL_TREE;
19517     }
19518
19519   return NULL_TREE;
19520 }
19521
19522 /* Returns TRUE if the upcoming token sequence is the start of a
19523    constructor declarator.  If FRIEND_P is true, the declarator is
19524    preceded by the `friend' specifier.  */
19525
19526 static bool
19527 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19528 {
19529   bool constructor_p;
19530   tree nested_name_specifier;
19531   cp_token *next_token;
19532
19533   /* The common case is that this is not a constructor declarator, so
19534      try to avoid doing lots of work if at all possible.  It's not
19535      valid declare a constructor at function scope.  */
19536   if (parser->in_function_body)
19537     return false;
19538   /* And only certain tokens can begin a constructor declarator.  */
19539   next_token = cp_lexer_peek_token (parser->lexer);
19540   if (next_token->type != CPP_NAME
19541       && next_token->type != CPP_SCOPE
19542       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19543       && next_token->type != CPP_TEMPLATE_ID)
19544     return false;
19545
19546   /* Parse tentatively; we are going to roll back all of the tokens
19547      consumed here.  */
19548   cp_parser_parse_tentatively (parser);
19549   /* Assume that we are looking at a constructor declarator.  */
19550   constructor_p = true;
19551
19552   /* Look for the optional `::' operator.  */
19553   cp_parser_global_scope_opt (parser,
19554                               /*current_scope_valid_p=*/false);
19555   /* Look for the nested-name-specifier.  */
19556   nested_name_specifier
19557     = (cp_parser_nested_name_specifier_opt (parser,
19558                                             /*typename_keyword_p=*/false,
19559                                             /*check_dependency_p=*/false,
19560                                             /*type_p=*/false,
19561                                             /*is_declaration=*/false));
19562   /* Outside of a class-specifier, there must be a
19563      nested-name-specifier.  */
19564   if (!nested_name_specifier &&
19565       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19566        || friend_p))
19567     constructor_p = false;
19568   else if (nested_name_specifier == error_mark_node)
19569     constructor_p = false;
19570
19571   /* If we have a class scope, this is easy; DR 147 says that S::S always
19572      names the constructor, and no other qualified name could.  */
19573   if (constructor_p && nested_name_specifier
19574       && TYPE_P (nested_name_specifier))
19575     {
19576       tree id = cp_parser_unqualified_id (parser,
19577                                           /*template_keyword_p=*/false,
19578                                           /*check_dependency_p=*/false,
19579                                           /*declarator_p=*/true,
19580                                           /*optional_p=*/false);
19581       if (is_overloaded_fn (id))
19582         id = DECL_NAME (get_first_fn (id));
19583       if (!constructor_name_p (id, nested_name_specifier))
19584         constructor_p = false;
19585     }
19586   /* If we still think that this might be a constructor-declarator,
19587      look for a class-name.  */
19588   else if (constructor_p)
19589     {
19590       /* If we have:
19591
19592            template <typename T> struct S {
19593              S();
19594            };
19595
19596          we must recognize that the nested `S' names a class.  */
19597       tree type_decl;
19598       type_decl = cp_parser_class_name (parser,
19599                                         /*typename_keyword_p=*/false,
19600                                         /*template_keyword_p=*/false,
19601                                         none_type,
19602                                         /*check_dependency_p=*/false,
19603                                         /*class_head_p=*/false,
19604                                         /*is_declaration=*/false);
19605       /* If there was no class-name, then this is not a constructor.  */
19606       constructor_p = !cp_parser_error_occurred (parser);
19607
19608       /* If we're still considering a constructor, we have to see a `(',
19609          to begin the parameter-declaration-clause, followed by either a
19610          `)', an `...', or a decl-specifier.  We need to check for a
19611          type-specifier to avoid being fooled into thinking that:
19612
19613            S (f) (int);
19614
19615          is a constructor.  (It is actually a function named `f' that
19616          takes one parameter (of type `int') and returns a value of type
19617          `S'.  */
19618       if (constructor_p
19619           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19620         constructor_p = false;
19621
19622       if (constructor_p
19623           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19624           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19625           /* A parameter declaration begins with a decl-specifier,
19626              which is either the "attribute" keyword, a storage class
19627              specifier, or (usually) a type-specifier.  */
19628           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19629         {
19630           tree type;
19631           tree pushed_scope = NULL_TREE;
19632           unsigned saved_num_template_parameter_lists;
19633
19634           /* Names appearing in the type-specifier should be looked up
19635              in the scope of the class.  */
19636           if (current_class_type)
19637             type = NULL_TREE;
19638           else
19639             {
19640               type = TREE_TYPE (type_decl);
19641               if (TREE_CODE (type) == TYPENAME_TYPE)
19642                 {
19643                   type = resolve_typename_type (type,
19644                                                 /*only_current_p=*/false);
19645                   if (TREE_CODE (type) == TYPENAME_TYPE)
19646                     {
19647                       cp_parser_abort_tentative_parse (parser);
19648                       return false;
19649                     }
19650                 }
19651               pushed_scope = push_scope (type);
19652             }
19653
19654           /* Inside the constructor parameter list, surrounding
19655              template-parameter-lists do not apply.  */
19656           saved_num_template_parameter_lists
19657             = parser->num_template_parameter_lists;
19658           parser->num_template_parameter_lists = 0;
19659
19660           /* Look for the type-specifier.  */
19661           cp_parser_type_specifier (parser,
19662                                     CP_PARSER_FLAGS_NONE,
19663                                     /*decl_specs=*/NULL,
19664                                     /*is_declarator=*/true,
19665                                     /*declares_class_or_enum=*/NULL,
19666                                     /*is_cv_qualifier=*/NULL);
19667
19668           parser->num_template_parameter_lists
19669             = saved_num_template_parameter_lists;
19670
19671           /* Leave the scope of the class.  */
19672           if (pushed_scope)
19673             pop_scope (pushed_scope);
19674
19675           constructor_p = !cp_parser_error_occurred (parser);
19676         }
19677     }
19678
19679   /* We did not really want to consume any tokens.  */
19680   cp_parser_abort_tentative_parse (parser);
19681
19682   return constructor_p;
19683 }
19684
19685 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19686    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19687    they must be performed once we are in the scope of the function.
19688
19689    Returns the function defined.  */
19690
19691 static tree
19692 cp_parser_function_definition_from_specifiers_and_declarator
19693   (cp_parser* parser,
19694    cp_decl_specifier_seq *decl_specifiers,
19695    tree attributes,
19696    const cp_declarator *declarator)
19697 {
19698   tree fn;
19699   bool success_p;
19700
19701   /* Begin the function-definition.  */
19702   success_p = start_function (decl_specifiers, declarator, attributes);
19703
19704   /* The things we're about to see are not directly qualified by any
19705      template headers we've seen thus far.  */
19706   reset_specialization ();
19707
19708   /* If there were names looked up in the decl-specifier-seq that we
19709      did not check, check them now.  We must wait until we are in the
19710      scope of the function to perform the checks, since the function
19711      might be a friend.  */
19712   perform_deferred_access_checks ();
19713
19714   if (!success_p)
19715     {
19716       /* Skip the entire function.  */
19717       cp_parser_skip_to_end_of_block_or_statement (parser);
19718       fn = error_mark_node;
19719     }
19720   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19721     {
19722       /* Seen already, skip it.  An error message has already been output.  */
19723       cp_parser_skip_to_end_of_block_or_statement (parser);
19724       fn = current_function_decl;
19725       current_function_decl = NULL_TREE;
19726       /* If this is a function from a class, pop the nested class.  */
19727       if (current_class_name)
19728         pop_nested_class ();
19729     }
19730   else
19731     fn = cp_parser_function_definition_after_declarator (parser,
19732                                                          /*inline_p=*/false);
19733
19734   return fn;
19735 }
19736
19737 /* Parse the part of a function-definition that follows the
19738    declarator.  INLINE_P is TRUE iff this function is an inline
19739    function defined within a class-specifier.
19740
19741    Returns the function defined.  */
19742
19743 static tree
19744 cp_parser_function_definition_after_declarator (cp_parser* parser,
19745                                                 bool inline_p)
19746 {
19747   tree fn;
19748   bool ctor_initializer_p = false;
19749   bool saved_in_unbraced_linkage_specification_p;
19750   bool saved_in_function_body;
19751   unsigned saved_num_template_parameter_lists;
19752   cp_token *token;
19753
19754   saved_in_function_body = parser->in_function_body;
19755   parser->in_function_body = true;
19756   /* If the next token is `return', then the code may be trying to
19757      make use of the "named return value" extension that G++ used to
19758      support.  */
19759   token = cp_lexer_peek_token (parser->lexer);
19760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19761     {
19762       /* Consume the `return' keyword.  */
19763       cp_lexer_consume_token (parser->lexer);
19764       /* Look for the identifier that indicates what value is to be
19765          returned.  */
19766       cp_parser_identifier (parser);
19767       /* Issue an error message.  */
19768       error_at (token->location,
19769                 "named return values are no longer supported");
19770       /* Skip tokens until we reach the start of the function body.  */
19771       while (true)
19772         {
19773           cp_token *token = cp_lexer_peek_token (parser->lexer);
19774           if (token->type == CPP_OPEN_BRACE
19775               || token->type == CPP_EOF
19776               || token->type == CPP_PRAGMA_EOL)
19777             break;
19778           cp_lexer_consume_token (parser->lexer);
19779         }
19780     }
19781   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19782      anything declared inside `f'.  */
19783   saved_in_unbraced_linkage_specification_p
19784     = parser->in_unbraced_linkage_specification_p;
19785   parser->in_unbraced_linkage_specification_p = false;
19786   /* Inside the function, surrounding template-parameter-lists do not
19787      apply.  */
19788   saved_num_template_parameter_lists
19789     = parser->num_template_parameter_lists;
19790   parser->num_template_parameter_lists = 0;
19791
19792   start_lambda_scope (current_function_decl);
19793
19794   /* If the next token is `try', then we are looking at a
19795      function-try-block.  */
19796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19797     ctor_initializer_p = cp_parser_function_try_block (parser);
19798   /* A function-try-block includes the function-body, so we only do
19799      this next part if we're not processing a function-try-block.  */
19800   else
19801     ctor_initializer_p
19802       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19803
19804   finish_lambda_scope ();
19805
19806   /* Finish the function.  */
19807   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19808                         (inline_p ? 2 : 0));
19809   /* Generate code for it, if necessary.  */
19810   expand_or_defer_fn (fn);
19811   /* Restore the saved values.  */
19812   parser->in_unbraced_linkage_specification_p
19813     = saved_in_unbraced_linkage_specification_p;
19814   parser->num_template_parameter_lists
19815     = saved_num_template_parameter_lists;
19816   parser->in_function_body = saved_in_function_body;
19817
19818   return fn;
19819 }
19820
19821 /* Parse a template-declaration, assuming that the `export' (and
19822    `extern') keywords, if present, has already been scanned.  MEMBER_P
19823    is as for cp_parser_template_declaration.  */
19824
19825 static void
19826 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19827 {
19828   tree decl = NULL_TREE;
19829   VEC (deferred_access_check,gc) *checks;
19830   tree parameter_list;
19831   bool friend_p = false;
19832   bool need_lang_pop;
19833   cp_token *token;
19834
19835   /* Look for the `template' keyword.  */
19836   token = cp_lexer_peek_token (parser->lexer);
19837   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19838     return;
19839
19840   /* And the `<'.  */
19841   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19842     return;
19843   if (at_class_scope_p () && current_function_decl)
19844     {
19845       /* 14.5.2.2 [temp.mem]
19846
19847          A local class shall not have member templates.  */
19848       error_at (token->location,
19849                 "invalid declaration of member template in local class");
19850       cp_parser_skip_to_end_of_block_or_statement (parser);
19851       return;
19852     }
19853   /* [temp]
19854
19855      A template ... shall not have C linkage.  */
19856   if (current_lang_name == lang_name_c)
19857     {
19858       error_at (token->location, "template with C linkage");
19859       /* Give it C++ linkage to avoid confusing other parts of the
19860          front end.  */
19861       push_lang_context (lang_name_cplusplus);
19862       need_lang_pop = true;
19863     }
19864   else
19865     need_lang_pop = false;
19866
19867   /* We cannot perform access checks on the template parameter
19868      declarations until we know what is being declared, just as we
19869      cannot check the decl-specifier list.  */
19870   push_deferring_access_checks (dk_deferred);
19871
19872   /* If the next token is `>', then we have an invalid
19873      specialization.  Rather than complain about an invalid template
19874      parameter, issue an error message here.  */
19875   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19876     {
19877       cp_parser_error (parser, "invalid explicit specialization");
19878       begin_specialization ();
19879       parameter_list = NULL_TREE;
19880     }
19881   else
19882     /* Parse the template parameters.  */
19883     parameter_list = cp_parser_template_parameter_list (parser);
19884
19885   /* Get the deferred access checks from the parameter list.  These
19886      will be checked once we know what is being declared, as for a
19887      member template the checks must be performed in the scope of the
19888      class containing the member.  */
19889   checks = get_deferred_access_checks ();
19890
19891   /* Look for the `>'.  */
19892   cp_parser_skip_to_end_of_template_parameter_list (parser);
19893   /* We just processed one more parameter list.  */
19894   ++parser->num_template_parameter_lists;
19895   /* If the next token is `template', there are more template
19896      parameters.  */
19897   if (cp_lexer_next_token_is_keyword (parser->lexer,
19898                                       RID_TEMPLATE))
19899     cp_parser_template_declaration_after_export (parser, member_p);
19900   else
19901     {
19902       /* There are no access checks when parsing a template, as we do not
19903          know if a specialization will be a friend.  */
19904       push_deferring_access_checks (dk_no_check);
19905       token = cp_lexer_peek_token (parser->lexer);
19906       decl = cp_parser_single_declaration (parser,
19907                                            checks,
19908                                            member_p,
19909                                            /*explicit_specialization_p=*/false,
19910                                            &friend_p);
19911       pop_deferring_access_checks ();
19912
19913       /* If this is a member template declaration, let the front
19914          end know.  */
19915       if (member_p && !friend_p && decl)
19916         {
19917           if (TREE_CODE (decl) == TYPE_DECL)
19918             cp_parser_check_access_in_redeclaration (decl, token->location);
19919
19920           decl = finish_member_template_decl (decl);
19921         }
19922       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19923         make_friend_class (current_class_type, TREE_TYPE (decl),
19924                            /*complain=*/true);
19925     }
19926   /* We are done with the current parameter list.  */
19927   --parser->num_template_parameter_lists;
19928
19929   pop_deferring_access_checks ();
19930
19931   /* Finish up.  */
19932   finish_template_decl (parameter_list);
19933
19934   /* Register member declarations.  */
19935   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19936     finish_member_declaration (decl);
19937   /* For the erroneous case of a template with C linkage, we pushed an
19938      implicit C++ linkage scope; exit that scope now.  */
19939   if (need_lang_pop)
19940     pop_lang_context ();
19941   /* If DECL is a function template, we must return to parse it later.
19942      (Even though there is no definition, there might be default
19943      arguments that need handling.)  */
19944   if (member_p && decl
19945       && (TREE_CODE (decl) == FUNCTION_DECL
19946           || DECL_FUNCTION_TEMPLATE_P (decl)))
19947     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19948 }
19949
19950 /* Perform the deferred access checks from a template-parameter-list.
19951    CHECKS is a TREE_LIST of access checks, as returned by
19952    get_deferred_access_checks.  */
19953
19954 static void
19955 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19956 {
19957   ++processing_template_parmlist;
19958   perform_access_checks (checks);
19959   --processing_template_parmlist;
19960 }
19961
19962 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19963    `function-definition' sequence.  MEMBER_P is true, this declaration
19964    appears in a class scope.
19965
19966    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19967    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19968
19969 static tree
19970 cp_parser_single_declaration (cp_parser* parser,
19971                               VEC (deferred_access_check,gc)* checks,
19972                               bool member_p,
19973                               bool explicit_specialization_p,
19974                               bool* friend_p)
19975 {
19976   int declares_class_or_enum;
19977   tree decl = NULL_TREE;
19978   cp_decl_specifier_seq decl_specifiers;
19979   bool function_definition_p = false;
19980   cp_token *decl_spec_token_start;
19981
19982   /* This function is only used when processing a template
19983      declaration.  */
19984   gcc_assert (innermost_scope_kind () == sk_template_parms
19985               || innermost_scope_kind () == sk_template_spec);
19986
19987   /* Defer access checks until we know what is being declared.  */
19988   push_deferring_access_checks (dk_deferred);
19989
19990   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19991      alternative.  */
19992   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19993   cp_parser_decl_specifier_seq (parser,
19994                                 CP_PARSER_FLAGS_OPTIONAL,
19995                                 &decl_specifiers,
19996                                 &declares_class_or_enum);
19997   if (friend_p)
19998     *friend_p = cp_parser_friend_p (&decl_specifiers);
19999
20000   /* There are no template typedefs.  */
20001   if (decl_specifiers.specs[(int) ds_typedef])
20002     {
20003       error_at (decl_spec_token_start->location,
20004                 "template declaration of %<typedef%>");
20005       decl = error_mark_node;
20006     }
20007
20008   /* Gather up the access checks that occurred the
20009      decl-specifier-seq.  */
20010   stop_deferring_access_checks ();
20011
20012   /* Check for the declaration of a template class.  */
20013   if (declares_class_or_enum)
20014     {
20015       if (cp_parser_declares_only_class_p (parser))
20016         {
20017           decl = shadow_tag (&decl_specifiers);
20018
20019           /* In this case:
20020
20021                struct C {
20022                  friend template <typename T> struct A<T>::B;
20023                };
20024
20025              A<T>::B will be represented by a TYPENAME_TYPE, and
20026              therefore not recognized by shadow_tag.  */
20027           if (friend_p && *friend_p
20028               && !decl
20029               && decl_specifiers.type
20030               && TYPE_P (decl_specifiers.type))
20031             decl = decl_specifiers.type;
20032
20033           if (decl && decl != error_mark_node)
20034             decl = TYPE_NAME (decl);
20035           else
20036             decl = error_mark_node;
20037
20038           /* Perform access checks for template parameters.  */
20039           cp_parser_perform_template_parameter_access_checks (checks);
20040         }
20041     }
20042
20043   /* Complain about missing 'typename' or other invalid type names.  */
20044   if (!decl_specifiers.any_type_specifiers_p)
20045     cp_parser_parse_and_diagnose_invalid_type_name (parser);
20046
20047   /* If it's not a template class, try for a template function.  If
20048      the next token is a `;', then this declaration does not declare
20049      anything.  But, if there were errors in the decl-specifiers, then
20050      the error might well have come from an attempted class-specifier.
20051      In that case, there's no need to warn about a missing declarator.  */
20052   if (!decl
20053       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20054           || decl_specifiers.type != error_mark_node))
20055     {
20056       decl = cp_parser_init_declarator (parser,
20057                                         &decl_specifiers,
20058                                         checks,
20059                                         /*function_definition_allowed_p=*/true,
20060                                         member_p,
20061                                         declares_class_or_enum,
20062                                         &function_definition_p);
20063
20064     /* 7.1.1-1 [dcl.stc]
20065
20066        A storage-class-specifier shall not be specified in an explicit
20067        specialization...  */
20068     if (decl
20069         && explicit_specialization_p
20070         && decl_specifiers.storage_class != sc_none)
20071       {
20072         error_at (decl_spec_token_start->location,
20073                   "explicit template specialization cannot have a storage class");
20074         decl = error_mark_node;
20075       }
20076     }
20077
20078   pop_deferring_access_checks ();
20079
20080   /* Clear any current qualification; whatever comes next is the start
20081      of something new.  */
20082   parser->scope = NULL_TREE;
20083   parser->qualifying_scope = NULL_TREE;
20084   parser->object_scope = NULL_TREE;
20085   /* Look for a trailing `;' after the declaration.  */
20086   if (!function_definition_p
20087       && (decl == error_mark_node
20088           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20089     cp_parser_skip_to_end_of_block_or_statement (parser);
20090
20091   return decl;
20092 }
20093
20094 /* Parse a cast-expression that is not the operand of a unary "&".  */
20095
20096 static tree
20097 cp_parser_simple_cast_expression (cp_parser *parser)
20098 {
20099   return cp_parser_cast_expression (parser, /*address_p=*/false,
20100                                     /*cast_p=*/false, NULL);
20101 }
20102
20103 /* Parse a functional cast to TYPE.  Returns an expression
20104    representing the cast.  */
20105
20106 static tree
20107 cp_parser_functional_cast (cp_parser* parser, tree type)
20108 {
20109   VEC(tree,gc) *vec;
20110   tree expression_list;
20111   tree cast;
20112   bool nonconst_p;
20113
20114   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20115     {
20116       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20117       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20118       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20119       if (TREE_CODE (type) == TYPE_DECL)
20120         type = TREE_TYPE (type);
20121       return finish_compound_literal (type, expression_list);
20122     }
20123
20124
20125   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20126                                                  /*cast_p=*/true,
20127                                                  /*allow_expansion_p=*/true,
20128                                                  /*non_constant_p=*/NULL);
20129   if (vec == NULL)
20130     expression_list = error_mark_node;
20131   else
20132     {
20133       expression_list = build_tree_list_vec (vec);
20134       release_tree_vector (vec);
20135     }
20136
20137   cast = build_functional_cast (type, expression_list,
20138                                 tf_warning_or_error);
20139   /* [expr.const]/1: In an integral constant expression "only type
20140      conversions to integral or enumeration type can be used".  */
20141   if (TREE_CODE (type) == TYPE_DECL)
20142     type = TREE_TYPE (type);
20143   if (cast != error_mark_node
20144       && !cast_valid_in_integral_constant_expression_p (type)
20145       && cp_parser_non_integral_constant_expression (parser,
20146                                                      NIC_CONSTRUCTOR))
20147     return error_mark_node;
20148   return cast;
20149 }
20150
20151 /* Save the tokens that make up the body of a member function defined
20152    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20153    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20154    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20155    for the member function.  */
20156
20157 static tree
20158 cp_parser_save_member_function_body (cp_parser* parser,
20159                                      cp_decl_specifier_seq *decl_specifiers,
20160                                      cp_declarator *declarator,
20161                                      tree attributes)
20162 {
20163   cp_token *first;
20164   cp_token *last;
20165   tree fn;
20166
20167   /* Create the FUNCTION_DECL.  */
20168   fn = grokmethod (decl_specifiers, declarator, attributes);
20169   /* If something went badly wrong, bail out now.  */
20170   if (fn == error_mark_node)
20171     {
20172       /* If there's a function-body, skip it.  */
20173       if (cp_parser_token_starts_function_definition_p
20174           (cp_lexer_peek_token (parser->lexer)))
20175         cp_parser_skip_to_end_of_block_or_statement (parser);
20176       return error_mark_node;
20177     }
20178
20179   /* Remember it, if there default args to post process.  */
20180   cp_parser_save_default_args (parser, fn);
20181
20182   /* Save away the tokens that make up the body of the
20183      function.  */
20184   first = parser->lexer->next_token;
20185   /* We can have braced-init-list mem-initializers before the fn body.  */
20186   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20187     {
20188       cp_lexer_consume_token (parser->lexer);
20189       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20190              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20191         {
20192           /* cache_group will stop after an un-nested { } pair, too.  */
20193           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20194             break;
20195
20196           /* variadic mem-inits have ... after the ')'.  */
20197           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20198             cp_lexer_consume_token (parser->lexer);
20199         }
20200     }
20201   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20202   /* Handle function try blocks.  */
20203   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20204     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20205   last = parser->lexer->next_token;
20206
20207   /* Save away the inline definition; we will process it when the
20208      class is complete.  */
20209   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20210   DECL_PENDING_INLINE_P (fn) = 1;
20211
20212   /* We need to know that this was defined in the class, so that
20213      friend templates are handled correctly.  */
20214   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20215
20216   /* Add FN to the queue of functions to be parsed later.  */
20217   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20218
20219   return fn;
20220 }
20221
20222 /* Parse a template-argument-list, as well as the trailing ">" (but
20223    not the opening ">").  See cp_parser_template_argument_list for the
20224    return value.  */
20225
20226 static tree
20227 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20228 {
20229   tree arguments;
20230   tree saved_scope;
20231   tree saved_qualifying_scope;
20232   tree saved_object_scope;
20233   bool saved_greater_than_is_operator_p;
20234   int saved_unevaluated_operand;
20235   int saved_inhibit_evaluation_warnings;
20236
20237   /* [temp.names]
20238
20239      When parsing a template-id, the first non-nested `>' is taken as
20240      the end of the template-argument-list rather than a greater-than
20241      operator.  */
20242   saved_greater_than_is_operator_p
20243     = parser->greater_than_is_operator_p;
20244   parser->greater_than_is_operator_p = false;
20245   /* Parsing the argument list may modify SCOPE, so we save it
20246      here.  */
20247   saved_scope = parser->scope;
20248   saved_qualifying_scope = parser->qualifying_scope;
20249   saved_object_scope = parser->object_scope;
20250   /* We need to evaluate the template arguments, even though this
20251      template-id may be nested within a "sizeof".  */
20252   saved_unevaluated_operand = cp_unevaluated_operand;
20253   cp_unevaluated_operand = 0;
20254   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20255   c_inhibit_evaluation_warnings = 0;
20256   /* Parse the template-argument-list itself.  */
20257   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20258       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20259     arguments = NULL_TREE;
20260   else
20261     arguments = cp_parser_template_argument_list (parser);
20262   /* Look for the `>' that ends the template-argument-list. If we find
20263      a '>>' instead, it's probably just a typo.  */
20264   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20265     {
20266       if (cxx_dialect != cxx98)
20267         {
20268           /* In C++0x, a `>>' in a template argument list or cast
20269              expression is considered to be two separate `>'
20270              tokens. So, change the current token to a `>', but don't
20271              consume it: it will be consumed later when the outer
20272              template argument list (or cast expression) is parsed.
20273              Note that this replacement of `>' for `>>' is necessary
20274              even if we are parsing tentatively: in the tentative
20275              case, after calling
20276              cp_parser_enclosed_template_argument_list we will always
20277              throw away all of the template arguments and the first
20278              closing `>', either because the template argument list
20279              was erroneous or because we are replacing those tokens
20280              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20281              not have been thrown away) is needed either to close an
20282              outer template argument list or to complete a new-style
20283              cast.  */
20284           cp_token *token = cp_lexer_peek_token (parser->lexer);
20285           token->type = CPP_GREATER;
20286         }
20287       else if (!saved_greater_than_is_operator_p)
20288         {
20289           /* If we're in a nested template argument list, the '>>' has
20290             to be a typo for '> >'. We emit the error message, but we
20291             continue parsing and we push a '>' as next token, so that
20292             the argument list will be parsed correctly.  Note that the
20293             global source location is still on the token before the
20294             '>>', so we need to say explicitly where we want it.  */
20295           cp_token *token = cp_lexer_peek_token (parser->lexer);
20296           error_at (token->location, "%<>>%> should be %<> >%> "
20297                     "within a nested template argument list");
20298
20299           token->type = CPP_GREATER;
20300         }
20301       else
20302         {
20303           /* If this is not a nested template argument list, the '>>'
20304             is a typo for '>'. Emit an error message and continue.
20305             Same deal about the token location, but here we can get it
20306             right by consuming the '>>' before issuing the diagnostic.  */
20307           cp_token *token = cp_lexer_consume_token (parser->lexer);
20308           error_at (token->location,
20309                     "spurious %<>>%>, use %<>%> to terminate "
20310                     "a template argument list");
20311         }
20312     }
20313   else
20314     cp_parser_skip_to_end_of_template_parameter_list (parser);
20315   /* The `>' token might be a greater-than operator again now.  */
20316   parser->greater_than_is_operator_p
20317     = saved_greater_than_is_operator_p;
20318   /* Restore the SAVED_SCOPE.  */
20319   parser->scope = saved_scope;
20320   parser->qualifying_scope = saved_qualifying_scope;
20321   parser->object_scope = saved_object_scope;
20322   cp_unevaluated_operand = saved_unevaluated_operand;
20323   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20324
20325   return arguments;
20326 }
20327
20328 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20329    arguments, or the body of the function have not yet been parsed,
20330    parse them now.  */
20331
20332 static void
20333 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20334 {
20335   /* If this member is a template, get the underlying
20336      FUNCTION_DECL.  */
20337   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20338     member_function = DECL_TEMPLATE_RESULT (member_function);
20339
20340   /* There should not be any class definitions in progress at this
20341      point; the bodies of members are only parsed outside of all class
20342      definitions.  */
20343   gcc_assert (parser->num_classes_being_defined == 0);
20344   /* While we're parsing the member functions we might encounter more
20345      classes.  We want to handle them right away, but we don't want
20346      them getting mixed up with functions that are currently in the
20347      queue.  */
20348   push_unparsed_function_queues (parser);
20349
20350   /* Make sure that any template parameters are in scope.  */
20351   maybe_begin_member_template_processing (member_function);
20352
20353   /* If the body of the function has not yet been parsed, parse it
20354      now.  */
20355   if (DECL_PENDING_INLINE_P (member_function))
20356     {
20357       tree function_scope;
20358       cp_token_cache *tokens;
20359
20360       /* The function is no longer pending; we are processing it.  */
20361       tokens = DECL_PENDING_INLINE_INFO (member_function);
20362       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20363       DECL_PENDING_INLINE_P (member_function) = 0;
20364
20365       /* If this is a local class, enter the scope of the containing
20366          function.  */
20367       function_scope = current_function_decl;
20368       if (function_scope)
20369         push_function_context ();
20370
20371       /* Push the body of the function onto the lexer stack.  */
20372       cp_parser_push_lexer_for_tokens (parser, tokens);
20373
20374       /* Let the front end know that we going to be defining this
20375          function.  */
20376       start_preparsed_function (member_function, NULL_TREE,
20377                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20378
20379       /* Don't do access checking if it is a templated function.  */
20380       if (processing_template_decl)
20381         push_deferring_access_checks (dk_no_check);
20382
20383       /* Now, parse the body of the function.  */
20384       cp_parser_function_definition_after_declarator (parser,
20385                                                       /*inline_p=*/true);
20386
20387       if (processing_template_decl)
20388         pop_deferring_access_checks ();
20389
20390       /* Leave the scope of the containing function.  */
20391       if (function_scope)
20392         pop_function_context ();
20393       cp_parser_pop_lexer (parser);
20394     }
20395
20396   /* Remove any template parameters from the symbol table.  */
20397   maybe_end_member_template_processing ();
20398
20399   /* Restore the queue.  */
20400   pop_unparsed_function_queues (parser);
20401 }
20402
20403 /* If DECL contains any default args, remember it on the unparsed
20404    functions queue.  */
20405
20406 static void
20407 cp_parser_save_default_args (cp_parser* parser, tree decl)
20408 {
20409   tree probe;
20410
20411   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20412        probe;
20413        probe = TREE_CHAIN (probe))
20414     if (TREE_PURPOSE (probe))
20415       {
20416         cp_default_arg_entry *entry
20417           = VEC_safe_push (cp_default_arg_entry, gc,
20418                            unparsed_funs_with_default_args, NULL);
20419         entry->class_type = current_class_type;
20420         entry->decl = decl;
20421         break;
20422       }
20423 }
20424
20425 /* FN is a FUNCTION_DECL which may contains a parameter with an
20426    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20427    assumes that the current scope is the scope in which the default
20428    argument should be processed.  */
20429
20430 static void
20431 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20432 {
20433   bool saved_local_variables_forbidden_p;
20434   tree parm, parmdecl;
20435
20436   /* While we're parsing the default args, we might (due to the
20437      statement expression extension) encounter more classes.  We want
20438      to handle them right away, but we don't want them getting mixed
20439      up with default args that are currently in the queue.  */
20440   push_unparsed_function_queues (parser);
20441
20442   /* Local variable names (and the `this' keyword) may not appear
20443      in a default argument.  */
20444   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20445   parser->local_variables_forbidden_p = true;
20446
20447   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20448          parmdecl = DECL_ARGUMENTS (fn);
20449        parm && parm != void_list_node;
20450        parm = TREE_CHAIN (parm),
20451          parmdecl = DECL_CHAIN (parmdecl))
20452     {
20453       cp_token_cache *tokens;
20454       tree default_arg = TREE_PURPOSE (parm);
20455       tree parsed_arg;
20456       VEC(tree,gc) *insts;
20457       tree copy;
20458       unsigned ix;
20459
20460       if (!default_arg)
20461         continue;
20462
20463       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20464         /* This can happen for a friend declaration for a function
20465            already declared with default arguments.  */
20466         continue;
20467
20468        /* Push the saved tokens for the default argument onto the parser's
20469           lexer stack.  */
20470       tokens = DEFARG_TOKENS (default_arg);
20471       cp_parser_push_lexer_for_tokens (parser, tokens);
20472
20473       start_lambda_scope (parmdecl);
20474
20475       /* Parse the assignment-expression.  */
20476       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20477       if (parsed_arg == error_mark_node)
20478         {
20479           cp_parser_pop_lexer (parser);
20480           continue;
20481         }
20482
20483       if (!processing_template_decl)
20484         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20485
20486       TREE_PURPOSE (parm) = parsed_arg;
20487
20488       /* Update any instantiations we've already created.  */
20489       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20490            VEC_iterate (tree, insts, ix, copy); ix++)
20491         TREE_PURPOSE (copy) = parsed_arg;
20492
20493       finish_lambda_scope ();
20494
20495       /* If the token stream has not been completely used up, then
20496          there was extra junk after the end of the default
20497          argument.  */
20498       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20499         cp_parser_error (parser, "expected %<,%>");
20500
20501       /* Revert to the main lexer.  */
20502       cp_parser_pop_lexer (parser);
20503     }
20504
20505   /* Make sure no default arg is missing.  */
20506   check_default_args (fn);
20507
20508   /* Restore the state of local_variables_forbidden_p.  */
20509   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20510
20511   /* Restore the queue.  */
20512   pop_unparsed_function_queues (parser);
20513 }
20514
20515 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20516    either a TYPE or an expression, depending on the form of the
20517    input.  The KEYWORD indicates which kind of expression we have
20518    encountered.  */
20519
20520 static tree
20521 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20522 {
20523   tree expr = NULL_TREE;
20524   const char *saved_message;
20525   char *tmp;
20526   bool saved_integral_constant_expression_p;
20527   bool saved_non_integral_constant_expression_p;
20528   bool pack_expansion_p = false;
20529
20530   /* Types cannot be defined in a `sizeof' expression.  Save away the
20531      old message.  */
20532   saved_message = parser->type_definition_forbidden_message;
20533   /* And create the new one.  */
20534   tmp = concat ("types may not be defined in %<",
20535                 IDENTIFIER_POINTER (ridpointers[keyword]),
20536                 "%> expressions", NULL);
20537   parser->type_definition_forbidden_message = tmp;
20538
20539   /* The restrictions on constant-expressions do not apply inside
20540      sizeof expressions.  */
20541   saved_integral_constant_expression_p
20542     = parser->integral_constant_expression_p;
20543   saved_non_integral_constant_expression_p
20544     = parser->non_integral_constant_expression_p;
20545   parser->integral_constant_expression_p = false;
20546
20547   /* If it's a `...', then we are computing the length of a parameter
20548      pack.  */
20549   if (keyword == RID_SIZEOF
20550       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20551     {
20552       /* Consume the `...'.  */
20553       cp_lexer_consume_token (parser->lexer);
20554       maybe_warn_variadic_templates ();
20555
20556       /* Note that this is an expansion.  */
20557       pack_expansion_p = true;
20558     }
20559
20560   /* Do not actually evaluate the expression.  */
20561   ++cp_unevaluated_operand;
20562   ++c_inhibit_evaluation_warnings;
20563   /* If it's a `(', then we might be looking at the type-id
20564      construction.  */
20565   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20566     {
20567       tree type;
20568       bool saved_in_type_id_in_expr_p;
20569
20570       /* We can't be sure yet whether we're looking at a type-id or an
20571          expression.  */
20572       cp_parser_parse_tentatively (parser);
20573       /* Consume the `('.  */
20574       cp_lexer_consume_token (parser->lexer);
20575       /* Parse the type-id.  */
20576       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20577       parser->in_type_id_in_expr_p = true;
20578       type = cp_parser_type_id (parser);
20579       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20580       /* Now, look for the trailing `)'.  */
20581       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20582       /* If all went well, then we're done.  */
20583       if (cp_parser_parse_definitely (parser))
20584         {
20585           cp_decl_specifier_seq decl_specs;
20586
20587           /* Build a trivial decl-specifier-seq.  */
20588           clear_decl_specs (&decl_specs);
20589           decl_specs.type = type;
20590
20591           /* Call grokdeclarator to figure out what type this is.  */
20592           expr = grokdeclarator (NULL,
20593                                  &decl_specs,
20594                                  TYPENAME,
20595                                  /*initialized=*/0,
20596                                  /*attrlist=*/NULL);
20597         }
20598     }
20599
20600   /* If the type-id production did not work out, then we must be
20601      looking at the unary-expression production.  */
20602   if (!expr)
20603     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20604                                        /*cast_p=*/false, NULL);
20605
20606   if (pack_expansion_p)
20607     /* Build a pack expansion. */
20608     expr = make_pack_expansion (expr);
20609
20610   /* Go back to evaluating expressions.  */
20611   --cp_unevaluated_operand;
20612   --c_inhibit_evaluation_warnings;
20613
20614   /* Free the message we created.  */
20615   free (tmp);
20616   /* And restore the old one.  */
20617   parser->type_definition_forbidden_message = saved_message;
20618   parser->integral_constant_expression_p
20619     = saved_integral_constant_expression_p;
20620   parser->non_integral_constant_expression_p
20621     = saved_non_integral_constant_expression_p;
20622
20623   return expr;
20624 }
20625
20626 /* If the current declaration has no declarator, return true.  */
20627
20628 static bool
20629 cp_parser_declares_only_class_p (cp_parser *parser)
20630 {
20631   /* If the next token is a `;' or a `,' then there is no
20632      declarator.  */
20633   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20634           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20635 }
20636
20637 /* Update the DECL_SPECS to reflect the storage class indicated by
20638    KEYWORD.  */
20639
20640 static void
20641 cp_parser_set_storage_class (cp_parser *parser,
20642                              cp_decl_specifier_seq *decl_specs,
20643                              enum rid keyword,
20644                              location_t location)
20645 {
20646   cp_storage_class storage_class;
20647
20648   if (parser->in_unbraced_linkage_specification_p)
20649     {
20650       error_at (location, "invalid use of %qD in linkage specification",
20651                 ridpointers[keyword]);
20652       return;
20653     }
20654   else if (decl_specs->storage_class != sc_none)
20655     {
20656       decl_specs->conflicting_specifiers_p = true;
20657       return;
20658     }
20659
20660   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20661       && decl_specs->specs[(int) ds_thread])
20662     {
20663       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20664       decl_specs->specs[(int) ds_thread] = 0;
20665     }
20666
20667   switch (keyword)
20668     {
20669     case RID_AUTO:
20670       storage_class = sc_auto;
20671       break;
20672     case RID_REGISTER:
20673       storage_class = sc_register;
20674       break;
20675     case RID_STATIC:
20676       storage_class = sc_static;
20677       break;
20678     case RID_EXTERN:
20679       storage_class = sc_extern;
20680       break;
20681     case RID_MUTABLE:
20682       storage_class = sc_mutable;
20683       break;
20684     default:
20685       gcc_unreachable ();
20686     }
20687   decl_specs->storage_class = storage_class;
20688
20689   /* A storage class specifier cannot be applied alongside a typedef 
20690      specifier. If there is a typedef specifier present then set 
20691      conflicting_specifiers_p which will trigger an error later
20692      on in grokdeclarator. */
20693   if (decl_specs->specs[(int)ds_typedef])
20694     decl_specs->conflicting_specifiers_p = true;
20695 }
20696
20697 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20698    is true, the type is a user-defined type; otherwise it is a
20699    built-in type specified by a keyword.  */
20700
20701 static void
20702 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20703                               tree type_spec,
20704                               location_t location,
20705                               bool user_defined_p)
20706 {
20707   decl_specs->any_specifiers_p = true;
20708
20709   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20710      (with, for example, in "typedef int wchar_t;") we remember that
20711      this is what happened.  In system headers, we ignore these
20712      declarations so that G++ can work with system headers that are not
20713      C++-safe.  */
20714   if (decl_specs->specs[(int) ds_typedef]
20715       && !user_defined_p
20716       && (type_spec == boolean_type_node
20717           || type_spec == char16_type_node
20718           || type_spec == char32_type_node
20719           || type_spec == wchar_type_node)
20720       && (decl_specs->type
20721           || decl_specs->specs[(int) ds_long]
20722           || decl_specs->specs[(int) ds_short]
20723           || decl_specs->specs[(int) ds_unsigned]
20724           || decl_specs->specs[(int) ds_signed]))
20725     {
20726       decl_specs->redefined_builtin_type = type_spec;
20727       if (!decl_specs->type)
20728         {
20729           decl_specs->type = type_spec;
20730           decl_specs->user_defined_type_p = false;
20731           decl_specs->type_location = location;
20732         }
20733     }
20734   else if (decl_specs->type)
20735     decl_specs->multiple_types_p = true;
20736   else
20737     {
20738       decl_specs->type = type_spec;
20739       decl_specs->user_defined_type_p = user_defined_p;
20740       decl_specs->redefined_builtin_type = NULL_TREE;
20741       decl_specs->type_location = location;
20742     }
20743 }
20744
20745 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20746    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20747
20748 static bool
20749 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20750 {
20751   return decl_specifiers->specs[(int) ds_friend] != 0;
20752 }
20753
20754 /* Issue an error message indicating that TOKEN_DESC was expected.
20755    If KEYWORD is true, it indicated this function is called by
20756    cp_parser_require_keword and the required token can only be
20757    a indicated keyword. */
20758
20759 static void
20760 cp_parser_required_error (cp_parser *parser,
20761                           required_token token_desc,
20762                           bool keyword)
20763 {
20764   switch (token_desc)
20765     {
20766       case RT_NEW:
20767         cp_parser_error (parser, "expected %<new%>");
20768         return;
20769       case RT_DELETE:
20770         cp_parser_error (parser, "expected %<delete%>");
20771         return;
20772       case RT_RETURN:
20773         cp_parser_error (parser, "expected %<return%>");
20774         return;
20775       case RT_WHILE:
20776         cp_parser_error (parser, "expected %<while%>");
20777         return;
20778       case RT_EXTERN:
20779         cp_parser_error (parser, "expected %<extern%>");
20780         return;
20781       case RT_STATIC_ASSERT:
20782         cp_parser_error (parser, "expected %<static_assert%>");
20783         return;
20784       case RT_DECLTYPE:
20785         cp_parser_error (parser, "expected %<decltype%>");
20786         return;
20787       case RT_OPERATOR:
20788         cp_parser_error (parser, "expected %<operator%>");
20789         return;
20790       case RT_CLASS:
20791         cp_parser_error (parser, "expected %<class%>");
20792         return;
20793       case RT_TEMPLATE:
20794         cp_parser_error (parser, "expected %<template%>");
20795         return;
20796       case RT_NAMESPACE:
20797         cp_parser_error (parser, "expected %<namespace%>");
20798         return;
20799       case RT_USING:
20800         cp_parser_error (parser, "expected %<using%>");
20801         return;
20802       case RT_ASM:
20803         cp_parser_error (parser, "expected %<asm%>");
20804         return;
20805       case RT_TRY:
20806         cp_parser_error (parser, "expected %<try%>");
20807         return;
20808       case RT_CATCH:
20809         cp_parser_error (parser, "expected %<catch%>");
20810         return;
20811       case RT_THROW:
20812         cp_parser_error (parser, "expected %<throw%>");
20813         return;
20814       case RT_LABEL:
20815         cp_parser_error (parser, "expected %<__label__%>");
20816         return;
20817       case RT_AT_TRY:
20818         cp_parser_error (parser, "expected %<@try%>");
20819         return;
20820       case RT_AT_SYNCHRONIZED:
20821         cp_parser_error (parser, "expected %<@synchronized%>");
20822         return;
20823       case RT_AT_THROW:
20824         cp_parser_error (parser, "expected %<@throw%>");
20825         return;
20826       default:
20827         break;
20828     }
20829   if (!keyword)
20830     {
20831       switch (token_desc)
20832         {
20833           case RT_SEMICOLON:
20834             cp_parser_error (parser, "expected %<;%>");
20835             return;
20836           case RT_OPEN_PAREN:
20837             cp_parser_error (parser, "expected %<(%>");
20838             return;
20839           case RT_CLOSE_BRACE:
20840             cp_parser_error (parser, "expected %<}%>");
20841             return;
20842           case RT_OPEN_BRACE:
20843             cp_parser_error (parser, "expected %<{%>");
20844             return;
20845           case RT_CLOSE_SQUARE:
20846             cp_parser_error (parser, "expected %<]%>");
20847             return;
20848           case RT_OPEN_SQUARE:
20849             cp_parser_error (parser, "expected %<[%>");
20850             return;
20851           case RT_COMMA:
20852             cp_parser_error (parser, "expected %<,%>");
20853             return;
20854           case RT_SCOPE:
20855             cp_parser_error (parser, "expected %<::%>");
20856             return;
20857           case RT_LESS:
20858             cp_parser_error (parser, "expected %<<%>");
20859             return;
20860           case RT_GREATER:
20861             cp_parser_error (parser, "expected %<>%>");
20862             return;
20863           case RT_EQ:
20864             cp_parser_error (parser, "expected %<=%>");
20865             return;
20866           case RT_ELLIPSIS:
20867             cp_parser_error (parser, "expected %<...%>");
20868             return;
20869           case RT_MULT:
20870             cp_parser_error (parser, "expected %<*%>");
20871             return;
20872           case RT_COMPL:
20873             cp_parser_error (parser, "expected %<~%>");
20874             return;
20875           case RT_COLON:
20876             cp_parser_error (parser, "expected %<:%>");
20877             return;
20878           case RT_COLON_SCOPE:
20879             cp_parser_error (parser, "expected %<:%> or %<::%>");
20880             return;
20881           case RT_CLOSE_PAREN:
20882             cp_parser_error (parser, "expected %<)%>");
20883             return;
20884           case RT_COMMA_CLOSE_PAREN:
20885             cp_parser_error (parser, "expected %<,%> or %<)%>");
20886             return;
20887           case RT_PRAGMA_EOL:
20888             cp_parser_error (parser, "expected end of line");
20889             return;
20890           case RT_NAME:
20891             cp_parser_error (parser, "expected identifier");
20892             return;
20893           case RT_SELECT:
20894             cp_parser_error (parser, "expected selection-statement");
20895             return;
20896           case RT_INTERATION:
20897             cp_parser_error (parser, "expected iteration-statement");
20898             return;
20899           case RT_JUMP:
20900             cp_parser_error (parser, "expected jump-statement");
20901             return;
20902           case RT_CLASS_KEY:
20903             cp_parser_error (parser, "expected class-key");
20904             return;
20905           case RT_CLASS_TYPENAME_TEMPLATE:
20906             cp_parser_error (parser,
20907                  "expected %<class%>, %<typename%>, or %<template%>");
20908             return;
20909           default:
20910             gcc_unreachable ();
20911         }
20912     }
20913   else
20914     gcc_unreachable ();
20915 }
20916
20917
20918
20919 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20920    issue an error message indicating that TOKEN_DESC was expected.
20921
20922    Returns the token consumed, if the token had the appropriate type.
20923    Otherwise, returns NULL.  */
20924
20925 static cp_token *
20926 cp_parser_require (cp_parser* parser,
20927                    enum cpp_ttype type,
20928                    required_token token_desc)
20929 {
20930   if (cp_lexer_next_token_is (parser->lexer, type))
20931     return cp_lexer_consume_token (parser->lexer);
20932   else
20933     {
20934       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20935       if (!cp_parser_simulate_error (parser))
20936         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20937       return NULL;
20938     }
20939 }
20940
20941 /* An error message is produced if the next token is not '>'.
20942    All further tokens are skipped until the desired token is
20943    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20944
20945 static void
20946 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20947 {
20948   /* Current level of '< ... >'.  */
20949   unsigned level = 0;
20950   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20951   unsigned nesting_depth = 0;
20952
20953   /* Are we ready, yet?  If not, issue error message.  */
20954   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20955     return;
20956
20957   /* Skip tokens until the desired token is found.  */
20958   while (true)
20959     {
20960       /* Peek at the next token.  */
20961       switch (cp_lexer_peek_token (parser->lexer)->type)
20962         {
20963         case CPP_LESS:
20964           if (!nesting_depth)
20965             ++level;
20966           break;
20967
20968         case CPP_RSHIFT:
20969           if (cxx_dialect == cxx98)
20970             /* C++0x views the `>>' operator as two `>' tokens, but
20971                C++98 does not. */
20972             break;
20973           else if (!nesting_depth && level-- == 0)
20974             {
20975               /* We've hit a `>>' where the first `>' closes the
20976                  template argument list, and the second `>' is
20977                  spurious.  Just consume the `>>' and stop; we've
20978                  already produced at least one error.  */
20979               cp_lexer_consume_token (parser->lexer);
20980               return;
20981             }
20982           /* Fall through for C++0x, so we handle the second `>' in
20983              the `>>'.  */
20984
20985         case CPP_GREATER:
20986           if (!nesting_depth && level-- == 0)
20987             {
20988               /* We've reached the token we want, consume it and stop.  */
20989               cp_lexer_consume_token (parser->lexer);
20990               return;
20991             }
20992           break;
20993
20994         case CPP_OPEN_PAREN:
20995         case CPP_OPEN_SQUARE:
20996           ++nesting_depth;
20997           break;
20998
20999         case CPP_CLOSE_PAREN:
21000         case CPP_CLOSE_SQUARE:
21001           if (nesting_depth-- == 0)
21002             return;
21003           break;
21004
21005         case CPP_EOF:
21006         case CPP_PRAGMA_EOL:
21007         case CPP_SEMICOLON:
21008         case CPP_OPEN_BRACE:
21009         case CPP_CLOSE_BRACE:
21010           /* The '>' was probably forgotten, don't look further.  */
21011           return;
21012
21013         default:
21014           break;
21015         }
21016
21017       /* Consume this token.  */
21018       cp_lexer_consume_token (parser->lexer);
21019     }
21020 }
21021
21022 /* If the next token is the indicated keyword, consume it.  Otherwise,
21023    issue an error message indicating that TOKEN_DESC was expected.
21024
21025    Returns the token consumed, if the token had the appropriate type.
21026    Otherwise, returns NULL.  */
21027
21028 static cp_token *
21029 cp_parser_require_keyword (cp_parser* parser,
21030                            enum rid keyword,
21031                            required_token token_desc)
21032 {
21033   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21034
21035   if (token && token->keyword != keyword)
21036     {
21037       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21038       return NULL;
21039     }
21040
21041   return token;
21042 }
21043
21044 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21045    function-definition.  */
21046
21047 static bool
21048 cp_parser_token_starts_function_definition_p (cp_token* token)
21049 {
21050   return (/* An ordinary function-body begins with an `{'.  */
21051           token->type == CPP_OPEN_BRACE
21052           /* A ctor-initializer begins with a `:'.  */
21053           || token->type == CPP_COLON
21054           /* A function-try-block begins with `try'.  */
21055           || token->keyword == RID_TRY
21056           /* The named return value extension begins with `return'.  */
21057           || token->keyword == RID_RETURN);
21058 }
21059
21060 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21061    definition.  */
21062
21063 static bool
21064 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21065 {
21066   cp_token *token;
21067
21068   token = cp_lexer_peek_token (parser->lexer);
21069   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21070 }
21071
21072 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21073    C++0x) ending a template-argument.  */
21074
21075 static bool
21076 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21077 {
21078   cp_token *token;
21079
21080   token = cp_lexer_peek_token (parser->lexer);
21081   return (token->type == CPP_COMMA 
21082           || token->type == CPP_GREATER
21083           || token->type == CPP_ELLIPSIS
21084           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21085 }
21086
21087 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21088    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21089
21090 static bool
21091 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21092                                                      size_t n)
21093 {
21094   cp_token *token;
21095
21096   token = cp_lexer_peek_nth_token (parser->lexer, n);
21097   if (token->type == CPP_LESS)
21098     return true;
21099   /* Check for the sequence `<::' in the original code. It would be lexed as
21100      `[:', where `[' is a digraph, and there is no whitespace before
21101      `:'.  */
21102   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21103     {
21104       cp_token *token2;
21105       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21106       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21107         return true;
21108     }
21109   return false;
21110 }
21111
21112 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21113    or none_type otherwise.  */
21114
21115 static enum tag_types
21116 cp_parser_token_is_class_key (cp_token* token)
21117 {
21118   switch (token->keyword)
21119     {
21120     case RID_CLASS:
21121       return class_type;
21122     case RID_STRUCT:
21123       return record_type;
21124     case RID_UNION:
21125       return union_type;
21126
21127     default:
21128       return none_type;
21129     }
21130 }
21131
21132 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21133
21134 static void
21135 cp_parser_check_class_key (enum tag_types class_key, tree type)
21136 {
21137   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21138     permerror (input_location, "%qs tag used in naming %q#T",
21139             class_key == union_type ? "union"
21140              : class_key == record_type ? "struct" : "class",
21141              type);
21142 }
21143
21144 /* Issue an error message if DECL is redeclared with different
21145    access than its original declaration [class.access.spec/3].
21146    This applies to nested classes and nested class templates.
21147    [class.mem/1].  */
21148
21149 static void
21150 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21151 {
21152   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21153     return;
21154
21155   if ((TREE_PRIVATE (decl)
21156        != (current_access_specifier == access_private_node))
21157       || (TREE_PROTECTED (decl)
21158           != (current_access_specifier == access_protected_node)))
21159     error_at (location, "%qD redeclared with different access", decl);
21160 }
21161
21162 /* Look for the `template' keyword, as a syntactic disambiguator.
21163    Return TRUE iff it is present, in which case it will be
21164    consumed.  */
21165
21166 static bool
21167 cp_parser_optional_template_keyword (cp_parser *parser)
21168 {
21169   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21170     {
21171       /* The `template' keyword can only be used within templates;
21172          outside templates the parser can always figure out what is a
21173          template and what is not.  */
21174       if (!processing_template_decl)
21175         {
21176           cp_token *token = cp_lexer_peek_token (parser->lexer);
21177           error_at (token->location,
21178                     "%<template%> (as a disambiguator) is only allowed "
21179                     "within templates");
21180           /* If this part of the token stream is rescanned, the same
21181              error message would be generated.  So, we purge the token
21182              from the stream.  */
21183           cp_lexer_purge_token (parser->lexer);
21184           return false;
21185         }
21186       else
21187         {
21188           /* Consume the `template' keyword.  */
21189           cp_lexer_consume_token (parser->lexer);
21190           return true;
21191         }
21192     }
21193
21194   return false;
21195 }
21196
21197 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21198    set PARSER->SCOPE, and perform other related actions.  */
21199
21200 static void
21201 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21202 {
21203   int i;
21204   struct tree_check *check_value;
21205   deferred_access_check *chk;
21206   VEC (deferred_access_check,gc) *checks;
21207
21208   /* Get the stored value.  */
21209   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21210   /* Perform any access checks that were deferred.  */
21211   checks = check_value->checks;
21212   if (checks)
21213     {
21214       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21215         perform_or_defer_access_check (chk->binfo,
21216                                        chk->decl,
21217                                        chk->diag_decl);
21218     }
21219   /* Set the scope from the stored value.  */
21220   parser->scope = check_value->value;
21221   parser->qualifying_scope = check_value->qualifying_scope;
21222   parser->object_scope = NULL_TREE;
21223 }
21224
21225 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21226    encounter the end of a block before what we were looking for.  */
21227
21228 static bool
21229 cp_parser_cache_group (cp_parser *parser,
21230                        enum cpp_ttype end,
21231                        unsigned depth)
21232 {
21233   while (true)
21234     {
21235       cp_token *token = cp_lexer_peek_token (parser->lexer);
21236
21237       /* Abort a parenthesized expression if we encounter a semicolon.  */
21238       if ((end == CPP_CLOSE_PAREN || depth == 0)
21239           && token->type == CPP_SEMICOLON)
21240         return true;
21241       /* If we've reached the end of the file, stop.  */
21242       if (token->type == CPP_EOF
21243           || (end != CPP_PRAGMA_EOL
21244               && token->type == CPP_PRAGMA_EOL))
21245         return true;
21246       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21247         /* We've hit the end of an enclosing block, so there's been some
21248            kind of syntax error.  */
21249         return true;
21250
21251       /* Consume the token.  */
21252       cp_lexer_consume_token (parser->lexer);
21253       /* See if it starts a new group.  */
21254       if (token->type == CPP_OPEN_BRACE)
21255         {
21256           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21257           /* In theory this should probably check end == '}', but
21258              cp_parser_save_member_function_body needs it to exit
21259              after either '}' or ')' when called with ')'.  */
21260           if (depth == 0)
21261             return false;
21262         }
21263       else if (token->type == CPP_OPEN_PAREN)
21264         {
21265           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21266           if (depth == 0 && end == CPP_CLOSE_PAREN)
21267             return false;
21268         }
21269       else if (token->type == CPP_PRAGMA)
21270         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21271       else if (token->type == end)
21272         return false;
21273     }
21274 }
21275
21276 /* Begin parsing tentatively.  We always save tokens while parsing
21277    tentatively so that if the tentative parsing fails we can restore the
21278    tokens.  */
21279
21280 static void
21281 cp_parser_parse_tentatively (cp_parser* parser)
21282 {
21283   /* Enter a new parsing context.  */
21284   parser->context = cp_parser_context_new (parser->context);
21285   /* Begin saving tokens.  */
21286   cp_lexer_save_tokens (parser->lexer);
21287   /* In order to avoid repetitive access control error messages,
21288      access checks are queued up until we are no longer parsing
21289      tentatively.  */
21290   push_deferring_access_checks (dk_deferred);
21291 }
21292
21293 /* Commit to the currently active tentative parse.  */
21294
21295 static void
21296 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21297 {
21298   cp_parser_context *context;
21299   cp_lexer *lexer;
21300
21301   /* Mark all of the levels as committed.  */
21302   lexer = parser->lexer;
21303   for (context = parser->context; context->next; context = context->next)
21304     {
21305       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21306         break;
21307       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21308       while (!cp_lexer_saving_tokens (lexer))
21309         lexer = lexer->next;
21310       cp_lexer_commit_tokens (lexer);
21311     }
21312 }
21313
21314 /* Abort the currently active tentative parse.  All consumed tokens
21315    will be rolled back, and no diagnostics will be issued.  */
21316
21317 static void
21318 cp_parser_abort_tentative_parse (cp_parser* parser)
21319 {
21320   cp_parser_simulate_error (parser);
21321   /* Now, pretend that we want to see if the construct was
21322      successfully parsed.  */
21323   cp_parser_parse_definitely (parser);
21324 }
21325
21326 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21327    token stream.  Otherwise, commit to the tokens we have consumed.
21328    Returns true if no error occurred; false otherwise.  */
21329
21330 static bool
21331 cp_parser_parse_definitely (cp_parser* parser)
21332 {
21333   bool error_occurred;
21334   cp_parser_context *context;
21335
21336   /* Remember whether or not an error occurred, since we are about to
21337      destroy that information.  */
21338   error_occurred = cp_parser_error_occurred (parser);
21339   /* Remove the topmost context from the stack.  */
21340   context = parser->context;
21341   parser->context = context->next;
21342   /* If no parse errors occurred, commit to the tentative parse.  */
21343   if (!error_occurred)
21344     {
21345       /* Commit to the tokens read tentatively, unless that was
21346          already done.  */
21347       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21348         cp_lexer_commit_tokens (parser->lexer);
21349
21350       pop_to_parent_deferring_access_checks ();
21351     }
21352   /* Otherwise, if errors occurred, roll back our state so that things
21353      are just as they were before we began the tentative parse.  */
21354   else
21355     {
21356       cp_lexer_rollback_tokens (parser->lexer);
21357       pop_deferring_access_checks ();
21358     }
21359   /* Add the context to the front of the free list.  */
21360   context->next = cp_parser_context_free_list;
21361   cp_parser_context_free_list = context;
21362
21363   return !error_occurred;
21364 }
21365
21366 /* Returns true if we are parsing tentatively and are not committed to
21367    this tentative parse.  */
21368
21369 static bool
21370 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21371 {
21372   return (cp_parser_parsing_tentatively (parser)
21373           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21374 }
21375
21376 /* Returns nonzero iff an error has occurred during the most recent
21377    tentative parse.  */
21378
21379 static bool
21380 cp_parser_error_occurred (cp_parser* parser)
21381 {
21382   return (cp_parser_parsing_tentatively (parser)
21383           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21384 }
21385
21386 /* Returns nonzero if GNU extensions are allowed.  */
21387
21388 static bool
21389 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21390 {
21391   return parser->allow_gnu_extensions_p;
21392 }
21393 \f
21394 /* Objective-C++ Productions */
21395
21396
21397 /* Parse an Objective-C expression, which feeds into a primary-expression
21398    above.
21399
21400    objc-expression:
21401      objc-message-expression
21402      objc-string-literal
21403      objc-encode-expression
21404      objc-protocol-expression
21405      objc-selector-expression
21406
21407   Returns a tree representation of the expression.  */
21408
21409 static tree
21410 cp_parser_objc_expression (cp_parser* parser)
21411 {
21412   /* Try to figure out what kind of declaration is present.  */
21413   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21414
21415   switch (kwd->type)
21416     {
21417     case CPP_OPEN_SQUARE:
21418       return cp_parser_objc_message_expression (parser);
21419
21420     case CPP_OBJC_STRING:
21421       kwd = cp_lexer_consume_token (parser->lexer);
21422       return objc_build_string_object (kwd->u.value);
21423
21424     case CPP_KEYWORD:
21425       switch (kwd->keyword)
21426         {
21427         case RID_AT_ENCODE:
21428           return cp_parser_objc_encode_expression (parser);
21429
21430         case RID_AT_PROTOCOL:
21431           return cp_parser_objc_protocol_expression (parser);
21432
21433         case RID_AT_SELECTOR:
21434           return cp_parser_objc_selector_expression (parser);
21435
21436         default:
21437           break;
21438         }
21439     default:
21440       error_at (kwd->location,
21441                 "misplaced %<@%D%> Objective-C++ construct",
21442                 kwd->u.value);
21443       cp_parser_skip_to_end_of_block_or_statement (parser);
21444     }
21445
21446   return error_mark_node;
21447 }
21448
21449 /* Parse an Objective-C message expression.
21450
21451    objc-message-expression:
21452      [ objc-message-receiver objc-message-args ]
21453
21454    Returns a representation of an Objective-C message.  */
21455
21456 static tree
21457 cp_parser_objc_message_expression (cp_parser* parser)
21458 {
21459   tree receiver, messageargs;
21460
21461   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21462   receiver = cp_parser_objc_message_receiver (parser);
21463   messageargs = cp_parser_objc_message_args (parser);
21464   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21465
21466   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21467 }
21468
21469 /* Parse an objc-message-receiver.
21470
21471    objc-message-receiver:
21472      expression
21473      simple-type-specifier
21474
21475   Returns a representation of the type or expression.  */
21476
21477 static tree
21478 cp_parser_objc_message_receiver (cp_parser* parser)
21479 {
21480   tree rcv;
21481
21482   /* An Objective-C message receiver may be either (1) a type
21483      or (2) an expression.  */
21484   cp_parser_parse_tentatively (parser);
21485   rcv = cp_parser_expression (parser, false, NULL);
21486
21487   if (cp_parser_parse_definitely (parser))
21488     return rcv;
21489
21490   rcv = cp_parser_simple_type_specifier (parser,
21491                                          /*decl_specs=*/NULL,
21492                                          CP_PARSER_FLAGS_NONE);
21493
21494   return objc_get_class_reference (rcv);
21495 }
21496
21497 /* Parse the arguments and selectors comprising an Objective-C message.
21498
21499    objc-message-args:
21500      objc-selector
21501      objc-selector-args
21502      objc-selector-args , objc-comma-args
21503
21504    objc-selector-args:
21505      objc-selector [opt] : assignment-expression
21506      objc-selector-args objc-selector [opt] : assignment-expression
21507
21508    objc-comma-args:
21509      assignment-expression
21510      objc-comma-args , assignment-expression
21511
21512    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21513    selector arguments and TREE_VALUE containing a list of comma
21514    arguments.  */
21515
21516 static tree
21517 cp_parser_objc_message_args (cp_parser* parser)
21518 {
21519   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21520   bool maybe_unary_selector_p = true;
21521   cp_token *token = cp_lexer_peek_token (parser->lexer);
21522
21523   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21524     {
21525       tree selector = NULL_TREE, arg;
21526
21527       if (token->type != CPP_COLON)
21528         selector = cp_parser_objc_selector (parser);
21529
21530       /* Detect if we have a unary selector.  */
21531       if (maybe_unary_selector_p
21532           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21533         return build_tree_list (selector, NULL_TREE);
21534
21535       maybe_unary_selector_p = false;
21536       cp_parser_require (parser, CPP_COLON, RT_COLON);
21537       arg = cp_parser_assignment_expression (parser, false, NULL);
21538
21539       sel_args
21540         = chainon (sel_args,
21541                    build_tree_list (selector, arg));
21542
21543       token = cp_lexer_peek_token (parser->lexer);
21544     }
21545
21546   /* Handle non-selector arguments, if any. */
21547   while (token->type == CPP_COMMA)
21548     {
21549       tree arg;
21550
21551       cp_lexer_consume_token (parser->lexer);
21552       arg = cp_parser_assignment_expression (parser, false, NULL);
21553
21554       addl_args
21555         = chainon (addl_args,
21556                    build_tree_list (NULL_TREE, arg));
21557
21558       token = cp_lexer_peek_token (parser->lexer);
21559     }
21560
21561   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21562     {
21563       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21564       return build_tree_list (error_mark_node, error_mark_node);
21565     }
21566
21567   return build_tree_list (sel_args, addl_args);
21568 }
21569
21570 /* Parse an Objective-C encode expression.
21571
21572    objc-encode-expression:
21573      @encode objc-typename
21574
21575    Returns an encoded representation of the type argument.  */
21576
21577 static tree
21578 cp_parser_objc_encode_expression (cp_parser* parser)
21579 {
21580   tree type;
21581   cp_token *token;
21582
21583   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21584   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21585   token = cp_lexer_peek_token (parser->lexer);
21586   type = complete_type (cp_parser_type_id (parser));
21587   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21588
21589   if (!type)
21590     {
21591       error_at (token->location, 
21592                 "%<@encode%> must specify a type as an argument");
21593       return error_mark_node;
21594     }
21595
21596   /* This happens if we find @encode(T) (where T is a template
21597      typename or something dependent on a template typename) when
21598      parsing a template.  In that case, we can't compile it
21599      immediately, but we rather create an AT_ENCODE_EXPR which will
21600      need to be instantiated when the template is used.
21601   */
21602   if (dependent_type_p (type))
21603     {
21604       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21605       TREE_READONLY (value) = 1;
21606       return value;
21607     }
21608
21609   return objc_build_encode_expr (type);
21610 }
21611
21612 /* Parse an Objective-C @defs expression.  */
21613
21614 static tree
21615 cp_parser_objc_defs_expression (cp_parser *parser)
21616 {
21617   tree name;
21618
21619   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21620   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21621   name = cp_parser_identifier (parser);
21622   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21623
21624   return objc_get_class_ivars (name);
21625 }
21626
21627 /* Parse an Objective-C protocol expression.
21628
21629   objc-protocol-expression:
21630     @protocol ( identifier )
21631
21632   Returns a representation of the protocol expression.  */
21633
21634 static tree
21635 cp_parser_objc_protocol_expression (cp_parser* parser)
21636 {
21637   tree proto;
21638
21639   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21640   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21641   proto = cp_parser_identifier (parser);
21642   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21643
21644   return objc_build_protocol_expr (proto);
21645 }
21646
21647 /* Parse an Objective-C selector expression.
21648
21649    objc-selector-expression:
21650      @selector ( objc-method-signature )
21651
21652    objc-method-signature:
21653      objc-selector
21654      objc-selector-seq
21655
21656    objc-selector-seq:
21657      objc-selector :
21658      objc-selector-seq objc-selector :
21659
21660   Returns a representation of the method selector.  */
21661
21662 static tree
21663 cp_parser_objc_selector_expression (cp_parser* parser)
21664 {
21665   tree sel_seq = NULL_TREE;
21666   bool maybe_unary_selector_p = true;
21667   cp_token *token;
21668   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21669
21670   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21671   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21672   token = cp_lexer_peek_token (parser->lexer);
21673
21674   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21675          || token->type == CPP_SCOPE)
21676     {
21677       tree selector = NULL_TREE;
21678
21679       if (token->type != CPP_COLON
21680           || token->type == CPP_SCOPE)
21681         selector = cp_parser_objc_selector (parser);
21682
21683       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21684           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21685         {
21686           /* Detect if we have a unary selector.  */
21687           if (maybe_unary_selector_p)
21688             {
21689               sel_seq = selector;
21690               goto finish_selector;
21691             }
21692           else
21693             {
21694               cp_parser_error (parser, "expected %<:%>");
21695             }
21696         }
21697       maybe_unary_selector_p = false;
21698       token = cp_lexer_consume_token (parser->lexer);
21699
21700       if (token->type == CPP_SCOPE)
21701         {
21702           sel_seq
21703             = chainon (sel_seq,
21704                        build_tree_list (selector, NULL_TREE));
21705           sel_seq
21706             = chainon (sel_seq,
21707                        build_tree_list (NULL_TREE, NULL_TREE));
21708         }
21709       else
21710         sel_seq
21711           = chainon (sel_seq,
21712                      build_tree_list (selector, NULL_TREE));
21713
21714       token = cp_lexer_peek_token (parser->lexer);
21715     }
21716
21717  finish_selector:
21718   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21719
21720   return objc_build_selector_expr (loc, sel_seq);
21721 }
21722
21723 /* Parse a list of identifiers.
21724
21725    objc-identifier-list:
21726      identifier
21727      objc-identifier-list , identifier
21728
21729    Returns a TREE_LIST of identifier nodes.  */
21730
21731 static tree
21732 cp_parser_objc_identifier_list (cp_parser* parser)
21733 {
21734   tree identifier;
21735   tree list;
21736   cp_token *sep;
21737
21738   identifier = cp_parser_identifier (parser);
21739   if (identifier == error_mark_node)
21740     return error_mark_node;      
21741
21742   list = build_tree_list (NULL_TREE, identifier);
21743   sep = cp_lexer_peek_token (parser->lexer);
21744
21745   while (sep->type == CPP_COMMA)
21746     {
21747       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21748       identifier = cp_parser_identifier (parser);
21749       if (identifier == error_mark_node)
21750         return list;
21751
21752       list = chainon (list, build_tree_list (NULL_TREE,
21753                                              identifier));
21754       sep = cp_lexer_peek_token (parser->lexer);
21755     }
21756   
21757   return list;
21758 }
21759
21760 /* Parse an Objective-C alias declaration.
21761
21762    objc-alias-declaration:
21763      @compatibility_alias identifier identifier ;
21764
21765    This function registers the alias mapping with the Objective-C front end.
21766    It returns nothing.  */
21767
21768 static void
21769 cp_parser_objc_alias_declaration (cp_parser* parser)
21770 {
21771   tree alias, orig;
21772
21773   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21774   alias = cp_parser_identifier (parser);
21775   orig = cp_parser_identifier (parser);
21776   objc_declare_alias (alias, orig);
21777   cp_parser_consume_semicolon_at_end_of_statement (parser);
21778 }
21779
21780 /* Parse an Objective-C class forward-declaration.
21781
21782    objc-class-declaration:
21783      @class objc-identifier-list ;
21784
21785    The function registers the forward declarations with the Objective-C
21786    front end.  It returns nothing.  */
21787
21788 static void
21789 cp_parser_objc_class_declaration (cp_parser* parser)
21790 {
21791   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21792   objc_declare_class (cp_parser_objc_identifier_list (parser));
21793   cp_parser_consume_semicolon_at_end_of_statement (parser);
21794 }
21795
21796 /* Parse a list of Objective-C protocol references.
21797
21798    objc-protocol-refs-opt:
21799      objc-protocol-refs [opt]
21800
21801    objc-protocol-refs:
21802      < objc-identifier-list >
21803
21804    Returns a TREE_LIST of identifiers, if any.  */
21805
21806 static tree
21807 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21808 {
21809   tree protorefs = NULL_TREE;
21810
21811   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21812     {
21813       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21814       protorefs = cp_parser_objc_identifier_list (parser);
21815       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21816     }
21817
21818   return protorefs;
21819 }
21820
21821 /* Parse a Objective-C visibility specification.  */
21822
21823 static void
21824 cp_parser_objc_visibility_spec (cp_parser* parser)
21825 {
21826   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21827
21828   switch (vis->keyword)
21829     {
21830     case RID_AT_PRIVATE:
21831       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21832       break;
21833     case RID_AT_PROTECTED:
21834       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21835       break;
21836     case RID_AT_PUBLIC:
21837       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21838       break;
21839     case RID_AT_PACKAGE:
21840       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21841       break;
21842     default:
21843       return;
21844     }
21845
21846   /* Eat '@private'/'@protected'/'@public'.  */
21847   cp_lexer_consume_token (parser->lexer);
21848 }
21849
21850 /* Parse an Objective-C method type.  Return 'true' if it is a class
21851    (+) method, and 'false' if it is an instance (-) method.  */
21852
21853 static inline bool
21854 cp_parser_objc_method_type (cp_parser* parser)
21855 {
21856   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21857     return true;
21858   else
21859     return false;
21860 }
21861
21862 /* Parse an Objective-C protocol qualifier.  */
21863
21864 static tree
21865 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21866 {
21867   tree quals = NULL_TREE, node;
21868   cp_token *token = cp_lexer_peek_token (parser->lexer);
21869
21870   node = token->u.value;
21871
21872   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21873          && (node == ridpointers [(int) RID_IN]
21874              || node == ridpointers [(int) RID_OUT]
21875              || node == ridpointers [(int) RID_INOUT]
21876              || node == ridpointers [(int) RID_BYCOPY]
21877              || node == ridpointers [(int) RID_BYREF]
21878              || node == ridpointers [(int) RID_ONEWAY]))
21879     {
21880       quals = tree_cons (NULL_TREE, node, quals);
21881       cp_lexer_consume_token (parser->lexer);
21882       token = cp_lexer_peek_token (parser->lexer);
21883       node = token->u.value;
21884     }
21885
21886   return quals;
21887 }
21888
21889 /* Parse an Objective-C typename.  */
21890
21891 static tree
21892 cp_parser_objc_typename (cp_parser* parser)
21893 {
21894   tree type_name = NULL_TREE;
21895
21896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21897     {
21898       tree proto_quals, cp_type = NULL_TREE;
21899
21900       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21901       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21902
21903       /* An ObjC type name may consist of just protocol qualifiers, in which
21904          case the type shall default to 'id'.  */
21905       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21906         cp_type = cp_parser_type_id (parser);
21907
21908       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21909       type_name = build_tree_list (proto_quals, cp_type);
21910     }
21911
21912   return type_name;
21913 }
21914
21915 /* Check to see if TYPE refers to an Objective-C selector name.  */
21916
21917 static bool
21918 cp_parser_objc_selector_p (enum cpp_ttype type)
21919 {
21920   return (type == CPP_NAME || type == CPP_KEYWORD
21921           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21922           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21923           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21924           || type == CPP_XOR || type == CPP_XOR_EQ);
21925 }
21926
21927 /* Parse an Objective-C selector.  */
21928
21929 static tree
21930 cp_parser_objc_selector (cp_parser* parser)
21931 {
21932   cp_token *token = cp_lexer_consume_token (parser->lexer);
21933
21934   if (!cp_parser_objc_selector_p (token->type))
21935     {
21936       error_at (token->location, "invalid Objective-C++ selector name");
21937       return error_mark_node;
21938     }
21939
21940   /* C++ operator names are allowed to appear in ObjC selectors.  */
21941   switch (token->type)
21942     {
21943     case CPP_AND_AND: return get_identifier ("and");
21944     case CPP_AND_EQ: return get_identifier ("and_eq");
21945     case CPP_AND: return get_identifier ("bitand");
21946     case CPP_OR: return get_identifier ("bitor");
21947     case CPP_COMPL: return get_identifier ("compl");
21948     case CPP_NOT: return get_identifier ("not");
21949     case CPP_NOT_EQ: return get_identifier ("not_eq");
21950     case CPP_OR_OR: return get_identifier ("or");
21951     case CPP_OR_EQ: return get_identifier ("or_eq");
21952     case CPP_XOR: return get_identifier ("xor");
21953     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21954     default: return token->u.value;
21955     }
21956 }
21957
21958 /* Parse an Objective-C params list.  */
21959
21960 static tree
21961 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21962 {
21963   tree params = NULL_TREE;
21964   bool maybe_unary_selector_p = true;
21965   cp_token *token = cp_lexer_peek_token (parser->lexer);
21966
21967   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21968     {
21969       tree selector = NULL_TREE, type_name, identifier;
21970       tree parm_attr = NULL_TREE;
21971
21972       if (token->keyword == RID_ATTRIBUTE)
21973         break;
21974
21975       if (token->type != CPP_COLON)
21976         selector = cp_parser_objc_selector (parser);
21977
21978       /* Detect if we have a unary selector.  */
21979       if (maybe_unary_selector_p
21980           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21981         {
21982           params = selector; /* Might be followed by attributes.  */
21983           break;
21984         }
21985
21986       maybe_unary_selector_p = false;
21987       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21988         {
21989           /* Something went quite wrong.  There should be a colon
21990              here, but there is not.  Stop parsing parameters.  */
21991           break;
21992         }
21993       type_name = cp_parser_objc_typename (parser);
21994       /* New ObjC allows attributes on parameters too.  */
21995       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21996         parm_attr = cp_parser_attributes_opt (parser);
21997       identifier = cp_parser_identifier (parser);
21998
21999       params
22000         = chainon (params,
22001                    objc_build_keyword_decl (selector,
22002                                             type_name,
22003                                             identifier,
22004                                             parm_attr));
22005
22006       token = cp_lexer_peek_token (parser->lexer);
22007     }
22008
22009   if (params == NULL_TREE)
22010     {
22011       cp_parser_error (parser, "objective-c++ method declaration is expected");
22012       return error_mark_node;
22013     }
22014
22015   /* We allow tail attributes for the method.  */
22016   if (token->keyword == RID_ATTRIBUTE)
22017     {
22018       *attributes = cp_parser_attributes_opt (parser);
22019       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22020           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22021         return params;
22022       cp_parser_error (parser, 
22023                        "method attributes must be specified at the end");
22024       return error_mark_node;
22025     }
22026
22027   if (params == NULL_TREE)
22028     {
22029       cp_parser_error (parser, "objective-c++ method declaration is expected");
22030       return error_mark_node;
22031     }
22032   return params;
22033 }
22034
22035 /* Parse the non-keyword Objective-C params.  */
22036
22037 static tree
22038 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22039                                        tree* attributes)
22040 {
22041   tree params = make_node (TREE_LIST);
22042   cp_token *token = cp_lexer_peek_token (parser->lexer);
22043   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22044
22045   while (token->type == CPP_COMMA)
22046     {
22047       cp_parameter_declarator *parmdecl;
22048       tree parm;
22049
22050       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22051       token = cp_lexer_peek_token (parser->lexer);
22052
22053       if (token->type == CPP_ELLIPSIS)
22054         {
22055           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22056           *ellipsisp = true;
22057           token = cp_lexer_peek_token (parser->lexer);
22058           break;
22059         }
22060
22061       /* TODO: parse attributes for tail parameters.  */
22062       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22063       parm = grokdeclarator (parmdecl->declarator,
22064                              &parmdecl->decl_specifiers,
22065                              PARM, /*initialized=*/0,
22066                              /*attrlist=*/NULL);
22067
22068       chainon (params, build_tree_list (NULL_TREE, parm));
22069       token = cp_lexer_peek_token (parser->lexer);
22070     }
22071
22072   /* We allow tail attributes for the method.  */
22073   if (token->keyword == RID_ATTRIBUTE)
22074     {
22075       if (*attributes == NULL_TREE)
22076         {
22077           *attributes = cp_parser_attributes_opt (parser);
22078           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22079               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22080             return params;
22081         }
22082       else        
22083         /* We have an error, but parse the attributes, so that we can 
22084            carry on.  */
22085         *attributes = cp_parser_attributes_opt (parser);
22086
22087       cp_parser_error (parser, 
22088                        "method attributes must be specified at the end");
22089       return error_mark_node;
22090     }
22091
22092   return params;
22093 }
22094
22095 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22096
22097 static void
22098 cp_parser_objc_interstitial_code (cp_parser* parser)
22099 {
22100   cp_token *token = cp_lexer_peek_token (parser->lexer);
22101
22102   /* If the next token is `extern' and the following token is a string
22103      literal, then we have a linkage specification.  */
22104   if (token->keyword == RID_EXTERN
22105       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22106     cp_parser_linkage_specification (parser);
22107   /* Handle #pragma, if any.  */
22108   else if (token->type == CPP_PRAGMA)
22109     cp_parser_pragma (parser, pragma_external);
22110   /* Allow stray semicolons.  */
22111   else if (token->type == CPP_SEMICOLON)
22112     cp_lexer_consume_token (parser->lexer);
22113   /* Mark methods as optional or required, when building protocols.  */
22114   else if (token->keyword == RID_AT_OPTIONAL)
22115     {
22116       cp_lexer_consume_token (parser->lexer);
22117       objc_set_method_opt (true);
22118     }
22119   else if (token->keyword == RID_AT_REQUIRED)
22120     {
22121       cp_lexer_consume_token (parser->lexer);
22122       objc_set_method_opt (false);
22123     }
22124   else if (token->keyword == RID_NAMESPACE)
22125     cp_parser_namespace_definition (parser);
22126   /* Other stray characters must generate errors.  */
22127   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22128     {
22129       cp_lexer_consume_token (parser->lexer);
22130       error ("stray %qs between Objective-C++ methods",
22131              token->type == CPP_OPEN_BRACE ? "{" : "}");
22132     }
22133   /* Finally, try to parse a block-declaration, or a function-definition.  */
22134   else
22135     cp_parser_block_declaration (parser, /*statement_p=*/false);
22136 }
22137
22138 /* Parse a method signature.  */
22139
22140 static tree
22141 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22142 {
22143   tree rettype, kwdparms, optparms;
22144   bool ellipsis = false;
22145   bool is_class_method;
22146
22147   is_class_method = cp_parser_objc_method_type (parser);
22148   rettype = cp_parser_objc_typename (parser);
22149   *attributes = NULL_TREE;
22150   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22151   if (kwdparms == error_mark_node)
22152     return error_mark_node;
22153   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22154   if (optparms == error_mark_node)
22155     return error_mark_node;
22156
22157   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22158 }
22159
22160 static bool
22161 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22162 {
22163   tree tattr;  
22164   cp_lexer_save_tokens (parser->lexer);
22165   tattr = cp_parser_attributes_opt (parser);
22166   gcc_assert (tattr) ;
22167   
22168   /* If the attributes are followed by a method introducer, this is not allowed.
22169      Dump the attributes and flag the situation.  */
22170   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22171       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22172     return true;
22173
22174   /* Otherwise, the attributes introduce some interstitial code, possibly so
22175      rewind to allow that check.  */
22176   cp_lexer_rollback_tokens (parser->lexer);
22177   return false;  
22178 }
22179
22180 /* Parse an Objective-C method prototype list.  */
22181
22182 static void
22183 cp_parser_objc_method_prototype_list (cp_parser* parser)
22184 {
22185   cp_token *token = cp_lexer_peek_token (parser->lexer);
22186
22187   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22188     {
22189       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22190         {
22191           tree attributes, sig;
22192           bool is_class_method;
22193           if (token->type == CPP_PLUS)
22194             is_class_method = true;
22195           else
22196             is_class_method = false;
22197           sig = cp_parser_objc_method_signature (parser, &attributes);
22198           if (sig == error_mark_node)
22199             {
22200               cp_parser_skip_to_end_of_block_or_statement (parser);
22201               token = cp_lexer_peek_token (parser->lexer);
22202               continue;
22203             }
22204           objc_add_method_declaration (is_class_method, sig, attributes);
22205           cp_parser_consume_semicolon_at_end_of_statement (parser);
22206         }
22207       else if (token->keyword == RID_AT_PROPERTY)
22208         cp_parser_objc_at_property_declaration (parser);
22209       else if (token->keyword == RID_ATTRIBUTE 
22210                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22211         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22212                     OPT_Wattributes, 
22213                     "prefix attributes are ignored for methods");
22214       else
22215         /* Allow for interspersed non-ObjC++ code.  */
22216         cp_parser_objc_interstitial_code (parser);
22217
22218       token = cp_lexer_peek_token (parser->lexer);
22219     }
22220
22221   if (token->type != CPP_EOF)
22222     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22223   else
22224     cp_parser_error (parser, "expected %<@end%>");
22225
22226   objc_finish_interface ();
22227 }
22228
22229 /* Parse an Objective-C method definition list.  */
22230
22231 static void
22232 cp_parser_objc_method_definition_list (cp_parser* parser)
22233 {
22234   cp_token *token = cp_lexer_peek_token (parser->lexer);
22235
22236   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22237     {
22238       tree meth;
22239
22240       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22241         {
22242           cp_token *ptk;
22243           tree sig, attribute;
22244           bool is_class_method;
22245           if (token->type == CPP_PLUS)
22246             is_class_method = true;
22247           else
22248             is_class_method = false;
22249           push_deferring_access_checks (dk_deferred);
22250           sig = cp_parser_objc_method_signature (parser, &attribute);
22251           if (sig == error_mark_node)
22252             {
22253               cp_parser_skip_to_end_of_block_or_statement (parser);
22254               token = cp_lexer_peek_token (parser->lexer);
22255               continue;
22256             }
22257           objc_start_method_definition (is_class_method, sig, attribute);
22258
22259           /* For historical reasons, we accept an optional semicolon.  */
22260           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22261             cp_lexer_consume_token (parser->lexer);
22262
22263           ptk = cp_lexer_peek_token (parser->lexer);
22264           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22265                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22266             {
22267               perform_deferred_access_checks ();
22268               stop_deferring_access_checks ();
22269               meth = cp_parser_function_definition_after_declarator (parser,
22270                                                                      false);
22271               pop_deferring_access_checks ();
22272               objc_finish_method_definition (meth);
22273             }
22274         }
22275       /* The following case will be removed once @synthesize is
22276          completely implemented.  */
22277       else if (token->keyword == RID_AT_PROPERTY)
22278         cp_parser_objc_at_property_declaration (parser);
22279       else if (token->keyword == RID_AT_SYNTHESIZE)
22280         cp_parser_objc_at_synthesize_declaration (parser);
22281       else if (token->keyword == RID_AT_DYNAMIC)
22282         cp_parser_objc_at_dynamic_declaration (parser);
22283       else if (token->keyword == RID_ATTRIBUTE 
22284                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22285         warning_at (token->location, OPT_Wattributes,
22286                     "prefix attributes are ignored for methods");
22287       else
22288         /* Allow for interspersed non-ObjC++ code.  */
22289         cp_parser_objc_interstitial_code (parser);
22290
22291       token = cp_lexer_peek_token (parser->lexer);
22292     }
22293
22294   if (token->type != CPP_EOF)
22295     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22296   else
22297     cp_parser_error (parser, "expected %<@end%>");
22298
22299   objc_finish_implementation ();
22300 }
22301
22302 /* Parse Objective-C ivars.  */
22303
22304 static void
22305 cp_parser_objc_class_ivars (cp_parser* parser)
22306 {
22307   cp_token *token = cp_lexer_peek_token (parser->lexer);
22308
22309   if (token->type != CPP_OPEN_BRACE)
22310     return;     /* No ivars specified.  */
22311
22312   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22313   token = cp_lexer_peek_token (parser->lexer);
22314
22315   while (token->type != CPP_CLOSE_BRACE 
22316         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22317     {
22318       cp_decl_specifier_seq declspecs;
22319       int decl_class_or_enum_p;
22320       tree prefix_attributes;
22321
22322       cp_parser_objc_visibility_spec (parser);
22323
22324       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22325         break;
22326
22327       cp_parser_decl_specifier_seq (parser,
22328                                     CP_PARSER_FLAGS_OPTIONAL,
22329                                     &declspecs,
22330                                     &decl_class_or_enum_p);
22331
22332       /* auto, register, static, extern, mutable.  */
22333       if (declspecs.storage_class != sc_none)
22334         {
22335           cp_parser_error (parser, "invalid type for instance variable");         
22336           declspecs.storage_class = sc_none;
22337         }
22338
22339       /* __thread.  */
22340       if (declspecs.specs[(int) ds_thread])
22341         {
22342           cp_parser_error (parser, "invalid type for instance variable");
22343           declspecs.specs[(int) ds_thread] = 0;
22344         }
22345       
22346       /* typedef.  */
22347       if (declspecs.specs[(int) ds_typedef])
22348         {
22349           cp_parser_error (parser, "invalid type for instance variable");
22350           declspecs.specs[(int) ds_typedef] = 0;
22351         }
22352
22353       prefix_attributes = declspecs.attributes;
22354       declspecs.attributes = NULL_TREE;
22355
22356       /* Keep going until we hit the `;' at the end of the
22357          declaration.  */
22358       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22359         {
22360           tree width = NULL_TREE, attributes, first_attribute, decl;
22361           cp_declarator *declarator = NULL;
22362           int ctor_dtor_or_conv_p;
22363
22364           /* Check for a (possibly unnamed) bitfield declaration.  */
22365           token = cp_lexer_peek_token (parser->lexer);
22366           if (token->type == CPP_COLON)
22367             goto eat_colon;
22368
22369           if (token->type == CPP_NAME
22370               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22371                   == CPP_COLON))
22372             {
22373               /* Get the name of the bitfield.  */
22374               declarator = make_id_declarator (NULL_TREE,
22375                                                cp_parser_identifier (parser),
22376                                                sfk_none);
22377
22378              eat_colon:
22379               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22380               /* Get the width of the bitfield.  */
22381               width
22382                 = cp_parser_constant_expression (parser,
22383                                                  /*allow_non_constant=*/false,
22384                                                  NULL);
22385             }
22386           else
22387             {
22388               /* Parse the declarator.  */
22389               declarator
22390                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22391                                         &ctor_dtor_or_conv_p,
22392                                         /*parenthesized_p=*/NULL,
22393                                         /*member_p=*/false);
22394             }
22395
22396           /* Look for attributes that apply to the ivar.  */
22397           attributes = cp_parser_attributes_opt (parser);
22398           /* Remember which attributes are prefix attributes and
22399              which are not.  */
22400           first_attribute = attributes;
22401           /* Combine the attributes.  */
22402           attributes = chainon (prefix_attributes, attributes);
22403
22404           if (width)
22405               /* Create the bitfield declaration.  */
22406               decl = grokbitfield (declarator, &declspecs,
22407                                    width,
22408                                    attributes);
22409           else
22410             decl = grokfield (declarator, &declspecs,
22411                               NULL_TREE, /*init_const_expr_p=*/false,
22412                               NULL_TREE, attributes);
22413
22414           /* Add the instance variable.  */
22415           objc_add_instance_variable (decl);
22416
22417           /* Reset PREFIX_ATTRIBUTES.  */
22418           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22419             attributes = TREE_CHAIN (attributes);
22420           if (attributes)
22421             TREE_CHAIN (attributes) = NULL_TREE;
22422
22423           token = cp_lexer_peek_token (parser->lexer);
22424
22425           if (token->type == CPP_COMMA)
22426             {
22427               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22428               continue;
22429             }
22430           break;
22431         }
22432
22433       cp_parser_consume_semicolon_at_end_of_statement (parser);
22434       token = cp_lexer_peek_token (parser->lexer);
22435     }
22436
22437   if (token->keyword == RID_AT_END)
22438     cp_parser_error (parser, "expected %<}%>");
22439
22440   /* Do not consume the RID_AT_END, so it will be read again as terminating
22441      the @interface of @implementation.  */ 
22442   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22443     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22444     
22445   /* For historical reasons, we accept an optional semicolon.  */
22446   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22447     cp_lexer_consume_token (parser->lexer);
22448 }
22449
22450 /* Parse an Objective-C protocol declaration.  */
22451
22452 static void
22453 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22454 {
22455   tree proto, protorefs;
22456   cp_token *tok;
22457
22458   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22459   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22460     {
22461       tok = cp_lexer_peek_token (parser->lexer);
22462       error_at (tok->location, "identifier expected after %<@protocol%>");
22463       goto finish;
22464     }
22465
22466   /* See if we have a forward declaration or a definition.  */
22467   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22468
22469   /* Try a forward declaration first.  */
22470   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22471     {
22472       objc_declare_protocols (cp_parser_objc_identifier_list (parser), 
22473                               attributes);
22474      finish:
22475       cp_parser_consume_semicolon_at_end_of_statement (parser);
22476     }
22477
22478   /* Ok, we got a full-fledged definition (or at least should).  */
22479   else
22480     {
22481       proto = cp_parser_identifier (parser);
22482       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22483       objc_start_protocol (proto, protorefs, attributes);
22484       cp_parser_objc_method_prototype_list (parser);
22485     }
22486 }
22487
22488 /* Parse an Objective-C superclass or category.  */
22489
22490 static void
22491 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22492                                        bool iface_p,
22493                                        tree *super,
22494                                        tree *categ, bool *is_class_extension)
22495 {
22496   cp_token *next = cp_lexer_peek_token (parser->lexer);
22497
22498   *super = *categ = NULL_TREE;
22499   *is_class_extension = false;
22500   if (next->type == CPP_COLON)
22501     {
22502       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22503       *super = cp_parser_identifier (parser);
22504     }
22505   else if (next->type == CPP_OPEN_PAREN)
22506     {
22507       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22508
22509       /* If there is no category name, and this is an @interface, we
22510          have a class extension.  */
22511       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22512         {
22513           *categ = NULL_TREE;
22514           *is_class_extension = true;
22515         }
22516       else
22517         *categ = cp_parser_identifier (parser);
22518
22519       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22520     }
22521 }
22522
22523 /* Parse an Objective-C class interface.  */
22524
22525 static void
22526 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22527 {
22528   tree name, super, categ, protos;
22529   bool is_class_extension;
22530
22531   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22532   name = cp_parser_identifier (parser);
22533   if (name == error_mark_node)
22534     {
22535       /* It's hard to recover because even if valid @interface stuff
22536          is to follow, we can't compile it (or validate it) if we
22537          don't even know which class it refers to.  Let's assume this
22538          was a stray '@interface' token in the stream and skip it.
22539       */
22540       return;
22541     }
22542   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22543                                          &is_class_extension);
22544   protos = cp_parser_objc_protocol_refs_opt (parser);
22545
22546   /* We have either a class or a category on our hands.  */
22547   if (categ || is_class_extension)
22548     objc_start_category_interface (name, categ, protos, attributes);
22549   else
22550     {
22551       objc_start_class_interface (name, super, protos, attributes);
22552       /* Handle instance variable declarations, if any.  */
22553       cp_parser_objc_class_ivars (parser);
22554       objc_continue_interface ();
22555     }
22556
22557   cp_parser_objc_method_prototype_list (parser);
22558 }
22559
22560 /* Parse an Objective-C class implementation.  */
22561
22562 static void
22563 cp_parser_objc_class_implementation (cp_parser* parser)
22564 {
22565   tree name, super, categ;
22566   bool is_class_extension;
22567
22568   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22569   name = cp_parser_identifier (parser);
22570   if (name == error_mark_node)
22571     {
22572       /* It's hard to recover because even if valid @implementation
22573          stuff is to follow, we can't compile it (or validate it) if
22574          we don't even know which class it refers to.  Let's assume
22575          this was a stray '@implementation' token in the stream and
22576          skip it.
22577       */
22578       return;
22579     }
22580   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22581                                          &is_class_extension);
22582
22583   /* We have either a class or a category on our hands.  */
22584   if (categ)
22585     objc_start_category_implementation (name, categ);
22586   else
22587     {
22588       objc_start_class_implementation (name, super);
22589       /* Handle instance variable declarations, if any.  */
22590       cp_parser_objc_class_ivars (parser);
22591       objc_continue_implementation ();
22592     }
22593
22594   cp_parser_objc_method_definition_list (parser);
22595 }
22596
22597 /* Consume the @end token and finish off the implementation.  */
22598
22599 static void
22600 cp_parser_objc_end_implementation (cp_parser* parser)
22601 {
22602   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22603   objc_finish_implementation ();
22604 }
22605
22606 /* Parse an Objective-C declaration.  */
22607
22608 static void
22609 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22610 {
22611   /* Try to figure out what kind of declaration is present.  */
22612   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22613
22614   if (attributes)
22615     switch (kwd->keyword)
22616       {
22617         case RID_AT_ALIAS:
22618         case RID_AT_CLASS:
22619         case RID_AT_END:
22620           error_at (kwd->location, "attributes may not be specified before"
22621                     " the %<@%D%> Objective-C++ keyword",
22622                     kwd->u.value);
22623           attributes = NULL;
22624           break;
22625         case RID_AT_IMPLEMENTATION:
22626           warning_at (kwd->location, OPT_Wattributes,
22627                       "prefix attributes are ignored before %<@%D%>",
22628                       kwd->u.value);
22629           attributes = NULL;
22630         default:
22631           break;
22632       }
22633
22634   switch (kwd->keyword)
22635     {
22636     case RID_AT_ALIAS:
22637       cp_parser_objc_alias_declaration (parser);
22638       break;
22639     case RID_AT_CLASS:
22640       cp_parser_objc_class_declaration (parser);
22641       break;
22642     case RID_AT_PROTOCOL:
22643       cp_parser_objc_protocol_declaration (parser, attributes);
22644       break;
22645     case RID_AT_INTERFACE:
22646       cp_parser_objc_class_interface (parser, attributes);
22647       break;
22648     case RID_AT_IMPLEMENTATION:
22649       cp_parser_objc_class_implementation (parser);
22650       break;
22651     case RID_AT_END:
22652       cp_parser_objc_end_implementation (parser);
22653       break;
22654     default:
22655       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22656                 kwd->u.value);
22657       cp_parser_skip_to_end_of_block_or_statement (parser);
22658     }
22659 }
22660
22661 /* Parse an Objective-C try-catch-finally statement.
22662
22663    objc-try-catch-finally-stmt:
22664      @try compound-statement objc-catch-clause-seq [opt]
22665        objc-finally-clause [opt]
22666
22667    objc-catch-clause-seq:
22668      objc-catch-clause objc-catch-clause-seq [opt]
22669
22670    objc-catch-clause:
22671      @catch ( objc-exception-declaration ) compound-statement
22672
22673    objc-finally-clause:
22674      @finally compound-statement
22675
22676    objc-exception-declaration:
22677      parameter-declaration
22678      '...'
22679
22680    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22681
22682    Returns NULL_TREE.
22683
22684    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22685    for C.  Keep them in sync.  */   
22686
22687 static tree
22688 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22689 {
22690   location_t location;
22691   tree stmt;
22692
22693   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22694   location = cp_lexer_peek_token (parser->lexer)->location;
22695   objc_maybe_warn_exceptions (location);
22696   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22697      node, lest it get absorbed into the surrounding block.  */
22698   stmt = push_stmt_list ();
22699   cp_parser_compound_statement (parser, NULL, false);
22700   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22701
22702   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22703     {
22704       cp_parameter_declarator *parm;
22705       tree parameter_declaration = error_mark_node;
22706       bool seen_open_paren = false;
22707
22708       cp_lexer_consume_token (parser->lexer);
22709       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22710         seen_open_paren = true;
22711       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22712         {
22713           /* We have "@catch (...)" (where the '...' are literally
22714              what is in the code).  Skip the '...'.
22715              parameter_declaration is set to NULL_TREE, and
22716              objc_being_catch_clauses() knows that that means
22717              '...'.  */
22718           cp_lexer_consume_token (parser->lexer);
22719           parameter_declaration = NULL_TREE;
22720         }
22721       else
22722         {
22723           /* We have "@catch (NSException *exception)" or something
22724              like that.  Parse the parameter declaration.  */
22725           parm = cp_parser_parameter_declaration (parser, false, NULL);
22726           if (parm == NULL)
22727             parameter_declaration = error_mark_node;
22728           else
22729             parameter_declaration = grokdeclarator (parm->declarator,
22730                                                     &parm->decl_specifiers,
22731                                                     PARM, /*initialized=*/0,
22732                                                     /*attrlist=*/NULL);
22733         }
22734       if (seen_open_paren)
22735         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22736       else
22737         {
22738           /* If there was no open parenthesis, we are recovering from
22739              an error, and we are trying to figure out what mistake
22740              the user has made.  */
22741
22742           /* If there is an immediate closing parenthesis, the user
22743              probably forgot the opening one (ie, they typed "@catch
22744              NSException *e)".  Parse the closing parenthesis and keep
22745              going.  */
22746           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22747             cp_lexer_consume_token (parser->lexer);
22748           
22749           /* If these is no immediate closing parenthesis, the user
22750              probably doesn't know that parenthesis are required at
22751              all (ie, they typed "@catch NSException *e").  So, just
22752              forget about the closing parenthesis and keep going.  */
22753         }
22754       objc_begin_catch_clause (parameter_declaration);
22755       cp_parser_compound_statement (parser, NULL, false);
22756       objc_finish_catch_clause ();
22757     }
22758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22759     {
22760       cp_lexer_consume_token (parser->lexer);
22761       location = cp_lexer_peek_token (parser->lexer)->location;
22762       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22763          node, lest it get absorbed into the surrounding block.  */
22764       stmt = push_stmt_list ();
22765       cp_parser_compound_statement (parser, NULL, false);
22766       objc_build_finally_clause (location, pop_stmt_list (stmt));
22767     }
22768
22769   return objc_finish_try_stmt ();
22770 }
22771
22772 /* Parse an Objective-C synchronized statement.
22773
22774    objc-synchronized-stmt:
22775      @synchronized ( expression ) compound-statement
22776
22777    Returns NULL_TREE.  */
22778
22779 static tree
22780 cp_parser_objc_synchronized_statement (cp_parser *parser)
22781 {
22782   location_t location;
22783   tree lock, stmt;
22784
22785   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22786
22787   location = cp_lexer_peek_token (parser->lexer)->location;
22788   objc_maybe_warn_exceptions (location);
22789   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22790   lock = cp_parser_expression (parser, false, NULL);
22791   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22792
22793   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22794      node, lest it get absorbed into the surrounding block.  */
22795   stmt = push_stmt_list ();
22796   cp_parser_compound_statement (parser, NULL, false);
22797
22798   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22799 }
22800
22801 /* Parse an Objective-C throw statement.
22802
22803    objc-throw-stmt:
22804      @throw assignment-expression [opt] ;
22805
22806    Returns a constructed '@throw' statement.  */
22807
22808 static tree
22809 cp_parser_objc_throw_statement (cp_parser *parser)
22810 {
22811   tree expr = NULL_TREE;
22812   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22813
22814   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22815
22816   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22817     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22818
22819   cp_parser_consume_semicolon_at_end_of_statement (parser);
22820
22821   return objc_build_throw_stmt (loc, expr);
22822 }
22823
22824 /* Parse an Objective-C statement.  */
22825
22826 static tree
22827 cp_parser_objc_statement (cp_parser * parser)
22828 {
22829   /* Try to figure out what kind of declaration is present.  */
22830   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22831
22832   switch (kwd->keyword)
22833     {
22834     case RID_AT_TRY:
22835       return cp_parser_objc_try_catch_finally_statement (parser);
22836     case RID_AT_SYNCHRONIZED:
22837       return cp_parser_objc_synchronized_statement (parser);
22838     case RID_AT_THROW:
22839       return cp_parser_objc_throw_statement (parser);
22840     default:
22841       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22842                kwd->u.value);
22843       cp_parser_skip_to_end_of_block_or_statement (parser);
22844     }
22845
22846   return error_mark_node;
22847 }
22848
22849 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22850    look ahead to see if an objc keyword follows the attributes.  This
22851    is to detect the use of prefix attributes on ObjC @interface and 
22852    @protocol.  */
22853
22854 static bool
22855 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22856 {
22857   cp_lexer_save_tokens (parser->lexer);
22858   *attrib = cp_parser_attributes_opt (parser);
22859   gcc_assert (*attrib);
22860   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22861     {
22862       cp_lexer_commit_tokens (parser->lexer);
22863       return true;
22864     }
22865   cp_lexer_rollback_tokens (parser->lexer);
22866   return false;  
22867 }
22868
22869 /* This routine is a minimal replacement for
22870    c_parser_struct_declaration () used when parsing the list of
22871    types/names or ObjC++ properties.  For example, when parsing the
22872    code
22873
22874    @property (readonly) int a, b, c;
22875
22876    this function is responsible for parsing "int a, int b, int c" and
22877    returning the declarations as CHAIN of DECLs.
22878
22879    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22880    similar parsing.  */
22881 static tree
22882 cp_parser_objc_struct_declaration (cp_parser *parser)
22883 {
22884   tree decls = NULL_TREE;
22885   cp_decl_specifier_seq declspecs;
22886   int decl_class_or_enum_p;
22887   tree prefix_attributes;
22888
22889   cp_parser_decl_specifier_seq (parser,
22890                                 CP_PARSER_FLAGS_NONE,
22891                                 &declspecs,
22892                                 &decl_class_or_enum_p);
22893
22894   if (declspecs.type == error_mark_node)
22895     return error_mark_node;
22896
22897   /* auto, register, static, extern, mutable.  */
22898   if (declspecs.storage_class != sc_none)
22899     {
22900       cp_parser_error (parser, "invalid type for property");
22901       declspecs.storage_class = sc_none;
22902     }
22903   
22904   /* __thread.  */
22905   if (declspecs.specs[(int) ds_thread])
22906     {
22907       cp_parser_error (parser, "invalid type for property");
22908       declspecs.specs[(int) ds_thread] = 0;
22909     }
22910   
22911   /* typedef.  */
22912   if (declspecs.specs[(int) ds_typedef])
22913     {
22914       cp_parser_error (parser, "invalid type for property");
22915       declspecs.specs[(int) ds_typedef] = 0;
22916     }
22917
22918   prefix_attributes = declspecs.attributes;
22919   declspecs.attributes = NULL_TREE;
22920
22921   /* Keep going until we hit the `;' at the end of the declaration. */
22922   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22923     {
22924       tree attributes, first_attribute, decl;
22925       cp_declarator *declarator;
22926       cp_token *token;
22927
22928       /* Parse the declarator.  */
22929       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22930                                          NULL, NULL, false);
22931
22932       /* Look for attributes that apply to the ivar.  */
22933       attributes = cp_parser_attributes_opt (parser);
22934       /* Remember which attributes are prefix attributes and
22935          which are not.  */
22936       first_attribute = attributes;
22937       /* Combine the attributes.  */
22938       attributes = chainon (prefix_attributes, attributes);
22939       
22940       decl = grokfield (declarator, &declspecs,
22941                         NULL_TREE, /*init_const_expr_p=*/false,
22942                         NULL_TREE, attributes);
22943
22944       if (decl == error_mark_node || decl == NULL_TREE)
22945         return error_mark_node;
22946       
22947       /* Reset PREFIX_ATTRIBUTES.  */
22948       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22949         attributes = TREE_CHAIN (attributes);
22950       if (attributes)
22951         TREE_CHAIN (attributes) = NULL_TREE;
22952
22953       DECL_CHAIN (decl) = decls;
22954       decls = decl;
22955
22956       token = cp_lexer_peek_token (parser->lexer);
22957       if (token->type == CPP_COMMA)
22958         {
22959           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22960           continue;
22961         }
22962       else
22963         break;
22964     }
22965   return decls;
22966 }
22967
22968 /* Parse an Objective-C @property declaration.  The syntax is:
22969
22970    objc-property-declaration:
22971      '@property' objc-property-attributes[opt] struct-declaration ;
22972
22973    objc-property-attributes:
22974     '(' objc-property-attribute-list ')'
22975
22976    objc-property-attribute-list:
22977      objc-property-attribute
22978      objc-property-attribute-list, objc-property-attribute
22979
22980    objc-property-attribute
22981      'getter' = identifier
22982      'setter' = identifier
22983      'readonly'
22984      'readwrite'
22985      'assign'
22986      'retain'
22987      'copy'
22988      'nonatomic'
22989
22990   For example:
22991     @property NSString *name;
22992     @property (readonly) id object;
22993     @property (retain, nonatomic, getter=getTheName) id name;
22994     @property int a, b, c;
22995
22996    PS: This function is identical to
22997    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
22998 static void 
22999 cp_parser_objc_at_property_declaration (cp_parser *parser)
23000 {
23001   /* The following variables hold the attributes of the properties as
23002      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23003      seen.  When we see an attribute, we set them to 'true' (if they
23004      are boolean properties) or to the identifier (if they have an
23005      argument, ie, for getter and setter).  Note that here we only
23006      parse the list of attributes, check the syntax and accumulate the
23007      attributes that we find.  objc_add_property_declaration() will
23008      then process the information.  */
23009   bool property_assign = false;
23010   bool property_copy = false;
23011   tree property_getter_ident = NULL_TREE;
23012   bool property_nonatomic = false;
23013   bool property_readonly = false;
23014   bool property_readwrite = false;
23015   bool property_retain = false;
23016   tree property_setter_ident = NULL_TREE;
23017
23018   /* 'properties' is the list of properties that we read.  Usually a
23019      single one, but maybe more (eg, in "@property int a, b, c;" there
23020      are three).  */
23021   tree properties;
23022   location_t loc;
23023
23024   loc = cp_lexer_peek_token (parser->lexer)->location;
23025
23026   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23027
23028   /* Parse the optional attribute list...  */
23029   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23030     {
23031       /* Eat the '('.  */
23032       cp_lexer_consume_token (parser->lexer);
23033
23034       while (true)
23035         {
23036           bool syntax_error = false;
23037           cp_token *token = cp_lexer_peek_token (parser->lexer);
23038           enum rid keyword;
23039
23040           if (token->type != CPP_NAME)
23041             {
23042               cp_parser_error (parser, "expected identifier");
23043               break;
23044             }
23045           keyword = C_RID_CODE (token->u.value);
23046           cp_lexer_consume_token (parser->lexer);
23047           switch (keyword)
23048             {
23049             case RID_ASSIGN:    property_assign = true;    break;
23050             case RID_COPY:      property_copy = true;      break;
23051             case RID_NONATOMIC: property_nonatomic = true; break;
23052             case RID_READONLY:  property_readonly = true;  break;
23053             case RID_READWRITE: property_readwrite = true; break;
23054             case RID_RETAIN:    property_retain = true;    break;
23055
23056             case RID_GETTER:
23057             case RID_SETTER:
23058               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23059                 {
23060                   cp_parser_error (parser,
23061                                    "getter/setter/ivar attribute must be followed by %<=%>");
23062                   syntax_error = true;
23063                   break;
23064                 }
23065               cp_lexer_consume_token (parser->lexer); /* eat the = */
23066               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23067                 {
23068                   cp_parser_error (parser, "expected identifier");
23069                   syntax_error = true;
23070                   break;
23071                 }
23072               if (keyword == RID_SETTER)
23073                 {
23074                   if (property_setter_ident != NULL_TREE)
23075                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23076                   else
23077                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23078                   cp_lexer_consume_token (parser->lexer);
23079                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23080                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23081                   else
23082                     cp_lexer_consume_token (parser->lexer);
23083                 }
23084               else
23085                 {
23086                   if (property_getter_ident != NULL_TREE)
23087                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23088                   else
23089                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23090                   cp_lexer_consume_token (parser->lexer);
23091                 }
23092               break;
23093             default:
23094               cp_parser_error (parser, "unknown property attribute");
23095               syntax_error = true;
23096               break;
23097             }
23098
23099           if (syntax_error)
23100             break;
23101           
23102           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23103             cp_lexer_consume_token (parser->lexer);
23104           else
23105             break;
23106         }
23107
23108       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23109         {
23110           cp_parser_skip_to_closing_parenthesis (parser,
23111                                                  /*recovering=*/true,
23112                                                  /*or_comma=*/false,
23113                                                  /*consume_paren=*/true);
23114         }
23115     }
23116
23117   /* ... and the property declaration(s).  */
23118   properties = cp_parser_objc_struct_declaration (parser);
23119
23120   if (properties == error_mark_node)
23121     {
23122       cp_parser_skip_to_end_of_statement (parser);
23123       /* If the next token is now a `;', consume it.  */
23124       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23125         cp_lexer_consume_token (parser->lexer);
23126       return;
23127     }
23128
23129   if (properties == NULL_TREE)
23130     cp_parser_error (parser, "expected identifier");
23131   else
23132     {
23133       /* Comma-separated properties are chained together in
23134          reverse order; add them one by one.  */
23135       properties = nreverse (properties);
23136       
23137       for (; properties; properties = TREE_CHAIN (properties))
23138         objc_add_property_declaration (loc, copy_node (properties),
23139                                        property_readonly, property_readwrite,
23140                                        property_assign, property_retain,
23141                                        property_copy, property_nonatomic,
23142                                        property_getter_ident, property_setter_ident);
23143     }
23144   
23145   cp_parser_consume_semicolon_at_end_of_statement (parser);
23146 }
23147
23148 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23149
23150    objc-synthesize-declaration:
23151      @synthesize objc-synthesize-identifier-list ;
23152
23153    objc-synthesize-identifier-list:
23154      objc-synthesize-identifier
23155      objc-synthesize-identifier-list, objc-synthesize-identifier
23156
23157    objc-synthesize-identifier
23158      identifier
23159      identifier = identifier
23160
23161   For example:
23162     @synthesize MyProperty;
23163     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23164
23165   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23166   for C.  Keep them in sync.
23167 */
23168 static void 
23169 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23170 {
23171   tree list = NULL_TREE;
23172   location_t loc;
23173   loc = cp_lexer_peek_token (parser->lexer)->location;
23174
23175   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23176   while (true)
23177     {
23178       tree property, ivar;
23179       property = cp_parser_identifier (parser);
23180       if (property == error_mark_node)
23181         {
23182           cp_parser_consume_semicolon_at_end_of_statement (parser);
23183           return;
23184         }
23185       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23186         {
23187           cp_lexer_consume_token (parser->lexer);
23188           ivar = cp_parser_identifier (parser);
23189           if (ivar == error_mark_node)
23190             {
23191               cp_parser_consume_semicolon_at_end_of_statement (parser);
23192               return;
23193             }
23194         }
23195       else
23196         ivar = NULL_TREE;
23197       list = chainon (list, build_tree_list (ivar, property));
23198       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23199         cp_lexer_consume_token (parser->lexer);
23200       else
23201         break;
23202     }
23203   cp_parser_consume_semicolon_at_end_of_statement (parser);
23204   objc_add_synthesize_declaration (loc, list);
23205 }
23206
23207 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23208
23209    objc-dynamic-declaration:
23210      @dynamic identifier-list ;
23211
23212    For example:
23213      @dynamic MyProperty;
23214      @dynamic MyProperty, AnotherProperty;
23215
23216   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23217   for C.  Keep them in sync.
23218 */
23219 static void 
23220 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23221 {
23222   tree list = NULL_TREE;
23223   location_t loc;
23224   loc = cp_lexer_peek_token (parser->lexer)->location;
23225
23226   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23227   while (true)
23228     {
23229       tree property;
23230       property = cp_parser_identifier (parser);
23231       if (property == error_mark_node)
23232         {
23233           cp_parser_consume_semicolon_at_end_of_statement (parser);
23234           return;
23235         }
23236       list = chainon (list, build_tree_list (NULL, property));
23237       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23238         cp_lexer_consume_token (parser->lexer);
23239       else
23240         break;
23241     }
23242   cp_parser_consume_semicolon_at_end_of_statement (parser);
23243   objc_add_dynamic_declaration (loc, list);
23244 }
23245
23246 \f
23247 /* OpenMP 2.5 parsing routines.  */
23248
23249 /* Returns name of the next clause.
23250    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23251    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23252    returned and the token is consumed.  */
23253
23254 static pragma_omp_clause
23255 cp_parser_omp_clause_name (cp_parser *parser)
23256 {
23257   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23258
23259   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23260     result = PRAGMA_OMP_CLAUSE_IF;
23261   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23262     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23263   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23264     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23265   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23266     {
23267       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23268       const char *p = IDENTIFIER_POINTER (id);
23269
23270       switch (p[0])
23271         {
23272         case 'c':
23273           if (!strcmp ("collapse", p))
23274             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23275           else if (!strcmp ("copyin", p))
23276             result = PRAGMA_OMP_CLAUSE_COPYIN;
23277           else if (!strcmp ("copyprivate", p))
23278             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23279           break;
23280         case 'f':
23281           if (!strcmp ("firstprivate", p))
23282             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23283           break;
23284         case 'l':
23285           if (!strcmp ("lastprivate", p))
23286             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23287           break;
23288         case 'n':
23289           if (!strcmp ("nowait", p))
23290             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23291           else if (!strcmp ("num_threads", p))
23292             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23293           break;
23294         case 'o':
23295           if (!strcmp ("ordered", p))
23296             result = PRAGMA_OMP_CLAUSE_ORDERED;
23297           break;
23298         case 'r':
23299           if (!strcmp ("reduction", p))
23300             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23301           break;
23302         case 's':
23303           if (!strcmp ("schedule", p))
23304             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23305           else if (!strcmp ("shared", p))
23306             result = PRAGMA_OMP_CLAUSE_SHARED;
23307           break;
23308         case 'u':
23309           if (!strcmp ("untied", p))
23310             result = PRAGMA_OMP_CLAUSE_UNTIED;
23311           break;
23312         }
23313     }
23314
23315   if (result != PRAGMA_OMP_CLAUSE_NONE)
23316     cp_lexer_consume_token (parser->lexer);
23317
23318   return result;
23319 }
23320
23321 /* Validate that a clause of the given type does not already exist.  */
23322
23323 static void
23324 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23325                            const char *name, location_t location)
23326 {
23327   tree c;
23328
23329   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23330     if (OMP_CLAUSE_CODE (c) == code)
23331       {
23332         error_at (location, "too many %qs clauses", name);
23333         break;
23334       }
23335 }
23336
23337 /* OpenMP 2.5:
23338    variable-list:
23339      identifier
23340      variable-list , identifier
23341
23342    In addition, we match a closing parenthesis.  An opening parenthesis
23343    will have been consumed by the caller.
23344
23345    If KIND is nonzero, create the appropriate node and install the decl
23346    in OMP_CLAUSE_DECL and add the node to the head of the list.
23347
23348    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23349    return the list created.  */
23350
23351 static tree
23352 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23353                                 tree list)
23354 {
23355   cp_token *token;
23356   while (1)
23357     {
23358       tree name, decl;
23359
23360       token = cp_lexer_peek_token (parser->lexer);
23361       name = cp_parser_id_expression (parser, /*template_p=*/false,
23362                                       /*check_dependency_p=*/true,
23363                                       /*template_p=*/NULL,
23364                                       /*declarator_p=*/false,
23365                                       /*optional_p=*/false);
23366       if (name == error_mark_node)
23367         goto skip_comma;
23368
23369       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23370       if (decl == error_mark_node)
23371         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23372                                      token->location);
23373       else if (kind != 0)
23374         {
23375           tree u = build_omp_clause (token->location, kind);
23376           OMP_CLAUSE_DECL (u) = decl;
23377           OMP_CLAUSE_CHAIN (u) = list;
23378           list = u;
23379         }
23380       else
23381         list = tree_cons (decl, NULL_TREE, list);
23382
23383     get_comma:
23384       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23385         break;
23386       cp_lexer_consume_token (parser->lexer);
23387     }
23388
23389   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23390     {
23391       int ending;
23392
23393       /* Try to resync to an unnested comma.  Copied from
23394          cp_parser_parenthesized_expression_list.  */
23395     skip_comma:
23396       ending = cp_parser_skip_to_closing_parenthesis (parser,
23397                                                       /*recovering=*/true,
23398                                                       /*or_comma=*/true,
23399                                                       /*consume_paren=*/true);
23400       if (ending < 0)
23401         goto get_comma;
23402     }
23403
23404   return list;
23405 }
23406
23407 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23408    common case for omp clauses.  */
23409
23410 static tree
23411 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23412 {
23413   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23414     return cp_parser_omp_var_list_no_open (parser, kind, list);
23415   return list;
23416 }
23417
23418 /* OpenMP 3.0:
23419    collapse ( constant-expression ) */
23420
23421 static tree
23422 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23423 {
23424   tree c, num;
23425   location_t loc;
23426   HOST_WIDE_INT n;
23427
23428   loc = cp_lexer_peek_token (parser->lexer)->location;
23429   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23430     return list;
23431
23432   num = cp_parser_constant_expression (parser, false, NULL);
23433
23434   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23435     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23436                                            /*or_comma=*/false,
23437                                            /*consume_paren=*/true);
23438
23439   if (num == error_mark_node)
23440     return list;
23441   num = fold_non_dependent_expr (num);
23442   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23443       || !host_integerp (num, 0)
23444       || (n = tree_low_cst (num, 0)) <= 0
23445       || (int) n != n)
23446     {
23447       error_at (loc, "collapse argument needs positive constant integer expression");
23448       return list;
23449     }
23450
23451   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23452   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23453   OMP_CLAUSE_CHAIN (c) = list;
23454   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23455
23456   return c;
23457 }
23458
23459 /* OpenMP 2.5:
23460    default ( shared | none ) */
23461
23462 static tree
23463 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23464 {
23465   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23466   tree c;
23467
23468   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23469     return list;
23470   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23471     {
23472       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23473       const char *p = IDENTIFIER_POINTER (id);
23474
23475       switch (p[0])
23476         {
23477         case 'n':
23478           if (strcmp ("none", p) != 0)
23479             goto invalid_kind;
23480           kind = OMP_CLAUSE_DEFAULT_NONE;
23481           break;
23482
23483         case 's':
23484           if (strcmp ("shared", p) != 0)
23485             goto invalid_kind;
23486           kind = OMP_CLAUSE_DEFAULT_SHARED;
23487           break;
23488
23489         default:
23490           goto invalid_kind;
23491         }
23492
23493       cp_lexer_consume_token (parser->lexer);
23494     }
23495   else
23496     {
23497     invalid_kind:
23498       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23499     }
23500
23501   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23502     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23503                                            /*or_comma=*/false,
23504                                            /*consume_paren=*/true);
23505
23506   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23507     return list;
23508
23509   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23510   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23511   OMP_CLAUSE_CHAIN (c) = list;
23512   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23513
23514   return c;
23515 }
23516
23517 /* OpenMP 2.5:
23518    if ( expression ) */
23519
23520 static tree
23521 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23522 {
23523   tree t, c;
23524
23525   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23526     return list;
23527
23528   t = cp_parser_condition (parser);
23529
23530   if (t == error_mark_node
23531       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23532     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23533                                            /*or_comma=*/false,
23534                                            /*consume_paren=*/true);
23535
23536   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23537
23538   c = build_omp_clause (location, OMP_CLAUSE_IF);
23539   OMP_CLAUSE_IF_EXPR (c) = t;
23540   OMP_CLAUSE_CHAIN (c) = list;
23541
23542   return c;
23543 }
23544
23545 /* OpenMP 2.5:
23546    nowait */
23547
23548 static tree
23549 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23550                              tree list, location_t location)
23551 {
23552   tree c;
23553
23554   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23555
23556   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23557   OMP_CLAUSE_CHAIN (c) = list;
23558   return c;
23559 }
23560
23561 /* OpenMP 2.5:
23562    num_threads ( expression ) */
23563
23564 static tree
23565 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23566                                   location_t location)
23567 {
23568   tree t, c;
23569
23570   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23571     return list;
23572
23573   t = cp_parser_expression (parser, false, NULL);
23574
23575   if (t == error_mark_node
23576       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23577     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23578                                            /*or_comma=*/false,
23579                                            /*consume_paren=*/true);
23580
23581   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23582                              "num_threads", location);
23583
23584   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23585   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23586   OMP_CLAUSE_CHAIN (c) = list;
23587
23588   return c;
23589 }
23590
23591 /* OpenMP 2.5:
23592    ordered */
23593
23594 static tree
23595 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23596                               tree list, location_t location)
23597 {
23598   tree c;
23599
23600   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23601                              "ordered", location);
23602
23603   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23604   OMP_CLAUSE_CHAIN (c) = list;
23605   return c;
23606 }
23607
23608 /* OpenMP 2.5:
23609    reduction ( reduction-operator : variable-list )
23610
23611    reduction-operator:
23612      One of: + * - & ^ | && || */
23613
23614 static tree
23615 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23616 {
23617   enum tree_code code;
23618   tree nlist, c;
23619
23620   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23621     return list;
23622
23623   switch (cp_lexer_peek_token (parser->lexer)->type)
23624     {
23625     case CPP_PLUS:
23626       code = PLUS_EXPR;
23627       break;
23628     case CPP_MULT:
23629       code = MULT_EXPR;
23630       break;
23631     case CPP_MINUS:
23632       code = MINUS_EXPR;
23633       break;
23634     case CPP_AND:
23635       code = BIT_AND_EXPR;
23636       break;
23637     case CPP_XOR:
23638       code = BIT_XOR_EXPR;
23639       break;
23640     case CPP_OR:
23641       code = BIT_IOR_EXPR;
23642       break;
23643     case CPP_AND_AND:
23644       code = TRUTH_ANDIF_EXPR;
23645       break;
23646     case CPP_OR_OR:
23647       code = TRUTH_ORIF_EXPR;
23648       break;
23649     default:
23650       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23651                                "%<|%>, %<&&%>, or %<||%>");
23652     resync_fail:
23653       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23654                                              /*or_comma=*/false,
23655                                              /*consume_paren=*/true);
23656       return list;
23657     }
23658   cp_lexer_consume_token (parser->lexer);
23659
23660   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23661     goto resync_fail;
23662
23663   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23664   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23665     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23666
23667   return nlist;
23668 }
23669
23670 /* OpenMP 2.5:
23671    schedule ( schedule-kind )
23672    schedule ( schedule-kind , expression )
23673
23674    schedule-kind:
23675      static | dynamic | guided | runtime | auto  */
23676
23677 static tree
23678 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23679 {
23680   tree c, t;
23681
23682   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23683     return list;
23684
23685   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23686
23687   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23688     {
23689       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23690       const char *p = IDENTIFIER_POINTER (id);
23691
23692       switch (p[0])
23693         {
23694         case 'd':
23695           if (strcmp ("dynamic", p) != 0)
23696             goto invalid_kind;
23697           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23698           break;
23699
23700         case 'g':
23701           if (strcmp ("guided", p) != 0)
23702             goto invalid_kind;
23703           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23704           break;
23705
23706         case 'r':
23707           if (strcmp ("runtime", p) != 0)
23708             goto invalid_kind;
23709           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23710           break;
23711
23712         default:
23713           goto invalid_kind;
23714         }
23715     }
23716   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23717     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23718   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23719     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23720   else
23721     goto invalid_kind;
23722   cp_lexer_consume_token (parser->lexer);
23723
23724   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23725     {
23726       cp_token *token;
23727       cp_lexer_consume_token (parser->lexer);
23728
23729       token = cp_lexer_peek_token (parser->lexer);
23730       t = cp_parser_assignment_expression (parser, false, NULL);
23731
23732       if (t == error_mark_node)
23733         goto resync_fail;
23734       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23735         error_at (token->location, "schedule %<runtime%> does not take "
23736                   "a %<chunk_size%> parameter");
23737       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23738         error_at (token->location, "schedule %<auto%> does not take "
23739                   "a %<chunk_size%> parameter");
23740       else
23741         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23742
23743       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23744         goto resync_fail;
23745     }
23746   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23747     goto resync_fail;
23748
23749   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23750   OMP_CLAUSE_CHAIN (c) = list;
23751   return c;
23752
23753  invalid_kind:
23754   cp_parser_error (parser, "invalid schedule kind");
23755  resync_fail:
23756   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23757                                          /*or_comma=*/false,
23758                                          /*consume_paren=*/true);
23759   return list;
23760 }
23761
23762 /* OpenMP 3.0:
23763    untied */
23764
23765 static tree
23766 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23767                              tree list, location_t location)
23768 {
23769   tree c;
23770
23771   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23772
23773   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23774   OMP_CLAUSE_CHAIN (c) = list;
23775   return c;
23776 }
23777
23778 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23779    is a bitmask in MASK.  Return the list of clauses found; the result
23780    of clause default goes in *pdefault.  */
23781
23782 static tree
23783 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23784                            const char *where, cp_token *pragma_tok)
23785 {
23786   tree clauses = NULL;
23787   bool first = true;
23788   cp_token *token = NULL;
23789
23790   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23791     {
23792       pragma_omp_clause c_kind;
23793       const char *c_name;
23794       tree prev = clauses;
23795
23796       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23797         cp_lexer_consume_token (parser->lexer);
23798
23799       token = cp_lexer_peek_token (parser->lexer);
23800       c_kind = cp_parser_omp_clause_name (parser);
23801       first = false;
23802
23803       switch (c_kind)
23804         {
23805         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23806           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23807                                                    token->location);
23808           c_name = "collapse";
23809           break;
23810         case PRAGMA_OMP_CLAUSE_COPYIN:
23811           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23812           c_name = "copyin";
23813           break;
23814         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23815           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23816                                             clauses);
23817           c_name = "copyprivate";
23818           break;
23819         case PRAGMA_OMP_CLAUSE_DEFAULT:
23820           clauses = cp_parser_omp_clause_default (parser, clauses,
23821                                                   token->location);
23822           c_name = "default";
23823           break;
23824         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23825           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23826                                             clauses);
23827           c_name = "firstprivate";
23828           break;
23829         case PRAGMA_OMP_CLAUSE_IF:
23830           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23831           c_name = "if";
23832           break;
23833         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23834           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23835                                             clauses);
23836           c_name = "lastprivate";
23837           break;
23838         case PRAGMA_OMP_CLAUSE_NOWAIT:
23839           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23840           c_name = "nowait";
23841           break;
23842         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23843           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23844                                                       token->location);
23845           c_name = "num_threads";
23846           break;
23847         case PRAGMA_OMP_CLAUSE_ORDERED:
23848           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23849                                                   token->location);
23850           c_name = "ordered";
23851           break;
23852         case PRAGMA_OMP_CLAUSE_PRIVATE:
23853           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23854                                             clauses);
23855           c_name = "private";
23856           break;
23857         case PRAGMA_OMP_CLAUSE_REDUCTION:
23858           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23859           c_name = "reduction";
23860           break;
23861         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23862           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23863                                                    token->location);
23864           c_name = "schedule";
23865           break;
23866         case PRAGMA_OMP_CLAUSE_SHARED:
23867           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23868                                             clauses);
23869           c_name = "shared";
23870           break;
23871         case PRAGMA_OMP_CLAUSE_UNTIED:
23872           clauses = cp_parser_omp_clause_untied (parser, clauses,
23873                                                  token->location);
23874           c_name = "nowait";
23875           break;
23876         default:
23877           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23878           goto saw_error;
23879         }
23880
23881       if (((mask >> c_kind) & 1) == 0)
23882         {
23883           /* Remove the invalid clause(s) from the list to avoid
23884              confusing the rest of the compiler.  */
23885           clauses = prev;
23886           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23887         }
23888     }
23889  saw_error:
23890   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23891   return finish_omp_clauses (clauses);
23892 }
23893
23894 /* OpenMP 2.5:
23895    structured-block:
23896      statement
23897
23898    In practice, we're also interested in adding the statement to an
23899    outer node.  So it is convenient if we work around the fact that
23900    cp_parser_statement calls add_stmt.  */
23901
23902 static unsigned
23903 cp_parser_begin_omp_structured_block (cp_parser *parser)
23904 {
23905   unsigned save = parser->in_statement;
23906
23907   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23908      This preserves the "not within loop or switch" style error messages
23909      for nonsense cases like
23910         void foo() {
23911         #pragma omp single
23912           break;
23913         }
23914   */
23915   if (parser->in_statement)
23916     parser->in_statement = IN_OMP_BLOCK;
23917
23918   return save;
23919 }
23920
23921 static void
23922 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23923 {
23924   parser->in_statement = save;
23925 }
23926
23927 static tree
23928 cp_parser_omp_structured_block (cp_parser *parser)
23929 {
23930   tree stmt = begin_omp_structured_block ();
23931   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23932
23933   cp_parser_statement (parser, NULL_TREE, false, NULL);
23934
23935   cp_parser_end_omp_structured_block (parser, save);
23936   return finish_omp_structured_block (stmt);
23937 }
23938
23939 /* OpenMP 2.5:
23940    # pragma omp atomic new-line
23941      expression-stmt
23942
23943    expression-stmt:
23944      x binop= expr | x++ | ++x | x-- | --x
23945    binop:
23946      +, *, -, /, &, ^, |, <<, >>
23947
23948   where x is an lvalue expression with scalar type.  */
23949
23950 static void
23951 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23952 {
23953   tree lhs, rhs;
23954   enum tree_code code;
23955
23956   cp_parser_require_pragma_eol (parser, pragma_tok);
23957
23958   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23959                                     /*cast_p=*/false, NULL);
23960   switch (TREE_CODE (lhs))
23961     {
23962     case ERROR_MARK:
23963       goto saw_error;
23964
23965     case PREINCREMENT_EXPR:
23966     case POSTINCREMENT_EXPR:
23967       lhs = TREE_OPERAND (lhs, 0);
23968       code = PLUS_EXPR;
23969       rhs = integer_one_node;
23970       break;
23971
23972     case PREDECREMENT_EXPR:
23973     case POSTDECREMENT_EXPR:
23974       lhs = TREE_OPERAND (lhs, 0);
23975       code = MINUS_EXPR;
23976       rhs = integer_one_node;
23977       break;
23978
23979     case COMPOUND_EXPR:
23980       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23981          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23982          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23983          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23984          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23985                                              (TREE_OPERAND (lhs, 1), 0), 0)))
23986             == BOOLEAN_TYPE)
23987        /* Undo effects of boolean_increment for post {in,de}crement.  */
23988        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23989       /* FALLTHRU */
23990     case MODIFY_EXPR:
23991       if (TREE_CODE (lhs) == MODIFY_EXPR
23992          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23993        {
23994          /* Undo effects of boolean_increment.  */
23995          if (integer_onep (TREE_OPERAND (lhs, 1)))
23996            {
23997              /* This is pre or post increment.  */
23998              rhs = TREE_OPERAND (lhs, 1);
23999              lhs = TREE_OPERAND (lhs, 0);
24000              code = NOP_EXPR;
24001              break;
24002            }
24003        }
24004       /* FALLTHRU */
24005     default:
24006       switch (cp_lexer_peek_token (parser->lexer)->type)
24007         {
24008         case CPP_MULT_EQ:
24009           code = MULT_EXPR;
24010           break;
24011         case CPP_DIV_EQ:
24012           code = TRUNC_DIV_EXPR;
24013           break;
24014         case CPP_PLUS_EQ:
24015           code = PLUS_EXPR;
24016           break;
24017         case CPP_MINUS_EQ:
24018           code = MINUS_EXPR;
24019           break;
24020         case CPP_LSHIFT_EQ:
24021           code = LSHIFT_EXPR;
24022           break;
24023         case CPP_RSHIFT_EQ:
24024           code = RSHIFT_EXPR;
24025           break;
24026         case CPP_AND_EQ:
24027           code = BIT_AND_EXPR;
24028           break;
24029         case CPP_OR_EQ:
24030           code = BIT_IOR_EXPR;
24031           break;
24032         case CPP_XOR_EQ:
24033           code = BIT_XOR_EXPR;
24034           break;
24035         default:
24036           cp_parser_error (parser,
24037                            "invalid operator for %<#pragma omp atomic%>");
24038           goto saw_error;
24039         }
24040       cp_lexer_consume_token (parser->lexer);
24041
24042       rhs = cp_parser_expression (parser, false, NULL);
24043       if (rhs == error_mark_node)
24044         goto saw_error;
24045       break;
24046     }
24047   finish_omp_atomic (code, lhs, rhs);
24048   cp_parser_consume_semicolon_at_end_of_statement (parser);
24049   return;
24050
24051  saw_error:
24052   cp_parser_skip_to_end_of_block_or_statement (parser);
24053 }
24054
24055
24056 /* OpenMP 2.5:
24057    # pragma omp barrier new-line  */
24058
24059 static void
24060 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24061 {
24062   cp_parser_require_pragma_eol (parser, pragma_tok);
24063   finish_omp_barrier ();
24064 }
24065
24066 /* OpenMP 2.5:
24067    # pragma omp critical [(name)] new-line
24068      structured-block  */
24069
24070 static tree
24071 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24072 {
24073   tree stmt, name = NULL;
24074
24075   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24076     {
24077       cp_lexer_consume_token (parser->lexer);
24078
24079       name = cp_parser_identifier (parser);
24080
24081       if (name == error_mark_node
24082           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24083         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24084                                                /*or_comma=*/false,
24085                                                /*consume_paren=*/true);
24086       if (name == error_mark_node)
24087         name = NULL;
24088     }
24089   cp_parser_require_pragma_eol (parser, pragma_tok);
24090
24091   stmt = cp_parser_omp_structured_block (parser);
24092   return c_finish_omp_critical (input_location, stmt, name);
24093 }
24094
24095 /* OpenMP 2.5:
24096    # pragma omp flush flush-vars[opt] new-line
24097
24098    flush-vars:
24099      ( variable-list ) */
24100
24101 static void
24102 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24103 {
24104   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24105     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24106   cp_parser_require_pragma_eol (parser, pragma_tok);
24107
24108   finish_omp_flush ();
24109 }
24110
24111 /* Helper function, to parse omp for increment expression.  */
24112
24113 static tree
24114 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24115 {
24116   tree cond = cp_parser_binary_expression (parser, false, true,
24117                                            PREC_NOT_OPERATOR, NULL);
24118   bool overloaded_p;
24119
24120   if (cond == error_mark_node
24121       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24122     {
24123       cp_parser_skip_to_end_of_statement (parser);
24124       return error_mark_node;
24125     }
24126
24127   switch (TREE_CODE (cond))
24128     {
24129     case GT_EXPR:
24130     case GE_EXPR:
24131     case LT_EXPR:
24132     case LE_EXPR:
24133       break;
24134     default:
24135       return error_mark_node;
24136     }
24137
24138   /* If decl is an iterator, preserve LHS and RHS of the relational
24139      expr until finish_omp_for.  */
24140   if (decl
24141       && (type_dependent_expression_p (decl)
24142           || CLASS_TYPE_P (TREE_TYPE (decl))))
24143     return cond;
24144
24145   return build_x_binary_op (TREE_CODE (cond),
24146                             TREE_OPERAND (cond, 0), ERROR_MARK,
24147                             TREE_OPERAND (cond, 1), ERROR_MARK,
24148                             &overloaded_p, tf_warning_or_error);
24149 }
24150
24151 /* Helper function, to parse omp for increment expression.  */
24152
24153 static tree
24154 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24155 {
24156   cp_token *token = cp_lexer_peek_token (parser->lexer);
24157   enum tree_code op;
24158   tree lhs, rhs;
24159   cp_id_kind idk;
24160   bool decl_first;
24161
24162   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24163     {
24164       op = (token->type == CPP_PLUS_PLUS
24165             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24166       cp_lexer_consume_token (parser->lexer);
24167       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24168       if (lhs != decl)
24169         return error_mark_node;
24170       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24171     }
24172
24173   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24174   if (lhs != decl)
24175     return error_mark_node;
24176
24177   token = cp_lexer_peek_token (parser->lexer);
24178   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24179     {
24180       op = (token->type == CPP_PLUS_PLUS
24181             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24182       cp_lexer_consume_token (parser->lexer);
24183       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24184     }
24185
24186   op = cp_parser_assignment_operator_opt (parser);
24187   if (op == ERROR_MARK)
24188     return error_mark_node;
24189
24190   if (op != NOP_EXPR)
24191     {
24192       rhs = cp_parser_assignment_expression (parser, false, NULL);
24193       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24194       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24195     }
24196
24197   lhs = cp_parser_binary_expression (parser, false, false,
24198                                      PREC_ADDITIVE_EXPRESSION, NULL);
24199   token = cp_lexer_peek_token (parser->lexer);
24200   decl_first = lhs == decl;
24201   if (decl_first)
24202     lhs = NULL_TREE;
24203   if (token->type != CPP_PLUS
24204       && token->type != CPP_MINUS)
24205     return error_mark_node;
24206
24207   do
24208     {
24209       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24210       cp_lexer_consume_token (parser->lexer);
24211       rhs = cp_parser_binary_expression (parser, false, false,
24212                                          PREC_ADDITIVE_EXPRESSION, NULL);
24213       token = cp_lexer_peek_token (parser->lexer);
24214       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24215         {
24216           if (lhs == NULL_TREE)
24217             {
24218               if (op == PLUS_EXPR)
24219                 lhs = rhs;
24220               else
24221                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24222             }
24223           else
24224             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24225                                      NULL, tf_warning_or_error);
24226         }
24227     }
24228   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24229
24230   if (!decl_first)
24231     {
24232       if (rhs != decl || op == MINUS_EXPR)
24233         return error_mark_node;
24234       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24235     }
24236   else
24237     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24238
24239   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24240 }
24241
24242 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24243
24244 static tree
24245 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24246 {
24247   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24248   tree real_decl, initv, condv, incrv, declv;
24249   tree this_pre_body, cl;
24250   location_t loc_first;
24251   bool collapse_err = false;
24252   int i, collapse = 1, nbraces = 0;
24253   VEC(tree,gc) *for_block = make_tree_vector ();
24254
24255   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24256     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24257       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24258
24259   gcc_assert (collapse >= 1);
24260
24261   declv = make_tree_vec (collapse);
24262   initv = make_tree_vec (collapse);
24263   condv = make_tree_vec (collapse);
24264   incrv = make_tree_vec (collapse);
24265
24266   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24267
24268   for (i = 0; i < collapse; i++)
24269     {
24270       int bracecount = 0;
24271       bool add_private_clause = false;
24272       location_t loc;
24273
24274       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24275         {
24276           cp_parser_error (parser, "for statement expected");
24277           return NULL;
24278         }
24279       loc = cp_lexer_consume_token (parser->lexer)->location;
24280
24281       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24282         return NULL;
24283
24284       init = decl = real_decl = NULL;
24285       this_pre_body = push_stmt_list ();
24286       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24287         {
24288           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24289
24290              init-expr:
24291                        var = lb
24292                        integer-type var = lb
24293                        random-access-iterator-type var = lb
24294                        pointer-type var = lb
24295           */
24296           cp_decl_specifier_seq type_specifiers;
24297
24298           /* First, try to parse as an initialized declaration.  See
24299              cp_parser_condition, from whence the bulk of this is copied.  */
24300
24301           cp_parser_parse_tentatively (parser);
24302           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24303                                         /*is_trailing_return=*/false,
24304                                         &type_specifiers);
24305           if (cp_parser_parse_definitely (parser))
24306             {
24307               /* If parsing a type specifier seq succeeded, then this
24308                  MUST be a initialized declaration.  */
24309               tree asm_specification, attributes;
24310               cp_declarator *declarator;
24311
24312               declarator = cp_parser_declarator (parser,
24313                                                  CP_PARSER_DECLARATOR_NAMED,
24314                                                  /*ctor_dtor_or_conv_p=*/NULL,
24315                                                  /*parenthesized_p=*/NULL,
24316                                                  /*member_p=*/false);
24317               attributes = cp_parser_attributes_opt (parser);
24318               asm_specification = cp_parser_asm_specification_opt (parser);
24319
24320               if (declarator == cp_error_declarator) 
24321                 cp_parser_skip_to_end_of_statement (parser);
24322
24323               else 
24324                 {
24325                   tree pushed_scope, auto_node;
24326
24327                   decl = start_decl (declarator, &type_specifiers,
24328                                      SD_INITIALIZED, attributes,
24329                                      /*prefix_attributes=*/NULL_TREE,
24330                                      &pushed_scope);
24331
24332                   auto_node = type_uses_auto (TREE_TYPE (decl));
24333                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24334                     {
24335                       if (cp_lexer_next_token_is (parser->lexer, 
24336                                                   CPP_OPEN_PAREN))
24337                         error ("parenthesized initialization is not allowed in "
24338                                "OpenMP %<for%> loop");
24339                       else
24340                         /* Trigger an error.  */
24341                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24342
24343                       init = error_mark_node;
24344                       cp_parser_skip_to_end_of_statement (parser);
24345                     }
24346                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24347                            || type_dependent_expression_p (decl)
24348                            || auto_node)
24349                     {
24350                       bool is_direct_init, is_non_constant_init;
24351
24352                       init = cp_parser_initializer (parser,
24353                                                     &is_direct_init,
24354                                                     &is_non_constant_init);
24355
24356                       if (auto_node && describable_type (init))
24357                         {
24358                           TREE_TYPE (decl)
24359                             = do_auto_deduction (TREE_TYPE (decl), init,
24360                                                  auto_node);
24361
24362                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24363                               && !type_dependent_expression_p (decl))
24364                             goto non_class;
24365                         }
24366                       
24367                       cp_finish_decl (decl, init, !is_non_constant_init,
24368                                       asm_specification,
24369                                       LOOKUP_ONLYCONVERTING);
24370                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24371                         {
24372                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24373                           init = NULL_TREE;
24374                         }
24375                       else
24376                         init = pop_stmt_list (this_pre_body);
24377                       this_pre_body = NULL_TREE;
24378                     }
24379                   else
24380                     {
24381                       /* Consume '='.  */
24382                       cp_lexer_consume_token (parser->lexer);
24383                       init = cp_parser_assignment_expression (parser, false, NULL);
24384
24385                     non_class:
24386                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24387                         init = error_mark_node;
24388                       else
24389                         cp_finish_decl (decl, NULL_TREE,
24390                                         /*init_const_expr_p=*/false,
24391                                         asm_specification,
24392                                         LOOKUP_ONLYCONVERTING);
24393                     }
24394
24395                   if (pushed_scope)
24396                     pop_scope (pushed_scope);
24397                 }
24398             }
24399           else 
24400             {
24401               cp_id_kind idk;
24402               /* If parsing a type specifier sequence failed, then
24403                  this MUST be a simple expression.  */
24404               cp_parser_parse_tentatively (parser);
24405               decl = cp_parser_primary_expression (parser, false, false,
24406                                                    false, &idk);
24407               if (!cp_parser_error_occurred (parser)
24408                   && decl
24409                   && DECL_P (decl)
24410                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24411                 {
24412                   tree rhs;
24413
24414                   cp_parser_parse_definitely (parser);
24415                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24416                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24417                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24418                                                          rhs,
24419                                                          tf_warning_or_error));
24420                   add_private_clause = true;
24421                 }
24422               else
24423                 {
24424                   decl = NULL;
24425                   cp_parser_abort_tentative_parse (parser);
24426                   init = cp_parser_expression (parser, false, NULL);
24427                   if (init)
24428                     {
24429                       if (TREE_CODE (init) == MODIFY_EXPR
24430                           || TREE_CODE (init) == MODOP_EXPR)
24431                         real_decl = TREE_OPERAND (init, 0);
24432                     }
24433                 }
24434             }
24435         }
24436       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24437       if (this_pre_body)
24438         {
24439           this_pre_body = pop_stmt_list (this_pre_body);
24440           if (pre_body)
24441             {
24442               tree t = pre_body;
24443               pre_body = push_stmt_list ();
24444               add_stmt (t);
24445               add_stmt (this_pre_body);
24446               pre_body = pop_stmt_list (pre_body);
24447             }
24448           else
24449             pre_body = this_pre_body;
24450         }
24451
24452       if (decl)
24453         real_decl = decl;
24454       if (par_clauses != NULL && real_decl != NULL_TREE)
24455         {
24456           tree *c;
24457           for (c = par_clauses; *c ; )
24458             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24459                 && OMP_CLAUSE_DECL (*c) == real_decl)
24460               {
24461                 error_at (loc, "iteration variable %qD"
24462                           " should not be firstprivate", real_decl);
24463                 *c = OMP_CLAUSE_CHAIN (*c);
24464               }
24465             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24466                      && OMP_CLAUSE_DECL (*c) == real_decl)
24467               {
24468                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24469                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24470                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24471                 OMP_CLAUSE_DECL (l) = real_decl;
24472                 OMP_CLAUSE_CHAIN (l) = clauses;
24473                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24474                 clauses = l;
24475                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24476                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24477                 add_private_clause = false;
24478               }
24479             else
24480               {
24481                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24482                     && OMP_CLAUSE_DECL (*c) == real_decl)
24483                   add_private_clause = false;
24484                 c = &OMP_CLAUSE_CHAIN (*c);
24485               }
24486         }
24487
24488       if (add_private_clause)
24489         {
24490           tree c;
24491           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24492             {
24493               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24494                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24495                   && OMP_CLAUSE_DECL (c) == decl)
24496                 break;
24497               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24498                        && OMP_CLAUSE_DECL (c) == decl)
24499                 error_at (loc, "iteration variable %qD "
24500                           "should not be firstprivate",
24501                           decl);
24502               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24503                        && OMP_CLAUSE_DECL (c) == decl)
24504                 error_at (loc, "iteration variable %qD should not be reduction",
24505                           decl);
24506             }
24507           if (c == NULL)
24508             {
24509               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24510               OMP_CLAUSE_DECL (c) = decl;
24511               c = finish_omp_clauses (c);
24512               if (c)
24513                 {
24514                   OMP_CLAUSE_CHAIN (c) = clauses;
24515                   clauses = c;
24516                 }
24517             }
24518         }
24519
24520       cond = NULL;
24521       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24522         cond = cp_parser_omp_for_cond (parser, decl);
24523       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24524
24525       incr = NULL;
24526       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24527         {
24528           /* If decl is an iterator, preserve the operator on decl
24529              until finish_omp_for.  */
24530           if (decl
24531               && (type_dependent_expression_p (decl)
24532                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24533             incr = cp_parser_omp_for_incr (parser, decl);
24534           else
24535             incr = cp_parser_expression (parser, false, NULL);
24536         }
24537
24538       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24539         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24540                                                /*or_comma=*/false,
24541                                                /*consume_paren=*/true);
24542
24543       TREE_VEC_ELT (declv, i) = decl;
24544       TREE_VEC_ELT (initv, i) = init;
24545       TREE_VEC_ELT (condv, i) = cond;
24546       TREE_VEC_ELT (incrv, i) = incr;
24547
24548       if (i == collapse - 1)
24549         break;
24550
24551       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24552          in between the collapsed for loops to be still considered perfectly
24553          nested.  Hopefully the final version clarifies this.
24554          For now handle (multiple) {'s and empty statements.  */
24555       cp_parser_parse_tentatively (parser);
24556       do
24557         {
24558           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24559             break;
24560           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24561             {
24562               cp_lexer_consume_token (parser->lexer);
24563               bracecount++;
24564             }
24565           else if (bracecount
24566                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24567             cp_lexer_consume_token (parser->lexer);
24568           else
24569             {
24570               loc = cp_lexer_peek_token (parser->lexer)->location;
24571               error_at (loc, "not enough collapsed for loops");
24572               collapse_err = true;
24573               cp_parser_abort_tentative_parse (parser);
24574               declv = NULL_TREE;
24575               break;
24576             }
24577         }
24578       while (1);
24579
24580       if (declv)
24581         {
24582           cp_parser_parse_definitely (parser);
24583           nbraces += bracecount;
24584         }
24585     }
24586
24587   /* Note that we saved the original contents of this flag when we entered
24588      the structured block, and so we don't need to re-save it here.  */
24589   parser->in_statement = IN_OMP_FOR;
24590
24591   /* Note that the grammar doesn't call for a structured block here,
24592      though the loop as a whole is a structured block.  */
24593   body = push_stmt_list ();
24594   cp_parser_statement (parser, NULL_TREE, false, NULL);
24595   body = pop_stmt_list (body);
24596
24597   if (declv == NULL_TREE)
24598     ret = NULL_TREE;
24599   else
24600     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24601                           pre_body, clauses);
24602
24603   while (nbraces)
24604     {
24605       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24606         {
24607           cp_lexer_consume_token (parser->lexer);
24608           nbraces--;
24609         }
24610       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24611         cp_lexer_consume_token (parser->lexer);
24612       else
24613         {
24614           if (!collapse_err)
24615             {
24616               error_at (cp_lexer_peek_token (parser->lexer)->location,
24617                         "collapsed loops not perfectly nested");
24618             }
24619           collapse_err = true;
24620           cp_parser_statement_seq_opt (parser, NULL);
24621           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24622             break;
24623         }
24624     }
24625
24626   while (!VEC_empty (tree, for_block))
24627     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24628   release_tree_vector (for_block);
24629
24630   return ret;
24631 }
24632
24633 /* OpenMP 2.5:
24634    #pragma omp for for-clause[optseq] new-line
24635      for-loop  */
24636
24637 #define OMP_FOR_CLAUSE_MASK                             \
24638         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24639         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24640         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24641         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24642         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24643         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24644         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24645         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24646
24647 static tree
24648 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24649 {
24650   tree clauses, sb, ret;
24651   unsigned int save;
24652
24653   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24654                                        "#pragma omp for", pragma_tok);
24655
24656   sb = begin_omp_structured_block ();
24657   save = cp_parser_begin_omp_structured_block (parser);
24658
24659   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24660
24661   cp_parser_end_omp_structured_block (parser, save);
24662   add_stmt (finish_omp_structured_block (sb));
24663
24664   return ret;
24665 }
24666
24667 /* OpenMP 2.5:
24668    # pragma omp master new-line
24669      structured-block  */
24670
24671 static tree
24672 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24673 {
24674   cp_parser_require_pragma_eol (parser, pragma_tok);
24675   return c_finish_omp_master (input_location,
24676                               cp_parser_omp_structured_block (parser));
24677 }
24678
24679 /* OpenMP 2.5:
24680    # pragma omp ordered new-line
24681      structured-block  */
24682
24683 static tree
24684 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24685 {
24686   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24687   cp_parser_require_pragma_eol (parser, pragma_tok);
24688   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24689 }
24690
24691 /* OpenMP 2.5:
24692
24693    section-scope:
24694      { section-sequence }
24695
24696    section-sequence:
24697      section-directive[opt] structured-block
24698      section-sequence section-directive structured-block  */
24699
24700 static tree
24701 cp_parser_omp_sections_scope (cp_parser *parser)
24702 {
24703   tree stmt, substmt;
24704   bool error_suppress = false;
24705   cp_token *tok;
24706
24707   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24708     return NULL_TREE;
24709
24710   stmt = push_stmt_list ();
24711
24712   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24713     {
24714       unsigned save;
24715
24716       substmt = begin_omp_structured_block ();
24717       save = cp_parser_begin_omp_structured_block (parser);
24718
24719       while (1)
24720         {
24721           cp_parser_statement (parser, NULL_TREE, false, NULL);
24722
24723           tok = cp_lexer_peek_token (parser->lexer);
24724           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24725             break;
24726           if (tok->type == CPP_CLOSE_BRACE)
24727             break;
24728           if (tok->type == CPP_EOF)
24729             break;
24730         }
24731
24732       cp_parser_end_omp_structured_block (parser, save);
24733       substmt = finish_omp_structured_block (substmt);
24734       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24735       add_stmt (substmt);
24736     }
24737
24738   while (1)
24739     {
24740       tok = cp_lexer_peek_token (parser->lexer);
24741       if (tok->type == CPP_CLOSE_BRACE)
24742         break;
24743       if (tok->type == CPP_EOF)
24744         break;
24745
24746       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24747         {
24748           cp_lexer_consume_token (parser->lexer);
24749           cp_parser_require_pragma_eol (parser, tok);
24750           error_suppress = false;
24751         }
24752       else if (!error_suppress)
24753         {
24754           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24755           error_suppress = true;
24756         }
24757
24758       substmt = cp_parser_omp_structured_block (parser);
24759       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24760       add_stmt (substmt);
24761     }
24762   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24763
24764   substmt = pop_stmt_list (stmt);
24765
24766   stmt = make_node (OMP_SECTIONS);
24767   TREE_TYPE (stmt) = void_type_node;
24768   OMP_SECTIONS_BODY (stmt) = substmt;
24769
24770   add_stmt (stmt);
24771   return stmt;
24772 }
24773
24774 /* OpenMP 2.5:
24775    # pragma omp sections sections-clause[optseq] newline
24776      sections-scope  */
24777
24778 #define OMP_SECTIONS_CLAUSE_MASK                        \
24779         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24780         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24781         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24782         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24783         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24784
24785 static tree
24786 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24787 {
24788   tree clauses, ret;
24789
24790   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24791                                        "#pragma omp sections", pragma_tok);
24792
24793   ret = cp_parser_omp_sections_scope (parser);
24794   if (ret)
24795     OMP_SECTIONS_CLAUSES (ret) = clauses;
24796
24797   return ret;
24798 }
24799
24800 /* OpenMP 2.5:
24801    # pragma parallel parallel-clause new-line
24802    # pragma parallel for parallel-for-clause new-line
24803    # pragma parallel sections parallel-sections-clause new-line  */
24804
24805 #define OMP_PARALLEL_CLAUSE_MASK                        \
24806         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24807         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24808         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24809         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24810         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24811         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24812         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24813         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24814
24815 static tree
24816 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24817 {
24818   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24819   const char *p_name = "#pragma omp parallel";
24820   tree stmt, clauses, par_clause, ws_clause, block;
24821   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24822   unsigned int save;
24823   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24824
24825   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24826     {
24827       cp_lexer_consume_token (parser->lexer);
24828       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24829       p_name = "#pragma omp parallel for";
24830       mask |= OMP_FOR_CLAUSE_MASK;
24831       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24832     }
24833   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24834     {
24835       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24836       const char *p = IDENTIFIER_POINTER (id);
24837       if (strcmp (p, "sections") == 0)
24838         {
24839           cp_lexer_consume_token (parser->lexer);
24840           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24841           p_name = "#pragma omp parallel sections";
24842           mask |= OMP_SECTIONS_CLAUSE_MASK;
24843           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24844         }
24845     }
24846
24847   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24848   block = begin_omp_parallel ();
24849   save = cp_parser_begin_omp_structured_block (parser);
24850
24851   switch (p_kind)
24852     {
24853     case PRAGMA_OMP_PARALLEL:
24854       cp_parser_statement (parser, NULL_TREE, false, NULL);
24855       par_clause = clauses;
24856       break;
24857
24858     case PRAGMA_OMP_PARALLEL_FOR:
24859       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24860       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24861       break;
24862
24863     case PRAGMA_OMP_PARALLEL_SECTIONS:
24864       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24865       stmt = cp_parser_omp_sections_scope (parser);
24866       if (stmt)
24867         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24868       break;
24869
24870     default:
24871       gcc_unreachable ();
24872     }
24873
24874   cp_parser_end_omp_structured_block (parser, save);
24875   stmt = finish_omp_parallel (par_clause, block);
24876   if (p_kind != PRAGMA_OMP_PARALLEL)
24877     OMP_PARALLEL_COMBINED (stmt) = 1;
24878   return stmt;
24879 }
24880
24881 /* OpenMP 2.5:
24882    # pragma omp single single-clause[optseq] new-line
24883      structured-block  */
24884
24885 #define OMP_SINGLE_CLAUSE_MASK                          \
24886         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24887         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24888         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24889         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24890
24891 static tree
24892 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24893 {
24894   tree stmt = make_node (OMP_SINGLE);
24895   TREE_TYPE (stmt) = void_type_node;
24896
24897   OMP_SINGLE_CLAUSES (stmt)
24898     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24899                                  "#pragma omp single", pragma_tok);
24900   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24901
24902   return add_stmt (stmt);
24903 }
24904
24905 /* OpenMP 3.0:
24906    # pragma omp task task-clause[optseq] new-line
24907      structured-block  */
24908
24909 #define OMP_TASK_CLAUSE_MASK                            \
24910         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24911         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24912         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24913         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24914         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24915         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24916
24917 static tree
24918 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24919 {
24920   tree clauses, block;
24921   unsigned int save;
24922
24923   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24924                                        "#pragma omp task", pragma_tok);
24925   block = begin_omp_task ();
24926   save = cp_parser_begin_omp_structured_block (parser);
24927   cp_parser_statement (parser, NULL_TREE, false, NULL);
24928   cp_parser_end_omp_structured_block (parser, save);
24929   return finish_omp_task (clauses, block);
24930 }
24931
24932 /* OpenMP 3.0:
24933    # pragma omp taskwait new-line  */
24934
24935 static void
24936 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24937 {
24938   cp_parser_require_pragma_eol (parser, pragma_tok);
24939   finish_omp_taskwait ();
24940 }
24941
24942 /* OpenMP 2.5:
24943    # pragma omp threadprivate (variable-list) */
24944
24945 static void
24946 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24947 {
24948   tree vars;
24949
24950   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24951   cp_parser_require_pragma_eol (parser, pragma_tok);
24952
24953   finish_omp_threadprivate (vars);
24954 }
24955
24956 /* Main entry point to OpenMP statement pragmas.  */
24957
24958 static void
24959 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24960 {
24961   tree stmt;
24962
24963   switch (pragma_tok->pragma_kind)
24964     {
24965     case PRAGMA_OMP_ATOMIC:
24966       cp_parser_omp_atomic (parser, pragma_tok);
24967       return;
24968     case PRAGMA_OMP_CRITICAL:
24969       stmt = cp_parser_omp_critical (parser, pragma_tok);
24970       break;
24971     case PRAGMA_OMP_FOR:
24972       stmt = cp_parser_omp_for (parser, pragma_tok);
24973       break;
24974     case PRAGMA_OMP_MASTER:
24975       stmt = cp_parser_omp_master (parser, pragma_tok);
24976       break;
24977     case PRAGMA_OMP_ORDERED:
24978       stmt = cp_parser_omp_ordered (parser, pragma_tok);
24979       break;
24980     case PRAGMA_OMP_PARALLEL:
24981       stmt = cp_parser_omp_parallel (parser, pragma_tok);
24982       break;
24983     case PRAGMA_OMP_SECTIONS:
24984       stmt = cp_parser_omp_sections (parser, pragma_tok);
24985       break;
24986     case PRAGMA_OMP_SINGLE:
24987       stmt = cp_parser_omp_single (parser, pragma_tok);
24988       break;
24989     case PRAGMA_OMP_TASK:
24990       stmt = cp_parser_omp_task (parser, pragma_tok);
24991       break;
24992     default:
24993       gcc_unreachable ();
24994     }
24995
24996   if (stmt)
24997     SET_EXPR_LOCATION (stmt, pragma_tok->location);
24998 }
24999 \f
25000 /* The parser.  */
25001
25002 static GTY (()) cp_parser *the_parser;
25003
25004 \f
25005 /* Special handling for the first token or line in the file.  The first
25006    thing in the file might be #pragma GCC pch_preprocess, which loads a
25007    PCH file, which is a GC collection point.  So we need to handle this
25008    first pragma without benefit of an existing lexer structure.
25009
25010    Always returns one token to the caller in *FIRST_TOKEN.  This is
25011    either the true first token of the file, or the first token after
25012    the initial pragma.  */
25013
25014 static void
25015 cp_parser_initial_pragma (cp_token *first_token)
25016 {
25017   tree name = NULL;
25018
25019   cp_lexer_get_preprocessor_token (NULL, first_token);
25020   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25021     return;
25022
25023   cp_lexer_get_preprocessor_token (NULL, first_token);
25024   if (first_token->type == CPP_STRING)
25025     {
25026       name = first_token->u.value;
25027
25028       cp_lexer_get_preprocessor_token (NULL, first_token);
25029       if (first_token->type != CPP_PRAGMA_EOL)
25030         error_at (first_token->location,
25031                   "junk at end of %<#pragma GCC pch_preprocess%>");
25032     }
25033   else
25034     error_at (first_token->location, "expected string literal");
25035
25036   /* Skip to the end of the pragma.  */
25037   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25038     cp_lexer_get_preprocessor_token (NULL, first_token);
25039
25040   /* Now actually load the PCH file.  */
25041   if (name)
25042     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25043
25044   /* Read one more token to return to our caller.  We have to do this
25045      after reading the PCH file in, since its pointers have to be
25046      live.  */
25047   cp_lexer_get_preprocessor_token (NULL, first_token);
25048 }
25049
25050 /* Normal parsing of a pragma token.  Here we can (and must) use the
25051    regular lexer.  */
25052
25053 static bool
25054 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25055 {
25056   cp_token *pragma_tok;
25057   unsigned int id;
25058
25059   pragma_tok = cp_lexer_consume_token (parser->lexer);
25060   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25061   parser->lexer->in_pragma = true;
25062
25063   id = pragma_tok->pragma_kind;
25064   switch (id)
25065     {
25066     case PRAGMA_GCC_PCH_PREPROCESS:
25067       error_at (pragma_tok->location,
25068                 "%<#pragma GCC pch_preprocess%> must be first");
25069       break;
25070
25071     case PRAGMA_OMP_BARRIER:
25072       switch (context)
25073         {
25074         case pragma_compound:
25075           cp_parser_omp_barrier (parser, pragma_tok);
25076           return false;
25077         case pragma_stmt:
25078           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25079                     "used in compound statements");
25080           break;
25081         default:
25082           goto bad_stmt;
25083         }
25084       break;
25085
25086     case PRAGMA_OMP_FLUSH:
25087       switch (context)
25088         {
25089         case pragma_compound:
25090           cp_parser_omp_flush (parser, pragma_tok);
25091           return false;
25092         case pragma_stmt:
25093           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25094                     "used in compound statements");
25095           break;
25096         default:
25097           goto bad_stmt;
25098         }
25099       break;
25100
25101     case PRAGMA_OMP_TASKWAIT:
25102       switch (context)
25103         {
25104         case pragma_compound:
25105           cp_parser_omp_taskwait (parser, pragma_tok);
25106           return false;
25107         case pragma_stmt:
25108           error_at (pragma_tok->location,
25109                     "%<#pragma omp taskwait%> may only be "
25110                     "used in compound statements");
25111           break;
25112         default:
25113           goto bad_stmt;
25114         }
25115       break;
25116
25117     case PRAGMA_OMP_THREADPRIVATE:
25118       cp_parser_omp_threadprivate (parser, pragma_tok);
25119       return false;
25120
25121     case PRAGMA_OMP_ATOMIC:
25122     case PRAGMA_OMP_CRITICAL:
25123     case PRAGMA_OMP_FOR:
25124     case PRAGMA_OMP_MASTER:
25125     case PRAGMA_OMP_ORDERED:
25126     case PRAGMA_OMP_PARALLEL:
25127     case PRAGMA_OMP_SECTIONS:
25128     case PRAGMA_OMP_SINGLE:
25129     case PRAGMA_OMP_TASK:
25130       if (context == pragma_external)
25131         goto bad_stmt;
25132       cp_parser_omp_construct (parser, pragma_tok);
25133       return true;
25134
25135     case PRAGMA_OMP_SECTION:
25136       error_at (pragma_tok->location, 
25137                 "%<#pragma omp section%> may only be used in "
25138                 "%<#pragma omp sections%> construct");
25139       break;
25140
25141     default:
25142       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25143       c_invoke_pragma_handler (id);
25144       break;
25145
25146     bad_stmt:
25147       cp_parser_error (parser, "expected declaration specifiers");
25148       break;
25149     }
25150
25151   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25152   return false;
25153 }
25154
25155 /* The interface the pragma parsers have to the lexer.  */
25156
25157 enum cpp_ttype
25158 pragma_lex (tree *value)
25159 {
25160   cp_token *tok;
25161   enum cpp_ttype ret;
25162
25163   tok = cp_lexer_peek_token (the_parser->lexer);
25164
25165   ret = tok->type;
25166   *value = tok->u.value;
25167
25168   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25169     ret = CPP_EOF;
25170   else if (ret == CPP_STRING)
25171     *value = cp_parser_string_literal (the_parser, false, false);
25172   else
25173     {
25174       cp_lexer_consume_token (the_parser->lexer);
25175       if (ret == CPP_KEYWORD)
25176         ret = CPP_NAME;
25177     }
25178
25179   return ret;
25180 }
25181
25182 \f
25183 /* External interface.  */
25184
25185 /* Parse one entire translation unit.  */
25186
25187 void
25188 c_parse_file (void)
25189 {
25190   static bool already_called = false;
25191
25192   if (already_called)
25193     {
25194       sorry ("inter-module optimizations not implemented for C++");
25195       return;
25196     }
25197   already_called = true;
25198
25199   the_parser = cp_parser_new ();
25200   push_deferring_access_checks (flag_access_control
25201                                 ? dk_no_deferred : dk_no_check);
25202   cp_parser_translation_unit (the_parser);
25203   the_parser = NULL;
25204 }
25205
25206 #include "gt-cp-parser.h"