OSDN Git Service

3ed0579538a9a2f450410d5e01ee7f23f9747eaa
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "plugin.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct GTY(()) tree_check {
51   /* The value associated with the token.  */
52   tree value;
53   /* The checks that have been associated with value.  */
54   VEC (deferred_access_check, gc)* checks;
55   /* The token's qualifying scope (used when it is a
56      CPP_NESTED_NAME_SPECIFIER).  */
57   tree qualifying_scope;
58 };
59
60 /* A C++ token.  */
61
62 typedef struct GTY (()) cp_token {
63   /* The kind of token.  */
64   ENUM_BITFIELD (cpp_ttype) type : 8;
65   /* If this token is a keyword, this value indicates which keyword.
66      Otherwise, this value is RID_MAX.  */
67   ENUM_BITFIELD (rid) keyword : 8;
68   /* Token flags.  */
69   unsigned char flags;
70   /* Identifier for the pragma.  */
71   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72   /* True if this token is from a context where it is implicitly extern "C" */
73   BOOL_BITFIELD implicit_extern_c : 1;
74   /* True for a CPP_NAME token that is not a keyword (i.e., for which
75      KEYWORD is RID_MAX) iff this name was looked up and found to be
76      ambiguous.  An error has already been reported.  */
77   BOOL_BITFIELD ambiguous_p : 1;
78   /* The location at which this token was found.  */
79   location_t location;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
88
89 /* We use a stack of token pointer for saving token sets.  */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
93
94 static cp_token eof_token =
95 {
96   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
97 };
98
99 /* The cp_lexer structure represents the C++ lexer.  It is responsible
100    for managing the token stream from the preprocessor and supplying
101    it to the parser.  Tokens are never added to the cp_lexer after
102    it is created.  */
103
104 typedef struct GTY (()) cp_lexer {
105   /* The memory allocated for the buffer.  NULL if this lexer does not
106      own the token buffer.  */
107   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108   /* If the lexer owns the buffer, this is the number of tokens in the
109      buffer.  */
110   size_t buffer_length;
111
112   /* A pointer just past the last available token.  The tokens
113      in this lexer are [buffer, last_token).  */
114   cp_token_position GTY ((skip)) last_token;
115
116   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
117      no more available tokens.  */
118   cp_token_position GTY ((skip)) next_token;
119
120   /* A stack indicating positions at which cp_lexer_save_tokens was
121      called.  The top entry is the most recent position at which we
122      began saving tokens.  If the stack is non-empty, we are saving
123      tokens.  */
124   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125
126   /* The next lexer in a linked list of lexers.  */
127   struct cp_lexer *next;
128
129   /* True if we should output debugging information.  */
130   bool debugging_p;
131
132   /* True if we're in the context of parsing a pragma, and should not
133      increment past the end-of-line marker.  */
134   bool in_pragma;
135 } cp_lexer;
136
137 /* cp_token_cache is a range of tokens.  There is no need to represent
138    allocate heap memory for it, since tokens are never removed from the
139    lexer's array.  There is also no need for the GC to walk through
140    a cp_token_cache, since everything in here is referenced through
141    a lexer.  */
142
143 typedef struct GTY(()) cp_token_cache {
144   /* The beginning of the token range.  */
145   cp_token * GTY((skip)) first;
146
147   /* Points immediately after the last token in the range.  */
148   cp_token * GTY ((skip)) last;
149 } cp_token_cache;
150
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153   NIC_NONE,
154   /* floating-point literal */
155   NIC_FLOAT,
156   /* %<this%> */
157   NIC_THIS,
158   /* %<__FUNCTION__%> */
159   NIC_FUNC_NAME,
160   /* %<__PRETTY_FUNCTION__%> */
161   NIC_PRETTY_FUNC,
162   /* %<__func__%> */
163   NIC_C99_FUNC,
164   /* "%<va_arg%> */
165   NIC_VA_ARG,
166   /* a cast */
167   NIC_CAST,
168   /* %<typeid%> operator */
169   NIC_TYPEID,
170   /* non-constant compound literals */
171   NIC_NCC,
172   /* a function call */
173   NIC_FUNC_CALL,
174   /* an increment */
175   NIC_INC,
176   /* an decrement */
177   NIC_DEC,
178   /* an array reference */
179   NIC_ARRAY_REF,
180   /* %<->%> */
181   NIC_ARROW,
182   /* %<.%> */
183   NIC_POINT,
184   /* the address of a label */
185   NIC_ADDR_LABEL,
186   /* %<*%> */
187   NIC_STAR,
188   /* %<&%> */
189   NIC_ADDR,
190   /* %<++%> */
191   NIC_PREINCREMENT,
192   /* %<--%> */
193   NIC_PREDECREMENT,
194   /* %<new%> */
195   NIC_NEW,
196   /* %<delete%> */
197   NIC_DEL,
198   /* calls to overloaded operators */
199   NIC_OVERLOADED,
200   /* an assignment */
201   NIC_ASSIGNMENT,
202   /* a comma operator */
203   NIC_COMMA,
204   /* a call to a constructor */
205   NIC_CONSTRUCTOR
206 } non_integral_constant;
207
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210   /* NULL */
211   NLE_NULL,
212   /* is not a type */
213   NLE_TYPE,
214   /* is not a class or namespace */
215   NLE_CXX98,
216   /* is not a class, namespace, or enumeration */
217   NLE_NOT_CXX98
218 } name_lookup_error;
219
220 /* The various kinds of required token */
221 typedef enum required_token {
222   RT_NONE,
223   RT_SEMICOLON,  /* ';' */
224   RT_OPEN_PAREN, /* '(' */
225   RT_CLOSE_BRACE, /* '}' */
226   RT_OPEN_BRACE,  /* '{' */
227   RT_CLOSE_SQUARE, /* ']' */
228   RT_OPEN_SQUARE,  /* '[' */
229   RT_COMMA, /* ',' */
230   RT_SCOPE, /* '::' */
231   RT_LESS, /* '<' */
232   RT_GREATER, /* '>' */
233   RT_EQ, /* '=' */
234   RT_ELLIPSIS, /* '...' */
235   RT_MULT, /* '*' */
236   RT_COMPL, /* '~' */
237   RT_COLON, /* ':' */
238   RT_COLON_SCOPE, /* ':' or '::' */
239   RT_CLOSE_PAREN, /* ')' */
240   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241   RT_PRAGMA_EOL, /* end of line */
242   RT_NAME, /* identifier */
243
244   /* The type is CPP_KEYWORD */
245   RT_NEW, /* new */
246   RT_DELETE, /* delete */
247   RT_RETURN, /* return */
248   RT_WHILE, /* while */
249   RT_EXTERN, /* extern */
250   RT_STATIC_ASSERT, /* static_assert */
251   RT_DECLTYPE, /* decltype */
252   RT_OPERATOR, /* operator */
253   RT_CLASS, /* class */
254   RT_TEMPLATE, /* template */
255   RT_NAMESPACE, /* namespace */
256   RT_USING, /* using */
257   RT_ASM, /* asm */
258   RT_TRY, /* try */
259   RT_CATCH, /* catch */
260   RT_THROW, /* throw */
261   RT_LABEL, /* __label__ */
262   RT_AT_TRY, /* @try */
263   RT_AT_SYNCHRONIZED, /* @synchronized */
264   RT_AT_THROW, /* @throw */
265
266   RT_SELECT,  /* selection-statement */
267   RT_INTERATION, /* iteration-statement */
268   RT_JUMP, /* jump-statement */
269   RT_CLASS_KEY, /* class-key */
270   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
272
273 /* Prototypes.  */
274
275 static cp_lexer *cp_lexer_new_main
276   (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278   (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280   (cp_lexer *);
281 static int cp_lexer_saving_tokens
282   (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284   (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286   (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288   (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290   (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292   (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294   (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296   (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298   (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300   (cp_lexer *);
301 static void cp_lexer_purge_token
302   (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304   (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306   (cp_lexer *);
307 static void cp_lexer_commit_tokens
308   (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310   (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313   (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315   (cp_lexer *);
316 static void cp_lexer_start_debugging
317   (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319   (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322    about passing NULL to functions that require non-NULL arguments
323    (fputs, fprintf).  It will never be used, so all we need is a value
324    of the right type that's guaranteed not to be NULL.  */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
329
330 static cp_token_cache *cp_token_cache_new
331   (cp_token *, cp_token *);
332
333 static void cp_parser_initial_pragma
334   (cp_token *);
335
336 /* Manifest constants.  */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
339
340 /* A token type for keywords, as opposed to ordinary identifiers.  */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342
343 /* A token type for template-ids.  If a template-id is processed while
344    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345    the value of the CPP_TEMPLATE_ID is whatever was returned by
346    cp_parser_template_id.  */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348
349 /* A token type for nested-name-specifiers.  If a
350    nested-name-specifier is processed while parsing tentatively, it is
351    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353    cp_parser_nested_name_specifier_opt.  */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355
356 /* A token type for tokens that are not tokens at all; these are used
357    to represent slots in the array where there used to be a token
358    that has now been deleted.  */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360
361 /* The number of token types, including C++-specific ones.  */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363
364 /* Variables.  */
365
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written.  */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
370
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372    sizeof, typeof, or alignof.  */
373 int cp_unevaluated_operand;
374
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376    preprocessor.  */
377
378 static cp_lexer *
379 cp_lexer_new_main (void)
380 {
381   cp_token first_token;
382   cp_lexer *lexer;
383   cp_token *pos;
384   size_t alloc;
385   size_t space;
386   cp_token *buffer;
387
388   /* It's possible that parsing the first pragma will load a PCH file,
389      which is a GC collection point.  So we have to do that before
390      allocating any memory.  */
391   cp_parser_initial_pragma (&first_token);
392
393   c_common_no_more_pch ();
394
395   /* Allocate the memory.  */
396   lexer = ggc_alloc_cleared_cp_lexer ();
397
398 #ifdef ENABLE_CHECKING
399   /* Initially we are not debugging.  */
400   lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403                                    CP_SAVED_TOKEN_STACK);
404
405   /* Create the buffer.  */
406   alloc = CP_LEXER_BUFFER_SIZE;
407   buffer = ggc_alloc_vec_cp_token (alloc);
408
409   /* Put the first token in the buffer.  */
410   space = alloc;
411   pos = buffer;
412   *pos = first_token;
413
414   /* Get the remaining tokens from the preprocessor.  */
415   while (pos->type != CPP_EOF)
416     {
417       pos++;
418       if (!--space)
419         {
420           space = alloc;
421           alloc *= 2;
422           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423           pos = buffer + space;
424         }
425       cp_lexer_get_preprocessor_token (lexer, pos);
426     }
427   lexer->buffer = buffer;
428   lexer->buffer_length = alloc - space;
429   lexer->last_token = pos;
430   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431
432   /* Subsequent preprocessor diagnostics should use compiler
433      diagnostic functions to get the compiler source location.  */
434   done_lexing = true;
435
436   gcc_assert (lexer->next_token->type != CPP_PURGED);
437   return lexer;
438 }
439
440 /* Create a new lexer whose token stream is primed with the tokens in
441    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
442
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 {
446   cp_token *first = cache->first;
447   cp_token *last = cache->last;
448   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449
450   /* We do not own the buffer.  */
451   lexer->buffer = NULL;
452   lexer->buffer_length = 0;
453   lexer->next_token = first == last ? &eof_token : first;
454   lexer->last_token = last;
455
456   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457                                    CP_SAVED_TOKEN_STACK);
458
459 #ifdef ENABLE_CHECKING
460   /* Initially we are not debugging.  */
461   lexer->debugging_p = false;
462 #endif
463
464   gcc_assert (lexer->next_token->type != CPP_PURGED);
465   return lexer;
466 }
467
468 /* Frees all resources associated with LEXER.  */
469
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
472 {
473   if (lexer->buffer)
474     ggc_free (lexer->buffer);
475   VEC_free (cp_token_position, heap, lexer->saved_tokens);
476   ggc_free (lexer);
477 }
478
479 /* Returns nonzero if debugging information should be output.  */
480
481 #ifdef ENABLE_CHECKING
482
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
485 {
486   return lexer->debugging_p;
487 }
488
489 #endif /* ENABLE_CHECKING */
490
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 {
494   gcc_assert (!previous_p || lexer->next_token != &eof_token);
495
496   return lexer->next_token - previous_p;
497 }
498
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 {
502   return pos;
503 }
504
505 static inline void
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
507 {
508   lexer->next_token = cp_lexer_token_at (lexer, pos);
509 }
510
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
513 {
514   if (lexer->next_token == &eof_token)
515     return lexer->last_token - 1;
516   else
517     return cp_lexer_token_position (lexer, true);
518 }
519
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
522 {
523   cp_token_position tp = cp_lexer_previous_token_position (lexer);
524
525   return cp_lexer_token_at (lexer, tp);
526 }
527
528 /* nonzero if we are presently saving tokens.  */
529
530 static inline int
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
532 {
533   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
534 }
535
536 /* Store the next token from the preprocessor in *TOKEN.  Return true
537    if we reach EOF.  If LEXER is NULL, assume we are handling an
538    initial #pragma pch_preprocess, and thus want the lexer to return
539    processed strings.  */
540
541 static void
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
543 {
544   static int is_extern_c = 0;
545
546    /* Get a new token from the preprocessor.  */
547   token->type
548     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550   token->keyword = RID_MAX;
551   token->pragma_kind = PRAGMA_NONE;
552
553   /* On some systems, some header files are surrounded by an
554      implicit extern "C" block.  Set a flag in the token if it
555      comes from such a header.  */
556   is_extern_c += pending_lang_change;
557   pending_lang_change = 0;
558   token->implicit_extern_c = is_extern_c > 0;
559
560   /* Check to see if this token is a keyword.  */
561   if (token->type == CPP_NAME)
562     {
563       if (C_IS_RESERVED_WORD (token->u.value))
564         {
565           /* Mark this token as a keyword.  */
566           token->type = CPP_KEYWORD;
567           /* Record which keyword.  */
568           token->keyword = C_RID_CODE (token->u.value);
569         }
570       else
571         {
572           if (warn_cxx0x_compat
573               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
575             {
576               /* Warn about the C++0x keyword (but still treat it as
577                  an identifier).  */
578               warning (OPT_Wc__0x_compat, 
579                        "identifier %qE will become a keyword in C++0x",
580                        token->u.value);
581
582               /* Clear out the C_RID_CODE so we don't warn about this
583                  particular identifier-turned-keyword again.  */
584               C_SET_RID_CODE (token->u.value, RID_MAX);
585             }
586
587           token->ambiguous_p = false;
588           token->keyword = RID_MAX;
589         }
590     }
591   else if (token->type == CPP_AT_NAME)
592     {
593       /* This only happens in Objective-C++; it must be a keyword.  */
594       token->type = CPP_KEYWORD;
595       switch (C_RID_CODE (token->u.value))
596         {
597           /* Replace 'class' with '@class', 'private' with '@private',
598              etc.  This prevents confusion with the C++ keyword
599              'class', and makes the tokens consistent with other
600              Objective-C 'AT' keywords.  For example '@class' is
601              reported as RID_AT_CLASS which is consistent with
602              '@synchronized', which is reported as
603              RID_AT_SYNCHRONIZED.
604           */
605         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
606         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
607         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
609         case RID_THROW:     token->keyword = RID_AT_THROW; break;
610         case RID_TRY:       token->keyword = RID_AT_TRY; break;
611         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
612         default:            token->keyword = C_RID_CODE (token->u.value);
613         }
614     }
615   else if (token->type == CPP_PRAGMA)
616     {
617       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
618       token->pragma_kind = ((enum pragma_kind)
619                             TREE_INT_CST_LOW (token->u.value));
620       token->u.value = NULL_TREE;
621     }
622 }
623
624 /* Update the globals input_location and the input file stack from TOKEN.  */
625 static inline void
626 cp_lexer_set_source_position_from_token (cp_token *token)
627 {
628   if (token->type != CPP_EOF)
629     {
630       input_location = token->location;
631     }
632 }
633
634 /* Return a pointer to the next token in the token stream, but do not
635    consume it.  */
636
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
639 {
640   if (cp_lexer_debugging_p (lexer))
641     {
642       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644       putc ('\n', cp_lexer_debug_stream);
645     }
646   return lexer->next_token;
647 }
648
649 /* Return true if the next token has the indicated TYPE.  */
650
651 static inline bool
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
653 {
654   return cp_lexer_peek_token (lexer)->type == type;
655 }
656
657 /* Return true if the next token does not have the indicated TYPE.  */
658
659 static inline bool
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
661 {
662   return !cp_lexer_next_token_is (lexer, type);
663 }
664
665 /* Return true if the next token is the indicated KEYWORD.  */
666
667 static inline bool
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
669 {
670   return cp_lexer_peek_token (lexer)->keyword == keyword;
671 }
672
673 /* Return true if the next token is not the indicated KEYWORD.  */
674
675 static inline bool
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
677 {
678   return cp_lexer_peek_token (lexer)->keyword != keyword;
679 }
680
681 /* Return true if the next token is a keyword for a decl-specifier.  */
682
683 static bool
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
685 {
686   cp_token *token;
687
688   token = cp_lexer_peek_token (lexer);
689   switch (token->keyword) 
690     {
691       /* auto specifier: storage-class-specifier in C++,
692          simple-type-specifier in C++0x.  */
693     case RID_AUTO:
694       /* Storage classes.  */
695     case RID_REGISTER:
696     case RID_STATIC:
697     case RID_EXTERN:
698     case RID_MUTABLE:
699     case RID_THREAD:
700       /* Elaborated type specifiers.  */
701     case RID_ENUM:
702     case RID_CLASS:
703     case RID_STRUCT:
704     case RID_UNION:
705     case RID_TYPENAME:
706       /* Simple type specifiers.  */
707     case RID_CHAR:
708     case RID_CHAR16:
709     case RID_CHAR32:
710     case RID_WCHAR:
711     case RID_BOOL:
712     case RID_SHORT:
713     case RID_INT:
714     case RID_LONG:
715     case RID_INT128:
716     case RID_SIGNED:
717     case RID_UNSIGNED:
718     case RID_FLOAT:
719     case RID_DOUBLE:
720     case RID_VOID:
721       /* GNU extensions.  */ 
722     case RID_ATTRIBUTE:
723     case RID_TYPEOF:
724       /* C++0x extensions.  */
725     case RID_DECLTYPE:
726       return true;
727
728     default:
729       return false;
730     }
731 }
732
733 /* Return a pointer to the Nth token in the token stream.  If N is 1,
734    then this is precisely equivalent to cp_lexer_peek_token (except
735    that it is not inline).  One would like to disallow that case, but
736    there is one case (cp_parser_nth_token_starts_template_id) where
737    the caller passes a variable for N and it might be 1.  */
738
739 static cp_token *
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
741 {
742   cp_token *token;
743
744   /* N is 1-based, not zero-based.  */
745   gcc_assert (n > 0);
746
747   if (cp_lexer_debugging_p (lexer))
748     fprintf (cp_lexer_debug_stream,
749              "cp_lexer: peeking ahead %ld at token: ", (long)n);
750
751   --n;
752   token = lexer->next_token;
753   gcc_assert (!n || token != &eof_token);
754   while (n != 0)
755     {
756       ++token;
757       if (token == lexer->last_token)
758         {
759           token = &eof_token;
760           break;
761         }
762
763       if (token->type != CPP_PURGED)
764         --n;
765     }
766
767   if (cp_lexer_debugging_p (lexer))
768     {
769       cp_lexer_print_token (cp_lexer_debug_stream, token);
770       putc ('\n', cp_lexer_debug_stream);
771     }
772
773   return token;
774 }
775
776 /* Return the next token, and advance the lexer's next_token pointer
777    to point to the next non-purged token.  */
778
779 static cp_token *
780 cp_lexer_consume_token (cp_lexer* lexer)
781 {
782   cp_token *token = lexer->next_token;
783
784   gcc_assert (token != &eof_token);
785   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
786
787   do
788     {
789       lexer->next_token++;
790       if (lexer->next_token == lexer->last_token)
791         {
792           lexer->next_token = &eof_token;
793           break;
794         }
795
796     }
797   while (lexer->next_token->type == CPP_PURGED);
798
799   cp_lexer_set_source_position_from_token (token);
800
801   /* Provide debugging output.  */
802   if (cp_lexer_debugging_p (lexer))
803     {
804       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805       cp_lexer_print_token (cp_lexer_debug_stream, token);
806       putc ('\n', cp_lexer_debug_stream);
807     }
808
809   return token;
810 }
811
812 /* Permanently remove the next token from the token stream, and
813    advance the next_token pointer to refer to the next non-purged
814    token.  */
815
816 static void
817 cp_lexer_purge_token (cp_lexer *lexer)
818 {
819   cp_token *tok = lexer->next_token;
820
821   gcc_assert (tok != &eof_token);
822   tok->type = CPP_PURGED;
823   tok->location = UNKNOWN_LOCATION;
824   tok->u.value = NULL_TREE;
825   tok->keyword = RID_MAX;
826
827   do
828     {
829       tok++;
830       if (tok == lexer->last_token)
831         {
832           tok = &eof_token;
833           break;
834         }
835     }
836   while (tok->type == CPP_PURGED);
837   lexer->next_token = tok;
838 }
839
840 /* Permanently remove all tokens after TOK, up to, but not
841    including, the token that will be returned next by
842    cp_lexer_peek_token.  */
843
844 static void
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
846 {
847   cp_token *peek = lexer->next_token;
848
849   if (peek == &eof_token)
850     peek = lexer->last_token;
851
852   gcc_assert (tok < peek);
853
854   for ( tok += 1; tok != peek; tok += 1)
855     {
856       tok->type = CPP_PURGED;
857       tok->location = UNKNOWN_LOCATION;
858       tok->u.value = NULL_TREE;
859       tok->keyword = RID_MAX;
860     }
861 }
862
863 /* Begin saving tokens.  All tokens consumed after this point will be
864    preserved.  */
865
866 static void
867 cp_lexer_save_tokens (cp_lexer* lexer)
868 {
869   /* Provide debugging output.  */
870   if (cp_lexer_debugging_p (lexer))
871     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
872
873   VEC_safe_push (cp_token_position, heap,
874                  lexer->saved_tokens, lexer->next_token);
875 }
876
877 /* Commit to the portion of the token stream most recently saved.  */
878
879 static void
880 cp_lexer_commit_tokens (cp_lexer* lexer)
881 {
882   /* Provide debugging output.  */
883   if (cp_lexer_debugging_p (lexer))
884     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
885
886   VEC_pop (cp_token_position, lexer->saved_tokens);
887 }
888
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890    to the token stream.  Stop saving tokens.  */
891
892 static void
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
894 {
895   /* Provide debugging output.  */
896   if (cp_lexer_debugging_p (lexer))
897     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
898
899   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
900 }
901
902 /* Print a representation of the TOKEN on the STREAM.  */
903
904 #ifdef ENABLE_CHECKING
905
906 static void
907 cp_lexer_print_token (FILE * stream, cp_token *token)
908 {
909   /* We don't use cpp_type2name here because the parser defines
910      a few tokens of its own.  */
911   static const char *const token_names[] = {
912     /* cpplib-defined token types */
913 #define OP(e, s) #e,
914 #define TK(e, s) #e,
915     TTYPE_TABLE
916 #undef OP
917 #undef TK
918     /* C++ parser token types - see "Manifest constants", above.  */
919     "KEYWORD",
920     "TEMPLATE_ID",
921     "NESTED_NAME_SPECIFIER",
922     "PURGED"
923   };
924
925   /* If we have a name for the token, print it out.  Otherwise, we
926      simply give the numeric code.  */
927   gcc_assert (token->type < ARRAY_SIZE(token_names));
928   fputs (token_names[token->type], stream);
929
930   /* For some tokens, print the associated data.  */
931   switch (token->type)
932     {
933     case CPP_KEYWORD:
934       /* Some keywords have a value that is not an IDENTIFIER_NODE.
935          For example, `struct' is mapped to an INTEGER_CST.  */
936       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
937         break;
938       /* else fall through */
939     case CPP_NAME:
940       fputs (IDENTIFIER_POINTER (token->u.value), stream);
941       break;
942
943     case CPP_STRING:
944     case CPP_STRING16:
945     case CPP_STRING32:
946     case CPP_WSTRING:
947     case CPP_UTF8STRING:
948       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
949       break;
950
951     default:
952       break;
953     }
954 }
955
956 /* Start emitting debugging information.  */
957
958 static void
959 cp_lexer_start_debugging (cp_lexer* lexer)
960 {
961   lexer->debugging_p = true;
962 }
963
964 /* Stop emitting debugging information.  */
965
966 static void
967 cp_lexer_stop_debugging (cp_lexer* lexer)
968 {
969   lexer->debugging_p = false;
970 }
971
972 #endif /* ENABLE_CHECKING */
973
974 /* Create a new cp_token_cache, representing a range of tokens.  */
975
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
978 {
979   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980   cache->first = first;
981   cache->last = last;
982   return cache;
983 }
984
985 \f
986 /* Decl-specifiers.  */
987
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
989
990 static void
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
992 {
993   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
994 }
995
996 /* Declarators.  */
997
998 /* Nothing other than the parser should be creating declarators;
999    declarators are a semi-syntactic representation of C++ entities.
1000    Other parts of the front end that need to create entities (like
1001    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1002
1003 static cp_declarator *make_call_declarator
1004   (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006   (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008   (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010   (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012   (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014   (cp_cv_quals, tree, cp_declarator *);
1015
1016 /* An erroneous declarator.  */
1017 static cp_declarator *cp_error_declarator;
1018
1019 /* The obstack on which declarators and related data structures are
1020    allocated.  */
1021 static struct obstack declarator_obstack;
1022
1023 /* Alloc BYTES from the declarator memory pool.  */
1024
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1027 {
1028   return obstack_alloc (&declarator_obstack, bytes);
1029 }
1030
1031 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1032    common to all declarators.  */
1033
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1036 {
1037   cp_declarator *declarator;
1038
1039   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040   declarator->kind = kind;
1041   declarator->attributes = NULL_TREE;
1042   declarator->declarator = NULL;
1043   declarator->parameter_pack_p = false;
1044   declarator->id_loc = UNKNOWN_LOCATION;
1045
1046   return declarator;
1047 }
1048
1049 /* Make a declarator for a generalized identifier.  If
1050    QUALIFYING_SCOPE is non-NULL, the identifier is
1051    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1053    is, if any.   */
1054
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057                     special_function_kind sfk)
1058 {
1059   cp_declarator *declarator;
1060
1061   /* It is valid to write:
1062
1063        class C { void f(); };
1064        typedef C D;
1065        void D::f();
1066
1067      The standard is not clear about whether `typedef const C D' is
1068      legal; as of 2002-09-15 the committee is considering that
1069      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1070      well.  */
1071   if (qualifying_scope && TYPE_P (qualifying_scope))
1072     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1073
1074   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1077
1078   declarator = make_declarator (cdk_id);
1079   declarator->u.id.qualifying_scope = qualifying_scope;
1080   declarator->u.id.unqualified_name = unqualified_name;
1081   declarator->u.id.sfk = sfk;
1082   
1083   return declarator;
1084 }
1085
1086 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1087    of modifiers such as const or volatile to apply to the pointer
1088    type, represented as identifiers.  */
1089
1090 cp_declarator *
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1092 {
1093   cp_declarator *declarator;
1094
1095   declarator = make_declarator (cdk_pointer);
1096   declarator->declarator = target;
1097   declarator->u.pointer.qualifiers = cv_qualifiers;
1098   declarator->u.pointer.class_type = NULL_TREE;
1099   if (target)
1100     {
1101       declarator->id_loc = target->id_loc;
1102       declarator->parameter_pack_p = target->parameter_pack_p;
1103       target->parameter_pack_p = false;
1104     }
1105   else
1106     declarator->parameter_pack_p = false;
1107
1108   return declarator;
1109 }
1110
1111 /* Like make_pointer_declarator -- but for references.  */
1112
1113 cp_declarator *
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1115                            bool rvalue_ref)
1116 {
1117   cp_declarator *declarator;
1118
1119   declarator = make_declarator (cdk_reference);
1120   declarator->declarator = target;
1121   declarator->u.reference.qualifiers = cv_qualifiers;
1122   declarator->u.reference.rvalue_ref = rvalue_ref;
1123   if (target)
1124     {
1125       declarator->id_loc = target->id_loc;
1126       declarator->parameter_pack_p = target->parameter_pack_p;
1127       target->parameter_pack_p = false;
1128     }
1129   else
1130     declarator->parameter_pack_p = false;
1131
1132   return declarator;
1133 }
1134
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136    member of CLASS_TYPE.  */
1137
1138 cp_declarator *
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140                         cp_declarator *pointee)
1141 {
1142   cp_declarator *declarator;
1143
1144   declarator = make_declarator (cdk_ptrmem);
1145   declarator->declarator = pointee;
1146   declarator->u.pointer.qualifiers = cv_qualifiers;
1147   declarator->u.pointer.class_type = class_type;
1148
1149   if (pointee)
1150     {
1151       declarator->parameter_pack_p = pointee->parameter_pack_p;
1152       pointee->parameter_pack_p = false;
1153     }
1154   else
1155     declarator->parameter_pack_p = false;
1156
1157   return declarator;
1158 }
1159
1160 /* Make a declarator for the function given by TARGET, with the
1161    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1162    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1163    indicates what exceptions can be thrown.  */
1164
1165 cp_declarator *
1166 make_call_declarator (cp_declarator *target,
1167                       tree parms,
1168                       cp_cv_quals cv_qualifiers,
1169                       tree exception_specification,
1170                       tree late_return_type)
1171 {
1172   cp_declarator *declarator;
1173
1174   declarator = make_declarator (cdk_function);
1175   declarator->declarator = target;
1176   declarator->u.function.parameters = parms;
1177   declarator->u.function.qualifiers = cv_qualifiers;
1178   declarator->u.function.exception_specification = exception_specification;
1179   declarator->u.function.late_return_type = late_return_type;
1180   if (target)
1181     {
1182       declarator->id_loc = target->id_loc;
1183       declarator->parameter_pack_p = target->parameter_pack_p;
1184       target->parameter_pack_p = false;
1185     }
1186   else
1187     declarator->parameter_pack_p = false;
1188
1189   return declarator;
1190 }
1191
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193    defined by ELEMENT.  */
1194
1195 cp_declarator *
1196 make_array_declarator (cp_declarator *element, tree bounds)
1197 {
1198   cp_declarator *declarator;
1199
1200   declarator = make_declarator (cdk_array);
1201   declarator->declarator = element;
1202   declarator->u.array.bounds = bounds;
1203   if (element)
1204     {
1205       declarator->id_loc = element->id_loc;
1206       declarator->parameter_pack_p = element->parameter_pack_p;
1207       element->parameter_pack_p = false;
1208     }
1209   else
1210     declarator->parameter_pack_p = false;
1211
1212   return declarator;
1213 }
1214
1215 /* Determine whether the declarator we've seen so far can be a
1216    parameter pack, when followed by an ellipsis.  */
1217 static bool 
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1219 {
1220   /* Search for a declarator name, or any other declarator that goes
1221      after the point where the ellipsis could appear in a parameter
1222      pack. If we find any of these, then this declarator can not be
1223      made into a parameter pack.  */
1224   bool found = false;
1225   while (declarator && !found)
1226     {
1227       switch ((int)declarator->kind)
1228         {
1229         case cdk_id:
1230         case cdk_array:
1231           found = true;
1232           break;
1233
1234         case cdk_error:
1235           return true;
1236
1237         default:
1238           declarator = declarator->declarator;
1239           break;
1240         }
1241     }
1242
1243   return !found;
1244 }
1245
1246 cp_parameter_declarator *no_parameters;
1247
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249    DECLARATOR and DEFAULT_ARGUMENT.  */
1250
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253                            cp_declarator *declarator,
1254                            tree default_argument)
1255 {
1256   cp_parameter_declarator *parameter;
1257
1258   parameter = ((cp_parameter_declarator *)
1259                alloc_declarator (sizeof (cp_parameter_declarator)));
1260   parameter->next = NULL;
1261   if (decl_specifiers)
1262     parameter->decl_specifiers = *decl_specifiers;
1263   else
1264     clear_decl_specs (&parameter->decl_specifiers);
1265   parameter->declarator = declarator;
1266   parameter->default_argument = default_argument;
1267   parameter->ellipsis_p = false;
1268
1269   return parameter;
1270 }
1271
1272 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1273
1274 static bool
1275 function_declarator_p (const cp_declarator *declarator)
1276 {
1277   while (declarator)
1278     {
1279       if (declarator->kind == cdk_function
1280           && declarator->declarator->kind == cdk_id)
1281         return true;
1282       if (declarator->kind == cdk_id
1283           || declarator->kind == cdk_error)
1284         return false;
1285       declarator = declarator->declarator;
1286     }
1287   return false;
1288 }
1289  
1290 /* The parser.  */
1291
1292 /* Overview
1293    --------
1294
1295    A cp_parser parses the token stream as specified by the C++
1296    grammar.  Its job is purely parsing, not semantic analysis.  For
1297    example, the parser breaks the token stream into declarators,
1298    expressions, statements, and other similar syntactic constructs.
1299    It does not check that the types of the expressions on either side
1300    of an assignment-statement are compatible, or that a function is
1301    not declared with a parameter of type `void'.
1302
1303    The parser invokes routines elsewhere in the compiler to perform
1304    semantic analysis and to build up the abstract syntax tree for the
1305    code processed.
1306
1307    The parser (and the template instantiation code, which is, in a
1308    way, a close relative of parsing) are the only parts of the
1309    compiler that should be calling push_scope and pop_scope, or
1310    related functions.  The parser (and template instantiation code)
1311    keeps track of what scope is presently active; everything else
1312    should simply honor that.  (The code that generates static
1313    initializers may also need to set the scope, in order to check
1314    access control correctly when emitting the initializers.)
1315
1316    Methodology
1317    -----------
1318
1319    The parser is of the standard recursive-descent variety.  Upcoming
1320    tokens in the token stream are examined in order to determine which
1321    production to use when parsing a non-terminal.  Some C++ constructs
1322    require arbitrary look ahead to disambiguate.  For example, it is
1323    impossible, in the general case, to tell whether a statement is an
1324    expression or declaration without scanning the entire statement.
1325    Therefore, the parser is capable of "parsing tentatively."  When the
1326    parser is not sure what construct comes next, it enters this mode.
1327    Then, while we attempt to parse the construct, the parser queues up
1328    error messages, rather than issuing them immediately, and saves the
1329    tokens it consumes.  If the construct is parsed successfully, the
1330    parser "commits", i.e., it issues any queued error messages and
1331    the tokens that were being preserved are permanently discarded.
1332    If, however, the construct is not parsed successfully, the parser
1333    rolls back its state completely so that it can resume parsing using
1334    a different alternative.
1335
1336    Future Improvements
1337    -------------------
1338
1339    The performance of the parser could probably be improved substantially.
1340    We could often eliminate the need to parse tentatively by looking ahead
1341    a little bit.  In some places, this approach might not entirely eliminate
1342    the need to parse tentatively, but it might still speed up the average
1343    case.  */
1344
1345 /* Flags that are passed to some parsing functions.  These values can
1346    be bitwise-ored together.  */
1347
1348 enum
1349 {
1350   /* No flags.  */
1351   CP_PARSER_FLAGS_NONE = 0x0,
1352   /* The construct is optional.  If it is not present, then no error
1353      should be issued.  */
1354   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355   /* When parsing a type-specifier, treat user-defined type-names
1356      as non-type identifiers.  */
1357   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358   /* When parsing a type-specifier, do not try to parse a class-specifier
1359      or enum-specifier.  */
1360   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361   /* When parsing a decl-specifier-seq, only allow type-specifier or
1362      constexpr.  */
1363   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1364 };
1365
1366 /* This type is used for parameters and variables which hold
1367    combinations of the above flags.  */
1368 typedef int cp_parser_flags;
1369
1370 /* The different kinds of declarators we want to parse.  */
1371
1372 typedef enum cp_parser_declarator_kind
1373 {
1374   /* We want an abstract declarator.  */
1375   CP_PARSER_DECLARATOR_ABSTRACT,
1376   /* We want a named declarator.  */
1377   CP_PARSER_DECLARATOR_NAMED,
1378   /* We don't mind, but the name must be an unqualified-id.  */
1379   CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1381
1382 /* The precedence values used to parse binary expressions.  The minimum value
1383    of PREC must be 1, because zero is reserved to quickly discriminate
1384    binary operators from other tokens.  */
1385
1386 enum cp_parser_prec
1387 {
1388   PREC_NOT_OPERATOR,
1389   PREC_LOGICAL_OR_EXPRESSION,
1390   PREC_LOGICAL_AND_EXPRESSION,
1391   PREC_INCLUSIVE_OR_EXPRESSION,
1392   PREC_EXCLUSIVE_OR_EXPRESSION,
1393   PREC_AND_EXPRESSION,
1394   PREC_EQUALITY_EXPRESSION,
1395   PREC_RELATIONAL_EXPRESSION,
1396   PREC_SHIFT_EXPRESSION,
1397   PREC_ADDITIVE_EXPRESSION,
1398   PREC_MULTIPLICATIVE_EXPRESSION,
1399   PREC_PM_EXPRESSION,
1400   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1401 };
1402
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404    precedence value.  */
1405
1406 typedef struct cp_parser_binary_operations_map_node
1407 {
1408   /* The token type.  */
1409   enum cpp_ttype token_type;
1410   /* The corresponding tree code.  */
1411   enum tree_code tree_type;
1412   /* The precedence of this operator.  */
1413   enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1415
1416 /* The status of a tentative parse.  */
1417
1418 typedef enum cp_parser_status_kind
1419 {
1420   /* No errors have occurred.  */
1421   CP_PARSER_STATUS_KIND_NO_ERROR,
1422   /* An error has occurred.  */
1423   CP_PARSER_STATUS_KIND_ERROR,
1424   /* We are committed to this tentative parse, whether or not an error
1425      has occurred.  */
1426   CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1428
1429 typedef struct cp_parser_expression_stack_entry
1430 {
1431   /* Left hand side of the binary operation we are currently
1432      parsing.  */
1433   tree lhs;
1434   /* Original tree code for left hand side, if it was a binary
1435      expression itself (used for -Wparentheses).  */
1436   enum tree_code lhs_type;
1437   /* Tree code for the binary operation we are parsing.  */
1438   enum tree_code tree_type;
1439   /* Precedence of the binary operation we are parsing.  */
1440   enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1442
1443 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1444    entries because precedence levels on the stack are monotonically
1445    increasing.  */
1446 typedef struct cp_parser_expression_stack_entry
1447   cp_parser_expression_stack[NUM_PREC_VALUES];
1448
1449 /* Context that is saved and restored when parsing tentatively.  */
1450 typedef struct GTY (()) cp_parser_context {
1451   /* If this is a tentative parsing context, the status of the
1452      tentative parse.  */
1453   enum cp_parser_status_kind status;
1454   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1455      that are looked up in this context must be looked up both in the
1456      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457      the context of the containing expression.  */
1458   tree object_type;
1459
1460   /* The next parsing context in the stack.  */
1461   struct cp_parser_context *next;
1462 } cp_parser_context;
1463
1464 /* Prototypes.  */
1465
1466 /* Constructors and destructors.  */
1467
1468 static cp_parser_context *cp_parser_context_new
1469   (cp_parser_context *);
1470
1471 /* Class variables.  */
1472
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1474
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476    Transformed into an associative array (binops_by_token) by
1477    cp_parser_new.  */
1478
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1482
1483   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1486
1487   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1489
1490   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1492
1493   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1497
1498   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1500
1501   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1502
1503   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1504
1505   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1506
1507   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1508
1509   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1510 };
1511
1512 /* The same as binops, but initialized by cp_parser_new so that
1513    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1514    for speed.  */
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1516
1517 /* Constructors and destructors.  */
1518
1519 /* Construct a new context.  The context below this one on the stack
1520    is given by NEXT.  */
1521
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1524 {
1525   cp_parser_context *context;
1526
1527   /* Allocate the storage.  */
1528   if (cp_parser_context_free_list != NULL)
1529     {
1530       /* Pull the first entry from the free list.  */
1531       context = cp_parser_context_free_list;
1532       cp_parser_context_free_list = context->next;
1533       memset (context, 0, sizeof (*context));
1534     }
1535   else
1536     context = ggc_alloc_cleared_cp_parser_context ();
1537
1538   /* No errors have occurred yet in this context.  */
1539   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540   /* If this is not the bottommost context, copy information that we
1541      need from the previous context.  */
1542   if (next)
1543     {
1544       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545          expression, then we are parsing one in this context, too.  */
1546       context->object_type = next->object_type;
1547       /* Thread the stack.  */
1548       context->next = next;
1549     }
1550
1551   return context;
1552 }
1553
1554 /* An entry in a queue of function arguments that require post-processing.  */
1555
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557   /* The current_class_type when we parsed this arg.  */
1558   tree class_type;
1559
1560   /* The function decl itself.  */
1561   tree decl;
1562 } cp_default_arg_entry;
1563
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1566
1567 /* An entry in a stack for member functions of local classes.  */
1568
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570   /* Functions with default arguments that require post-processing.
1571      Functions appear in this list in declaration order.  */
1572   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1573
1574   /* Functions with defintions that require post-processing.  Functions
1575      appear in this list in declaration order.  */
1576   VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1578
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1581
1582 /* The cp_parser structure represents the C++ parser.  */
1583
1584 typedef struct GTY(()) cp_parser {
1585   /* The lexer from which we are obtaining tokens.  */
1586   cp_lexer *lexer;
1587
1588   /* The scope in which names should be looked up.  If NULL_TREE, then
1589      we look up names in the scope that is currently open in the
1590      source program.  If non-NULL, this is either a TYPE or
1591      NAMESPACE_DECL for the scope in which we should look.  It can
1592      also be ERROR_MARK, when we've parsed a bogus scope.
1593
1594      This value is not cleared automatically after a name is looked
1595      up, so we must be careful to clear it before starting a new look
1596      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1597      will look up `Z' in the scope of `X', rather than the current
1598      scope.)  Unfortunately, it is difficult to tell when name lookup
1599      is complete, because we sometimes peek at a token, look it up,
1600      and then decide not to consume it.   */
1601   tree scope;
1602
1603   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604      last lookup took place.  OBJECT_SCOPE is used if an expression
1605      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606      respectively.  QUALIFYING_SCOPE is used for an expression of the
1607      form "X::Y"; it refers to X.  */
1608   tree object_scope;
1609   tree qualifying_scope;
1610
1611   /* A stack of parsing contexts.  All but the bottom entry on the
1612      stack will be tentative contexts.
1613
1614      We parse tentatively in order to determine which construct is in
1615      use in some situations.  For example, in order to determine
1616      whether a statement is an expression-statement or a
1617      declaration-statement we parse it tentatively as a
1618      declaration-statement.  If that fails, we then reparse the same
1619      token stream as an expression-statement.  */
1620   cp_parser_context *context;
1621
1622   /* True if we are parsing GNU C++.  If this flag is not set, then
1623      GNU extensions are not recognized.  */
1624   bool allow_gnu_extensions_p;
1625
1626   /* TRUE if the `>' token should be interpreted as the greater-than
1627      operator.  FALSE if it is the end of a template-id or
1628      template-parameter-list. In C++0x mode, this flag also applies to
1629      `>>' tokens, which are viewed as two consecutive `>' tokens when
1630      this flag is FALSE.  */
1631   bool greater_than_is_operator_p;
1632
1633   /* TRUE if default arguments are allowed within a parameter list
1634      that starts at this point. FALSE if only a gnu extension makes
1635      them permissible.  */
1636   bool default_arg_ok_p;
1637
1638   /* TRUE if we are parsing an integral constant-expression.  See
1639      [expr.const] for a precise definition.  */
1640   bool integral_constant_expression_p;
1641
1642   /* TRUE if we are parsing an integral constant-expression -- but a
1643      non-constant expression should be permitted as well.  This flag
1644      is used when parsing an array bound so that GNU variable-length
1645      arrays are tolerated.  */
1646   bool allow_non_integral_constant_expression_p;
1647
1648   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649      been seen that makes the expression non-constant.  */
1650   bool non_integral_constant_expression_p;
1651
1652   /* TRUE if local variable names and `this' are forbidden in the
1653      current context.  */
1654   bool local_variables_forbidden_p;
1655
1656   /* TRUE if the declaration we are parsing is part of a
1657      linkage-specification of the form `extern string-literal
1658      declaration'.  */
1659   bool in_unbraced_linkage_specification_p;
1660
1661   /* TRUE if we are presently parsing a declarator, after the
1662      direct-declarator.  */
1663   bool in_declarator_p;
1664
1665   /* TRUE if we are presently parsing a template-argument-list.  */
1666   bool in_template_argument_list_p;
1667
1668   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669      to IN_OMP_BLOCK if parsing OpenMP structured block and
1670      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1671      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672      iteration-statement, OpenMP block or loop within that switch.  */
1673 #define IN_SWITCH_STMT          1
1674 #define IN_ITERATION_STMT       2
1675 #define IN_OMP_BLOCK            4
1676 #define IN_OMP_FOR              8
1677 #define IN_IF_STMT             16
1678   unsigned char in_statement;
1679
1680   /* TRUE if we are presently parsing the body of a switch statement.
1681      Note that this doesn't quite overlap with in_statement above.
1682      The difference relates to giving the right sets of error messages:
1683      "case not in switch" vs "break statement used with OpenMP...".  */
1684   bool in_switch_statement_p;
1685
1686   /* TRUE if we are parsing a type-id in an expression context.  In
1687      such a situation, both "type (expr)" and "type (type)" are valid
1688      alternatives.  */
1689   bool in_type_id_in_expr_p;
1690
1691   /* TRUE if we are currently in a header file where declarations are
1692      implicitly extern "C".  */
1693   bool implicit_extern_c;
1694
1695   /* TRUE if strings in expressions should be translated to the execution
1696      character set.  */
1697   bool translate_strings_p;
1698
1699   /* TRUE if we are presently parsing the body of a function, but not
1700      a local class.  */
1701   bool in_function_body;
1702
1703   /* If non-NULL, then we are parsing a construct where new type
1704      definitions are not permitted.  The string stored here will be
1705      issued as an error message if a type is defined.  */
1706   const char *type_definition_forbidden_message;
1707
1708   /* A stack used for member functions of local classes.  The lists
1709      contained in an individual entry can only be processed once the
1710      outermost class being defined is complete.  */
1711   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1712
1713   /* The number of classes whose definitions are currently in
1714      progress.  */
1715   unsigned num_classes_being_defined;
1716
1717   /* The number of template parameter lists that apply directly to the
1718      current declaration.  */
1719   unsigned num_template_parameter_lists;
1720 } cp_parser;
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728
1729 static void
1730 push_unparsed_function_queues (cp_parser *parser)
1731 {
1732   VEC_safe_push (cp_unparsed_functions_entry, gc,
1733                  parser->unparsed_queues, NULL);
1734   unparsed_funs_with_default_args = NULL;
1735   unparsed_funs_with_definitions = make_tree_vector ();
1736 }
1737
1738 static void
1739 pop_unparsed_function_queues (cp_parser *parser)
1740 {
1741   release_tree_vector (unparsed_funs_with_definitions);
1742   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1743 }
1744
1745 /* Prototypes.  */
1746
1747 /* Constructors and destructors.  */
1748
1749 static cp_parser *cp_parser_new
1750   (void);
1751
1752 /* Routines to parse various constructs.
1753
1754    Those that return `tree' will return the error_mark_node (rather
1755    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756    Sometimes, they will return an ordinary node if error-recovery was
1757    attempted, even though a parse error occurred.  So, to check
1758    whether or not a parse error occurred, you should always use
1759    cp_parser_error_occurred.  If the construct is optional (indicated
1760    either by an `_opt' in the name of the function that does the
1761    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762    the construct is not present.  */
1763
1764 /* Lexical conventions [gram.lex]  */
1765
1766 static tree cp_parser_identifier
1767   (cp_parser *);
1768 static tree cp_parser_string_literal
1769   (cp_parser *, bool, bool);
1770
1771 /* Basic concepts [gram.basic]  */
1772
1773 static bool cp_parser_translation_unit
1774   (cp_parser *);
1775
1776 /* Expressions [gram.expr]  */
1777
1778 static tree cp_parser_primary_expression
1779   (cp_parser *, bool, bool, bool, cp_id_kind *);
1780 static tree cp_parser_id_expression
1781   (cp_parser *, bool, bool, bool *, bool, bool);
1782 static tree cp_parser_unqualified_id
1783   (cp_parser *, bool, bool, bool, bool);
1784 static tree cp_parser_nested_name_specifier_opt
1785   (cp_parser *, bool, bool, bool, bool);
1786 static tree cp_parser_nested_name_specifier
1787   (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_qualifying_entity
1789   (cp_parser *, bool, bool, bool, bool, bool);
1790 static tree cp_parser_postfix_expression
1791   (cp_parser *, bool, bool, bool, cp_id_kind *);
1792 static tree cp_parser_postfix_open_square_expression
1793   (cp_parser *, tree, bool);
1794 static tree cp_parser_postfix_dot_deref_expression
1795   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1796 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1797   (cp_parser *, int, bool, bool, bool *);
1798 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1799 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1800 static void cp_parser_pseudo_destructor_name
1801   (cp_parser *, tree *, tree *);
1802 static tree cp_parser_unary_expression
1803   (cp_parser *, bool, bool, cp_id_kind *);
1804 static enum tree_code cp_parser_unary_operator
1805   (cp_token *);
1806 static tree cp_parser_new_expression
1807   (cp_parser *);
1808 static VEC(tree,gc) *cp_parser_new_placement
1809   (cp_parser *);
1810 static tree cp_parser_new_type_id
1811   (cp_parser *, tree *);
1812 static cp_declarator *cp_parser_new_declarator_opt
1813   (cp_parser *);
1814 static cp_declarator *cp_parser_direct_new_declarator
1815   (cp_parser *);
1816 static VEC(tree,gc) *cp_parser_new_initializer
1817   (cp_parser *);
1818 static tree cp_parser_delete_expression
1819   (cp_parser *);
1820 static tree cp_parser_cast_expression
1821   (cp_parser *, bool, bool, cp_id_kind *);
1822 static tree cp_parser_binary_expression
1823   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1824 static tree cp_parser_question_colon_clause
1825   (cp_parser *, tree);
1826 static tree cp_parser_assignment_expression
1827   (cp_parser *, bool, cp_id_kind *);
1828 static enum tree_code cp_parser_assignment_operator_opt
1829   (cp_parser *);
1830 static tree cp_parser_expression
1831   (cp_parser *, bool, cp_id_kind *);
1832 static tree cp_parser_constant_expression
1833   (cp_parser *, bool, bool *);
1834 static tree cp_parser_builtin_offsetof
1835   (cp_parser *);
1836 static tree cp_parser_lambda_expression
1837   (cp_parser *);
1838 static void cp_parser_lambda_introducer
1839   (cp_parser *, tree);
1840 static void cp_parser_lambda_declarator_opt
1841   (cp_parser *, tree);
1842 static void cp_parser_lambda_body
1843   (cp_parser *, tree);
1844
1845 /* Statements [gram.stmt.stmt]  */
1846
1847 static void cp_parser_statement
1848   (cp_parser *, tree, bool, bool *);
1849 static void cp_parser_label_for_labeled_statement
1850   (cp_parser *);
1851 static tree cp_parser_expression_statement
1852   (cp_parser *, tree);
1853 static tree cp_parser_compound_statement
1854   (cp_parser *, tree, bool);
1855 static void cp_parser_statement_seq_opt
1856   (cp_parser *, tree);
1857 static tree cp_parser_selection_statement
1858   (cp_parser *, bool *);
1859 static tree cp_parser_condition
1860   (cp_parser *);
1861 static tree cp_parser_iteration_statement
1862   (cp_parser *);
1863 static void cp_parser_for_init_statement
1864   (cp_parser *);
1865 static tree  cp_parser_c_for
1866   (cp_parser *);
1867 static tree  cp_parser_range_for
1868   (cp_parser *);
1869 static tree cp_parser_jump_statement
1870   (cp_parser *);
1871 static void cp_parser_declaration_statement
1872   (cp_parser *);
1873
1874 static tree cp_parser_implicitly_scoped_statement
1875   (cp_parser *, bool *);
1876 static void cp_parser_already_scoped_statement
1877   (cp_parser *);
1878
1879 /* Declarations [gram.dcl.dcl] */
1880
1881 static void cp_parser_declaration_seq_opt
1882   (cp_parser *);
1883 static void cp_parser_declaration
1884   (cp_parser *);
1885 static void cp_parser_block_declaration
1886   (cp_parser *, bool);
1887 static void cp_parser_simple_declaration
1888   (cp_parser *, bool);
1889 static void cp_parser_decl_specifier_seq
1890   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1891 static tree cp_parser_storage_class_specifier_opt
1892   (cp_parser *);
1893 static tree cp_parser_function_specifier_opt
1894   (cp_parser *, cp_decl_specifier_seq *);
1895 static tree cp_parser_type_specifier
1896   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1897    int *, bool *);
1898 static tree cp_parser_simple_type_specifier
1899   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1900 static tree cp_parser_type_name
1901   (cp_parser *);
1902 static tree cp_parser_nonclass_name 
1903   (cp_parser* parser);
1904 static tree cp_parser_elaborated_type_specifier
1905   (cp_parser *, bool, bool);
1906 static tree cp_parser_enum_specifier
1907   (cp_parser *);
1908 static void cp_parser_enumerator_list
1909   (cp_parser *, tree);
1910 static void cp_parser_enumerator_definition
1911   (cp_parser *, tree);
1912 static tree cp_parser_namespace_name
1913   (cp_parser *);
1914 static void cp_parser_namespace_definition
1915   (cp_parser *);
1916 static void cp_parser_namespace_body
1917   (cp_parser *);
1918 static tree cp_parser_qualified_namespace_specifier
1919   (cp_parser *);
1920 static void cp_parser_namespace_alias_definition
1921   (cp_parser *);
1922 static bool cp_parser_using_declaration
1923   (cp_parser *, bool);
1924 static void cp_parser_using_directive
1925   (cp_parser *);
1926 static void cp_parser_asm_definition
1927   (cp_parser *);
1928 static void cp_parser_linkage_specification
1929   (cp_parser *);
1930 static void cp_parser_static_assert
1931   (cp_parser *, bool);
1932 static tree cp_parser_decltype
1933   (cp_parser *);
1934
1935 /* Declarators [gram.dcl.decl] */
1936
1937 static tree cp_parser_init_declarator
1938   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1939 static cp_declarator *cp_parser_declarator
1940   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1941 static cp_declarator *cp_parser_direct_declarator
1942   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1943 static enum tree_code cp_parser_ptr_operator
1944   (cp_parser *, tree *, cp_cv_quals *);
1945 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1946   (cp_parser *);
1947 static tree cp_parser_late_return_type_opt
1948   (cp_parser *);
1949 static tree cp_parser_declarator_id
1950   (cp_parser *, bool);
1951 static tree cp_parser_type_id
1952   (cp_parser *);
1953 static tree cp_parser_template_type_arg
1954   (cp_parser *);
1955 static tree cp_parser_trailing_type_id (cp_parser *);
1956 static tree cp_parser_type_id_1
1957   (cp_parser *, bool, bool);
1958 static void cp_parser_type_specifier_seq
1959   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1960 static tree cp_parser_parameter_declaration_clause
1961   (cp_parser *);
1962 static tree cp_parser_parameter_declaration_list
1963   (cp_parser *, bool *);
1964 static cp_parameter_declarator *cp_parser_parameter_declaration
1965   (cp_parser *, bool, bool *);
1966 static tree cp_parser_default_argument 
1967   (cp_parser *, bool);
1968 static void cp_parser_function_body
1969   (cp_parser *);
1970 static tree cp_parser_initializer
1971   (cp_parser *, bool *, bool *);
1972 static tree cp_parser_initializer_clause
1973   (cp_parser *, bool *);
1974 static tree cp_parser_braced_list
1975   (cp_parser*, bool*);
1976 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1977   (cp_parser *, bool *);
1978
1979 static bool cp_parser_ctor_initializer_opt_and_function_body
1980   (cp_parser *);
1981
1982 /* Classes [gram.class] */
1983
1984 static tree cp_parser_class_name
1985   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1986 static tree cp_parser_class_specifier
1987   (cp_parser *);
1988 static tree cp_parser_class_head
1989   (cp_parser *, bool *, tree *, tree *);
1990 static enum tag_types cp_parser_class_key
1991   (cp_parser *);
1992 static void cp_parser_member_specification_opt
1993   (cp_parser *);
1994 static void cp_parser_member_declaration
1995   (cp_parser *);
1996 static tree cp_parser_pure_specifier
1997   (cp_parser *);
1998 static tree cp_parser_constant_initializer
1999   (cp_parser *);
2000
2001 /* Derived classes [gram.class.derived] */
2002
2003 static tree cp_parser_base_clause
2004   (cp_parser *);
2005 static tree cp_parser_base_specifier
2006   (cp_parser *);
2007
2008 /* Special member functions [gram.special] */
2009
2010 static tree cp_parser_conversion_function_id
2011   (cp_parser *);
2012 static tree cp_parser_conversion_type_id
2013   (cp_parser *);
2014 static cp_declarator *cp_parser_conversion_declarator_opt
2015   (cp_parser *);
2016 static bool cp_parser_ctor_initializer_opt
2017   (cp_parser *);
2018 static void cp_parser_mem_initializer_list
2019   (cp_parser *);
2020 static tree cp_parser_mem_initializer
2021   (cp_parser *);
2022 static tree cp_parser_mem_initializer_id
2023   (cp_parser *);
2024
2025 /* Overloading [gram.over] */
2026
2027 static tree cp_parser_operator_function_id
2028   (cp_parser *);
2029 static tree cp_parser_operator
2030   (cp_parser *);
2031
2032 /* Templates [gram.temp] */
2033
2034 static void cp_parser_template_declaration
2035   (cp_parser *, bool);
2036 static tree cp_parser_template_parameter_list
2037   (cp_parser *);
2038 static tree cp_parser_template_parameter
2039   (cp_parser *, bool *, bool *);
2040 static tree cp_parser_type_parameter
2041   (cp_parser *, bool *);
2042 static tree cp_parser_template_id
2043   (cp_parser *, bool, bool, bool);
2044 static tree cp_parser_template_name
2045   (cp_parser *, bool, bool, bool, bool *);
2046 static tree cp_parser_template_argument_list
2047   (cp_parser *);
2048 static tree cp_parser_template_argument
2049   (cp_parser *);
2050 static void cp_parser_explicit_instantiation
2051   (cp_parser *);
2052 static void cp_parser_explicit_specialization
2053   (cp_parser *);
2054
2055 /* Exception handling [gram.exception] */
2056
2057 static tree cp_parser_try_block
2058   (cp_parser *);
2059 static bool cp_parser_function_try_block
2060   (cp_parser *);
2061 static void cp_parser_handler_seq
2062   (cp_parser *);
2063 static void cp_parser_handler
2064   (cp_parser *);
2065 static tree cp_parser_exception_declaration
2066   (cp_parser *);
2067 static tree cp_parser_throw_expression
2068   (cp_parser *);
2069 static tree cp_parser_exception_specification_opt
2070   (cp_parser *);
2071 static tree cp_parser_type_id_list
2072   (cp_parser *);
2073
2074 /* GNU Extensions */
2075
2076 static tree cp_parser_asm_specification_opt
2077   (cp_parser *);
2078 static tree cp_parser_asm_operand_list
2079   (cp_parser *);
2080 static tree cp_parser_asm_clobber_list
2081   (cp_parser *);
2082 static tree cp_parser_asm_label_list
2083   (cp_parser *);
2084 static tree cp_parser_attributes_opt
2085   (cp_parser *);
2086 static tree cp_parser_attribute_list
2087   (cp_parser *);
2088 static bool cp_parser_extension_opt
2089   (cp_parser *, int *);
2090 static void cp_parser_label_declaration
2091   (cp_parser *);
2092
2093 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2094 static bool cp_parser_pragma
2095   (cp_parser *, enum pragma_context);
2096
2097 /* Objective-C++ Productions */
2098
2099 static tree cp_parser_objc_message_receiver
2100   (cp_parser *);
2101 static tree cp_parser_objc_message_args
2102   (cp_parser *);
2103 static tree cp_parser_objc_message_expression
2104   (cp_parser *);
2105 static tree cp_parser_objc_encode_expression
2106   (cp_parser *);
2107 static tree cp_parser_objc_defs_expression
2108   (cp_parser *);
2109 static tree cp_parser_objc_protocol_expression
2110   (cp_parser *);
2111 static tree cp_parser_objc_selector_expression
2112   (cp_parser *);
2113 static tree cp_parser_objc_expression
2114   (cp_parser *);
2115 static bool cp_parser_objc_selector_p
2116   (enum cpp_ttype);
2117 static tree cp_parser_objc_selector
2118   (cp_parser *);
2119 static tree cp_parser_objc_protocol_refs_opt
2120   (cp_parser *);
2121 static void cp_parser_objc_declaration
2122   (cp_parser *, tree);
2123 static tree cp_parser_objc_statement
2124   (cp_parser *);
2125 static bool cp_parser_objc_valid_prefix_attributes
2126   (cp_parser *, tree *);
2127 static void cp_parser_objc_at_property_declaration 
2128   (cp_parser *) ;
2129 static void cp_parser_objc_at_synthesize_declaration 
2130   (cp_parser *) ;
2131 static void cp_parser_objc_at_dynamic_declaration
2132   (cp_parser *) ;
2133 static tree cp_parser_objc_struct_declaration
2134   (cp_parser *) ;
2135
2136 /* Utility Routines */
2137
2138 static tree cp_parser_lookup_name
2139   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2140 static tree cp_parser_lookup_name_simple
2141   (cp_parser *, tree, location_t);
2142 static tree cp_parser_maybe_treat_template_as_class
2143   (tree, bool);
2144 static bool cp_parser_check_declarator_template_parameters
2145   (cp_parser *, cp_declarator *, location_t);
2146 static bool cp_parser_check_template_parameters
2147   (cp_parser *, unsigned, location_t, cp_declarator *);
2148 static tree cp_parser_simple_cast_expression
2149   (cp_parser *);
2150 static tree cp_parser_global_scope_opt
2151   (cp_parser *, bool);
2152 static bool cp_parser_constructor_declarator_p
2153   (cp_parser *, bool);
2154 static tree cp_parser_function_definition_from_specifiers_and_declarator
2155   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2156 static tree cp_parser_function_definition_after_declarator
2157   (cp_parser *, bool);
2158 static void cp_parser_template_declaration_after_export
2159   (cp_parser *, bool);
2160 static void cp_parser_perform_template_parameter_access_checks
2161   (VEC (deferred_access_check,gc)*);
2162 static tree cp_parser_single_declaration
2163   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2164 static tree cp_parser_functional_cast
2165   (cp_parser *, tree);
2166 static tree cp_parser_save_member_function_body
2167   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2168 static tree cp_parser_enclosed_template_argument_list
2169   (cp_parser *);
2170 static void cp_parser_save_default_args
2171   (cp_parser *, tree);
2172 static void cp_parser_late_parsing_for_member
2173   (cp_parser *, tree);
2174 static void cp_parser_late_parsing_default_args
2175   (cp_parser *, tree);
2176 static tree cp_parser_sizeof_operand
2177   (cp_parser *, enum rid);
2178 static tree cp_parser_trait_expr
2179   (cp_parser *, enum rid);
2180 static bool cp_parser_declares_only_class_p
2181   (cp_parser *);
2182 static void cp_parser_set_storage_class
2183   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2184 static void cp_parser_set_decl_spec_type
2185   (cp_decl_specifier_seq *, tree, location_t, bool);
2186 static bool cp_parser_friend_p
2187   (const cp_decl_specifier_seq *);
2188 static void cp_parser_required_error
2189   (cp_parser *, required_token, bool);
2190 static cp_token *cp_parser_require
2191   (cp_parser *, enum cpp_ttype, required_token);
2192 static cp_token *cp_parser_require_keyword
2193   (cp_parser *, enum rid, required_token);
2194 static bool cp_parser_token_starts_function_definition_p
2195   (cp_token *);
2196 static bool cp_parser_next_token_starts_class_definition_p
2197   (cp_parser *);
2198 static bool cp_parser_next_token_ends_template_argument_p
2199   (cp_parser *);
2200 static bool cp_parser_nth_token_starts_template_argument_list_p
2201   (cp_parser *, size_t);
2202 static enum tag_types cp_parser_token_is_class_key
2203   (cp_token *);
2204 static void cp_parser_check_class_key
2205   (enum tag_types, tree type);
2206 static void cp_parser_check_access_in_redeclaration
2207   (tree type, location_t location);
2208 static bool cp_parser_optional_template_keyword
2209   (cp_parser *);
2210 static void cp_parser_pre_parsed_nested_name_specifier
2211   (cp_parser *);
2212 static bool cp_parser_cache_group
2213   (cp_parser *, enum cpp_ttype, unsigned);
2214 static void cp_parser_parse_tentatively
2215   (cp_parser *);
2216 static void cp_parser_commit_to_tentative_parse
2217   (cp_parser *);
2218 static void cp_parser_abort_tentative_parse
2219   (cp_parser *);
2220 static bool cp_parser_parse_definitely
2221   (cp_parser *);
2222 static inline bool cp_parser_parsing_tentatively
2223   (cp_parser *);
2224 static bool cp_parser_uncommitted_to_tentative_parse_p
2225   (cp_parser *);
2226 static void cp_parser_error
2227   (cp_parser *, const char *);
2228 static void cp_parser_name_lookup_error
2229   (cp_parser *, tree, tree, name_lookup_error, location_t);
2230 static bool cp_parser_simulate_error
2231   (cp_parser *);
2232 static bool cp_parser_check_type_definition
2233   (cp_parser *);
2234 static void cp_parser_check_for_definition_in_return_type
2235   (cp_declarator *, tree, location_t type_location);
2236 static void cp_parser_check_for_invalid_template_id
2237   (cp_parser *, tree, location_t location);
2238 static bool cp_parser_non_integral_constant_expression
2239   (cp_parser *, non_integral_constant);
2240 static void cp_parser_diagnose_invalid_type_name
2241   (cp_parser *, tree, tree, location_t);
2242 static bool cp_parser_parse_and_diagnose_invalid_type_name
2243   (cp_parser *);
2244 static int cp_parser_skip_to_closing_parenthesis
2245   (cp_parser *, bool, bool, bool);
2246 static void cp_parser_skip_to_end_of_statement
2247   (cp_parser *);
2248 static void cp_parser_consume_semicolon_at_end_of_statement
2249   (cp_parser *);
2250 static void cp_parser_skip_to_end_of_block_or_statement
2251   (cp_parser *);
2252 static bool cp_parser_skip_to_closing_brace
2253   (cp_parser *);
2254 static void cp_parser_skip_to_end_of_template_parameter_list
2255   (cp_parser *);
2256 static void cp_parser_skip_to_pragma_eol
2257   (cp_parser*, cp_token *);
2258 static bool cp_parser_error_occurred
2259   (cp_parser *);
2260 static bool cp_parser_allow_gnu_extensions_p
2261   (cp_parser *);
2262 static bool cp_parser_is_string_literal
2263   (cp_token *);
2264 static bool cp_parser_is_keyword
2265   (cp_token *, enum rid);
2266 static tree cp_parser_make_typename_type
2267   (cp_parser *, tree, tree, location_t location);
2268 static cp_declarator * cp_parser_make_indirect_declarator
2269   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2270
2271 /* Returns nonzero if we are parsing tentatively.  */
2272
2273 static inline bool
2274 cp_parser_parsing_tentatively (cp_parser* parser)
2275 {
2276   return parser->context->next != NULL;
2277 }
2278
2279 /* Returns nonzero if TOKEN is a string literal.  */
2280
2281 static bool
2282 cp_parser_is_string_literal (cp_token* token)
2283 {
2284   return (token->type == CPP_STRING ||
2285           token->type == CPP_STRING16 ||
2286           token->type == CPP_STRING32 ||
2287           token->type == CPP_WSTRING ||
2288           token->type == CPP_UTF8STRING);
2289 }
2290
2291 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2292
2293 static bool
2294 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2295 {
2296   return token->keyword == keyword;
2297 }
2298
2299 /* If not parsing tentatively, issue a diagnostic of the form
2300       FILE:LINE: MESSAGE before TOKEN
2301    where TOKEN is the next token in the input stream.  MESSAGE
2302    (specified by the caller) is usually of the form "expected
2303    OTHER-TOKEN".  */
2304
2305 static void
2306 cp_parser_error (cp_parser* parser, const char* gmsgid)
2307 {
2308   if (!cp_parser_simulate_error (parser))
2309     {
2310       cp_token *token = cp_lexer_peek_token (parser->lexer);
2311       /* This diagnostic makes more sense if it is tagged to the line
2312          of the token we just peeked at.  */
2313       cp_lexer_set_source_position_from_token (token);
2314
2315       if (token->type == CPP_PRAGMA)
2316         {
2317           error_at (token->location,
2318                     "%<#pragma%> is not allowed here");
2319           cp_parser_skip_to_pragma_eol (parser, token);
2320           return;
2321         }
2322
2323       c_parse_error (gmsgid,
2324                      /* Because c_parser_error does not understand
2325                         CPP_KEYWORD, keywords are treated like
2326                         identifiers.  */
2327                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2328                      token->u.value, token->flags);
2329     }
2330 }
2331
2332 /* Issue an error about name-lookup failing.  NAME is the
2333    IDENTIFIER_NODE DECL is the result of
2334    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2335    the thing that we hoped to find.  */
2336
2337 static void
2338 cp_parser_name_lookup_error (cp_parser* parser,
2339                              tree name,
2340                              tree decl,
2341                              name_lookup_error desired,
2342                              location_t location)
2343 {
2344   /* If name lookup completely failed, tell the user that NAME was not
2345      declared.  */
2346   if (decl == error_mark_node)
2347     {
2348       if (parser->scope && parser->scope != global_namespace)
2349         error_at (location, "%<%E::%E%> has not been declared",
2350                   parser->scope, name);
2351       else if (parser->scope == global_namespace)
2352         error_at (location, "%<::%E%> has not been declared", name);
2353       else if (parser->object_scope
2354                && !CLASS_TYPE_P (parser->object_scope))
2355         error_at (location, "request for member %qE in non-class type %qT",
2356                   name, parser->object_scope);
2357       else if (parser->object_scope)
2358         error_at (location, "%<%T::%E%> has not been declared",
2359                   parser->object_scope, name);
2360       else
2361         error_at (location, "%qE has not been declared", name);
2362     }
2363   else if (parser->scope && parser->scope != global_namespace)
2364     {
2365       switch (desired)
2366         {
2367           case NLE_TYPE:
2368             error_at (location, "%<%E::%E%> is not a type",
2369                                 parser->scope, name);
2370             break;
2371           case NLE_CXX98:
2372             error_at (location, "%<%E::%E%> is not a class or namespace",
2373                                 parser->scope, name);
2374             break;
2375           case NLE_NOT_CXX98:
2376             error_at (location,
2377                       "%<%E::%E%> is not a class, namespace, or enumeration",
2378                       parser->scope, name);
2379             break;
2380           default:
2381             gcc_unreachable ();
2382             
2383         }
2384     }
2385   else if (parser->scope == global_namespace)
2386     {
2387       switch (desired)
2388         {
2389           case NLE_TYPE:
2390             error_at (location, "%<::%E%> is not a type", name);
2391             break;
2392           case NLE_CXX98:
2393             error_at (location, "%<::%E%> is not a class or namespace", name);
2394             break;
2395           case NLE_NOT_CXX98:
2396             error_at (location,
2397                       "%<::%E%> is not a class, namespace, or enumeration",
2398                       name);
2399             break;
2400           default:
2401             gcc_unreachable ();
2402         }
2403     }
2404   else
2405     {
2406       switch (desired)
2407         {
2408           case NLE_TYPE:
2409             error_at (location, "%qE is not a type", name);
2410             break;
2411           case NLE_CXX98:
2412             error_at (location, "%qE is not a class or namespace", name);
2413             break;
2414           case NLE_NOT_CXX98:
2415             error_at (location,
2416                       "%qE is not a class, namespace, or enumeration", name);
2417             break;
2418           default:
2419             gcc_unreachable ();
2420         }
2421     }
2422 }
2423
2424 /* If we are parsing tentatively, remember that an error has occurred
2425    during this tentative parse.  Returns true if the error was
2426    simulated; false if a message should be issued by the caller.  */
2427
2428 static bool
2429 cp_parser_simulate_error (cp_parser* parser)
2430 {
2431   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2432     {
2433       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2434       return true;
2435     }
2436   return false;
2437 }
2438
2439 /* Check for repeated decl-specifiers.  */
2440
2441 static void
2442 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2443                            location_t location)
2444 {
2445   int ds;
2446
2447   for (ds = ds_first; ds != ds_last; ++ds)
2448     {
2449       unsigned count = decl_specs->specs[ds];
2450       if (count < 2)
2451         continue;
2452       /* The "long" specifier is a special case because of "long long".  */
2453       if (ds == ds_long)
2454         {
2455           if (count > 2)
2456             error_at (location, "%<long long long%> is too long for GCC");
2457           else 
2458             pedwarn_cxx98 (location, OPT_Wlong_long, 
2459                            "ISO C++ 1998 does not support %<long long%>");
2460         }
2461       else if (count > 1)
2462         {
2463           static const char *const decl_spec_names[] = {
2464             "signed",
2465             "unsigned",
2466             "short",
2467             "long",
2468             "const",
2469             "volatile",
2470             "restrict",
2471             "inline",
2472             "virtual",
2473             "explicit",
2474             "friend",
2475             "typedef",
2476             "constexpr",
2477             "__complex",
2478             "__thread"
2479           };
2480           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2481         }
2482     }
2483 }
2484
2485 /* This function is called when a type is defined.  If type
2486    definitions are forbidden at this point, an error message is
2487    issued.  */
2488
2489 static bool
2490 cp_parser_check_type_definition (cp_parser* parser)
2491 {
2492   /* If types are forbidden here, issue a message.  */
2493   if (parser->type_definition_forbidden_message)
2494     {
2495       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2496          in the message need to be interpreted.  */
2497       error (parser->type_definition_forbidden_message);
2498       return false;
2499     }
2500   return true;
2501 }
2502
2503 /* This function is called when the DECLARATOR is processed.  The TYPE
2504    was a type defined in the decl-specifiers.  If it is invalid to
2505    define a type in the decl-specifiers for DECLARATOR, an error is
2506    issued. TYPE_LOCATION is the location of TYPE and is used
2507    for error reporting.  */
2508
2509 static void
2510 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2511                                                tree type, location_t type_location)
2512 {
2513   /* [dcl.fct] forbids type definitions in return types.
2514      Unfortunately, it's not easy to know whether or not we are
2515      processing a return type until after the fact.  */
2516   while (declarator
2517          && (declarator->kind == cdk_pointer
2518              || declarator->kind == cdk_reference
2519              || declarator->kind == cdk_ptrmem))
2520     declarator = declarator->declarator;
2521   if (declarator
2522       && declarator->kind == cdk_function)
2523     {
2524       error_at (type_location,
2525                 "new types may not be defined in a return type");
2526       inform (type_location, 
2527               "(perhaps a semicolon is missing after the definition of %qT)",
2528               type);
2529     }
2530 }
2531
2532 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2533    "<" in any valid C++ program.  If the next token is indeed "<",
2534    issue a message warning the user about what appears to be an
2535    invalid attempt to form a template-id. LOCATION is the location
2536    of the type-specifier (TYPE) */
2537
2538 static void
2539 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2540                                          tree type, location_t location)
2541 {
2542   cp_token_position start = 0;
2543
2544   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2545     {
2546       if (TYPE_P (type))
2547         error_at (location, "%qT is not a template", type);
2548       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2549         error_at (location, "%qE is not a template", type);
2550       else
2551         error_at (location, "invalid template-id");
2552       /* Remember the location of the invalid "<".  */
2553       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2554         start = cp_lexer_token_position (parser->lexer, true);
2555       /* Consume the "<".  */
2556       cp_lexer_consume_token (parser->lexer);
2557       /* Parse the template arguments.  */
2558       cp_parser_enclosed_template_argument_list (parser);
2559       /* Permanently remove the invalid template arguments so that
2560          this error message is not issued again.  */
2561       if (start)
2562         cp_lexer_purge_tokens_after (parser->lexer, start);
2563     }
2564 }
2565
2566 /* If parsing an integral constant-expression, issue an error message
2567    about the fact that THING appeared and return true.  Otherwise,
2568    return false.  In either case, set
2569    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2570
2571 static bool
2572 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2573                                             non_integral_constant thing)
2574 {
2575   parser->non_integral_constant_expression_p = true;
2576   if (parser->integral_constant_expression_p)
2577     {
2578       if (!parser->allow_non_integral_constant_expression_p)
2579         {
2580           const char *msg = NULL;
2581           switch (thing)
2582             {
2583               case NIC_FLOAT:
2584                 error ("floating-point literal "
2585                        "cannot appear in a constant-expression");
2586                 return true;
2587               case NIC_CAST:
2588                 error ("a cast to a type other than an integral or "
2589                        "enumeration type cannot appear in a "
2590                        "constant-expression");
2591                 return true;
2592               case NIC_TYPEID:
2593                 error ("%<typeid%> operator "
2594                        "cannot appear in a constant-expression");
2595                 return true;
2596               case NIC_NCC:
2597                 error ("non-constant compound literals "
2598                        "cannot appear in a constant-expression");
2599                 return true;
2600               case NIC_FUNC_CALL:
2601                 error ("a function call "
2602                        "cannot appear in a constant-expression");
2603                 return true;
2604               case NIC_INC:
2605                 error ("an increment "
2606                        "cannot appear in a constant-expression");
2607                 return true;
2608               case NIC_DEC:
2609                 error ("an decrement "
2610                        "cannot appear in a constant-expression");
2611                 return true;
2612               case NIC_ARRAY_REF:
2613                 error ("an array reference "
2614                        "cannot appear in a constant-expression");
2615                 return true;
2616               case NIC_ADDR_LABEL:
2617                 error ("the address of a label "
2618                        "cannot appear in a constant-expression");
2619                 return true;
2620               case NIC_OVERLOADED:
2621                 error ("calls to overloaded operators "
2622                        "cannot appear in a constant-expression");
2623                 return true;
2624               case NIC_ASSIGNMENT:
2625                 error ("an assignment cannot appear in a constant-expression");
2626                 return true;
2627               case NIC_COMMA:
2628                 error ("a comma operator "
2629                        "cannot appear in a constant-expression");
2630                 return true;
2631               case NIC_CONSTRUCTOR:
2632                 error ("a call to a constructor "
2633                        "cannot appear in a constant-expression");
2634                 return true;
2635               case NIC_THIS:
2636                 msg = "this";
2637                 break;
2638               case NIC_FUNC_NAME:
2639                 msg = "__FUNCTION__";
2640                 break;
2641               case NIC_PRETTY_FUNC:
2642                 msg = "__PRETTY_FUNCTION__";
2643                 break;
2644               case NIC_C99_FUNC:
2645                 msg = "__func__";
2646                 break;
2647               case NIC_VA_ARG:
2648                 msg = "va_arg";
2649                 break;
2650               case NIC_ARROW:
2651                 msg = "->";
2652                 break;
2653               case NIC_POINT:
2654                 msg = ".";
2655                 break;
2656               case NIC_STAR:
2657                 msg = "*";
2658                 break;
2659               case NIC_ADDR:
2660                 msg = "&";
2661                 break;
2662               case NIC_PREINCREMENT:
2663                 msg = "++";
2664                 break;
2665               case NIC_PREDECREMENT:
2666                 msg = "--";
2667                 break;
2668               case NIC_NEW:
2669                 msg = "new";
2670                 break;
2671               case NIC_DEL:
2672                 msg = "delete";
2673                 break;
2674               default:
2675                 gcc_unreachable ();
2676             }
2677           if (msg)
2678             error ("%qs cannot appear in a constant-expression", msg);
2679           return true;
2680         }
2681     }
2682   return false;
2683 }
2684
2685 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2686    qualifying scope (or NULL, if none) for ID.  This function commits
2687    to the current active tentative parse, if any.  (Otherwise, the
2688    problematic construct might be encountered again later, resulting
2689    in duplicate error messages.) LOCATION is the location of ID.  */
2690
2691 static void
2692 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2693                                       tree scope, tree id,
2694                                       location_t location)
2695 {
2696   tree decl, old_scope;
2697   /* Try to lookup the identifier.  */
2698   old_scope = parser->scope;
2699   parser->scope = scope;
2700   decl = cp_parser_lookup_name_simple (parser, id, location);
2701   parser->scope = old_scope;
2702   /* If the lookup found a template-name, it means that the user forgot
2703   to specify an argument list. Emit a useful error message.  */
2704   if (TREE_CODE (decl) == TEMPLATE_DECL)
2705     error_at (location,
2706               "invalid use of template-name %qE without an argument list",
2707               decl);
2708   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2709     error_at (location, "invalid use of destructor %qD as a type", id);
2710   else if (TREE_CODE (decl) == TYPE_DECL)
2711     /* Something like 'unsigned A a;'  */
2712     error_at (location, "invalid combination of multiple type-specifiers");
2713   else if (!parser->scope)
2714     {
2715       /* Issue an error message.  */
2716       error_at (location, "%qE does not name a type", id);
2717       /* If we're in a template class, it's possible that the user was
2718          referring to a type from a base class.  For example:
2719
2720            template <typename T> struct A { typedef T X; };
2721            template <typename T> struct B : public A<T> { X x; };
2722
2723          The user should have said "typename A<T>::X".  */
2724       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2725         inform (location, "C++0x %<constexpr%> only available with "
2726                 "-std=c++0x or -std=gnu++0x");
2727       else if (processing_template_decl && current_class_type
2728                && TYPE_BINFO (current_class_type))
2729         {
2730           tree b;
2731
2732           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2733                b;
2734                b = TREE_CHAIN (b))
2735             {
2736               tree base_type = BINFO_TYPE (b);
2737               if (CLASS_TYPE_P (base_type)
2738                   && dependent_type_p (base_type))
2739                 {
2740                   tree field;
2741                   /* Go from a particular instantiation of the
2742                      template (which will have an empty TYPE_FIELDs),
2743                      to the main version.  */
2744                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2745                   for (field = TYPE_FIELDS (base_type);
2746                        field;
2747                        field = DECL_CHAIN (field))
2748                     if (TREE_CODE (field) == TYPE_DECL
2749                         && DECL_NAME (field) == id)
2750                       {
2751                         inform (location, 
2752                                 "(perhaps %<typename %T::%E%> was intended)",
2753                                 BINFO_TYPE (b), id);
2754                         break;
2755                       }
2756                   if (field)
2757                     break;
2758                 }
2759             }
2760         }
2761     }
2762   /* Here we diagnose qualified-ids where the scope is actually correct,
2763      but the identifier does not resolve to a valid type name.  */
2764   else if (parser->scope != error_mark_node)
2765     {
2766       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2767         error_at (location, "%qE in namespace %qE does not name a type",
2768                   id, parser->scope);
2769       else if (CLASS_TYPE_P (parser->scope)
2770                && constructor_name_p (id, parser->scope))
2771         {
2772           /* A<T>::A<T>() */
2773           error_at (location, "%<%T::%E%> names the constructor, not"
2774                     " the type", parser->scope, id);
2775           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2776             error_at (location, "and %qT has no template constructors",
2777                       parser->scope);
2778         }
2779       else if (TYPE_P (parser->scope)
2780                && dependent_scope_p (parser->scope))
2781         error_at (location, "need %<typename%> before %<%T::%E%> because "
2782                   "%qT is a dependent scope",
2783                   parser->scope, id, parser->scope);
2784       else if (TYPE_P (parser->scope))
2785         error_at (location, "%qE in class %qT does not name a type",
2786                   id, parser->scope);
2787       else
2788         gcc_unreachable ();
2789     }
2790   cp_parser_commit_to_tentative_parse (parser);
2791 }
2792
2793 /* Check for a common situation where a type-name should be present,
2794    but is not, and issue a sensible error message.  Returns true if an
2795    invalid type-name was detected.
2796
2797    The situation handled by this function are variable declarations of the
2798    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2799    Usually, `ID' should name a type, but if we got here it means that it
2800    does not. We try to emit the best possible error message depending on
2801    how exactly the id-expression looks like.  */
2802
2803 static bool
2804 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2805 {
2806   tree id;
2807   cp_token *token = cp_lexer_peek_token (parser->lexer);
2808
2809   /* Avoid duplicate error about ambiguous lookup.  */
2810   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2811     {
2812       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2813       if (next->type == CPP_NAME && next->ambiguous_p)
2814         goto out;
2815     }
2816
2817   cp_parser_parse_tentatively (parser);
2818   id = cp_parser_id_expression (parser,
2819                                 /*template_keyword_p=*/false,
2820                                 /*check_dependency_p=*/true,
2821                                 /*template_p=*/NULL,
2822                                 /*declarator_p=*/true,
2823                                 /*optional_p=*/false);
2824   /* If the next token is a (, this is a function with no explicit return
2825      type, i.e. constructor, destructor or conversion op.  */
2826   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2827       || TREE_CODE (id) == TYPE_DECL)
2828     {
2829       cp_parser_abort_tentative_parse (parser);
2830       return false;
2831     }
2832   if (!cp_parser_parse_definitely (parser))
2833     return false;
2834
2835   /* Emit a diagnostic for the invalid type.  */
2836   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2837                                         id, token->location);
2838  out:
2839   /* If we aren't in the middle of a declarator (i.e. in a
2840      parameter-declaration-clause), skip to the end of the declaration;
2841      there's no point in trying to process it.  */
2842   if (!parser->in_declarator_p)
2843     cp_parser_skip_to_end_of_block_or_statement (parser);
2844   return true;
2845 }
2846
2847 /* Consume tokens up to, and including, the next non-nested closing `)'.
2848    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2849    are doing error recovery. Returns -1 if OR_COMMA is true and we
2850    found an unnested comma.  */
2851
2852 static int
2853 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2854                                        bool recovering,
2855                                        bool or_comma,
2856                                        bool consume_paren)
2857 {
2858   unsigned paren_depth = 0;
2859   unsigned brace_depth = 0;
2860   unsigned square_depth = 0;
2861
2862   if (recovering && !or_comma
2863       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2864     return 0;
2865
2866   while (true)
2867     {
2868       cp_token * token = cp_lexer_peek_token (parser->lexer);
2869
2870       switch (token->type)
2871         {
2872         case CPP_EOF:
2873         case CPP_PRAGMA_EOL:
2874           /* If we've run out of tokens, then there is no closing `)'.  */
2875           return 0;
2876
2877         /* This is good for lambda expression capture-lists.  */
2878         case CPP_OPEN_SQUARE:
2879           ++square_depth;
2880           break;
2881         case CPP_CLOSE_SQUARE:
2882           if (!square_depth--)
2883             return 0;
2884           break;
2885
2886         case CPP_SEMICOLON:
2887           /* This matches the processing in skip_to_end_of_statement.  */
2888           if (!brace_depth)
2889             return 0;
2890           break;
2891
2892         case CPP_OPEN_BRACE:
2893           ++brace_depth;
2894           break;
2895         case CPP_CLOSE_BRACE:
2896           if (!brace_depth--)
2897             return 0;
2898           break;
2899
2900         case CPP_COMMA:
2901           if (recovering && or_comma && !brace_depth && !paren_depth
2902               && !square_depth)
2903             return -1;
2904           break;
2905
2906         case CPP_OPEN_PAREN:
2907           if (!brace_depth)
2908             ++paren_depth;
2909           break;
2910
2911         case CPP_CLOSE_PAREN:
2912           if (!brace_depth && !paren_depth--)
2913             {
2914               if (consume_paren)
2915                 cp_lexer_consume_token (parser->lexer);
2916               return 1;
2917             }
2918           break;
2919
2920         default:
2921           break;
2922         }
2923
2924       /* Consume the token.  */
2925       cp_lexer_consume_token (parser->lexer);
2926     }
2927 }
2928
2929 /* Consume tokens until we reach the end of the current statement.
2930    Normally, that will be just before consuming a `;'.  However, if a
2931    non-nested `}' comes first, then we stop before consuming that.  */
2932
2933 static void
2934 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2935 {
2936   unsigned nesting_depth = 0;
2937
2938   while (true)
2939     {
2940       cp_token *token = cp_lexer_peek_token (parser->lexer);
2941
2942       switch (token->type)
2943         {
2944         case CPP_EOF:
2945         case CPP_PRAGMA_EOL:
2946           /* If we've run out of tokens, stop.  */
2947           return;
2948
2949         case CPP_SEMICOLON:
2950           /* If the next token is a `;', we have reached the end of the
2951              statement.  */
2952           if (!nesting_depth)
2953             return;
2954           break;
2955
2956         case CPP_CLOSE_BRACE:
2957           /* If this is a non-nested '}', stop before consuming it.
2958              That way, when confronted with something like:
2959
2960                { 3 + }
2961
2962              we stop before consuming the closing '}', even though we
2963              have not yet reached a `;'.  */
2964           if (nesting_depth == 0)
2965             return;
2966
2967           /* If it is the closing '}' for a block that we have
2968              scanned, stop -- but only after consuming the token.
2969              That way given:
2970
2971                 void f g () { ... }
2972                 typedef int I;
2973
2974              we will stop after the body of the erroneously declared
2975              function, but before consuming the following `typedef'
2976              declaration.  */
2977           if (--nesting_depth == 0)
2978             {
2979               cp_lexer_consume_token (parser->lexer);
2980               return;
2981             }
2982
2983         case CPP_OPEN_BRACE:
2984           ++nesting_depth;
2985           break;
2986
2987         default:
2988           break;
2989         }
2990
2991       /* Consume the token.  */
2992       cp_lexer_consume_token (parser->lexer);
2993     }
2994 }
2995
2996 /* This function is called at the end of a statement or declaration.
2997    If the next token is a semicolon, it is consumed; otherwise, error
2998    recovery is attempted.  */
2999
3000 static void
3001 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3002 {
3003   /* Look for the trailing `;'.  */
3004   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3005     {
3006       /* If there is additional (erroneous) input, skip to the end of
3007          the statement.  */
3008       cp_parser_skip_to_end_of_statement (parser);
3009       /* If the next token is now a `;', consume it.  */
3010       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3011         cp_lexer_consume_token (parser->lexer);
3012     }
3013 }
3014
3015 /* Skip tokens until we have consumed an entire block, or until we
3016    have consumed a non-nested `;'.  */
3017
3018 static void
3019 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3020 {
3021   int nesting_depth = 0;
3022
3023   while (nesting_depth >= 0)
3024     {
3025       cp_token *token = cp_lexer_peek_token (parser->lexer);
3026
3027       switch (token->type)
3028         {
3029         case CPP_EOF:
3030         case CPP_PRAGMA_EOL:
3031           /* If we've run out of tokens, stop.  */
3032           return;
3033
3034         case CPP_SEMICOLON:
3035           /* Stop if this is an unnested ';'. */
3036           if (!nesting_depth)
3037             nesting_depth = -1;
3038           break;
3039
3040         case CPP_CLOSE_BRACE:
3041           /* Stop if this is an unnested '}', or closes the outermost
3042              nesting level.  */
3043           nesting_depth--;
3044           if (nesting_depth < 0)
3045             return;
3046           if (!nesting_depth)
3047             nesting_depth = -1;
3048           break;
3049
3050         case CPP_OPEN_BRACE:
3051           /* Nest. */
3052           nesting_depth++;
3053           break;
3054
3055         default:
3056           break;
3057         }
3058
3059       /* Consume the token.  */
3060       cp_lexer_consume_token (parser->lexer);
3061     }
3062 }
3063
3064 /* Skip tokens until a non-nested closing curly brace is the next
3065    token, or there are no more tokens. Return true in the first case,
3066    false otherwise.  */
3067
3068 static bool
3069 cp_parser_skip_to_closing_brace (cp_parser *parser)
3070 {
3071   unsigned nesting_depth = 0;
3072
3073   while (true)
3074     {
3075       cp_token *token = cp_lexer_peek_token (parser->lexer);
3076
3077       switch (token->type)
3078         {
3079         case CPP_EOF:
3080         case CPP_PRAGMA_EOL:
3081           /* If we've run out of tokens, stop.  */
3082           return false;
3083
3084         case CPP_CLOSE_BRACE:
3085           /* If the next token is a non-nested `}', then we have reached
3086              the end of the current block.  */
3087           if (nesting_depth-- == 0)
3088             return true;
3089           break;
3090
3091         case CPP_OPEN_BRACE:
3092           /* If it the next token is a `{', then we are entering a new
3093              block.  Consume the entire block.  */
3094           ++nesting_depth;
3095           break;
3096
3097         default:
3098           break;
3099         }
3100
3101       /* Consume the token.  */
3102       cp_lexer_consume_token (parser->lexer);
3103     }
3104 }
3105
3106 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3107    parameter is the PRAGMA token, allowing us to purge the entire pragma
3108    sequence.  */
3109
3110 static void
3111 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3112 {
3113   cp_token *token;
3114
3115   parser->lexer->in_pragma = false;
3116
3117   do
3118     token = cp_lexer_consume_token (parser->lexer);
3119   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3120
3121   /* Ensure that the pragma is not parsed again.  */
3122   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3123 }
3124
3125 /* Require pragma end of line, resyncing with it as necessary.  The
3126    arguments are as for cp_parser_skip_to_pragma_eol.  */
3127
3128 static void
3129 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3130 {
3131   parser->lexer->in_pragma = false;
3132   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3133     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3134 }
3135
3136 /* This is a simple wrapper around make_typename_type. When the id is
3137    an unresolved identifier node, we can provide a superior diagnostic
3138    using cp_parser_diagnose_invalid_type_name.  */
3139
3140 static tree
3141 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3142                               tree id, location_t id_location)
3143 {
3144   tree result;
3145   if (TREE_CODE (id) == IDENTIFIER_NODE)
3146     {
3147       result = make_typename_type (scope, id, typename_type,
3148                                    /*complain=*/tf_none);
3149       if (result == error_mark_node)
3150         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3151       return result;
3152     }
3153   return make_typename_type (scope, id, typename_type, tf_error);
3154 }
3155
3156 /* This is a wrapper around the
3157    make_{pointer,ptrmem,reference}_declarator functions that decides
3158    which one to call based on the CODE and CLASS_TYPE arguments. The
3159    CODE argument should be one of the values returned by
3160    cp_parser_ptr_operator. */
3161 static cp_declarator *
3162 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3163                                     cp_cv_quals cv_qualifiers,
3164                                     cp_declarator *target)
3165 {
3166   if (code == ERROR_MARK)
3167     return cp_error_declarator;
3168
3169   if (code == INDIRECT_REF)
3170     if (class_type == NULL_TREE)
3171       return make_pointer_declarator (cv_qualifiers, target);
3172     else
3173       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3174   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3175     return make_reference_declarator (cv_qualifiers, target, false);
3176   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3177     return make_reference_declarator (cv_qualifiers, target, true);
3178   gcc_unreachable ();
3179 }
3180
3181 /* Create a new C++ parser.  */
3182
3183 static cp_parser *
3184 cp_parser_new (void)
3185 {
3186   cp_parser *parser;
3187   cp_lexer *lexer;
3188   unsigned i;
3189
3190   /* cp_lexer_new_main is called before doing GC allocation because
3191      cp_lexer_new_main might load a PCH file.  */
3192   lexer = cp_lexer_new_main ();
3193
3194   /* Initialize the binops_by_token so that we can get the tree
3195      directly from the token.  */
3196   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3197     binops_by_token[binops[i].token_type] = binops[i];
3198
3199   parser = ggc_alloc_cleared_cp_parser ();
3200   parser->lexer = lexer;
3201   parser->context = cp_parser_context_new (NULL);
3202
3203   /* For now, we always accept GNU extensions.  */
3204   parser->allow_gnu_extensions_p = 1;
3205
3206   /* The `>' token is a greater-than operator, not the end of a
3207      template-id.  */
3208   parser->greater_than_is_operator_p = true;
3209
3210   parser->default_arg_ok_p = true;
3211
3212   /* We are not parsing a constant-expression.  */
3213   parser->integral_constant_expression_p = false;
3214   parser->allow_non_integral_constant_expression_p = false;
3215   parser->non_integral_constant_expression_p = false;
3216
3217   /* Local variable names are not forbidden.  */
3218   parser->local_variables_forbidden_p = false;
3219
3220   /* We are not processing an `extern "C"' declaration.  */
3221   parser->in_unbraced_linkage_specification_p = false;
3222
3223   /* We are not processing a declarator.  */
3224   parser->in_declarator_p = false;
3225
3226   /* We are not processing a template-argument-list.  */
3227   parser->in_template_argument_list_p = false;
3228
3229   /* We are not in an iteration statement.  */
3230   parser->in_statement = 0;
3231
3232   /* We are not in a switch statement.  */
3233   parser->in_switch_statement_p = false;
3234
3235   /* We are not parsing a type-id inside an expression.  */
3236   parser->in_type_id_in_expr_p = false;
3237
3238   /* Declarations aren't implicitly extern "C".  */
3239   parser->implicit_extern_c = false;
3240
3241   /* String literals should be translated to the execution character set.  */
3242   parser->translate_strings_p = true;
3243
3244   /* We are not parsing a function body.  */
3245   parser->in_function_body = false;
3246
3247   /* The unparsed function queue is empty.  */
3248   push_unparsed_function_queues (parser);
3249
3250   /* There are no classes being defined.  */
3251   parser->num_classes_being_defined = 0;
3252
3253   /* No template parameters apply.  */
3254   parser->num_template_parameter_lists = 0;
3255
3256   return parser;
3257 }
3258
3259 /* Create a cp_lexer structure which will emit the tokens in CACHE
3260    and push it onto the parser's lexer stack.  This is used for delayed
3261    parsing of in-class method bodies and default arguments, and should
3262    not be confused with tentative parsing.  */
3263 static void
3264 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3265 {
3266   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3267   lexer->next = parser->lexer;
3268   parser->lexer = lexer;
3269
3270   /* Move the current source position to that of the first token in the
3271      new lexer.  */
3272   cp_lexer_set_source_position_from_token (lexer->next_token);
3273 }
3274
3275 /* Pop the top lexer off the parser stack.  This is never used for the
3276    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3277 static void
3278 cp_parser_pop_lexer (cp_parser *parser)
3279 {
3280   cp_lexer *lexer = parser->lexer;
3281   parser->lexer = lexer->next;
3282   cp_lexer_destroy (lexer);
3283
3284   /* Put the current source position back where it was before this
3285      lexer was pushed.  */
3286   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3287 }
3288
3289 /* Lexical conventions [gram.lex]  */
3290
3291 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3292    identifier.  */
3293
3294 static tree
3295 cp_parser_identifier (cp_parser* parser)
3296 {
3297   cp_token *token;
3298
3299   /* Look for the identifier.  */
3300   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3301   /* Return the value.  */
3302   return token ? token->u.value : error_mark_node;
3303 }
3304
3305 /* Parse a sequence of adjacent string constants.  Returns a
3306    TREE_STRING representing the combined, nul-terminated string
3307    constant.  If TRANSLATE is true, translate the string to the
3308    execution character set.  If WIDE_OK is true, a wide string is
3309    invalid here.
3310
3311    C++98 [lex.string] says that if a narrow string literal token is
3312    adjacent to a wide string literal token, the behavior is undefined.
3313    However, C99 6.4.5p4 says that this results in a wide string literal.
3314    We follow C99 here, for consistency with the C front end.
3315
3316    This code is largely lifted from lex_string() in c-lex.c.
3317
3318    FUTURE: ObjC++ will need to handle @-strings here.  */
3319 static tree
3320 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3321 {
3322   tree value;
3323   size_t count;
3324   struct obstack str_ob;
3325   cpp_string str, istr, *strs;
3326   cp_token *tok;
3327   enum cpp_ttype type;
3328
3329   tok = cp_lexer_peek_token (parser->lexer);
3330   if (!cp_parser_is_string_literal (tok))
3331     {
3332       cp_parser_error (parser, "expected string-literal");
3333       return error_mark_node;
3334     }
3335
3336   type = tok->type;
3337
3338   /* Try to avoid the overhead of creating and destroying an obstack
3339      for the common case of just one string.  */
3340   if (!cp_parser_is_string_literal
3341       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3342     {
3343       cp_lexer_consume_token (parser->lexer);
3344
3345       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3346       str.len = TREE_STRING_LENGTH (tok->u.value);
3347       count = 1;
3348
3349       strs = &str;
3350     }
3351   else
3352     {
3353       gcc_obstack_init (&str_ob);
3354       count = 0;
3355
3356       do
3357         {
3358           cp_lexer_consume_token (parser->lexer);
3359           count++;
3360           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3361           str.len = TREE_STRING_LENGTH (tok->u.value);
3362
3363           if (type != tok->type)
3364             {
3365               if (type == CPP_STRING)
3366                 type = tok->type;
3367               else if (tok->type != CPP_STRING)
3368                 error_at (tok->location,
3369                           "unsupported non-standard concatenation "
3370                           "of string literals");
3371             }
3372
3373           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3374
3375           tok = cp_lexer_peek_token (parser->lexer);
3376         }
3377       while (cp_parser_is_string_literal (tok));
3378
3379       strs = (cpp_string *) obstack_finish (&str_ob);
3380     }
3381
3382   if (type != CPP_STRING && !wide_ok)
3383     {
3384       cp_parser_error (parser, "a wide string is invalid in this context");
3385       type = CPP_STRING;
3386     }
3387
3388   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3389       (parse_in, strs, count, &istr, type))
3390     {
3391       value = build_string (istr.len, (const char *)istr.text);
3392       free (CONST_CAST (unsigned char *, istr.text));
3393
3394       switch (type)
3395         {
3396         default:
3397         case CPP_STRING:
3398         case CPP_UTF8STRING:
3399           TREE_TYPE (value) = char_array_type_node;
3400           break;
3401         case CPP_STRING16:
3402           TREE_TYPE (value) = char16_array_type_node;
3403           break;
3404         case CPP_STRING32:
3405           TREE_TYPE (value) = char32_array_type_node;
3406           break;
3407         case CPP_WSTRING:
3408           TREE_TYPE (value) = wchar_array_type_node;
3409           break;
3410         }
3411
3412       value = fix_string_type (value);
3413     }
3414   else
3415     /* cpp_interpret_string has issued an error.  */
3416     value = error_mark_node;
3417
3418   if (count > 1)
3419     obstack_free (&str_ob, 0);
3420
3421   return value;
3422 }
3423
3424
3425 /* Basic concepts [gram.basic]  */
3426
3427 /* Parse a translation-unit.
3428
3429    translation-unit:
3430      declaration-seq [opt]
3431
3432    Returns TRUE if all went well.  */
3433
3434 static bool
3435 cp_parser_translation_unit (cp_parser* parser)
3436 {
3437   /* The address of the first non-permanent object on the declarator
3438      obstack.  */
3439   static void *declarator_obstack_base;
3440
3441   bool success;
3442
3443   /* Create the declarator obstack, if necessary.  */
3444   if (!cp_error_declarator)
3445     {
3446       gcc_obstack_init (&declarator_obstack);
3447       /* Create the error declarator.  */
3448       cp_error_declarator = make_declarator (cdk_error);
3449       /* Create the empty parameter list.  */
3450       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3451       /* Remember where the base of the declarator obstack lies.  */
3452       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3453     }
3454
3455   cp_parser_declaration_seq_opt (parser);
3456
3457   /* If there are no tokens left then all went well.  */
3458   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3459     {
3460       /* Get rid of the token array; we don't need it any more.  */
3461       cp_lexer_destroy (parser->lexer);
3462       parser->lexer = NULL;
3463
3464       /* This file might have been a context that's implicitly extern
3465          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3466       if (parser->implicit_extern_c)
3467         {
3468           pop_lang_context ();
3469           parser->implicit_extern_c = false;
3470         }
3471
3472       /* Finish up.  */
3473       finish_translation_unit ();
3474
3475       success = true;
3476     }
3477   else
3478     {
3479       cp_parser_error (parser, "expected declaration");
3480       success = false;
3481     }
3482
3483   /* Make sure the declarator obstack was fully cleaned up.  */
3484   gcc_assert (obstack_next_free (&declarator_obstack)
3485               == declarator_obstack_base);
3486
3487   /* All went well.  */
3488   return success;
3489 }
3490
3491 /* Expressions [gram.expr] */
3492
3493 /* Parse a primary-expression.
3494
3495    primary-expression:
3496      literal
3497      this
3498      ( expression )
3499      id-expression
3500
3501    GNU Extensions:
3502
3503    primary-expression:
3504      ( compound-statement )
3505      __builtin_va_arg ( assignment-expression , type-id )
3506      __builtin_offsetof ( type-id , offsetof-expression )
3507
3508    C++ Extensions:
3509      __has_nothrow_assign ( type-id )   
3510      __has_nothrow_constructor ( type-id )
3511      __has_nothrow_copy ( type-id )
3512      __has_trivial_assign ( type-id )   
3513      __has_trivial_constructor ( type-id )
3514      __has_trivial_copy ( type-id )
3515      __has_trivial_destructor ( type-id )
3516      __has_virtual_destructor ( type-id )     
3517      __is_abstract ( type-id )
3518      __is_base_of ( type-id , type-id )
3519      __is_class ( type-id )
3520      __is_convertible_to ( type-id , type-id )     
3521      __is_empty ( type-id )
3522      __is_enum ( type-id )
3523      __is_pod ( type-id )
3524      __is_polymorphic ( type-id )
3525      __is_union ( type-id )
3526
3527    Objective-C++ Extension:
3528
3529    primary-expression:
3530      objc-expression
3531
3532    literal:
3533      __null
3534
3535    ADDRESS_P is true iff this expression was immediately preceded by
3536    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3537    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3538    true iff this expression is a template argument.
3539
3540    Returns a representation of the expression.  Upon return, *IDK
3541    indicates what kind of id-expression (if any) was present.  */
3542
3543 static tree
3544 cp_parser_primary_expression (cp_parser *parser,
3545                               bool address_p,
3546                               bool cast_p,
3547                               bool template_arg_p,
3548                               cp_id_kind *idk)
3549 {
3550   cp_token *token = NULL;
3551
3552   /* Assume the primary expression is not an id-expression.  */
3553   *idk = CP_ID_KIND_NONE;
3554
3555   /* Peek at the next token.  */
3556   token = cp_lexer_peek_token (parser->lexer);
3557   switch (token->type)
3558     {
3559       /* literal:
3560            integer-literal
3561            character-literal
3562            floating-literal
3563            string-literal
3564            boolean-literal  */
3565     case CPP_CHAR:
3566     case CPP_CHAR16:
3567     case CPP_CHAR32:
3568     case CPP_WCHAR:
3569     case CPP_NUMBER:
3570       token = cp_lexer_consume_token (parser->lexer);
3571       if (TREE_CODE (token->u.value) == FIXED_CST)
3572         {
3573           error_at (token->location,
3574                     "fixed-point types not supported in C++");
3575           return error_mark_node;
3576         }
3577       /* Floating-point literals are only allowed in an integral
3578          constant expression if they are cast to an integral or
3579          enumeration type.  */
3580       if (TREE_CODE (token->u.value) == REAL_CST
3581           && parser->integral_constant_expression_p
3582           && pedantic)
3583         {
3584           /* CAST_P will be set even in invalid code like "int(2.7 +
3585              ...)".   Therefore, we have to check that the next token
3586              is sure to end the cast.  */
3587           if (cast_p)
3588             {
3589               cp_token *next_token;
3590
3591               next_token = cp_lexer_peek_token (parser->lexer);
3592               if (/* The comma at the end of an
3593                      enumerator-definition.  */
3594                   next_token->type != CPP_COMMA
3595                   /* The curly brace at the end of an enum-specifier.  */
3596                   && next_token->type != CPP_CLOSE_BRACE
3597                   /* The end of a statement.  */
3598                   && next_token->type != CPP_SEMICOLON
3599                   /* The end of the cast-expression.  */
3600                   && next_token->type != CPP_CLOSE_PAREN
3601                   /* The end of an array bound.  */
3602                   && next_token->type != CPP_CLOSE_SQUARE
3603                   /* The closing ">" in a template-argument-list.  */
3604                   && (next_token->type != CPP_GREATER
3605                       || parser->greater_than_is_operator_p)
3606                   /* C++0x only: A ">>" treated like two ">" tokens,
3607                      in a template-argument-list.  */
3608                   && (next_token->type != CPP_RSHIFT
3609                       || (cxx_dialect == cxx98)
3610                       || parser->greater_than_is_operator_p))
3611                 cast_p = false;
3612             }
3613
3614           /* If we are within a cast, then the constraint that the
3615              cast is to an integral or enumeration type will be
3616              checked at that point.  If we are not within a cast, then
3617              this code is invalid.  */
3618           if (!cast_p)
3619             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3620         }
3621       return token->u.value;
3622
3623     case CPP_STRING:
3624     case CPP_STRING16:
3625     case CPP_STRING32:
3626     case CPP_WSTRING:
3627     case CPP_UTF8STRING:
3628       /* ??? Should wide strings be allowed when parser->translate_strings_p
3629          is false (i.e. in attributes)?  If not, we can kill the third
3630          argument to cp_parser_string_literal.  */
3631       return cp_parser_string_literal (parser,
3632                                        parser->translate_strings_p,
3633                                        true);
3634
3635     case CPP_OPEN_PAREN:
3636       {
3637         tree expr;
3638         bool saved_greater_than_is_operator_p;
3639
3640         /* Consume the `('.  */
3641         cp_lexer_consume_token (parser->lexer);
3642         /* Within a parenthesized expression, a `>' token is always
3643            the greater-than operator.  */
3644         saved_greater_than_is_operator_p
3645           = parser->greater_than_is_operator_p;
3646         parser->greater_than_is_operator_p = true;
3647         /* If we see `( { ' then we are looking at the beginning of
3648            a GNU statement-expression.  */
3649         if (cp_parser_allow_gnu_extensions_p (parser)
3650             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3651           {
3652             /* Statement-expressions are not allowed by the standard.  */
3653             pedwarn (token->location, OPT_pedantic, 
3654                      "ISO C++ forbids braced-groups within expressions");
3655
3656             /* And they're not allowed outside of a function-body; you
3657                cannot, for example, write:
3658
3659                  int i = ({ int j = 3; j + 1; });
3660
3661                at class or namespace scope.  */
3662             if (!parser->in_function_body
3663                 || parser->in_template_argument_list_p)
3664               {
3665                 error_at (token->location,
3666                           "statement-expressions are not allowed outside "
3667                           "functions nor in template-argument lists");
3668                 cp_parser_skip_to_end_of_block_or_statement (parser);
3669                 expr = error_mark_node;
3670               }
3671             else
3672               {
3673                 /* Start the statement-expression.  */
3674                 expr = begin_stmt_expr ();
3675                 /* Parse the compound-statement.  */
3676                 cp_parser_compound_statement (parser, expr, false);
3677                 /* Finish up.  */
3678                 expr = finish_stmt_expr (expr, false);
3679               }
3680           }
3681         else
3682           {
3683             /* Parse the parenthesized expression.  */
3684             expr = cp_parser_expression (parser, cast_p, idk);
3685             /* Let the front end know that this expression was
3686                enclosed in parentheses. This matters in case, for
3687                example, the expression is of the form `A::B', since
3688                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3689                not.  */
3690             finish_parenthesized_expr (expr);
3691           }
3692         /* The `>' token might be the end of a template-id or
3693            template-parameter-list now.  */
3694         parser->greater_than_is_operator_p
3695           = saved_greater_than_is_operator_p;
3696         /* Consume the `)'.  */
3697         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3698           cp_parser_skip_to_end_of_statement (parser);
3699
3700         return expr;
3701       }
3702
3703     case CPP_OPEN_SQUARE:
3704       if (c_dialect_objc ())
3705         /* We have an Objective-C++ message. */
3706         return cp_parser_objc_expression (parser);
3707       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3708       return cp_parser_lambda_expression (parser);
3709
3710     case CPP_OBJC_STRING:
3711       if (c_dialect_objc ())
3712         /* We have an Objective-C++ string literal. */
3713         return cp_parser_objc_expression (parser);
3714       cp_parser_error (parser, "expected primary-expression");
3715       return error_mark_node;
3716
3717     case CPP_KEYWORD:
3718       switch (token->keyword)
3719         {
3720           /* These two are the boolean literals.  */
3721         case RID_TRUE:
3722           cp_lexer_consume_token (parser->lexer);
3723           return boolean_true_node;
3724         case RID_FALSE:
3725           cp_lexer_consume_token (parser->lexer);
3726           return boolean_false_node;
3727
3728           /* The `__null' literal.  */
3729         case RID_NULL:
3730           cp_lexer_consume_token (parser->lexer);
3731           return null_node;
3732
3733           /* The `nullptr' literal.  */
3734         case RID_NULLPTR:
3735           cp_lexer_consume_token (parser->lexer);
3736           return nullptr_node;
3737
3738           /* Recognize the `this' keyword.  */
3739         case RID_THIS:
3740           cp_lexer_consume_token (parser->lexer);
3741           if (parser->local_variables_forbidden_p)
3742             {
3743               error_at (token->location,
3744                         "%<this%> may not be used in this context");
3745               return error_mark_node;
3746             }
3747           /* Pointers cannot appear in constant-expressions.  */
3748           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3749             return error_mark_node;
3750           return finish_this_expr ();
3751
3752           /* The `operator' keyword can be the beginning of an
3753              id-expression.  */
3754         case RID_OPERATOR:
3755           goto id_expression;
3756
3757         case RID_FUNCTION_NAME:
3758         case RID_PRETTY_FUNCTION_NAME:
3759         case RID_C99_FUNCTION_NAME:
3760           {
3761             non_integral_constant name;
3762
3763             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3764                __func__ are the names of variables -- but they are
3765                treated specially.  Therefore, they are handled here,
3766                rather than relying on the generic id-expression logic
3767                below.  Grammatically, these names are id-expressions.
3768
3769                Consume the token.  */
3770             token = cp_lexer_consume_token (parser->lexer);
3771
3772             switch (token->keyword)
3773               {
3774               case RID_FUNCTION_NAME:
3775                 name = NIC_FUNC_NAME;
3776                 break;
3777               case RID_PRETTY_FUNCTION_NAME:
3778                 name = NIC_PRETTY_FUNC;
3779                 break;
3780               case RID_C99_FUNCTION_NAME:
3781                 name = NIC_C99_FUNC;
3782                 break;
3783               default:
3784                 gcc_unreachable ();
3785               }
3786
3787             if (cp_parser_non_integral_constant_expression (parser, name))
3788               return error_mark_node;
3789
3790             /* Look up the name.  */
3791             return finish_fname (token->u.value);
3792           }
3793
3794         case RID_VA_ARG:
3795           {
3796             tree expression;
3797             tree type;
3798
3799             /* The `__builtin_va_arg' construct is used to handle
3800                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3801             cp_lexer_consume_token (parser->lexer);
3802             /* Look for the opening `('.  */
3803             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3804             /* Now, parse the assignment-expression.  */
3805             expression = cp_parser_assignment_expression (parser,
3806                                                           /*cast_p=*/false, NULL);
3807             /* Look for the `,'.  */
3808             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3809             /* Parse the type-id.  */
3810             type = cp_parser_type_id (parser);
3811             /* Look for the closing `)'.  */
3812             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3813             /* Using `va_arg' in a constant-expression is not
3814                allowed.  */
3815             if (cp_parser_non_integral_constant_expression (parser,
3816                                                             NIC_VA_ARG))
3817               return error_mark_node;
3818             return build_x_va_arg (expression, type);
3819           }
3820
3821         case RID_OFFSETOF:
3822           return cp_parser_builtin_offsetof (parser);
3823
3824         case RID_HAS_NOTHROW_ASSIGN:
3825         case RID_HAS_NOTHROW_CONSTRUCTOR:
3826         case RID_HAS_NOTHROW_COPY:        
3827         case RID_HAS_TRIVIAL_ASSIGN:
3828         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3829         case RID_HAS_TRIVIAL_COPY:        
3830         case RID_HAS_TRIVIAL_DESTRUCTOR:
3831         case RID_HAS_VIRTUAL_DESTRUCTOR:
3832         case RID_IS_ABSTRACT:
3833         case RID_IS_BASE_OF:
3834         case RID_IS_CLASS:
3835         case RID_IS_CONVERTIBLE_TO:
3836         case RID_IS_EMPTY:
3837         case RID_IS_ENUM:
3838         case RID_IS_POD:
3839         case RID_IS_POLYMORPHIC:
3840         case RID_IS_STD_LAYOUT:
3841         case RID_IS_TRIVIAL:
3842         case RID_IS_UNION:
3843         case RID_IS_LITERAL_TYPE:
3844           return cp_parser_trait_expr (parser, token->keyword);
3845
3846         /* Objective-C++ expressions.  */
3847         case RID_AT_ENCODE:
3848         case RID_AT_PROTOCOL:
3849         case RID_AT_SELECTOR:
3850           return cp_parser_objc_expression (parser);
3851
3852         case RID_TEMPLATE:
3853           if (parser->in_function_body
3854               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3855                   == CPP_LESS))
3856             {
3857               error_at (token->location,
3858                         "a template declaration cannot appear at block scope");
3859               cp_parser_skip_to_end_of_block_or_statement (parser);
3860               return error_mark_node;
3861             }
3862         default:
3863           cp_parser_error (parser, "expected primary-expression");
3864           return error_mark_node;
3865         }
3866
3867       /* An id-expression can start with either an identifier, a
3868          `::' as the beginning of a qualified-id, or the "operator"
3869          keyword.  */
3870     case CPP_NAME:
3871     case CPP_SCOPE:
3872     case CPP_TEMPLATE_ID:
3873     case CPP_NESTED_NAME_SPECIFIER:
3874       {
3875         tree id_expression;
3876         tree decl;
3877         const char *error_msg;
3878         bool template_p;
3879         bool done;
3880         cp_token *id_expr_token;
3881
3882       id_expression:
3883         /* Parse the id-expression.  */
3884         id_expression
3885           = cp_parser_id_expression (parser,
3886                                      /*template_keyword_p=*/false,
3887                                      /*check_dependency_p=*/true,
3888                                      &template_p,
3889                                      /*declarator_p=*/false,
3890                                      /*optional_p=*/false);
3891         if (id_expression == error_mark_node)
3892           return error_mark_node;
3893         id_expr_token = token;
3894         token = cp_lexer_peek_token (parser->lexer);
3895         done = (token->type != CPP_OPEN_SQUARE
3896                 && token->type != CPP_OPEN_PAREN
3897                 && token->type != CPP_DOT
3898                 && token->type != CPP_DEREF
3899                 && token->type != CPP_PLUS_PLUS
3900                 && token->type != CPP_MINUS_MINUS);
3901         /* If we have a template-id, then no further lookup is
3902            required.  If the template-id was for a template-class, we
3903            will sometimes have a TYPE_DECL at this point.  */
3904         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3905                  || TREE_CODE (id_expression) == TYPE_DECL)
3906           decl = id_expression;
3907         /* Look up the name.  */
3908         else
3909           {
3910             tree ambiguous_decls;
3911
3912             /* If we already know that this lookup is ambiguous, then
3913                we've already issued an error message; there's no reason
3914                to check again.  */
3915             if (id_expr_token->type == CPP_NAME
3916                 && id_expr_token->ambiguous_p)
3917               {
3918                 cp_parser_simulate_error (parser);
3919                 return error_mark_node;
3920               }
3921
3922             decl = cp_parser_lookup_name (parser, id_expression,
3923                                           none_type,
3924                                           template_p,
3925                                           /*is_namespace=*/false,
3926                                           /*check_dependency=*/true,
3927                                           &ambiguous_decls,
3928                                           id_expr_token->location);
3929             /* If the lookup was ambiguous, an error will already have
3930                been issued.  */
3931             if (ambiguous_decls)
3932               return error_mark_node;
3933
3934             /* In Objective-C++, we may have an Objective-C 2.0
3935                dot-syntax for classes here.  */
3936             if (c_dialect_objc ()
3937                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3938                 && TREE_CODE (decl) == TYPE_DECL
3939                 && objc_is_class_name (decl))
3940               {
3941                 tree component;
3942                 cp_lexer_consume_token (parser->lexer);
3943                 component = cp_parser_identifier (parser);
3944                 if (component == error_mark_node)
3945                   return error_mark_node;
3946
3947                 return objc_build_class_component_ref (id_expression, component);
3948               }
3949
3950             /* In Objective-C++, an instance variable (ivar) may be preferred
3951                to whatever cp_parser_lookup_name() found.  */
3952             decl = objc_lookup_ivar (decl, id_expression);
3953
3954             /* If name lookup gives us a SCOPE_REF, then the
3955                qualifying scope was dependent.  */
3956             if (TREE_CODE (decl) == SCOPE_REF)
3957               {
3958                 /* At this point, we do not know if DECL is a valid
3959                    integral constant expression.  We assume that it is
3960                    in fact such an expression, so that code like:
3961
3962                       template <int N> struct A {
3963                         int a[B<N>::i];
3964                       };
3965                      
3966                    is accepted.  At template-instantiation time, we
3967                    will check that B<N>::i is actually a constant.  */
3968                 return decl;
3969               }
3970             /* Check to see if DECL is a local variable in a context
3971                where that is forbidden.  */
3972             if (parser->local_variables_forbidden_p
3973                 && local_variable_p (decl))
3974               {
3975                 /* It might be that we only found DECL because we are
3976                    trying to be generous with pre-ISO scoping rules.
3977                    For example, consider:
3978
3979                      int i;
3980                      void g() {
3981                        for (int i = 0; i < 10; ++i) {}
3982                        extern void f(int j = i);
3983                      }
3984
3985                    Here, name look up will originally find the out
3986                    of scope `i'.  We need to issue a warning message,
3987                    but then use the global `i'.  */
3988                 decl = check_for_out_of_scope_variable (decl);
3989                 if (local_variable_p (decl))
3990                   {
3991                     error_at (id_expr_token->location,
3992                               "local variable %qD may not appear in this context",
3993                               decl);
3994                     return error_mark_node;
3995                   }
3996               }
3997           }
3998
3999         decl = (finish_id_expression
4000                 (id_expression, decl, parser->scope,
4001                  idk,
4002                  parser->integral_constant_expression_p,
4003                  parser->allow_non_integral_constant_expression_p,
4004                  &parser->non_integral_constant_expression_p,
4005                  template_p, done, address_p,
4006                  template_arg_p,
4007                  &error_msg,
4008                  id_expr_token->location));
4009         if (error_msg)
4010           cp_parser_error (parser, error_msg);
4011         return decl;
4012       }
4013
4014       /* Anything else is an error.  */
4015     default:
4016       cp_parser_error (parser, "expected primary-expression");
4017       return error_mark_node;
4018     }
4019 }
4020
4021 /* Parse an id-expression.
4022
4023    id-expression:
4024      unqualified-id
4025      qualified-id
4026
4027    qualified-id:
4028      :: [opt] nested-name-specifier template [opt] unqualified-id
4029      :: identifier
4030      :: operator-function-id
4031      :: template-id
4032
4033    Return a representation of the unqualified portion of the
4034    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4035    a `::' or nested-name-specifier.
4036
4037    Often, if the id-expression was a qualified-id, the caller will
4038    want to make a SCOPE_REF to represent the qualified-id.  This
4039    function does not do this in order to avoid wastefully creating
4040    SCOPE_REFs when they are not required.
4041
4042    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4043    `template' keyword.
4044
4045    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4046    uninstantiated templates.
4047
4048    If *TEMPLATE_P is non-NULL, it is set to true iff the
4049    `template' keyword is used to explicitly indicate that the entity
4050    named is a template.
4051
4052    If DECLARATOR_P is true, the id-expression is appearing as part of
4053    a declarator, rather than as part of an expression.  */
4054
4055 static tree
4056 cp_parser_id_expression (cp_parser *parser,
4057                          bool template_keyword_p,
4058                          bool check_dependency_p,
4059                          bool *template_p,
4060                          bool declarator_p,
4061                          bool optional_p)
4062 {
4063   bool global_scope_p;
4064   bool nested_name_specifier_p;
4065
4066   /* Assume the `template' keyword was not used.  */
4067   if (template_p)
4068     *template_p = template_keyword_p;
4069
4070   /* Look for the optional `::' operator.  */
4071   global_scope_p
4072     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4073        != NULL_TREE);
4074   /* Look for the optional nested-name-specifier.  */
4075   nested_name_specifier_p
4076     = (cp_parser_nested_name_specifier_opt (parser,
4077                                             /*typename_keyword_p=*/false,
4078                                             check_dependency_p,
4079                                             /*type_p=*/false,
4080                                             declarator_p)
4081        != NULL_TREE);
4082   /* If there is a nested-name-specifier, then we are looking at
4083      the first qualified-id production.  */
4084   if (nested_name_specifier_p)
4085     {
4086       tree saved_scope;
4087       tree saved_object_scope;
4088       tree saved_qualifying_scope;
4089       tree unqualified_id;
4090       bool is_template;
4091
4092       /* See if the next token is the `template' keyword.  */
4093       if (!template_p)
4094         template_p = &is_template;
4095       *template_p = cp_parser_optional_template_keyword (parser);
4096       /* Name lookup we do during the processing of the
4097          unqualified-id might obliterate SCOPE.  */
4098       saved_scope = parser->scope;
4099       saved_object_scope = parser->object_scope;
4100       saved_qualifying_scope = parser->qualifying_scope;
4101       /* Process the final unqualified-id.  */
4102       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4103                                                  check_dependency_p,
4104                                                  declarator_p,
4105                                                  /*optional_p=*/false);
4106       /* Restore the SAVED_SCOPE for our caller.  */
4107       parser->scope = saved_scope;
4108       parser->object_scope = saved_object_scope;
4109       parser->qualifying_scope = saved_qualifying_scope;
4110
4111       return unqualified_id;
4112     }
4113   /* Otherwise, if we are in global scope, then we are looking at one
4114      of the other qualified-id productions.  */
4115   else if (global_scope_p)
4116     {
4117       cp_token *token;
4118       tree id;
4119
4120       /* Peek at the next token.  */
4121       token = cp_lexer_peek_token (parser->lexer);
4122
4123       /* If it's an identifier, and the next token is not a "<", then
4124          we can avoid the template-id case.  This is an optimization
4125          for this common case.  */
4126       if (token->type == CPP_NAME
4127           && !cp_parser_nth_token_starts_template_argument_list_p
4128                (parser, 2))
4129         return cp_parser_identifier (parser);
4130
4131       cp_parser_parse_tentatively (parser);
4132       /* Try a template-id.  */
4133       id = cp_parser_template_id (parser,
4134                                   /*template_keyword_p=*/false,
4135                                   /*check_dependency_p=*/true,
4136                                   declarator_p);
4137       /* If that worked, we're done.  */
4138       if (cp_parser_parse_definitely (parser))
4139         return id;
4140
4141       /* Peek at the next token.  (Changes in the token buffer may
4142          have invalidated the pointer obtained above.)  */
4143       token = cp_lexer_peek_token (parser->lexer);
4144
4145       switch (token->type)
4146         {
4147         case CPP_NAME:
4148           return cp_parser_identifier (parser);
4149
4150         case CPP_KEYWORD:
4151           if (token->keyword == RID_OPERATOR)
4152             return cp_parser_operator_function_id (parser);
4153           /* Fall through.  */
4154
4155         default:
4156           cp_parser_error (parser, "expected id-expression");
4157           return error_mark_node;
4158         }
4159     }
4160   else
4161     return cp_parser_unqualified_id (parser, template_keyword_p,
4162                                      /*check_dependency_p=*/true,
4163                                      declarator_p,
4164                                      optional_p);
4165 }
4166
4167 /* Parse an unqualified-id.
4168
4169    unqualified-id:
4170      identifier
4171      operator-function-id
4172      conversion-function-id
4173      ~ class-name
4174      template-id
4175
4176    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4177    keyword, in a construct like `A::template ...'.
4178
4179    Returns a representation of unqualified-id.  For the `identifier'
4180    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4181    production a BIT_NOT_EXPR is returned; the operand of the
4182    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4183    other productions, see the documentation accompanying the
4184    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4185    names are looked up in uninstantiated templates.  If DECLARATOR_P
4186    is true, the unqualified-id is appearing as part of a declarator,
4187    rather than as part of an expression.  */
4188
4189 static tree
4190 cp_parser_unqualified_id (cp_parser* parser,
4191                           bool template_keyword_p,
4192                           bool check_dependency_p,
4193                           bool declarator_p,
4194                           bool optional_p)
4195 {
4196   cp_token *token;
4197
4198   /* Peek at the next token.  */
4199   token = cp_lexer_peek_token (parser->lexer);
4200
4201   switch (token->type)
4202     {
4203     case CPP_NAME:
4204       {
4205         tree id;
4206
4207         /* We don't know yet whether or not this will be a
4208            template-id.  */
4209         cp_parser_parse_tentatively (parser);
4210         /* Try a template-id.  */
4211         id = cp_parser_template_id (parser, template_keyword_p,
4212                                     check_dependency_p,
4213                                     declarator_p);
4214         /* If it worked, we're done.  */
4215         if (cp_parser_parse_definitely (parser))
4216           return id;
4217         /* Otherwise, it's an ordinary identifier.  */
4218         return cp_parser_identifier (parser);
4219       }
4220
4221     case CPP_TEMPLATE_ID:
4222       return cp_parser_template_id (parser, template_keyword_p,
4223                                     check_dependency_p,
4224                                     declarator_p);
4225
4226     case CPP_COMPL:
4227       {
4228         tree type_decl;
4229         tree qualifying_scope;
4230         tree object_scope;
4231         tree scope;
4232         bool done;
4233
4234         /* Consume the `~' token.  */
4235         cp_lexer_consume_token (parser->lexer);
4236         /* Parse the class-name.  The standard, as written, seems to
4237            say that:
4238
4239              template <typename T> struct S { ~S (); };
4240              template <typename T> S<T>::~S() {}
4241
4242            is invalid, since `~' must be followed by a class-name, but
4243            `S<T>' is dependent, and so not known to be a class.
4244            That's not right; we need to look in uninstantiated
4245            templates.  A further complication arises from:
4246
4247              template <typename T> void f(T t) {
4248                t.T::~T();
4249              }
4250
4251            Here, it is not possible to look up `T' in the scope of `T'
4252            itself.  We must look in both the current scope, and the
4253            scope of the containing complete expression.
4254
4255            Yet another issue is:
4256
4257              struct S {
4258                int S;
4259                ~S();
4260              };
4261
4262              S::~S() {}
4263
4264            The standard does not seem to say that the `S' in `~S'
4265            should refer to the type `S' and not the data member
4266            `S::S'.  */
4267
4268         /* DR 244 says that we look up the name after the "~" in the
4269            same scope as we looked up the qualifying name.  That idea
4270            isn't fully worked out; it's more complicated than that.  */
4271         scope = parser->scope;
4272         object_scope = parser->object_scope;
4273         qualifying_scope = parser->qualifying_scope;
4274
4275         /* Check for invalid scopes.  */
4276         if (scope == error_mark_node)
4277           {
4278             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4279               cp_lexer_consume_token (parser->lexer);
4280             return error_mark_node;
4281           }
4282         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4283           {
4284             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4285               error_at (token->location,
4286                         "scope %qT before %<~%> is not a class-name",
4287                         scope);
4288             cp_parser_simulate_error (parser);
4289             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4290               cp_lexer_consume_token (parser->lexer);
4291             return error_mark_node;
4292           }
4293         gcc_assert (!scope || TYPE_P (scope));
4294
4295         /* If the name is of the form "X::~X" it's OK even if X is a
4296            typedef.  */
4297         token = cp_lexer_peek_token (parser->lexer);
4298         if (scope
4299             && token->type == CPP_NAME
4300             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4301                 != CPP_LESS)
4302             && (token->u.value == TYPE_IDENTIFIER (scope)
4303                 || constructor_name_p (token->u.value, scope)))
4304           {
4305             cp_lexer_consume_token (parser->lexer);
4306             return build_nt (BIT_NOT_EXPR, scope);
4307           }
4308
4309         /* If there was an explicit qualification (S::~T), first look
4310            in the scope given by the qualification (i.e., S).
4311
4312            Note: in the calls to cp_parser_class_name below we pass
4313            typename_type so that lookup finds the injected-class-name
4314            rather than the constructor.  */
4315         done = false;
4316         type_decl = NULL_TREE;
4317         if (scope)
4318           {
4319             cp_parser_parse_tentatively (parser);
4320             type_decl = cp_parser_class_name (parser,
4321                                               /*typename_keyword_p=*/false,
4322                                               /*template_keyword_p=*/false,
4323                                               typename_type,
4324                                               /*check_dependency=*/false,
4325                                               /*class_head_p=*/false,
4326                                               declarator_p);
4327             if (cp_parser_parse_definitely (parser))
4328               done = true;
4329           }
4330         /* In "N::S::~S", look in "N" as well.  */
4331         if (!done && scope && qualifying_scope)
4332           {
4333             cp_parser_parse_tentatively (parser);
4334             parser->scope = qualifying_scope;
4335             parser->object_scope = NULL_TREE;
4336             parser->qualifying_scope = NULL_TREE;
4337             type_decl
4338               = cp_parser_class_name (parser,
4339                                       /*typename_keyword_p=*/false,
4340                                       /*template_keyword_p=*/false,
4341                                       typename_type,
4342                                       /*check_dependency=*/false,
4343                                       /*class_head_p=*/false,
4344                                       declarator_p);
4345             if (cp_parser_parse_definitely (parser))
4346               done = true;
4347           }
4348         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4349         else if (!done && object_scope)
4350           {
4351             cp_parser_parse_tentatively (parser);
4352             parser->scope = object_scope;
4353             parser->object_scope = NULL_TREE;
4354             parser->qualifying_scope = NULL_TREE;
4355             type_decl
4356               = cp_parser_class_name (parser,
4357                                       /*typename_keyword_p=*/false,
4358                                       /*template_keyword_p=*/false,
4359                                       typename_type,
4360                                       /*check_dependency=*/false,
4361                                       /*class_head_p=*/false,
4362                                       declarator_p);
4363             if (cp_parser_parse_definitely (parser))
4364               done = true;
4365           }
4366         /* Look in the surrounding context.  */
4367         if (!done)
4368           {
4369             parser->scope = NULL_TREE;
4370             parser->object_scope = NULL_TREE;
4371             parser->qualifying_scope = NULL_TREE;
4372             if (processing_template_decl)
4373               cp_parser_parse_tentatively (parser);
4374             type_decl
4375               = cp_parser_class_name (parser,
4376                                       /*typename_keyword_p=*/false,
4377                                       /*template_keyword_p=*/false,
4378                                       typename_type,
4379                                       /*check_dependency=*/false,
4380                                       /*class_head_p=*/false,
4381                                       declarator_p);
4382             if (processing_template_decl
4383                 && ! cp_parser_parse_definitely (parser))
4384               {
4385                 /* We couldn't find a type with this name, so just accept
4386                    it and check for a match at instantiation time.  */
4387                 type_decl = cp_parser_identifier (parser);
4388                 if (type_decl != error_mark_node)
4389                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4390                 return type_decl;
4391               }
4392           }
4393         /* If an error occurred, assume that the name of the
4394            destructor is the same as the name of the qualifying
4395            class.  That allows us to keep parsing after running
4396            into ill-formed destructor names.  */
4397         if (type_decl == error_mark_node && scope)
4398           return build_nt (BIT_NOT_EXPR, scope);
4399         else if (type_decl == error_mark_node)
4400           return error_mark_node;
4401
4402         /* Check that destructor name and scope match.  */
4403         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4404           {
4405             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4406               error_at (token->location,
4407                         "declaration of %<~%T%> as member of %qT",
4408                         type_decl, scope);
4409             cp_parser_simulate_error (parser);
4410             return error_mark_node;
4411           }
4412
4413         /* [class.dtor]
4414
4415            A typedef-name that names a class shall not be used as the
4416            identifier in the declarator for a destructor declaration.  */
4417         if (declarator_p
4418             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4419             && !DECL_SELF_REFERENCE_P (type_decl)
4420             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4421           error_at (token->location,
4422                     "typedef-name %qD used as destructor declarator",
4423                     type_decl);
4424
4425         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4426       }
4427
4428     case CPP_KEYWORD:
4429       if (token->keyword == RID_OPERATOR)
4430         {
4431           tree id;
4432
4433           /* This could be a template-id, so we try that first.  */
4434           cp_parser_parse_tentatively (parser);
4435           /* Try a template-id.  */
4436           id = cp_parser_template_id (parser, template_keyword_p,
4437                                       /*check_dependency_p=*/true,
4438                                       declarator_p);
4439           /* If that worked, we're done.  */
4440           if (cp_parser_parse_definitely (parser))
4441             return id;
4442           /* We still don't know whether we're looking at an
4443              operator-function-id or a conversion-function-id.  */
4444           cp_parser_parse_tentatively (parser);
4445           /* Try an operator-function-id.  */
4446           id = cp_parser_operator_function_id (parser);
4447           /* If that didn't work, try a conversion-function-id.  */
4448           if (!cp_parser_parse_definitely (parser))
4449             id = cp_parser_conversion_function_id (parser);
4450
4451           return id;
4452         }
4453       /* Fall through.  */
4454
4455     default:
4456       if (optional_p)
4457         return NULL_TREE;
4458       cp_parser_error (parser, "expected unqualified-id");
4459       return error_mark_node;
4460     }
4461 }
4462
4463 /* Parse an (optional) nested-name-specifier.
4464
4465    nested-name-specifier: [C++98]
4466      class-or-namespace-name :: nested-name-specifier [opt]
4467      class-or-namespace-name :: template nested-name-specifier [opt]
4468
4469    nested-name-specifier: [C++0x]
4470      type-name ::
4471      namespace-name ::
4472      nested-name-specifier identifier ::
4473      nested-name-specifier template [opt] simple-template-id ::
4474
4475    PARSER->SCOPE should be set appropriately before this function is
4476    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4477    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4478    in name lookups.
4479
4480    Sets PARSER->SCOPE to the class (TYPE) or namespace
4481    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4482    it unchanged if there is no nested-name-specifier.  Returns the new
4483    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4484
4485    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4486    part of a declaration and/or decl-specifier.  */
4487
4488 static tree
4489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4490                                      bool typename_keyword_p,
4491                                      bool check_dependency_p,
4492                                      bool type_p,
4493                                      bool is_declaration)
4494 {
4495   bool success = false;
4496   cp_token_position start = 0;
4497   cp_token *token;
4498
4499   /* Remember where the nested-name-specifier starts.  */
4500   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4501     {
4502       start = cp_lexer_token_position (parser->lexer, false);
4503       push_deferring_access_checks (dk_deferred);
4504     }
4505
4506   while (true)
4507     {
4508       tree new_scope;
4509       tree old_scope;
4510       tree saved_qualifying_scope;
4511       bool template_keyword_p;
4512
4513       /* Spot cases that cannot be the beginning of a
4514          nested-name-specifier.  */
4515       token = cp_lexer_peek_token (parser->lexer);
4516
4517       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4518          the already parsed nested-name-specifier.  */
4519       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4520         {
4521           /* Grab the nested-name-specifier and continue the loop.  */
4522           cp_parser_pre_parsed_nested_name_specifier (parser);
4523           /* If we originally encountered this nested-name-specifier
4524              with IS_DECLARATION set to false, we will not have
4525              resolved TYPENAME_TYPEs, so we must do so here.  */
4526           if (is_declaration
4527               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4528             {
4529               new_scope = resolve_typename_type (parser->scope,
4530                                                  /*only_current_p=*/false);
4531               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4532                 parser->scope = new_scope;
4533             }
4534           success = true;
4535           continue;
4536         }
4537
4538       /* Spot cases that cannot be the beginning of a
4539          nested-name-specifier.  On the second and subsequent times
4540          through the loop, we look for the `template' keyword.  */
4541       if (success && token->keyword == RID_TEMPLATE)
4542         ;
4543       /* A template-id can start a nested-name-specifier.  */
4544       else if (token->type == CPP_TEMPLATE_ID)
4545         ;
4546       else
4547         {
4548           /* If the next token is not an identifier, then it is
4549              definitely not a type-name or namespace-name.  */
4550           if (token->type != CPP_NAME)
4551             break;
4552           /* If the following token is neither a `<' (to begin a
4553              template-id), nor a `::', then we are not looking at a
4554              nested-name-specifier.  */
4555           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4556           if (token->type != CPP_SCOPE
4557               && !cp_parser_nth_token_starts_template_argument_list_p
4558                   (parser, 2))
4559             break;
4560         }
4561
4562       /* The nested-name-specifier is optional, so we parse
4563          tentatively.  */
4564       cp_parser_parse_tentatively (parser);
4565
4566       /* Look for the optional `template' keyword, if this isn't the
4567          first time through the loop.  */
4568       if (success)
4569         template_keyword_p = cp_parser_optional_template_keyword (parser);
4570       else
4571         template_keyword_p = false;
4572
4573       /* Save the old scope since the name lookup we are about to do
4574          might destroy it.  */
4575       old_scope = parser->scope;
4576       saved_qualifying_scope = parser->qualifying_scope;
4577       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4578          look up names in "X<T>::I" in order to determine that "Y" is
4579          a template.  So, if we have a typename at this point, we make
4580          an effort to look through it.  */
4581       if (is_declaration
4582           && !typename_keyword_p
4583           && parser->scope
4584           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4585         parser->scope = resolve_typename_type (parser->scope,
4586                                                /*only_current_p=*/false);
4587       /* Parse the qualifying entity.  */
4588       new_scope
4589         = cp_parser_qualifying_entity (parser,
4590                                        typename_keyword_p,
4591                                        template_keyword_p,
4592                                        check_dependency_p,
4593                                        type_p,
4594                                        is_declaration);
4595       /* Look for the `::' token.  */
4596       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4597
4598       /* If we found what we wanted, we keep going; otherwise, we're
4599          done.  */
4600       if (!cp_parser_parse_definitely (parser))
4601         {
4602           bool error_p = false;
4603
4604           /* Restore the OLD_SCOPE since it was valid before the
4605              failed attempt at finding the last
4606              class-or-namespace-name.  */
4607           parser->scope = old_scope;
4608           parser->qualifying_scope = saved_qualifying_scope;
4609           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4610             break;
4611           /* If the next token is an identifier, and the one after
4612              that is a `::', then any valid interpretation would have
4613              found a class-or-namespace-name.  */
4614           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4615                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4616                      == CPP_SCOPE)
4617                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4618                      != CPP_COMPL))
4619             {
4620               token = cp_lexer_consume_token (parser->lexer);
4621               if (!error_p)
4622                 {
4623                   if (!token->ambiguous_p)
4624                     {
4625                       tree decl;
4626                       tree ambiguous_decls;
4627
4628                       decl = cp_parser_lookup_name (parser, token->u.value,
4629                                                     none_type,
4630                                                     /*is_template=*/false,
4631                                                     /*is_namespace=*/false,
4632                                                     /*check_dependency=*/true,
4633                                                     &ambiguous_decls,
4634                                                     token->location);
4635                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4636                         error_at (token->location,
4637                                   "%qD used without template parameters",
4638                                   decl);
4639                       else if (ambiguous_decls)
4640                         {
4641                           error_at (token->location,
4642                                     "reference to %qD is ambiguous",
4643                                     token->u.value);
4644                           print_candidates (ambiguous_decls);
4645                           decl = error_mark_node;
4646                         }
4647                       else
4648                         {
4649                           if (cxx_dialect != cxx98)
4650                             cp_parser_name_lookup_error
4651                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4652                              token->location);
4653                           else
4654                             cp_parser_name_lookup_error
4655                             (parser, token->u.value, decl, NLE_CXX98,
4656                              token->location);
4657                         }
4658                     }
4659                   parser->scope = error_mark_node;
4660                   error_p = true;
4661                   /* Treat this as a successful nested-name-specifier
4662                      due to:
4663
4664                      [basic.lookup.qual]
4665
4666                      If the name found is not a class-name (clause
4667                      _class_) or namespace-name (_namespace.def_), the
4668                      program is ill-formed.  */
4669                   success = true;
4670                 }
4671               cp_lexer_consume_token (parser->lexer);
4672             }
4673           break;
4674         }
4675       /* We've found one valid nested-name-specifier.  */
4676       success = true;
4677       /* Name lookup always gives us a DECL.  */
4678       if (TREE_CODE (new_scope) == TYPE_DECL)
4679         new_scope = TREE_TYPE (new_scope);
4680       /* Uses of "template" must be followed by actual templates.  */
4681       if (template_keyword_p
4682           && !(CLASS_TYPE_P (new_scope)
4683                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4684                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4685                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4686           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4687                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4688                    == TEMPLATE_ID_EXPR)))
4689         permerror (input_location, TYPE_P (new_scope)
4690                    ? "%qT is not a template"
4691                    : "%qD is not a template",
4692                    new_scope);
4693       /* If it is a class scope, try to complete it; we are about to
4694          be looking up names inside the class.  */
4695       if (TYPE_P (new_scope)
4696           /* Since checking types for dependency can be expensive,
4697              avoid doing it if the type is already complete.  */
4698           && !COMPLETE_TYPE_P (new_scope)
4699           /* Do not try to complete dependent types.  */
4700           && !dependent_type_p (new_scope))
4701         {
4702           new_scope = complete_type (new_scope);
4703           /* If it is a typedef to current class, use the current
4704              class instead, as the typedef won't have any names inside
4705              it yet.  */
4706           if (!COMPLETE_TYPE_P (new_scope)
4707               && currently_open_class (new_scope))
4708             new_scope = TYPE_MAIN_VARIANT (new_scope);
4709         }
4710       /* Make sure we look in the right scope the next time through
4711          the loop.  */
4712       parser->scope = new_scope;
4713     }
4714
4715   /* If parsing tentatively, replace the sequence of tokens that makes
4716      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4717      token.  That way, should we re-parse the token stream, we will
4718      not have to repeat the effort required to do the parse, nor will
4719      we issue duplicate error messages.  */
4720   if (success && start)
4721     {
4722       cp_token *token;
4723
4724       token = cp_lexer_token_at (parser->lexer, start);
4725       /* Reset the contents of the START token.  */
4726       token->type = CPP_NESTED_NAME_SPECIFIER;
4727       /* Retrieve any deferred checks.  Do not pop this access checks yet
4728          so the memory will not be reclaimed during token replacing below.  */
4729       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4730       token->u.tree_check_value->value = parser->scope;
4731       token->u.tree_check_value->checks = get_deferred_access_checks ();
4732       token->u.tree_check_value->qualifying_scope =
4733         parser->qualifying_scope;
4734       token->keyword = RID_MAX;
4735
4736       /* Purge all subsequent tokens.  */
4737       cp_lexer_purge_tokens_after (parser->lexer, start);
4738     }
4739
4740   if (start)
4741     pop_to_parent_deferring_access_checks ();
4742
4743   return success ? parser->scope : NULL_TREE;
4744 }
4745
4746 /* Parse a nested-name-specifier.  See
4747    cp_parser_nested_name_specifier_opt for details.  This function
4748    behaves identically, except that it will an issue an error if no
4749    nested-name-specifier is present.  */
4750
4751 static tree
4752 cp_parser_nested_name_specifier (cp_parser *parser,
4753                                  bool typename_keyword_p,
4754                                  bool check_dependency_p,
4755                                  bool type_p,
4756                                  bool is_declaration)
4757 {
4758   tree scope;
4759
4760   /* Look for the nested-name-specifier.  */
4761   scope = cp_parser_nested_name_specifier_opt (parser,
4762                                                typename_keyword_p,
4763                                                check_dependency_p,
4764                                                type_p,
4765                                                is_declaration);
4766   /* If it was not present, issue an error message.  */
4767   if (!scope)
4768     {
4769       cp_parser_error (parser, "expected nested-name-specifier");
4770       parser->scope = NULL_TREE;
4771     }
4772
4773   return scope;
4774 }
4775
4776 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4777    this is either a class-name or a namespace-name (which corresponds
4778    to the class-or-namespace-name production in the grammar). For
4779    C++0x, it can also be a type-name that refers to an enumeration
4780    type.
4781
4782    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4783    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4784    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4785    TYPE_P is TRUE iff the next name should be taken as a class-name,
4786    even the same name is declared to be another entity in the same
4787    scope.
4788
4789    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4790    specified by the class-or-namespace-name.  If neither is found the
4791    ERROR_MARK_NODE is returned.  */
4792
4793 static tree
4794 cp_parser_qualifying_entity (cp_parser *parser,
4795                              bool typename_keyword_p,
4796                              bool template_keyword_p,
4797                              bool check_dependency_p,
4798                              bool type_p,
4799                              bool is_declaration)
4800 {
4801   tree saved_scope;
4802   tree saved_qualifying_scope;
4803   tree saved_object_scope;
4804   tree scope;
4805   bool only_class_p;
4806   bool successful_parse_p;
4807
4808   /* Before we try to parse the class-name, we must save away the
4809      current PARSER->SCOPE since cp_parser_class_name will destroy
4810      it.  */
4811   saved_scope = parser->scope;
4812   saved_qualifying_scope = parser->qualifying_scope;
4813   saved_object_scope = parser->object_scope;
4814   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4815      there is no need to look for a namespace-name.  */
4816   only_class_p = template_keyword_p 
4817     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4818   if (!only_class_p)
4819     cp_parser_parse_tentatively (parser);
4820   scope = cp_parser_class_name (parser,
4821                                 typename_keyword_p,
4822                                 template_keyword_p,
4823                                 type_p ? class_type : none_type,
4824                                 check_dependency_p,
4825                                 /*class_head_p=*/false,
4826                                 is_declaration);
4827   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4828   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4829   if (!only_class_p 
4830       && cxx_dialect != cxx98
4831       && !successful_parse_p)
4832     {
4833       /* Restore the saved scope.  */
4834       parser->scope = saved_scope;
4835       parser->qualifying_scope = saved_qualifying_scope;
4836       parser->object_scope = saved_object_scope;
4837
4838       /* Parse tentatively.  */
4839       cp_parser_parse_tentatively (parser);
4840      
4841       /* Parse a typedef-name or enum-name.  */
4842       scope = cp_parser_nonclass_name (parser);
4843
4844       /* "If the name found does not designate a namespace or a class,
4845          enumeration, or dependent type, the program is ill-formed."
4846
4847          We cover classes and dependent types above and namespaces below,
4848          so this code is only looking for enums.  */
4849       if (!scope || TREE_CODE (scope) != TYPE_DECL
4850           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4851         cp_parser_simulate_error (parser);
4852
4853       successful_parse_p = cp_parser_parse_definitely (parser);
4854     }
4855   /* If that didn't work, try for a namespace-name.  */
4856   if (!only_class_p && !successful_parse_p)
4857     {
4858       /* Restore the saved scope.  */
4859       parser->scope = saved_scope;
4860       parser->qualifying_scope = saved_qualifying_scope;
4861       parser->object_scope = saved_object_scope;
4862       /* If we are not looking at an identifier followed by the scope
4863          resolution operator, then this is not part of a
4864          nested-name-specifier.  (Note that this function is only used
4865          to parse the components of a nested-name-specifier.)  */
4866       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4867           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4868         return error_mark_node;
4869       scope = cp_parser_namespace_name (parser);
4870     }
4871
4872   return scope;
4873 }
4874
4875 /* Parse a postfix-expression.
4876
4877    postfix-expression:
4878      primary-expression
4879      postfix-expression [ expression ]
4880      postfix-expression ( expression-list [opt] )
4881      simple-type-specifier ( expression-list [opt] )
4882      typename :: [opt] nested-name-specifier identifier
4883        ( expression-list [opt] )
4884      typename :: [opt] nested-name-specifier template [opt] template-id
4885        ( expression-list [opt] )
4886      postfix-expression . template [opt] id-expression
4887      postfix-expression -> template [opt] id-expression
4888      postfix-expression . pseudo-destructor-name
4889      postfix-expression -> pseudo-destructor-name
4890      postfix-expression ++
4891      postfix-expression --
4892      dynamic_cast < type-id > ( expression )
4893      static_cast < type-id > ( expression )
4894      reinterpret_cast < type-id > ( expression )
4895      const_cast < type-id > ( expression )
4896      typeid ( expression )
4897      typeid ( type-id )
4898
4899    GNU Extension:
4900
4901    postfix-expression:
4902      ( type-id ) { initializer-list , [opt] }
4903
4904    This extension is a GNU version of the C99 compound-literal
4905    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4906    but they are essentially the same concept.)
4907
4908    If ADDRESS_P is true, the postfix expression is the operand of the
4909    `&' operator.  CAST_P is true if this expression is the target of a
4910    cast.
4911
4912    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4913    class member access expressions [expr.ref].
4914
4915    Returns a representation of the expression.  */
4916
4917 static tree
4918 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4919                               bool member_access_only_p,
4920                               cp_id_kind * pidk_return)
4921 {
4922   cp_token *token;
4923   enum rid keyword;
4924   cp_id_kind idk = CP_ID_KIND_NONE;
4925   tree postfix_expression = NULL_TREE;
4926   bool is_member_access = false;
4927
4928   /* Peek at the next token.  */
4929   token = cp_lexer_peek_token (parser->lexer);
4930   /* Some of the productions are determined by keywords.  */
4931   keyword = token->keyword;
4932   switch (keyword)
4933     {
4934     case RID_DYNCAST:
4935     case RID_STATCAST:
4936     case RID_REINTCAST:
4937     case RID_CONSTCAST:
4938       {
4939         tree type;
4940         tree expression;
4941         const char *saved_message;
4942
4943         /* All of these can be handled in the same way from the point
4944            of view of parsing.  Begin by consuming the token
4945            identifying the cast.  */
4946         cp_lexer_consume_token (parser->lexer);
4947
4948         /* New types cannot be defined in the cast.  */
4949         saved_message = parser->type_definition_forbidden_message;
4950         parser->type_definition_forbidden_message
4951           = G_("types may not be defined in casts");
4952
4953         /* Look for the opening `<'.  */
4954         cp_parser_require (parser, CPP_LESS, RT_LESS);
4955         /* Parse the type to which we are casting.  */
4956         type = cp_parser_type_id (parser);
4957         /* Look for the closing `>'.  */
4958         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4959         /* Restore the old message.  */
4960         parser->type_definition_forbidden_message = saved_message;
4961
4962         /* And the expression which is being cast.  */
4963         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4964         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4965         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4966
4967         /* Only type conversions to integral or enumeration types
4968            can be used in constant-expressions.  */
4969         if (!cast_valid_in_integral_constant_expression_p (type)
4970             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4971           return error_mark_node;
4972
4973         switch (keyword)
4974           {
4975           case RID_DYNCAST:
4976             postfix_expression
4977               = build_dynamic_cast (type, expression, tf_warning_or_error);
4978             break;
4979           case RID_STATCAST:
4980             postfix_expression
4981               = build_static_cast (type, expression, tf_warning_or_error);
4982             break;
4983           case RID_REINTCAST:
4984             postfix_expression
4985               = build_reinterpret_cast (type, expression, 
4986                                         tf_warning_or_error);
4987             break;
4988           case RID_CONSTCAST:
4989             postfix_expression
4990               = build_const_cast (type, expression, tf_warning_or_error);
4991             break;
4992           default:
4993             gcc_unreachable ();
4994           }
4995       }
4996       break;
4997
4998     case RID_TYPEID:
4999       {
5000         tree type;
5001         const char *saved_message;
5002         bool saved_in_type_id_in_expr_p;
5003
5004         /* Consume the `typeid' token.  */
5005         cp_lexer_consume_token (parser->lexer);
5006         /* Look for the `(' token.  */
5007         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5008         /* Types cannot be defined in a `typeid' expression.  */
5009         saved_message = parser->type_definition_forbidden_message;
5010         parser->type_definition_forbidden_message
5011           = G_("types may not be defined in a %<typeid%> expression");
5012         /* We can't be sure yet whether we're looking at a type-id or an
5013            expression.  */
5014         cp_parser_parse_tentatively (parser);
5015         /* Try a type-id first.  */
5016         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5017         parser->in_type_id_in_expr_p = true;
5018         type = cp_parser_type_id (parser);
5019         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5020         /* Look for the `)' token.  Otherwise, we can't be sure that
5021            we're not looking at an expression: consider `typeid (int
5022            (3))', for example.  */
5023         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5024         /* If all went well, simply lookup the type-id.  */
5025         if (cp_parser_parse_definitely (parser))
5026           postfix_expression = get_typeid (type);
5027         /* Otherwise, fall back to the expression variant.  */
5028         else
5029           {
5030             tree expression;
5031
5032             /* Look for an expression.  */
5033             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5034             /* Compute its typeid.  */
5035             postfix_expression = build_typeid (expression);
5036             /* Look for the `)' token.  */
5037             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5038           }
5039         /* Restore the saved message.  */
5040         parser->type_definition_forbidden_message = saved_message;
5041         /* `typeid' may not appear in an integral constant expression.  */
5042         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5043           return error_mark_node;
5044       }
5045       break;
5046
5047     case RID_TYPENAME:
5048       {
5049         tree type;
5050         /* The syntax permitted here is the same permitted for an
5051            elaborated-type-specifier.  */
5052         type = cp_parser_elaborated_type_specifier (parser,
5053                                                     /*is_friend=*/false,
5054                                                     /*is_declaration=*/false);
5055         postfix_expression = cp_parser_functional_cast (parser, type);
5056       }
5057       break;
5058
5059     default:
5060       {
5061         tree type;
5062
5063         /* If the next thing is a simple-type-specifier, we may be
5064            looking at a functional cast.  We could also be looking at
5065            an id-expression.  So, we try the functional cast, and if
5066            that doesn't work we fall back to the primary-expression.  */
5067         cp_parser_parse_tentatively (parser);
5068         /* Look for the simple-type-specifier.  */
5069         type = cp_parser_simple_type_specifier (parser,
5070                                                 /*decl_specs=*/NULL,
5071                                                 CP_PARSER_FLAGS_NONE);
5072         /* Parse the cast itself.  */
5073         if (!cp_parser_error_occurred (parser))
5074           postfix_expression
5075             = cp_parser_functional_cast (parser, type);
5076         /* If that worked, we're done.  */
5077         if (cp_parser_parse_definitely (parser))
5078           break;
5079
5080         /* If the functional-cast didn't work out, try a
5081            compound-literal.  */
5082         if (cp_parser_allow_gnu_extensions_p (parser)
5083             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5084           {
5085             VEC(constructor_elt,gc) *initializer_list = NULL;
5086             bool saved_in_type_id_in_expr_p;
5087
5088             cp_parser_parse_tentatively (parser);
5089             /* Consume the `('.  */
5090             cp_lexer_consume_token (parser->lexer);
5091             /* Parse the type.  */
5092             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5093             parser->in_type_id_in_expr_p = true;
5094             type = cp_parser_type_id (parser);
5095             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5096             /* Look for the `)'.  */
5097             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5098             /* Look for the `{'.  */
5099             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5100             /* If things aren't going well, there's no need to
5101                keep going.  */
5102             if (!cp_parser_error_occurred (parser))
5103               {
5104                 bool non_constant_p;
5105                 /* Parse the initializer-list.  */
5106                 initializer_list
5107                   = cp_parser_initializer_list (parser, &non_constant_p);
5108                 /* Allow a trailing `,'.  */
5109                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5110                   cp_lexer_consume_token (parser->lexer);
5111                 /* Look for the final `}'.  */
5112                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5113               }
5114             /* If that worked, we're definitely looking at a
5115                compound-literal expression.  */
5116             if (cp_parser_parse_definitely (parser))
5117               {
5118                 /* Warn the user that a compound literal is not
5119                    allowed in standard C++.  */
5120                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5121                 /* For simplicity, we disallow compound literals in
5122                    constant-expressions.  We could
5123                    allow compound literals of integer type, whose
5124                    initializer was a constant, in constant
5125                    expressions.  Permitting that usage, as a further
5126                    extension, would not change the meaning of any
5127                    currently accepted programs.  (Of course, as
5128                    compound literals are not part of ISO C++, the
5129                    standard has nothing to say.)  */
5130                 if (cp_parser_non_integral_constant_expression (parser,
5131                                                                 NIC_NCC))
5132                   {
5133                     postfix_expression = error_mark_node;
5134                     break;
5135                   }
5136                 /* Form the representation of the compound-literal.  */
5137                 postfix_expression
5138                   = (finish_compound_literal
5139                      (type, build_constructor (init_list_type_node,
5140                                                initializer_list)));
5141                 break;
5142               }
5143           }
5144
5145         /* It must be a primary-expression.  */
5146         postfix_expression
5147           = cp_parser_primary_expression (parser, address_p, cast_p,
5148                                           /*template_arg_p=*/false,
5149                                           &idk);
5150       }
5151       break;
5152     }
5153
5154   /* Keep looping until the postfix-expression is complete.  */
5155   while (true)
5156     {
5157       if (idk == CP_ID_KIND_UNQUALIFIED
5158           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5159           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5160         /* It is not a Koenig lookup function call.  */
5161         postfix_expression
5162           = unqualified_name_lookup_error (postfix_expression);
5163
5164       /* Peek at the next token.  */
5165       token = cp_lexer_peek_token (parser->lexer);
5166
5167       switch (token->type)
5168         {
5169         case CPP_OPEN_SQUARE:
5170           postfix_expression
5171             = cp_parser_postfix_open_square_expression (parser,
5172                                                         postfix_expression,
5173                                                         false);
5174           idk = CP_ID_KIND_NONE;
5175           is_member_access = false;
5176           break;
5177
5178         case CPP_OPEN_PAREN:
5179           /* postfix-expression ( expression-list [opt] ) */
5180           {
5181             bool koenig_p;
5182             bool is_builtin_constant_p;
5183             bool saved_integral_constant_expression_p = false;
5184             bool saved_non_integral_constant_expression_p = false;
5185             VEC(tree,gc) *args;
5186
5187             is_member_access = false;
5188
5189             is_builtin_constant_p
5190               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5191             if (is_builtin_constant_p)
5192               {
5193                 /* The whole point of __builtin_constant_p is to allow
5194                    non-constant expressions to appear as arguments.  */
5195                 saved_integral_constant_expression_p
5196                   = parser->integral_constant_expression_p;
5197                 saved_non_integral_constant_expression_p
5198                   = parser->non_integral_constant_expression_p;
5199                 parser->integral_constant_expression_p = false;
5200               }
5201             args = (cp_parser_parenthesized_expression_list
5202                     (parser, non_attr,
5203                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5204                      /*non_constant_p=*/NULL));
5205             if (is_builtin_constant_p)
5206               {
5207                 parser->integral_constant_expression_p
5208                   = saved_integral_constant_expression_p;
5209                 parser->non_integral_constant_expression_p
5210                   = saved_non_integral_constant_expression_p;
5211               }
5212
5213             if (args == NULL)
5214               {
5215                 postfix_expression = error_mark_node;
5216                 break;
5217               }
5218
5219             /* Function calls are not permitted in
5220                constant-expressions.  */
5221             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5222                 && cp_parser_non_integral_constant_expression (parser,
5223                                                                NIC_FUNC_CALL))
5224               {
5225                 postfix_expression = error_mark_node;
5226                 release_tree_vector (args);
5227                 break;
5228               }
5229
5230             koenig_p = false;
5231             if (idk == CP_ID_KIND_UNQUALIFIED
5232                 || idk == CP_ID_KIND_TEMPLATE_ID)
5233               {
5234                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5235                   {
5236                     if (!VEC_empty (tree, args))
5237                       {
5238                         koenig_p = true;
5239                         if (!any_type_dependent_arguments_p (args))
5240                           postfix_expression
5241                             = perform_koenig_lookup (postfix_expression, args,
5242                                                      /*include_std=*/false);
5243                       }
5244                     else
5245                       postfix_expression
5246                         = unqualified_fn_lookup_error (postfix_expression);
5247                   }
5248                 /* We do not perform argument-dependent lookup if
5249                    normal lookup finds a non-function, in accordance
5250                    with the expected resolution of DR 218.  */
5251                 else if (!VEC_empty (tree, args)
5252                          && is_overloaded_fn (postfix_expression))
5253                   {
5254                     tree fn = get_first_fn (postfix_expression);
5255                     fn = STRIP_TEMPLATE (fn);
5256
5257                     /* Do not do argument dependent lookup if regular
5258                        lookup finds a member function or a block-scope
5259                        function declaration.  [basic.lookup.argdep]/3  */
5260                     if (!DECL_FUNCTION_MEMBER_P (fn)
5261                         && !DECL_LOCAL_FUNCTION_P (fn))
5262                       {
5263                         koenig_p = true;
5264                         if (!any_type_dependent_arguments_p (args))
5265                           postfix_expression
5266                             = perform_koenig_lookup (postfix_expression, args,
5267                                                      /*include_std=*/false);
5268                       }
5269                   }
5270               }
5271
5272             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5273               {
5274                 tree instance = TREE_OPERAND (postfix_expression, 0);
5275                 tree fn = TREE_OPERAND (postfix_expression, 1);
5276
5277                 if (processing_template_decl
5278                     && (type_dependent_expression_p (instance)
5279                         || (!BASELINK_P (fn)
5280                             && TREE_CODE (fn) != FIELD_DECL)
5281                         || type_dependent_expression_p (fn)
5282                         || any_type_dependent_arguments_p (args)))
5283                   {
5284                     postfix_expression
5285                       = build_nt_call_vec (postfix_expression, args);
5286                     release_tree_vector (args);
5287                     break;
5288                   }
5289
5290                 if (BASELINK_P (fn))
5291                   {
5292                   postfix_expression
5293                     = (build_new_method_call
5294                        (instance, fn, &args, NULL_TREE,
5295                         (idk == CP_ID_KIND_QUALIFIED
5296                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5297                         /*fn_p=*/NULL,
5298                         tf_warning_or_error));
5299                   }
5300                 else
5301                   postfix_expression
5302                     = finish_call_expr (postfix_expression, &args,
5303                                         /*disallow_virtual=*/false,
5304                                         /*koenig_p=*/false,
5305                                         tf_warning_or_error);
5306               }
5307             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5308                      || TREE_CODE (postfix_expression) == MEMBER_REF
5309                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5310               postfix_expression = (build_offset_ref_call_from_tree
5311                                     (postfix_expression, &args));
5312             else if (idk == CP_ID_KIND_QUALIFIED)
5313               /* A call to a static class member, or a namespace-scope
5314                  function.  */
5315               postfix_expression
5316                 = finish_call_expr (postfix_expression, &args,
5317                                     /*disallow_virtual=*/true,
5318                                     koenig_p,
5319                                     tf_warning_or_error);
5320             else
5321               /* All other function calls.  */
5322               postfix_expression
5323                 = finish_call_expr (postfix_expression, &args,
5324                                     /*disallow_virtual=*/false,
5325                                     koenig_p,
5326                                     tf_warning_or_error);
5327
5328             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5329             idk = CP_ID_KIND_NONE;
5330
5331             release_tree_vector (args);
5332           }
5333           break;
5334
5335         case CPP_DOT:
5336         case CPP_DEREF:
5337           /* postfix-expression . template [opt] id-expression
5338              postfix-expression . pseudo-destructor-name
5339              postfix-expression -> template [opt] id-expression
5340              postfix-expression -> pseudo-destructor-name */
5341
5342           /* Consume the `.' or `->' operator.  */
5343           cp_lexer_consume_token (parser->lexer);
5344
5345           postfix_expression
5346             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5347                                                       postfix_expression,
5348                                                       false, &idk,
5349                                                       token->location);
5350
5351           is_member_access = true;
5352           break;
5353
5354         case CPP_PLUS_PLUS:
5355           /* postfix-expression ++  */
5356           /* Consume the `++' token.  */
5357           cp_lexer_consume_token (parser->lexer);
5358           /* Generate a representation for the complete expression.  */
5359           postfix_expression
5360             = finish_increment_expr (postfix_expression,
5361                                      POSTINCREMENT_EXPR);
5362           /* Increments may not appear in constant-expressions.  */
5363           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5364             postfix_expression = error_mark_node;
5365           idk = CP_ID_KIND_NONE;
5366           is_member_access = false;
5367           break;
5368
5369         case CPP_MINUS_MINUS:
5370           /* postfix-expression -- */
5371           /* Consume the `--' token.  */
5372           cp_lexer_consume_token (parser->lexer);
5373           /* Generate a representation for the complete expression.  */
5374           postfix_expression
5375             = finish_increment_expr (postfix_expression,
5376                                      POSTDECREMENT_EXPR);
5377           /* Decrements may not appear in constant-expressions.  */
5378           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5379             postfix_expression = error_mark_node;
5380           idk = CP_ID_KIND_NONE;
5381           is_member_access = false;
5382           break;
5383
5384         default:
5385           if (pidk_return != NULL)
5386             * pidk_return = idk;
5387           if (member_access_only_p)
5388             return is_member_access? postfix_expression : error_mark_node;
5389           else
5390             return postfix_expression;
5391         }
5392     }
5393
5394   /* We should never get here.  */
5395   gcc_unreachable ();
5396   return error_mark_node;
5397 }
5398
5399 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5400    by cp_parser_builtin_offsetof.  We're looking for
5401
5402      postfix-expression [ expression ]
5403
5404    FOR_OFFSETOF is set if we're being called in that context, which
5405    changes how we deal with integer constant expressions.  */
5406
5407 static tree
5408 cp_parser_postfix_open_square_expression (cp_parser *parser,
5409                                           tree postfix_expression,
5410                                           bool for_offsetof)
5411 {
5412   tree index;
5413
5414   /* Consume the `[' token.  */
5415   cp_lexer_consume_token (parser->lexer);
5416
5417   /* Parse the index expression.  */
5418   /* ??? For offsetof, there is a question of what to allow here.  If
5419      offsetof is not being used in an integral constant expression context,
5420      then we *could* get the right answer by computing the value at runtime.
5421      If we are in an integral constant expression context, then we might
5422      could accept any constant expression; hard to say without analysis.
5423      Rather than open the barn door too wide right away, allow only integer
5424      constant expressions here.  */
5425   if (for_offsetof)
5426     index = cp_parser_constant_expression (parser, false, NULL);
5427   else
5428     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5429
5430   /* Look for the closing `]'.  */
5431   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5432
5433   /* Build the ARRAY_REF.  */
5434   postfix_expression = grok_array_decl (postfix_expression, index);
5435
5436   /* When not doing offsetof, array references are not permitted in
5437      constant-expressions.  */
5438   if (!for_offsetof
5439       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5440     postfix_expression = error_mark_node;
5441
5442   return postfix_expression;
5443 }
5444
5445 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5446    by cp_parser_builtin_offsetof.  We're looking for
5447
5448      postfix-expression . template [opt] id-expression
5449      postfix-expression . pseudo-destructor-name
5450      postfix-expression -> template [opt] id-expression
5451      postfix-expression -> pseudo-destructor-name
5452
5453    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5454    limits what of the above we'll actually accept, but nevermind.
5455    TOKEN_TYPE is the "." or "->" token, which will already have been
5456    removed from the stream.  */
5457
5458 static tree
5459 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5460                                         enum cpp_ttype token_type,
5461                                         tree postfix_expression,
5462                                         bool for_offsetof, cp_id_kind *idk,
5463                                         location_t location)
5464 {
5465   tree name;
5466   bool dependent_p;
5467   bool pseudo_destructor_p;
5468   tree scope = NULL_TREE;
5469
5470   /* If this is a `->' operator, dereference the pointer.  */
5471   if (token_type == CPP_DEREF)
5472     postfix_expression = build_x_arrow (postfix_expression);
5473   /* Check to see whether or not the expression is type-dependent.  */
5474   dependent_p = type_dependent_expression_p (postfix_expression);
5475   /* The identifier following the `->' or `.' is not qualified.  */
5476   parser->scope = NULL_TREE;
5477   parser->qualifying_scope = NULL_TREE;
5478   parser->object_scope = NULL_TREE;
5479   *idk = CP_ID_KIND_NONE;
5480
5481   /* Enter the scope corresponding to the type of the object
5482      given by the POSTFIX_EXPRESSION.  */
5483   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5484     {
5485       scope = TREE_TYPE (postfix_expression);
5486       /* According to the standard, no expression should ever have
5487          reference type.  Unfortunately, we do not currently match
5488          the standard in this respect in that our internal representation
5489          of an expression may have reference type even when the standard
5490          says it does not.  Therefore, we have to manually obtain the
5491          underlying type here.  */
5492       scope = non_reference (scope);
5493       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5494       if (scope == unknown_type_node)
5495         {
5496           error_at (location, "%qE does not have class type",
5497                     postfix_expression);
5498           scope = NULL_TREE;
5499         }
5500       else
5501         scope = complete_type_or_else (scope, NULL_TREE);
5502       /* Let the name lookup machinery know that we are processing a
5503          class member access expression.  */
5504       parser->context->object_type = scope;
5505       /* If something went wrong, we want to be able to discern that case,
5506          as opposed to the case where there was no SCOPE due to the type
5507          of expression being dependent.  */
5508       if (!scope)
5509         scope = error_mark_node;
5510       /* If the SCOPE was erroneous, make the various semantic analysis
5511          functions exit quickly -- and without issuing additional error
5512          messages.  */
5513       if (scope == error_mark_node)
5514         postfix_expression = error_mark_node;
5515     }
5516
5517   /* Assume this expression is not a pseudo-destructor access.  */
5518   pseudo_destructor_p = false;
5519
5520   /* If the SCOPE is a scalar type, then, if this is a valid program,
5521      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5522      is type dependent, it can be pseudo-destructor-name or something else.
5523      Try to parse it as pseudo-destructor-name first.  */
5524   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5525     {
5526       tree s;
5527       tree type;
5528
5529       cp_parser_parse_tentatively (parser);
5530       /* Parse the pseudo-destructor-name.  */
5531       s = NULL_TREE;
5532       cp_parser_pseudo_destructor_name (parser, &s, &type);
5533       if (dependent_p
5534           && (cp_parser_error_occurred (parser)
5535               || TREE_CODE (type) != TYPE_DECL
5536               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5537         cp_parser_abort_tentative_parse (parser);
5538       else if (cp_parser_parse_definitely (parser))
5539         {
5540           pseudo_destructor_p = true;
5541           postfix_expression
5542             = finish_pseudo_destructor_expr (postfix_expression,
5543                                              s, TREE_TYPE (type));
5544         }
5545     }
5546
5547   if (!pseudo_destructor_p)
5548     {
5549       /* If the SCOPE is not a scalar type, we are looking at an
5550          ordinary class member access expression, rather than a
5551          pseudo-destructor-name.  */
5552       bool template_p;
5553       cp_token *token = cp_lexer_peek_token (parser->lexer);
5554       /* Parse the id-expression.  */
5555       name = (cp_parser_id_expression
5556               (parser,
5557                cp_parser_optional_template_keyword (parser),
5558                /*check_dependency_p=*/true,
5559                &template_p,
5560                /*declarator_p=*/false,
5561                /*optional_p=*/false));
5562       /* In general, build a SCOPE_REF if the member name is qualified.
5563          However, if the name was not dependent and has already been
5564          resolved; there is no need to build the SCOPE_REF.  For example;
5565
5566              struct X { void f(); };
5567              template <typename T> void f(T* t) { t->X::f(); }
5568
5569          Even though "t" is dependent, "X::f" is not and has been resolved
5570          to a BASELINK; there is no need to include scope information.  */
5571
5572       /* But we do need to remember that there was an explicit scope for
5573          virtual function calls.  */
5574       if (parser->scope)
5575         *idk = CP_ID_KIND_QUALIFIED;
5576
5577       /* If the name is a template-id that names a type, we will get a
5578          TYPE_DECL here.  That is invalid code.  */
5579       if (TREE_CODE (name) == TYPE_DECL)
5580         {
5581           error_at (token->location, "invalid use of %qD", name);
5582           postfix_expression = error_mark_node;
5583         }
5584       else
5585         {
5586           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5587             {
5588               name = build_qualified_name (/*type=*/NULL_TREE,
5589                                            parser->scope,
5590                                            name,
5591                                            template_p);
5592               parser->scope = NULL_TREE;
5593               parser->qualifying_scope = NULL_TREE;
5594               parser->object_scope = NULL_TREE;
5595             }
5596           if (scope && name && BASELINK_P (name))
5597             adjust_result_of_qualified_name_lookup
5598               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5599           postfix_expression
5600             = finish_class_member_access_expr (postfix_expression, name,
5601                                                template_p, 
5602                                                tf_warning_or_error);
5603         }
5604     }
5605
5606   /* We no longer need to look up names in the scope of the object on
5607      the left-hand side of the `.' or `->' operator.  */
5608   parser->context->object_type = NULL_TREE;
5609
5610   /* Outside of offsetof, these operators may not appear in
5611      constant-expressions.  */
5612   if (!for_offsetof
5613       && (cp_parser_non_integral_constant_expression
5614           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5615     postfix_expression = error_mark_node;
5616
5617   return postfix_expression;
5618 }
5619
5620 /* Parse a parenthesized expression-list.
5621
5622    expression-list:
5623      assignment-expression
5624      expression-list, assignment-expression
5625
5626    attribute-list:
5627      expression-list
5628      identifier
5629      identifier, expression-list
5630
5631    CAST_P is true if this expression is the target of a cast.
5632
5633    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5634    argument pack.
5635
5636    Returns a vector of trees.  Each element is a representation of an
5637    assignment-expression.  NULL is returned if the ( and or ) are
5638    missing.  An empty, but allocated, vector is returned on no
5639    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5640    if we are parsing an attribute list for an attribute that wants a
5641    plain identifier argument, normal_attr for an attribute that wants
5642    an expression, or non_attr if we aren't parsing an attribute list.  If
5643    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5644    not all of the expressions in the list were constant.  */
5645
5646 static VEC(tree,gc) *
5647 cp_parser_parenthesized_expression_list (cp_parser* parser,
5648                                          int is_attribute_list,
5649                                          bool cast_p,
5650                                          bool allow_expansion_p,
5651                                          bool *non_constant_p)
5652 {
5653   VEC(tree,gc) *expression_list;
5654   bool fold_expr_p = is_attribute_list != non_attr;
5655   tree identifier = NULL_TREE;
5656   bool saved_greater_than_is_operator_p;
5657
5658   /* Assume all the expressions will be constant.  */
5659   if (non_constant_p)
5660     *non_constant_p = false;
5661
5662   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5663     return NULL;
5664
5665   expression_list = make_tree_vector ();
5666
5667   /* Within a parenthesized expression, a `>' token is always
5668      the greater-than operator.  */
5669   saved_greater_than_is_operator_p
5670     = parser->greater_than_is_operator_p;
5671   parser->greater_than_is_operator_p = true;
5672
5673   /* Consume expressions until there are no more.  */
5674   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5675     while (true)
5676       {
5677         tree expr;
5678
5679         /* At the beginning of attribute lists, check to see if the
5680            next token is an identifier.  */
5681         if (is_attribute_list == id_attr
5682             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5683           {
5684             cp_token *token;
5685
5686             /* Consume the identifier.  */
5687             token = cp_lexer_consume_token (parser->lexer);
5688             /* Save the identifier.  */
5689             identifier = token->u.value;
5690           }
5691         else
5692           {
5693             bool expr_non_constant_p;
5694
5695             /* Parse the next assignment-expression.  */
5696             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5697               {
5698                 /* A braced-init-list.  */
5699                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5700                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5701                 if (non_constant_p && expr_non_constant_p)
5702                   *non_constant_p = true;
5703               }
5704             else if (non_constant_p)
5705               {
5706                 expr = (cp_parser_constant_expression
5707                         (parser, /*allow_non_constant_p=*/true,
5708                          &expr_non_constant_p));
5709                 if (expr_non_constant_p)
5710                   *non_constant_p = true;
5711               }
5712             else
5713               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5714
5715             if (fold_expr_p)
5716               expr = fold_non_dependent_expr (expr);
5717
5718             /* If we have an ellipsis, then this is an expression
5719                expansion.  */
5720             if (allow_expansion_p
5721                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5722               {
5723                 /* Consume the `...'.  */
5724                 cp_lexer_consume_token (parser->lexer);
5725
5726                 /* Build the argument pack.  */
5727                 expr = make_pack_expansion (expr);
5728               }
5729
5730              /* Add it to the list.  We add error_mark_node
5731                 expressions to the list, so that we can still tell if
5732                 the correct form for a parenthesized expression-list
5733                 is found. That gives better errors.  */
5734             VEC_safe_push (tree, gc, expression_list, expr);
5735
5736             if (expr == error_mark_node)
5737               goto skip_comma;
5738           }
5739
5740         /* After the first item, attribute lists look the same as
5741            expression lists.  */
5742         is_attribute_list = non_attr;
5743
5744       get_comma:;
5745         /* If the next token isn't a `,', then we are done.  */
5746         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5747           break;
5748
5749         /* Otherwise, consume the `,' and keep going.  */
5750         cp_lexer_consume_token (parser->lexer);
5751       }
5752
5753   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5754     {
5755       int ending;
5756
5757     skip_comma:;
5758       /* We try and resync to an unnested comma, as that will give the
5759          user better diagnostics.  */
5760       ending = cp_parser_skip_to_closing_parenthesis (parser,
5761                                                       /*recovering=*/true,
5762                                                       /*or_comma=*/true,
5763                                                       /*consume_paren=*/true);
5764       if (ending < 0)
5765         goto get_comma;
5766       if (!ending)
5767         {
5768           parser->greater_than_is_operator_p
5769             = saved_greater_than_is_operator_p;
5770           return NULL;
5771         }
5772     }
5773
5774   parser->greater_than_is_operator_p
5775     = saved_greater_than_is_operator_p;
5776
5777   if (identifier)
5778     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5779
5780   return expression_list;
5781 }
5782
5783 /* Parse a pseudo-destructor-name.
5784
5785    pseudo-destructor-name:
5786      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5787      :: [opt] nested-name-specifier template template-id :: ~ type-name
5788      :: [opt] nested-name-specifier [opt] ~ type-name
5789
5790    If either of the first two productions is used, sets *SCOPE to the
5791    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5792    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5793    or ERROR_MARK_NODE if the parse fails.  */
5794
5795 static void
5796 cp_parser_pseudo_destructor_name (cp_parser* parser,
5797                                   tree* scope,
5798                                   tree* type)
5799 {
5800   bool nested_name_specifier_p;
5801
5802   /* Assume that things will not work out.  */
5803   *type = error_mark_node;
5804
5805   /* Look for the optional `::' operator.  */
5806   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5807   /* Look for the optional nested-name-specifier.  */
5808   nested_name_specifier_p
5809     = (cp_parser_nested_name_specifier_opt (parser,
5810                                             /*typename_keyword_p=*/false,
5811                                             /*check_dependency_p=*/true,
5812                                             /*type_p=*/false,
5813                                             /*is_declaration=*/false)
5814        != NULL_TREE);
5815   /* Now, if we saw a nested-name-specifier, we might be doing the
5816      second production.  */
5817   if (nested_name_specifier_p
5818       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5819     {
5820       /* Consume the `template' keyword.  */
5821       cp_lexer_consume_token (parser->lexer);
5822       /* Parse the template-id.  */
5823       cp_parser_template_id (parser,
5824                              /*template_keyword_p=*/true,
5825                              /*check_dependency_p=*/false,
5826                              /*is_declaration=*/true);
5827       /* Look for the `::' token.  */
5828       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5829     }
5830   /* If the next token is not a `~', then there might be some
5831      additional qualification.  */
5832   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5833     {
5834       /* At this point, we're looking for "type-name :: ~".  The type-name
5835          must not be a class-name, since this is a pseudo-destructor.  So,
5836          it must be either an enum-name, or a typedef-name -- both of which
5837          are just identifiers.  So, we peek ahead to check that the "::"
5838          and "~" tokens are present; if they are not, then we can avoid
5839          calling type_name.  */
5840       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5841           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5842           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5843         {
5844           cp_parser_error (parser, "non-scalar type");
5845           return;
5846         }
5847
5848       /* Look for the type-name.  */
5849       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5850       if (*scope == error_mark_node)
5851         return;
5852
5853       /* Look for the `::' token.  */
5854       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5855     }
5856   else
5857     *scope = NULL_TREE;
5858
5859   /* Look for the `~'.  */
5860   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5861   /* Look for the type-name again.  We are not responsible for
5862      checking that it matches the first type-name.  */
5863   *type = cp_parser_nonclass_name (parser);
5864 }
5865
5866 /* Parse a unary-expression.
5867
5868    unary-expression:
5869      postfix-expression
5870      ++ cast-expression
5871      -- cast-expression
5872      unary-operator cast-expression
5873      sizeof unary-expression
5874      sizeof ( type-id )
5875      new-expression
5876      delete-expression
5877
5878    GNU Extensions:
5879
5880    unary-expression:
5881      __extension__ cast-expression
5882      __alignof__ unary-expression
5883      __alignof__ ( type-id )
5884      __real__ cast-expression
5885      __imag__ cast-expression
5886      && identifier
5887
5888    ADDRESS_P is true iff the unary-expression is appearing as the
5889    operand of the `&' operator.   CAST_P is true if this expression is
5890    the target of a cast.
5891
5892    Returns a representation of the expression.  */
5893
5894 static tree
5895 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5896                             cp_id_kind * pidk)
5897 {
5898   cp_token *token;
5899   enum tree_code unary_operator;
5900
5901   /* Peek at the next token.  */
5902   token = cp_lexer_peek_token (parser->lexer);
5903   /* Some keywords give away the kind of expression.  */
5904   if (token->type == CPP_KEYWORD)
5905     {
5906       enum rid keyword = token->keyword;
5907
5908       switch (keyword)
5909         {
5910         case RID_ALIGNOF:
5911         case RID_SIZEOF:
5912           {
5913             tree operand;
5914             enum tree_code op;
5915
5916             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5917             /* Consume the token.  */
5918             cp_lexer_consume_token (parser->lexer);
5919             /* Parse the operand.  */
5920             operand = cp_parser_sizeof_operand (parser, keyword);
5921
5922             if (TYPE_P (operand))
5923               return cxx_sizeof_or_alignof_type (operand, op, true);
5924             else
5925               return cxx_sizeof_or_alignof_expr (operand, op, true);
5926           }
5927
5928         case RID_NEW:
5929           return cp_parser_new_expression (parser);
5930
5931         case RID_DELETE:
5932           return cp_parser_delete_expression (parser);
5933
5934         case RID_EXTENSION:
5935           {
5936             /* The saved value of the PEDANTIC flag.  */
5937             int saved_pedantic;
5938             tree expr;
5939
5940             /* Save away the PEDANTIC flag.  */
5941             cp_parser_extension_opt (parser, &saved_pedantic);
5942             /* Parse the cast-expression.  */
5943             expr = cp_parser_simple_cast_expression (parser);
5944             /* Restore the PEDANTIC flag.  */
5945             pedantic = saved_pedantic;
5946
5947             return expr;
5948           }
5949
5950         case RID_REALPART:
5951         case RID_IMAGPART:
5952           {
5953             tree expression;
5954
5955             /* Consume the `__real__' or `__imag__' token.  */
5956             cp_lexer_consume_token (parser->lexer);
5957             /* Parse the cast-expression.  */
5958             expression = cp_parser_simple_cast_expression (parser);
5959             /* Create the complete representation.  */
5960             return build_x_unary_op ((keyword == RID_REALPART
5961                                       ? REALPART_EXPR : IMAGPART_EXPR),
5962                                      expression,
5963                                      tf_warning_or_error);
5964           }
5965           break;
5966
5967         case RID_NOEXCEPT:
5968           {
5969             tree expr;
5970             const char *saved_message;
5971             bool saved_integral_constant_expression_p;
5972             bool saved_non_integral_constant_expression_p;
5973             bool saved_greater_than_is_operator_p;
5974
5975             cp_lexer_consume_token (parser->lexer);
5976             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5977
5978             saved_message = parser->type_definition_forbidden_message;
5979             parser->type_definition_forbidden_message
5980               = G_("types may not be defined in %<noexcept%> expressions");
5981
5982             saved_integral_constant_expression_p
5983               = parser->integral_constant_expression_p;
5984             saved_non_integral_constant_expression_p
5985               = parser->non_integral_constant_expression_p;
5986             parser->integral_constant_expression_p = false;
5987
5988             saved_greater_than_is_operator_p
5989               = parser->greater_than_is_operator_p;
5990             parser->greater_than_is_operator_p = true;
5991
5992             ++cp_unevaluated_operand;
5993             ++c_inhibit_evaluation_warnings;
5994             expr = cp_parser_expression (parser, false, NULL);
5995             --c_inhibit_evaluation_warnings;
5996             --cp_unevaluated_operand;
5997
5998             parser->greater_than_is_operator_p
5999               = saved_greater_than_is_operator_p;
6000
6001             parser->integral_constant_expression_p
6002               = saved_integral_constant_expression_p;
6003             parser->non_integral_constant_expression_p
6004               = saved_non_integral_constant_expression_p;
6005
6006             parser->type_definition_forbidden_message = saved_message;
6007
6008             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6009             return finish_noexcept_expr (expr, tf_warning_or_error);
6010           }
6011
6012         default:
6013           break;
6014         }
6015     }
6016
6017   /* Look for the `:: new' and `:: delete', which also signal the
6018      beginning of a new-expression, or delete-expression,
6019      respectively.  If the next token is `::', then it might be one of
6020      these.  */
6021   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6022     {
6023       enum rid keyword;
6024
6025       /* See if the token after the `::' is one of the keywords in
6026          which we're interested.  */
6027       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6028       /* If it's `new', we have a new-expression.  */
6029       if (keyword == RID_NEW)
6030         return cp_parser_new_expression (parser);
6031       /* Similarly, for `delete'.  */
6032       else if (keyword == RID_DELETE)
6033         return cp_parser_delete_expression (parser);
6034     }
6035
6036   /* Look for a unary operator.  */
6037   unary_operator = cp_parser_unary_operator (token);
6038   /* The `++' and `--' operators can be handled similarly, even though
6039      they are not technically unary-operators in the grammar.  */
6040   if (unary_operator == ERROR_MARK)
6041     {
6042       if (token->type == CPP_PLUS_PLUS)
6043         unary_operator = PREINCREMENT_EXPR;
6044       else if (token->type == CPP_MINUS_MINUS)
6045         unary_operator = PREDECREMENT_EXPR;
6046       /* Handle the GNU address-of-label extension.  */
6047       else if (cp_parser_allow_gnu_extensions_p (parser)
6048                && token->type == CPP_AND_AND)
6049         {
6050           tree identifier;
6051           tree expression;
6052           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6053
6054           /* Consume the '&&' token.  */
6055           cp_lexer_consume_token (parser->lexer);
6056           /* Look for the identifier.  */
6057           identifier = cp_parser_identifier (parser);
6058           /* Create an expression representing the address.  */
6059           expression = finish_label_address_expr (identifier, loc);
6060           if (cp_parser_non_integral_constant_expression (parser,
6061                                                           NIC_ADDR_LABEL))
6062             expression = error_mark_node;
6063           return expression;
6064         }
6065     }
6066   if (unary_operator != ERROR_MARK)
6067     {
6068       tree cast_expression;
6069       tree expression = error_mark_node;
6070       non_integral_constant non_constant_p = NIC_NONE;
6071
6072       /* Consume the operator token.  */
6073       token = cp_lexer_consume_token (parser->lexer);
6074       /* Parse the cast-expression.  */
6075       cast_expression
6076         = cp_parser_cast_expression (parser,
6077                                      unary_operator == ADDR_EXPR,
6078                                      /*cast_p=*/false, pidk);
6079       /* Now, build an appropriate representation.  */
6080       switch (unary_operator)
6081         {
6082         case INDIRECT_REF:
6083           non_constant_p = NIC_STAR;
6084           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6085                                              tf_warning_or_error);
6086           break;
6087
6088         case ADDR_EXPR:
6089            non_constant_p = NIC_ADDR;
6090           /* Fall through.  */
6091         case BIT_NOT_EXPR:
6092           expression = build_x_unary_op (unary_operator, cast_expression,
6093                                          tf_warning_or_error);
6094           break;
6095
6096         case PREINCREMENT_EXPR:
6097         case PREDECREMENT_EXPR:
6098           non_constant_p = unary_operator == PREINCREMENT_EXPR
6099                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6100           /* Fall through.  */
6101         case UNARY_PLUS_EXPR:
6102         case NEGATE_EXPR:
6103         case TRUTH_NOT_EXPR:
6104           expression = finish_unary_op_expr (unary_operator, cast_expression);
6105           break;
6106
6107         default:
6108           gcc_unreachable ();
6109         }
6110
6111       if (non_constant_p != NIC_NONE
6112           && cp_parser_non_integral_constant_expression (parser,
6113                                                          non_constant_p))
6114         expression = error_mark_node;
6115
6116       return expression;
6117     }
6118
6119   return cp_parser_postfix_expression (parser, address_p, cast_p,
6120                                        /*member_access_only_p=*/false,
6121                                        pidk);
6122 }
6123
6124 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6125    unary-operator, the corresponding tree code is returned.  */
6126
6127 static enum tree_code
6128 cp_parser_unary_operator (cp_token* token)
6129 {
6130   switch (token->type)
6131     {
6132     case CPP_MULT:
6133       return INDIRECT_REF;
6134
6135     case CPP_AND:
6136       return ADDR_EXPR;
6137
6138     case CPP_PLUS:
6139       return UNARY_PLUS_EXPR;
6140
6141     case CPP_MINUS:
6142       return NEGATE_EXPR;
6143
6144     case CPP_NOT:
6145       return TRUTH_NOT_EXPR;
6146
6147     case CPP_COMPL:
6148       return BIT_NOT_EXPR;
6149
6150     default:
6151       return ERROR_MARK;
6152     }
6153 }
6154
6155 /* Parse a new-expression.
6156
6157    new-expression:
6158      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6159      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6160
6161    Returns a representation of the expression.  */
6162
6163 static tree
6164 cp_parser_new_expression (cp_parser* parser)
6165 {
6166   bool global_scope_p;
6167   VEC(tree,gc) *placement;
6168   tree type;
6169   VEC(tree,gc) *initializer;
6170   tree nelts;
6171   tree ret;
6172
6173   /* Look for the optional `::' operator.  */
6174   global_scope_p
6175     = (cp_parser_global_scope_opt (parser,
6176                                    /*current_scope_valid_p=*/false)
6177        != NULL_TREE);
6178   /* Look for the `new' operator.  */
6179   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6180   /* There's no easy way to tell a new-placement from the
6181      `( type-id )' construct.  */
6182   cp_parser_parse_tentatively (parser);
6183   /* Look for a new-placement.  */
6184   placement = cp_parser_new_placement (parser);
6185   /* If that didn't work out, there's no new-placement.  */
6186   if (!cp_parser_parse_definitely (parser))
6187     {
6188       if (placement != NULL)
6189         release_tree_vector (placement);
6190       placement = NULL;
6191     }
6192
6193   /* If the next token is a `(', then we have a parenthesized
6194      type-id.  */
6195   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6196     {
6197       cp_token *token;
6198       /* Consume the `('.  */
6199       cp_lexer_consume_token (parser->lexer);
6200       /* Parse the type-id.  */
6201       type = cp_parser_type_id (parser);
6202       /* Look for the closing `)'.  */
6203       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6204       token = cp_lexer_peek_token (parser->lexer);
6205       /* There should not be a direct-new-declarator in this production,
6206          but GCC used to allowed this, so we check and emit a sensible error
6207          message for this case.  */
6208       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6209         {
6210           error_at (token->location,
6211                     "array bound forbidden after parenthesized type-id");
6212           inform (token->location, 
6213                   "try removing the parentheses around the type-id");
6214           cp_parser_direct_new_declarator (parser);
6215         }
6216       nelts = NULL_TREE;
6217     }
6218   /* Otherwise, there must be a new-type-id.  */
6219   else
6220     type = cp_parser_new_type_id (parser, &nelts);
6221
6222   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6224       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6225     initializer = cp_parser_new_initializer (parser);
6226   else
6227     initializer = NULL;
6228
6229   /* A new-expression may not appear in an integral constant
6230      expression.  */
6231   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6232     ret = error_mark_node;
6233   else
6234     {
6235       /* Create a representation of the new-expression.  */
6236       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6237                        tf_warning_or_error);
6238     }
6239
6240   if (placement != NULL)
6241     release_tree_vector (placement);
6242   if (initializer != NULL)
6243     release_tree_vector (initializer);
6244
6245   return ret;
6246 }
6247
6248 /* Parse a new-placement.
6249
6250    new-placement:
6251      ( expression-list )
6252
6253    Returns the same representation as for an expression-list.  */
6254
6255 static VEC(tree,gc) *
6256 cp_parser_new_placement (cp_parser* parser)
6257 {
6258   VEC(tree,gc) *expression_list;
6259
6260   /* Parse the expression-list.  */
6261   expression_list = (cp_parser_parenthesized_expression_list
6262                      (parser, non_attr, /*cast_p=*/false,
6263                       /*allow_expansion_p=*/true,
6264                       /*non_constant_p=*/NULL));
6265
6266   return expression_list;
6267 }
6268
6269 /* Parse a new-type-id.
6270
6271    new-type-id:
6272      type-specifier-seq new-declarator [opt]
6273
6274    Returns the TYPE allocated.  If the new-type-id indicates an array
6275    type, *NELTS is set to the number of elements in the last array
6276    bound; the TYPE will not include the last array bound.  */
6277
6278 static tree
6279 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6280 {
6281   cp_decl_specifier_seq type_specifier_seq;
6282   cp_declarator *new_declarator;
6283   cp_declarator *declarator;
6284   cp_declarator *outer_declarator;
6285   const char *saved_message;
6286   tree type;
6287
6288   /* The type-specifier sequence must not contain type definitions.
6289      (It cannot contain declarations of new types either, but if they
6290      are not definitions we will catch that because they are not
6291      complete.)  */
6292   saved_message = parser->type_definition_forbidden_message;
6293   parser->type_definition_forbidden_message
6294     = G_("types may not be defined in a new-type-id");
6295   /* Parse the type-specifier-seq.  */
6296   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6297                                 /*is_trailing_return=*/false,
6298                                 &type_specifier_seq);
6299   /* Restore the old message.  */
6300   parser->type_definition_forbidden_message = saved_message;
6301   /* Parse the new-declarator.  */
6302   new_declarator = cp_parser_new_declarator_opt (parser);
6303
6304   /* Determine the number of elements in the last array dimension, if
6305      any.  */
6306   *nelts = NULL_TREE;
6307   /* Skip down to the last array dimension.  */
6308   declarator = new_declarator;
6309   outer_declarator = NULL;
6310   while (declarator && (declarator->kind == cdk_pointer
6311                         || declarator->kind == cdk_ptrmem))
6312     {
6313       outer_declarator = declarator;
6314       declarator = declarator->declarator;
6315     }
6316   while (declarator
6317          && declarator->kind == cdk_array
6318          && declarator->declarator
6319          && declarator->declarator->kind == cdk_array)
6320     {
6321       outer_declarator = declarator;
6322       declarator = declarator->declarator;
6323     }
6324
6325   if (declarator && declarator->kind == cdk_array)
6326     {
6327       *nelts = declarator->u.array.bounds;
6328       if (*nelts == error_mark_node)
6329         *nelts = integer_one_node;
6330
6331       if (outer_declarator)
6332         outer_declarator->declarator = declarator->declarator;
6333       else
6334         new_declarator = NULL;
6335     }
6336
6337   type = groktypename (&type_specifier_seq, new_declarator, false);
6338   return type;
6339 }
6340
6341 /* Parse an (optional) new-declarator.
6342
6343    new-declarator:
6344      ptr-operator new-declarator [opt]
6345      direct-new-declarator
6346
6347    Returns the declarator.  */
6348
6349 static cp_declarator *
6350 cp_parser_new_declarator_opt (cp_parser* parser)
6351 {
6352   enum tree_code code;
6353   tree type;
6354   cp_cv_quals cv_quals;
6355
6356   /* We don't know if there's a ptr-operator next, or not.  */
6357   cp_parser_parse_tentatively (parser);
6358   /* Look for a ptr-operator.  */
6359   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6360   /* If that worked, look for more new-declarators.  */
6361   if (cp_parser_parse_definitely (parser))
6362     {
6363       cp_declarator *declarator;
6364
6365       /* Parse another optional declarator.  */
6366       declarator = cp_parser_new_declarator_opt (parser);
6367
6368       return cp_parser_make_indirect_declarator
6369         (code, type, cv_quals, declarator);
6370     }
6371
6372   /* If the next token is a `[', there is a direct-new-declarator.  */
6373   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6374     return cp_parser_direct_new_declarator (parser);
6375
6376   return NULL;
6377 }
6378
6379 /* Parse a direct-new-declarator.
6380
6381    direct-new-declarator:
6382      [ expression ]
6383      direct-new-declarator [constant-expression]
6384
6385    */
6386
6387 static cp_declarator *
6388 cp_parser_direct_new_declarator (cp_parser* parser)
6389 {
6390   cp_declarator *declarator = NULL;
6391
6392   while (true)
6393     {
6394       tree expression;
6395
6396       /* Look for the opening `['.  */
6397       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6398       /* The first expression is not required to be constant.  */
6399       if (!declarator)
6400         {
6401           cp_token *token = cp_lexer_peek_token (parser->lexer);
6402           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6403           /* The standard requires that the expression have integral
6404              type.  DR 74 adds enumeration types.  We believe that the
6405              real intent is that these expressions be handled like the
6406              expression in a `switch' condition, which also allows
6407              classes with a single conversion to integral or
6408              enumeration type.  */
6409           if (!processing_template_decl)
6410             {
6411               expression
6412                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6413                                               expression,
6414                                               /*complain=*/true);
6415               if (!expression)
6416                 {
6417                   error_at (token->location,
6418                             "expression in new-declarator must have integral "
6419                             "or enumeration type");
6420                   expression = error_mark_node;
6421                 }
6422             }
6423         }
6424       /* But all the other expressions must be.  */
6425       else
6426         expression
6427           = cp_parser_constant_expression (parser,
6428                                            /*allow_non_constant=*/false,
6429                                            NULL);
6430       /* Look for the closing `]'.  */
6431       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6432
6433       /* Add this bound to the declarator.  */
6434       declarator = make_array_declarator (declarator, expression);
6435
6436       /* If the next token is not a `[', then there are no more
6437          bounds.  */
6438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6439         break;
6440     }
6441
6442   return declarator;
6443 }
6444
6445 /* Parse a new-initializer.
6446
6447    new-initializer:
6448      ( expression-list [opt] )
6449      braced-init-list
6450
6451    Returns a representation of the expression-list.  */
6452
6453 static VEC(tree,gc) *
6454 cp_parser_new_initializer (cp_parser* parser)
6455 {
6456   VEC(tree,gc) *expression_list;
6457
6458   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6459     {
6460       tree t;
6461       bool expr_non_constant_p;
6462       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6463       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6464       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6465       expression_list = make_tree_vector_single (t);
6466     }
6467   else
6468     expression_list = (cp_parser_parenthesized_expression_list
6469                        (parser, non_attr, /*cast_p=*/false,
6470                         /*allow_expansion_p=*/true,
6471                         /*non_constant_p=*/NULL));
6472
6473   return expression_list;
6474 }
6475
6476 /* Parse a delete-expression.
6477
6478    delete-expression:
6479      :: [opt] delete cast-expression
6480      :: [opt] delete [ ] cast-expression
6481
6482    Returns a representation of the expression.  */
6483
6484 static tree
6485 cp_parser_delete_expression (cp_parser* parser)
6486 {
6487   bool global_scope_p;
6488   bool array_p;
6489   tree expression;
6490
6491   /* Look for the optional `::' operator.  */
6492   global_scope_p
6493     = (cp_parser_global_scope_opt (parser,
6494                                    /*current_scope_valid_p=*/false)
6495        != NULL_TREE);
6496   /* Look for the `delete' keyword.  */
6497   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6498   /* See if the array syntax is in use.  */
6499   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6500     {
6501       /* Consume the `[' token.  */
6502       cp_lexer_consume_token (parser->lexer);
6503       /* Look for the `]' token.  */
6504       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6505       /* Remember that this is the `[]' construct.  */
6506       array_p = true;
6507     }
6508   else
6509     array_p = false;
6510
6511   /* Parse the cast-expression.  */
6512   expression = cp_parser_simple_cast_expression (parser);
6513
6514   /* A delete-expression may not appear in an integral constant
6515      expression.  */
6516   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6517     return error_mark_node;
6518
6519   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6520 }
6521
6522 /* Returns true if TOKEN may start a cast-expression and false
6523    otherwise.  */
6524
6525 static bool
6526 cp_parser_token_starts_cast_expression (cp_token *token)
6527 {
6528   switch (token->type)
6529     {
6530     case CPP_COMMA:
6531     case CPP_SEMICOLON:
6532     case CPP_QUERY:
6533     case CPP_COLON:
6534     case CPP_CLOSE_SQUARE:
6535     case CPP_CLOSE_PAREN:
6536     case CPP_CLOSE_BRACE:
6537     case CPP_DOT:
6538     case CPP_DOT_STAR:
6539     case CPP_DEREF:
6540     case CPP_DEREF_STAR:
6541     case CPP_DIV:
6542     case CPP_MOD:
6543     case CPP_LSHIFT:
6544     case CPP_RSHIFT:
6545     case CPP_LESS:
6546     case CPP_GREATER:
6547     case CPP_LESS_EQ:
6548     case CPP_GREATER_EQ:
6549     case CPP_EQ_EQ:
6550     case CPP_NOT_EQ:
6551     case CPP_EQ:
6552     case CPP_MULT_EQ:
6553     case CPP_DIV_EQ:
6554     case CPP_MOD_EQ:
6555     case CPP_PLUS_EQ:
6556     case CPP_MINUS_EQ:
6557     case CPP_RSHIFT_EQ:
6558     case CPP_LSHIFT_EQ:
6559     case CPP_AND_EQ:
6560     case CPP_XOR_EQ:
6561     case CPP_OR_EQ:
6562     case CPP_XOR:
6563     case CPP_OR:
6564     case CPP_OR_OR:
6565     case CPP_EOF:
6566       return false;
6567
6568       /* '[' may start a primary-expression in obj-c++.  */
6569     case CPP_OPEN_SQUARE:
6570       return c_dialect_objc ();
6571
6572     default:
6573       return true;
6574     }
6575 }
6576
6577 /* Parse a cast-expression.
6578
6579    cast-expression:
6580      unary-expression
6581      ( type-id ) cast-expression
6582
6583    ADDRESS_P is true iff the unary-expression is appearing as the
6584    operand of the `&' operator.   CAST_P is true if this expression is
6585    the target of a cast.
6586
6587    Returns a representation of the expression.  */
6588
6589 static tree
6590 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6591                            cp_id_kind * pidk)
6592 {
6593   /* If it's a `(', then we might be looking at a cast.  */
6594   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6595     {
6596       tree type = NULL_TREE;
6597       tree expr = NULL_TREE;
6598       bool compound_literal_p;
6599       const char *saved_message;
6600
6601       /* There's no way to know yet whether or not this is a cast.
6602          For example, `(int (3))' is a unary-expression, while `(int)
6603          3' is a cast.  So, we resort to parsing tentatively.  */
6604       cp_parser_parse_tentatively (parser);
6605       /* Types may not be defined in a cast.  */
6606       saved_message = parser->type_definition_forbidden_message;
6607       parser->type_definition_forbidden_message
6608         = G_("types may not be defined in casts");
6609       /* Consume the `('.  */
6610       cp_lexer_consume_token (parser->lexer);
6611       /* A very tricky bit is that `(struct S) { 3 }' is a
6612          compound-literal (which we permit in C++ as an extension).
6613          But, that construct is not a cast-expression -- it is a
6614          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6615          is legal; if the compound-literal were a cast-expression,
6616          you'd need an extra set of parentheses.)  But, if we parse
6617          the type-id, and it happens to be a class-specifier, then we
6618          will commit to the parse at that point, because we cannot
6619          undo the action that is done when creating a new class.  So,
6620          then we cannot back up and do a postfix-expression.
6621
6622          Therefore, we scan ahead to the closing `)', and check to see
6623          if the token after the `)' is a `{'.  If so, we are not
6624          looking at a cast-expression.
6625
6626          Save tokens so that we can put them back.  */
6627       cp_lexer_save_tokens (parser->lexer);
6628       /* Skip tokens until the next token is a closing parenthesis.
6629          If we find the closing `)', and the next token is a `{', then
6630          we are looking at a compound-literal.  */
6631       compound_literal_p
6632         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6633                                                   /*consume_paren=*/true)
6634            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6635       /* Roll back the tokens we skipped.  */
6636       cp_lexer_rollback_tokens (parser->lexer);
6637       /* If we were looking at a compound-literal, simulate an error
6638          so that the call to cp_parser_parse_definitely below will
6639          fail.  */
6640       if (compound_literal_p)
6641         cp_parser_simulate_error (parser);
6642       else
6643         {
6644           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6645           parser->in_type_id_in_expr_p = true;
6646           /* Look for the type-id.  */
6647           type = cp_parser_type_id (parser);
6648           /* Look for the closing `)'.  */
6649           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6650           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6651         }
6652
6653       /* Restore the saved message.  */
6654       parser->type_definition_forbidden_message = saved_message;
6655
6656       /* At this point this can only be either a cast or a
6657          parenthesized ctor such as `(T ())' that looks like a cast to
6658          function returning T.  */
6659       if (!cp_parser_error_occurred (parser)
6660           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6661                                                      (parser->lexer)))
6662         {
6663           cp_parser_parse_definitely (parser);
6664           expr = cp_parser_cast_expression (parser,
6665                                             /*address_p=*/false,
6666                                             /*cast_p=*/true, pidk);
6667
6668           /* Warn about old-style casts, if so requested.  */
6669           if (warn_old_style_cast
6670               && !in_system_header
6671               && !VOID_TYPE_P (type)
6672               && current_lang_name != lang_name_c)
6673             warning (OPT_Wold_style_cast, "use of old-style cast");
6674
6675           /* Only type conversions to integral or enumeration types
6676              can be used in constant-expressions.  */
6677           if (!cast_valid_in_integral_constant_expression_p (type)
6678               && cp_parser_non_integral_constant_expression (parser,
6679                                                              NIC_CAST))
6680             return error_mark_node;
6681
6682           /* Perform the cast.  */
6683           expr = build_c_cast (input_location, type, expr);
6684           return expr;
6685         }
6686       else 
6687         cp_parser_abort_tentative_parse (parser);
6688     }
6689
6690   /* If we get here, then it's not a cast, so it must be a
6691      unary-expression.  */
6692   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6693 }
6694
6695 /* Parse a binary expression of the general form:
6696
6697    pm-expression:
6698      cast-expression
6699      pm-expression .* cast-expression
6700      pm-expression ->* cast-expression
6701
6702    multiplicative-expression:
6703      pm-expression
6704      multiplicative-expression * pm-expression
6705      multiplicative-expression / pm-expression
6706      multiplicative-expression % pm-expression
6707
6708    additive-expression:
6709      multiplicative-expression
6710      additive-expression + multiplicative-expression
6711      additive-expression - multiplicative-expression
6712
6713    shift-expression:
6714      additive-expression
6715      shift-expression << additive-expression
6716      shift-expression >> additive-expression
6717
6718    relational-expression:
6719      shift-expression
6720      relational-expression < shift-expression
6721      relational-expression > shift-expression
6722      relational-expression <= shift-expression
6723      relational-expression >= shift-expression
6724
6725   GNU Extension:
6726
6727    relational-expression:
6728      relational-expression <? shift-expression
6729      relational-expression >? shift-expression
6730
6731    equality-expression:
6732      relational-expression
6733      equality-expression == relational-expression
6734      equality-expression != relational-expression
6735
6736    and-expression:
6737      equality-expression
6738      and-expression & equality-expression
6739
6740    exclusive-or-expression:
6741      and-expression
6742      exclusive-or-expression ^ and-expression
6743
6744    inclusive-or-expression:
6745      exclusive-or-expression
6746      inclusive-or-expression | exclusive-or-expression
6747
6748    logical-and-expression:
6749      inclusive-or-expression
6750      logical-and-expression && inclusive-or-expression
6751
6752    logical-or-expression:
6753      logical-and-expression
6754      logical-or-expression || logical-and-expression
6755
6756    All these are implemented with a single function like:
6757
6758    binary-expression:
6759      simple-cast-expression
6760      binary-expression <token> binary-expression
6761
6762    CAST_P is true if this expression is the target of a cast.
6763
6764    The binops_by_token map is used to get the tree codes for each <token> type.
6765    binary-expressions are associated according to a precedence table.  */
6766
6767 #define TOKEN_PRECEDENCE(token)                              \
6768 (((token->type == CPP_GREATER                                \
6769    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6770   && !parser->greater_than_is_operator_p)                    \
6771  ? PREC_NOT_OPERATOR                                         \
6772  : binops_by_token[token->type].prec)
6773
6774 static tree
6775 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6776                              bool no_toplevel_fold_p,
6777                              enum cp_parser_prec prec,
6778                              cp_id_kind * pidk)
6779 {
6780   cp_parser_expression_stack stack;
6781   cp_parser_expression_stack_entry *sp = &stack[0];
6782   tree lhs, rhs;
6783   cp_token *token;
6784   enum tree_code tree_type, lhs_type, rhs_type;
6785   enum cp_parser_prec new_prec, lookahead_prec;
6786   bool overloaded_p;
6787
6788   /* Parse the first expression.  */
6789   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6790   lhs_type = ERROR_MARK;
6791
6792   for (;;)
6793     {
6794       /* Get an operator token.  */
6795       token = cp_lexer_peek_token (parser->lexer);
6796
6797       if (warn_cxx0x_compat
6798           && token->type == CPP_RSHIFT
6799           && !parser->greater_than_is_operator_p)
6800         {
6801           if (warning_at (token->location, OPT_Wc__0x_compat, 
6802                           "%<>>%> operator will be treated as"
6803                           " two right angle brackets in C++0x"))
6804             inform (token->location,
6805                     "suggest parentheses around %<>>%> expression");
6806         }
6807
6808       new_prec = TOKEN_PRECEDENCE (token);
6809
6810       /* Popping an entry off the stack means we completed a subexpression:
6811          - either we found a token which is not an operator (`>' where it is not
6812            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6813            will happen repeatedly;
6814          - or, we found an operator which has lower priority.  This is the case
6815            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6816            parsing `3 * 4'.  */
6817       if (new_prec <= prec)
6818         {
6819           if (sp == stack)
6820             break;
6821           else
6822             goto pop;
6823         }
6824
6825      get_rhs:
6826       tree_type = binops_by_token[token->type].tree_type;
6827
6828       /* We used the operator token.  */
6829       cp_lexer_consume_token (parser->lexer);
6830
6831       /* For "false && x" or "true || x", x will never be executed;
6832          disable warnings while evaluating it.  */
6833       if (tree_type == TRUTH_ANDIF_EXPR)
6834         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6835       else if (tree_type == TRUTH_ORIF_EXPR)
6836         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6837
6838       /* Extract another operand.  It may be the RHS of this expression
6839          or the LHS of a new, higher priority expression.  */
6840       rhs = cp_parser_simple_cast_expression (parser);
6841       rhs_type = ERROR_MARK;
6842
6843       /* Get another operator token.  Look up its precedence to avoid
6844          building a useless (immediately popped) stack entry for common
6845          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6846       token = cp_lexer_peek_token (parser->lexer);
6847       lookahead_prec = TOKEN_PRECEDENCE (token);
6848       if (lookahead_prec > new_prec)
6849         {
6850           /* ... and prepare to parse the RHS of the new, higher priority
6851              expression.  Since precedence levels on the stack are
6852              monotonically increasing, we do not have to care about
6853              stack overflows.  */
6854           sp->prec = prec;
6855           sp->tree_type = tree_type;
6856           sp->lhs = lhs;
6857           sp->lhs_type = lhs_type;
6858           sp++;
6859           lhs = rhs;
6860           lhs_type = rhs_type;
6861           prec = new_prec;
6862           new_prec = lookahead_prec;
6863           goto get_rhs;
6864
6865          pop:
6866           lookahead_prec = new_prec;
6867           /* If the stack is not empty, we have parsed into LHS the right side
6868              (`4' in the example above) of an expression we had suspended.
6869              We can use the information on the stack to recover the LHS (`3')
6870              from the stack together with the tree code (`MULT_EXPR'), and
6871              the precedence of the higher level subexpression
6872              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6873              which will be used to actually build the additive expression.  */
6874           --sp;
6875           prec = sp->prec;
6876           tree_type = sp->tree_type;
6877           rhs = lhs;
6878           rhs_type = lhs_type;
6879           lhs = sp->lhs;
6880           lhs_type = sp->lhs_type;
6881         }
6882
6883       /* Undo the disabling of warnings done above.  */
6884       if (tree_type == TRUTH_ANDIF_EXPR)
6885         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6886       else if (tree_type == TRUTH_ORIF_EXPR)
6887         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6888
6889       overloaded_p = false;
6890       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6891          ERROR_MARK for everything that is not a binary expression.
6892          This makes warn_about_parentheses miss some warnings that
6893          involve unary operators.  For unary expressions we should
6894          pass the correct tree_code unless the unary expression was
6895          surrounded by parentheses.
6896       */
6897       if (no_toplevel_fold_p
6898           && lookahead_prec <= prec
6899           && sp == stack
6900           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6901         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6902       else
6903         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6904                                  &overloaded_p, tf_warning_or_error);
6905       lhs_type = tree_type;
6906
6907       /* If the binary operator required the use of an overloaded operator,
6908          then this expression cannot be an integral constant-expression.
6909          An overloaded operator can be used even if both operands are
6910          otherwise permissible in an integral constant-expression if at
6911          least one of the operands is of enumeration type.  */
6912
6913       if (overloaded_p
6914           && cp_parser_non_integral_constant_expression (parser,
6915                                                          NIC_OVERLOADED))
6916         return error_mark_node;
6917     }
6918
6919   return lhs;
6920 }
6921
6922
6923 /* Parse the `? expression : assignment-expression' part of a
6924    conditional-expression.  The LOGICAL_OR_EXPR is the
6925    logical-or-expression that started the conditional-expression.
6926    Returns a representation of the entire conditional-expression.
6927
6928    This routine is used by cp_parser_assignment_expression.
6929
6930      ? expression : assignment-expression
6931
6932    GNU Extensions:
6933
6934      ? : assignment-expression */
6935
6936 static tree
6937 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6938 {
6939   tree expr;
6940   tree assignment_expr;
6941   struct cp_token *token;
6942
6943   /* Consume the `?' token.  */
6944   cp_lexer_consume_token (parser->lexer);
6945   token = cp_lexer_peek_token (parser->lexer);
6946   if (cp_parser_allow_gnu_extensions_p (parser)
6947       && token->type == CPP_COLON)
6948     {
6949       pedwarn (token->location, OPT_pedantic, 
6950                "ISO C++ does not allow ?: with omitted middle operand");
6951       /* Implicit true clause.  */
6952       expr = NULL_TREE;
6953       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6954       warn_for_omitted_condop (token->location, logical_or_expr);
6955     }
6956   else
6957     {
6958       /* Parse the expression.  */
6959       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6960       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6961       c_inhibit_evaluation_warnings +=
6962         ((logical_or_expr == truthvalue_true_node)
6963          - (logical_or_expr == truthvalue_false_node));
6964     }
6965
6966   /* The next token should be a `:'.  */
6967   cp_parser_require (parser, CPP_COLON, RT_COLON);
6968   /* Parse the assignment-expression.  */
6969   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6970   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6971
6972   /* Build the conditional-expression.  */
6973   return build_x_conditional_expr (logical_or_expr,
6974                                    expr,
6975                                    assignment_expr,
6976                                    tf_warning_or_error);
6977 }
6978
6979 /* Parse an assignment-expression.
6980
6981    assignment-expression:
6982      conditional-expression
6983      logical-or-expression assignment-operator assignment_expression
6984      throw-expression
6985
6986    CAST_P is true if this expression is the target of a cast.
6987
6988    Returns a representation for the expression.  */
6989
6990 static tree
6991 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6992                                  cp_id_kind * pidk)
6993 {
6994   tree expr;
6995
6996   /* If the next token is the `throw' keyword, then we're looking at
6997      a throw-expression.  */
6998   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6999     expr = cp_parser_throw_expression (parser);
7000   /* Otherwise, it must be that we are looking at a
7001      logical-or-expression.  */
7002   else
7003     {
7004       /* Parse the binary expressions (logical-or-expression).  */
7005       expr = cp_parser_binary_expression (parser, cast_p, false,
7006                                           PREC_NOT_OPERATOR, pidk);
7007       /* If the next token is a `?' then we're actually looking at a
7008          conditional-expression.  */
7009       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7010         return cp_parser_question_colon_clause (parser, expr);
7011       else
7012         {
7013           enum tree_code assignment_operator;
7014
7015           /* If it's an assignment-operator, we're using the second
7016              production.  */
7017           assignment_operator
7018             = cp_parser_assignment_operator_opt (parser);
7019           if (assignment_operator != ERROR_MARK)
7020             {
7021               bool non_constant_p;
7022
7023               /* Parse the right-hand side of the assignment.  */
7024               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7025
7026               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7027                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7028
7029               /* An assignment may not appear in a
7030                  constant-expression.  */
7031               if (cp_parser_non_integral_constant_expression (parser,
7032                                                               NIC_ASSIGNMENT))
7033                 return error_mark_node;
7034               /* Build the assignment expression.  */
7035               expr = build_x_modify_expr (expr,
7036                                           assignment_operator,
7037                                           rhs,
7038                                           tf_warning_or_error);
7039             }
7040         }
7041     }
7042
7043   return expr;
7044 }
7045
7046 /* Parse an (optional) assignment-operator.
7047
7048    assignment-operator: one of
7049      = *= /= %= += -= >>= <<= &= ^= |=
7050
7051    GNU Extension:
7052
7053    assignment-operator: one of
7054      <?= >?=
7055
7056    If the next token is an assignment operator, the corresponding tree
7057    code is returned, and the token is consumed.  For example, for
7058    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7059    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7060    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7061    operator, ERROR_MARK is returned.  */
7062
7063 static enum tree_code
7064 cp_parser_assignment_operator_opt (cp_parser* parser)
7065 {
7066   enum tree_code op;
7067   cp_token *token;
7068
7069   /* Peek at the next token.  */
7070   token = cp_lexer_peek_token (parser->lexer);
7071
7072   switch (token->type)
7073     {
7074     case CPP_EQ:
7075       op = NOP_EXPR;
7076       break;
7077
7078     case CPP_MULT_EQ:
7079       op = MULT_EXPR;
7080       break;
7081
7082     case CPP_DIV_EQ:
7083       op = TRUNC_DIV_EXPR;
7084       break;
7085
7086     case CPP_MOD_EQ:
7087       op = TRUNC_MOD_EXPR;
7088       break;
7089
7090     case CPP_PLUS_EQ:
7091       op = PLUS_EXPR;
7092       break;
7093
7094     case CPP_MINUS_EQ:
7095       op = MINUS_EXPR;
7096       break;
7097
7098     case CPP_RSHIFT_EQ:
7099       op = RSHIFT_EXPR;
7100       break;
7101
7102     case CPP_LSHIFT_EQ:
7103       op = LSHIFT_EXPR;
7104       break;
7105
7106     case CPP_AND_EQ:
7107       op = BIT_AND_EXPR;
7108       break;
7109
7110     case CPP_XOR_EQ:
7111       op = BIT_XOR_EXPR;
7112       break;
7113
7114     case CPP_OR_EQ:
7115       op = BIT_IOR_EXPR;
7116       break;
7117
7118     default:
7119       /* Nothing else is an assignment operator.  */
7120       op = ERROR_MARK;
7121     }
7122
7123   /* If it was an assignment operator, consume it.  */
7124   if (op != ERROR_MARK)
7125     cp_lexer_consume_token (parser->lexer);
7126
7127   return op;
7128 }
7129
7130 /* Parse an expression.
7131
7132    expression:
7133      assignment-expression
7134      expression , assignment-expression
7135
7136    CAST_P is true if this expression is the target of a cast.
7137
7138    Returns a representation of the expression.  */
7139
7140 static tree
7141 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7142 {
7143   tree expression = NULL_TREE;
7144
7145   while (true)
7146     {
7147       tree assignment_expression;
7148
7149       /* Parse the next assignment-expression.  */
7150       assignment_expression
7151         = cp_parser_assignment_expression (parser, cast_p, pidk);
7152       /* If this is the first assignment-expression, we can just
7153          save it away.  */
7154       if (!expression)
7155         expression = assignment_expression;
7156       else
7157         expression = build_x_compound_expr (expression,
7158                                             assignment_expression,
7159                                             tf_warning_or_error);
7160       /* If the next token is not a comma, then we are done with the
7161          expression.  */
7162       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7163         break;
7164       /* Consume the `,'.  */
7165       cp_lexer_consume_token (parser->lexer);
7166       /* A comma operator cannot appear in a constant-expression.  */
7167       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7168         expression = error_mark_node;
7169     }
7170
7171   return expression;
7172 }
7173
7174 /* Parse a constant-expression.
7175
7176    constant-expression:
7177      conditional-expression
7178
7179   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7180   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7181   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7182   is false, NON_CONSTANT_P should be NULL.  */
7183
7184 static tree
7185 cp_parser_constant_expression (cp_parser* parser,
7186                                bool allow_non_constant_p,
7187                                bool *non_constant_p)
7188 {
7189   bool saved_integral_constant_expression_p;
7190   bool saved_allow_non_integral_constant_expression_p;
7191   bool saved_non_integral_constant_expression_p;
7192   tree expression;
7193
7194   /* It might seem that we could simply parse the
7195      conditional-expression, and then check to see if it were
7196      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7197      one that the compiler can figure out is constant, possibly after
7198      doing some simplifications or optimizations.  The standard has a
7199      precise definition of constant-expression, and we must honor
7200      that, even though it is somewhat more restrictive.
7201
7202      For example:
7203
7204        int i[(2, 3)];
7205
7206      is not a legal declaration, because `(2, 3)' is not a
7207      constant-expression.  The `,' operator is forbidden in a
7208      constant-expression.  However, GCC's constant-folding machinery
7209      will fold this operation to an INTEGER_CST for `3'.  */
7210
7211   /* Save the old settings.  */
7212   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7213   saved_allow_non_integral_constant_expression_p
7214     = parser->allow_non_integral_constant_expression_p;
7215   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7216   /* We are now parsing a constant-expression.  */
7217   parser->integral_constant_expression_p = true;
7218   parser->allow_non_integral_constant_expression_p
7219     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7220   parser->non_integral_constant_expression_p = false;
7221   /* Although the grammar says "conditional-expression", we parse an
7222      "assignment-expression", which also permits "throw-expression"
7223      and the use of assignment operators.  In the case that
7224      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7225      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7226      actually essential that we look for an assignment-expression.
7227      For example, cp_parser_initializer_clauses uses this function to
7228      determine whether a particular assignment-expression is in fact
7229      constant.  */
7230   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7231   /* Restore the old settings.  */
7232   parser->integral_constant_expression_p
7233     = saved_integral_constant_expression_p;
7234   parser->allow_non_integral_constant_expression_p
7235     = saved_allow_non_integral_constant_expression_p;
7236   if (allow_non_constant_p)
7237     *non_constant_p = parser->non_integral_constant_expression_p;
7238   else if (parser->non_integral_constant_expression_p
7239            && cxx_dialect < cxx0x)
7240     expression = error_mark_node;
7241   parser->non_integral_constant_expression_p
7242     = saved_non_integral_constant_expression_p;
7243
7244   return expression;
7245 }
7246
7247 /* Parse __builtin_offsetof.
7248
7249    offsetof-expression:
7250      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7251
7252    offsetof-member-designator:
7253      id-expression
7254      | offsetof-member-designator "." id-expression
7255      | offsetof-member-designator "[" expression "]"
7256      | offsetof-member-designator "->" id-expression  */
7257
7258 static tree
7259 cp_parser_builtin_offsetof (cp_parser *parser)
7260 {
7261   int save_ice_p, save_non_ice_p;
7262   tree type, expr;
7263   cp_id_kind dummy;
7264   cp_token *token;
7265
7266   /* We're about to accept non-integral-constant things, but will
7267      definitely yield an integral constant expression.  Save and
7268      restore these values around our local parsing.  */
7269   save_ice_p = parser->integral_constant_expression_p;
7270   save_non_ice_p = parser->non_integral_constant_expression_p;
7271
7272   /* Consume the "__builtin_offsetof" token.  */
7273   cp_lexer_consume_token (parser->lexer);
7274   /* Consume the opening `('.  */
7275   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7276   /* Parse the type-id.  */
7277   type = cp_parser_type_id (parser);
7278   /* Look for the `,'.  */
7279   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7280   token = cp_lexer_peek_token (parser->lexer);
7281
7282   /* Build the (type *)null that begins the traditional offsetof macro.  */
7283   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7284                             tf_warning_or_error);
7285
7286   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7287   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7288                                                  true, &dummy, token->location);
7289   while (true)
7290     {
7291       token = cp_lexer_peek_token (parser->lexer);
7292       switch (token->type)
7293         {
7294         case CPP_OPEN_SQUARE:
7295           /* offsetof-member-designator "[" expression "]" */
7296           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7297           break;
7298
7299         case CPP_DEREF:
7300           /* offsetof-member-designator "->" identifier */
7301           expr = grok_array_decl (expr, integer_zero_node);
7302           /* FALLTHRU */
7303
7304         case CPP_DOT:
7305           /* offsetof-member-designator "." identifier */
7306           cp_lexer_consume_token (parser->lexer);
7307           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7308                                                          expr, true, &dummy,
7309                                                          token->location);
7310           break;
7311
7312         case CPP_CLOSE_PAREN:
7313           /* Consume the ")" token.  */
7314           cp_lexer_consume_token (parser->lexer);
7315           goto success;
7316
7317         default:
7318           /* Error.  We know the following require will fail, but
7319              that gives the proper error message.  */
7320           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7321           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7322           expr = error_mark_node;
7323           goto failure;
7324         }
7325     }
7326
7327  success:
7328   /* If we're processing a template, we can't finish the semantics yet.
7329      Otherwise we can fold the entire expression now.  */
7330   if (processing_template_decl)
7331     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7332   else
7333     expr = finish_offsetof (expr);
7334
7335  failure:
7336   parser->integral_constant_expression_p = save_ice_p;
7337   parser->non_integral_constant_expression_p = save_non_ice_p;
7338
7339   return expr;
7340 }
7341
7342 /* Parse a trait expression.  */
7343
7344 static tree
7345 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7346 {
7347   cp_trait_kind kind;
7348   tree type1, type2 = NULL_TREE;
7349   bool binary = false;
7350   cp_decl_specifier_seq decl_specs;
7351
7352   switch (keyword)
7353     {
7354     case RID_HAS_NOTHROW_ASSIGN:
7355       kind = CPTK_HAS_NOTHROW_ASSIGN;
7356       break;
7357     case RID_HAS_NOTHROW_CONSTRUCTOR:
7358       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7359       break;
7360     case RID_HAS_NOTHROW_COPY:
7361       kind = CPTK_HAS_NOTHROW_COPY;
7362       break;
7363     case RID_HAS_TRIVIAL_ASSIGN:
7364       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7365       break;
7366     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7367       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7368       break;
7369     case RID_HAS_TRIVIAL_COPY:
7370       kind = CPTK_HAS_TRIVIAL_COPY;
7371       break;
7372     case RID_HAS_TRIVIAL_DESTRUCTOR:
7373       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7374       break;
7375     case RID_HAS_VIRTUAL_DESTRUCTOR:
7376       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7377       break;
7378     case RID_IS_ABSTRACT:
7379       kind = CPTK_IS_ABSTRACT;
7380       break;
7381     case RID_IS_BASE_OF:
7382       kind = CPTK_IS_BASE_OF;
7383       binary = true;
7384       break;
7385     case RID_IS_CLASS:
7386       kind = CPTK_IS_CLASS;
7387       break;
7388     case RID_IS_CONVERTIBLE_TO:
7389       kind = CPTK_IS_CONVERTIBLE_TO;
7390       binary = true;
7391       break;
7392     case RID_IS_EMPTY:
7393       kind = CPTK_IS_EMPTY;
7394       break;
7395     case RID_IS_ENUM:
7396       kind = CPTK_IS_ENUM;
7397       break;
7398     case RID_IS_POD:
7399       kind = CPTK_IS_POD;
7400       break;
7401     case RID_IS_POLYMORPHIC:
7402       kind = CPTK_IS_POLYMORPHIC;
7403       break;
7404     case RID_IS_STD_LAYOUT:
7405       kind = CPTK_IS_STD_LAYOUT;
7406       break;
7407     case RID_IS_TRIVIAL:
7408       kind = CPTK_IS_TRIVIAL;
7409       break;
7410     case RID_IS_UNION:
7411       kind = CPTK_IS_UNION;
7412       break;
7413     case RID_IS_LITERAL_TYPE:
7414       kind = CPTK_IS_LITERAL_TYPE;
7415       break;
7416     default:
7417       gcc_unreachable ();
7418     }
7419
7420   /* Consume the token.  */
7421   cp_lexer_consume_token (parser->lexer);
7422
7423   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7424
7425   type1 = cp_parser_type_id (parser);
7426
7427   if (type1 == error_mark_node)
7428     return error_mark_node;
7429
7430   /* Build a trivial decl-specifier-seq.  */
7431   clear_decl_specs (&decl_specs);
7432   decl_specs.type = type1;
7433
7434   /* Call grokdeclarator to figure out what type this is.  */
7435   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7436                           /*initialized=*/0, /*attrlist=*/NULL);
7437
7438   if (binary)
7439     {
7440       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7441  
7442       type2 = cp_parser_type_id (parser);
7443
7444       if (type2 == error_mark_node)
7445         return error_mark_node;
7446
7447       /* Build a trivial decl-specifier-seq.  */
7448       clear_decl_specs (&decl_specs);
7449       decl_specs.type = type2;
7450
7451       /* Call grokdeclarator to figure out what type this is.  */
7452       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7453                               /*initialized=*/0, /*attrlist=*/NULL);
7454     }
7455
7456   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7457
7458   /* Complete the trait expression, which may mean either processing
7459      the trait expr now or saving it for template instantiation.  */
7460   return finish_trait_expr (kind, type1, type2);
7461 }
7462
7463 /* Lambdas that appear in variable initializer or default argument scope
7464    get that in their mangling, so we need to record it.  We might as well
7465    use the count for function and namespace scopes as well.  */
7466 static GTY(()) tree lambda_scope;
7467 static GTY(()) int lambda_count;
7468 typedef struct GTY(()) tree_int
7469 {
7470   tree t;
7471   int i;
7472 } tree_int;
7473 DEF_VEC_O(tree_int);
7474 DEF_VEC_ALLOC_O(tree_int,gc);
7475 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7476
7477 static void
7478 start_lambda_scope (tree decl)
7479 {
7480   tree_int ti;
7481   gcc_assert (decl);
7482   /* Once we're inside a function, we ignore other scopes and just push
7483      the function again so that popping works properly.  */
7484   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7485     decl = current_function_decl;
7486   ti.t = lambda_scope;
7487   ti.i = lambda_count;
7488   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7489   if (lambda_scope != decl)
7490     {
7491       /* Don't reset the count if we're still in the same function.  */
7492       lambda_scope = decl;
7493       lambda_count = 0;
7494     }
7495 }
7496
7497 static void
7498 record_lambda_scope (tree lambda)
7499 {
7500   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7501   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7502 }
7503
7504 static void
7505 finish_lambda_scope (void)
7506 {
7507   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7508   if (lambda_scope != p->t)
7509     {
7510       lambda_scope = p->t;
7511       lambda_count = p->i;
7512     }
7513   VEC_pop (tree_int, lambda_scope_stack);
7514 }
7515
7516 /* Parse a lambda expression.
7517
7518    lambda-expression:
7519      lambda-introducer lambda-declarator [opt] compound-statement
7520
7521    Returns a representation of the expression.  */
7522
7523 static tree
7524 cp_parser_lambda_expression (cp_parser* parser)
7525 {
7526   tree lambda_expr = build_lambda_expr ();
7527   tree type;
7528
7529   LAMBDA_EXPR_LOCATION (lambda_expr)
7530     = cp_lexer_peek_token (parser->lexer)->location;
7531
7532   if (cp_unevaluated_operand)
7533     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7534               "lambda-expression in unevaluated context");
7535
7536   /* We may be in the middle of deferred access check.  Disable
7537      it now.  */
7538   push_deferring_access_checks (dk_no_deferred);
7539
7540   cp_parser_lambda_introducer (parser, lambda_expr);
7541
7542   type = begin_lambda_type (lambda_expr);
7543
7544   record_lambda_scope (lambda_expr);
7545
7546   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7547   determine_visibility (TYPE_NAME (type));
7548
7549   /* Now that we've started the type, add the capture fields for any
7550      explicit captures.  */
7551   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7552
7553   {
7554     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7555     unsigned int saved_num_template_parameter_lists
7556         = parser->num_template_parameter_lists;
7557
7558     parser->num_template_parameter_lists = 0;
7559
7560     /* By virtue of defining a local class, a lambda expression has access to
7561        the private variables of enclosing classes.  */
7562
7563     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7564
7565     cp_parser_lambda_body (parser, lambda_expr);
7566
7567     /* The capture list was built up in reverse order; fix that now.  */
7568     {
7569       tree newlist = NULL_TREE;
7570       tree elt, next;
7571
7572       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7573            elt; elt = next)
7574         {
7575           tree field = TREE_PURPOSE (elt);
7576           char *buf;
7577
7578           next = TREE_CHAIN (elt);
7579           TREE_CHAIN (elt) = newlist;
7580           newlist = elt;
7581
7582           /* Also add __ to the beginning of the field name so that code
7583              outside the lambda body can't see the captured name.  We could
7584              just remove the name entirely, but this is more useful for
7585              debugging.  */
7586           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7587             /* The 'this' capture already starts with __.  */
7588             continue;
7589
7590           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7591           buf[1] = buf[0] = '_';
7592           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7593                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7594           DECL_NAME (field) = get_identifier (buf);
7595         }
7596       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7597     }
7598
7599     maybe_add_lambda_conv_op (type);
7600
7601     type = finish_struct (type, /*attributes=*/NULL_TREE);
7602
7603     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7604   }
7605
7606   pop_deferring_access_checks ();
7607
7608   return build_lambda_object (lambda_expr);
7609 }
7610
7611 /* Parse the beginning of a lambda expression.
7612
7613    lambda-introducer:
7614      [ lambda-capture [opt] ]
7615
7616    LAMBDA_EXPR is the current representation of the lambda expression.  */
7617
7618 static void
7619 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7620 {
7621   /* Need commas after the first capture.  */
7622   bool first = true;
7623
7624   /* Eat the leading `['.  */
7625   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7626
7627   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7628   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7629       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7630     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7631   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7632     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7633
7634   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7635     {
7636       cp_lexer_consume_token (parser->lexer);
7637       first = false;
7638     }
7639
7640   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7641     {
7642       cp_token* capture_token;
7643       tree capture_id;
7644       tree capture_init_expr;
7645       cp_id_kind idk = CP_ID_KIND_NONE;
7646       bool explicit_init_p = false;
7647
7648       enum capture_kind_type
7649       {
7650         BY_COPY,
7651         BY_REFERENCE
7652       };
7653       enum capture_kind_type capture_kind = BY_COPY;
7654
7655       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7656         {
7657           error ("expected end of capture-list");
7658           return;
7659         }
7660
7661       if (first)
7662         first = false;
7663       else
7664         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7665
7666       /* Possibly capture `this'.  */
7667       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7668         {
7669           cp_lexer_consume_token (parser->lexer);
7670           add_capture (lambda_expr,
7671                        /*id=*/get_identifier ("__this"),
7672                        /*initializer=*/finish_this_expr(),
7673                        /*by_reference_p=*/false,
7674                        explicit_init_p);
7675           continue;
7676         }
7677
7678       /* Remember whether we want to capture as a reference or not.  */
7679       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7680         {
7681           capture_kind = BY_REFERENCE;
7682           cp_lexer_consume_token (parser->lexer);
7683         }
7684
7685       /* Get the identifier.  */
7686       capture_token = cp_lexer_peek_token (parser->lexer);
7687       capture_id = cp_parser_identifier (parser);
7688
7689       if (capture_id == error_mark_node)
7690         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7691            delimiters, but I modified this to stop on unnested ']' as well.  It
7692            was already changed to stop on unnested '}', so the
7693            "closing_parenthesis" name is no more misleading with my change.  */
7694         {
7695           cp_parser_skip_to_closing_parenthesis (parser,
7696                                                  /*recovering=*/true,
7697                                                  /*or_comma=*/true,
7698                                                  /*consume_paren=*/true);
7699           break;
7700         }
7701
7702       /* Find the initializer for this capture.  */
7703       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7704         {
7705           /* An explicit expression exists.  */
7706           cp_lexer_consume_token (parser->lexer);
7707           pedwarn (input_location, OPT_pedantic,
7708                    "ISO C++ does not allow initializers "
7709                    "in lambda expression capture lists");
7710           capture_init_expr = cp_parser_assignment_expression (parser,
7711                                                                /*cast_p=*/true,
7712                                                                &idk);
7713           explicit_init_p = true;
7714         }
7715       else
7716         {
7717           const char* error_msg;
7718
7719           /* Turn the identifier into an id-expression.  */
7720           capture_init_expr
7721             = cp_parser_lookup_name
7722                 (parser,
7723                  capture_id,
7724                  none_type,
7725                  /*is_template=*/false,
7726                  /*is_namespace=*/false,
7727                  /*check_dependency=*/true,
7728                  /*ambiguous_decls=*/NULL,
7729                  capture_token->location);
7730
7731           capture_init_expr
7732             = finish_id_expression
7733                 (capture_id,
7734                  capture_init_expr,
7735                  parser->scope,
7736                  &idk,
7737                  /*integral_constant_expression_p=*/false,
7738                  /*allow_non_integral_constant_expression_p=*/false,
7739                  /*non_integral_constant_expression_p=*/NULL,
7740                  /*template_p=*/false,
7741                  /*done=*/true,
7742                  /*address_p=*/false,
7743                  /*template_arg_p=*/false,
7744                  &error_msg,
7745                  capture_token->location);
7746         }
7747
7748       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7749         capture_init_expr
7750           = unqualified_name_lookup_error (capture_init_expr);
7751
7752       add_capture (lambda_expr,
7753                    capture_id,
7754                    capture_init_expr,
7755                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7756                    explicit_init_p);
7757     }
7758
7759   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7760 }
7761
7762 /* Parse the (optional) middle of a lambda expression.
7763
7764    lambda-declarator:
7765      ( parameter-declaration-clause [opt] )
7766        attribute-specifier [opt]
7767        mutable [opt]
7768        exception-specification [opt]
7769        lambda-return-type-clause [opt]
7770
7771    LAMBDA_EXPR is the current representation of the lambda expression.  */
7772
7773 static void
7774 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7775 {
7776   /* 5.1.1.4 of the standard says:
7777        If a lambda-expression does not include a lambda-declarator, it is as if
7778        the lambda-declarator were ().
7779      This means an empty parameter list, no attributes, and no exception
7780      specification.  */
7781   tree param_list = void_list_node;
7782   tree attributes = NULL_TREE;
7783   tree exception_spec = NULL_TREE;
7784   tree t;
7785
7786   /* The lambda-declarator is optional, but must begin with an opening
7787      parenthesis if present.  */
7788   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7789     {
7790       cp_lexer_consume_token (parser->lexer);
7791
7792       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7793
7794       /* Parse parameters.  */
7795       param_list = cp_parser_parameter_declaration_clause (parser);
7796
7797       /* Default arguments shall not be specified in the
7798          parameter-declaration-clause of a lambda-declarator.  */
7799       for (t = param_list; t; t = TREE_CHAIN (t))
7800         if (TREE_PURPOSE (t))
7801           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7802                    "default argument specified for lambda parameter");
7803
7804       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7805
7806       attributes = cp_parser_attributes_opt (parser);
7807
7808       /* Parse optional `mutable' keyword.  */
7809       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7810         {
7811           cp_lexer_consume_token (parser->lexer);
7812           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7813         }
7814
7815       /* Parse optional exception specification.  */
7816       exception_spec = cp_parser_exception_specification_opt (parser);
7817
7818       /* Parse optional trailing return type.  */
7819       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7820         {
7821           cp_lexer_consume_token (parser->lexer);
7822           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7823         }
7824
7825       /* The function parameters must be in scope all the way until after the
7826          trailing-return-type in case of decltype.  */
7827       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7828         pop_binding (DECL_NAME (t), t);
7829
7830       leave_scope ();
7831     }
7832
7833   /* Create the function call operator.
7834
7835      Messing with declarators like this is no uglier than building up the
7836      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7837      other code.  */
7838   {
7839     cp_decl_specifier_seq return_type_specs;
7840     cp_declarator* declarator;
7841     tree fco;
7842     int quals;
7843     void *p;
7844
7845     clear_decl_specs (&return_type_specs);
7846     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7847       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7848     else
7849       /* Maybe we will deduce the return type later, but we can use void
7850          as a placeholder return type anyways.  */
7851       return_type_specs.type = void_type_node;
7852
7853     p = obstack_alloc (&declarator_obstack, 0);
7854
7855     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7856                                      sfk_none);
7857
7858     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7859              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7860     declarator = make_call_declarator (declarator, param_list, quals,
7861                                        exception_spec,
7862                                        /*late_return_type=*/NULL_TREE);
7863     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7864
7865     fco = grokmethod (&return_type_specs,
7866                       declarator,
7867                       attributes);
7868     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7869     DECL_ARTIFICIAL (fco) = 1;
7870
7871     finish_member_declaration (fco);
7872
7873     obstack_free (&declarator_obstack, p);
7874   }
7875 }
7876
7877 /* Parse the body of a lambda expression, which is simply
7878
7879    compound-statement
7880
7881    but which requires special handling.
7882    LAMBDA_EXPR is the current representation of the lambda expression.  */
7883
7884 static void
7885 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7886 {
7887   bool nested = (current_function_decl != NULL_TREE);
7888   if (nested)
7889     push_function_context ();
7890
7891   /* Finish the function call operator
7892      - class_specifier
7893      + late_parsing_for_member
7894      + function_definition_after_declarator
7895      + ctor_initializer_opt_and_function_body  */
7896   {
7897     tree fco = lambda_function (lambda_expr);
7898     tree body;
7899     bool done = false;
7900
7901     /* Let the front end know that we are going to be defining this
7902        function.  */
7903     start_preparsed_function (fco,
7904                               NULL_TREE,
7905                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7906
7907     start_lambda_scope (fco);
7908     body = begin_function_body ();
7909
7910     /* 5.1.1.4 of the standard says:
7911          If a lambda-expression does not include a trailing-return-type, it
7912          is as if the trailing-return-type denotes the following type:
7913           * if the compound-statement is of the form
7914                { return attribute-specifier [opt] expression ; }
7915              the type of the returned expression after lvalue-to-rvalue
7916              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7917              (_conv.array_ 4.2), and function-to-pointer conversion
7918              (_conv.func_ 4.3);
7919           * otherwise, void.  */
7920
7921     /* In a lambda that has neither a lambda-return-type-clause
7922        nor a deducible form, errors should be reported for return statements
7923        in the body.  Since we used void as the placeholder return type, parsing
7924        the body as usual will give such desired behavior.  */
7925     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7926         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7927         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7928         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7929       {
7930         tree compound_stmt;
7931         tree expr = NULL_TREE;
7932         cp_id_kind idk = CP_ID_KIND_NONE;
7933
7934         /* Parse tentatively in case there's more after the initial return
7935            statement.  */
7936         cp_parser_parse_tentatively (parser);
7937
7938         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7939         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7940
7941         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7942
7943         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7944         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7945
7946         if (cp_parser_parse_definitely (parser))
7947           {
7948             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7949
7950             compound_stmt = begin_compound_stmt (0);
7951             /* Will get error here if type not deduced yet.  */
7952             finish_return_stmt (expr);
7953             finish_compound_stmt (compound_stmt);
7954
7955             done = true;
7956           }
7957       }
7958
7959     if (!done)
7960       {
7961         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7962           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7963         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7964            cp_parser_compound_stmt does not pass it.  */
7965         cp_parser_function_body (parser);
7966         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7967       }
7968
7969     finish_function_body (body);
7970     finish_lambda_scope ();
7971
7972     /* Finish the function and generate code for it if necessary.  */
7973     expand_or_defer_fn (finish_function (/*inline*/2));
7974   }
7975
7976   if (nested)
7977     pop_function_context();
7978 }
7979
7980 /* Statements [gram.stmt.stmt]  */
7981
7982 /* Parse a statement.
7983
7984    statement:
7985      labeled-statement
7986      expression-statement
7987      compound-statement
7988      selection-statement
7989      iteration-statement
7990      jump-statement
7991      declaration-statement
7992      try-block
7993
7994   IN_COMPOUND is true when the statement is nested inside a
7995   cp_parser_compound_statement; this matters for certain pragmas.
7996
7997   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7998   is a (possibly labeled) if statement which is not enclosed in braces
7999   and has an else clause.  This is used to implement -Wparentheses.  */
8000
8001 static void
8002 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8003                      bool in_compound, bool *if_p)
8004 {
8005   tree statement;
8006   cp_token *token;
8007   location_t statement_location;
8008
8009  restart:
8010   if (if_p != NULL)
8011     *if_p = false;
8012   /* There is no statement yet.  */
8013   statement = NULL_TREE;
8014   /* Peek at the next token.  */
8015   token = cp_lexer_peek_token (parser->lexer);
8016   /* Remember the location of the first token in the statement.  */
8017   statement_location = token->location;
8018   /* If this is a keyword, then that will often determine what kind of
8019      statement we have.  */
8020   if (token->type == CPP_KEYWORD)
8021     {
8022       enum rid keyword = token->keyword;
8023
8024       switch (keyword)
8025         {
8026         case RID_CASE:
8027         case RID_DEFAULT:
8028           /* Looks like a labeled-statement with a case label.
8029              Parse the label, and then use tail recursion to parse
8030              the statement.  */
8031           cp_parser_label_for_labeled_statement (parser);
8032           goto restart;
8033
8034         case RID_IF:
8035         case RID_SWITCH:
8036           statement = cp_parser_selection_statement (parser, if_p);
8037           break;
8038
8039         case RID_WHILE:
8040         case RID_DO:
8041         case RID_FOR:
8042           statement = cp_parser_iteration_statement (parser);
8043           break;
8044
8045         case RID_BREAK:
8046         case RID_CONTINUE:
8047         case RID_RETURN:
8048         case RID_GOTO:
8049           statement = cp_parser_jump_statement (parser);
8050           break;
8051
8052           /* Objective-C++ exception-handling constructs.  */
8053         case RID_AT_TRY:
8054         case RID_AT_CATCH:
8055         case RID_AT_FINALLY:
8056         case RID_AT_SYNCHRONIZED:
8057         case RID_AT_THROW:
8058           statement = cp_parser_objc_statement (parser);
8059           break;
8060
8061         case RID_TRY:
8062           statement = cp_parser_try_block (parser);
8063           break;
8064
8065         case RID_NAMESPACE:
8066           /* This must be a namespace alias definition.  */
8067           cp_parser_declaration_statement (parser);
8068           return;
8069           
8070         default:
8071           /* It might be a keyword like `int' that can start a
8072              declaration-statement.  */
8073           break;
8074         }
8075     }
8076   else if (token->type == CPP_NAME)
8077     {
8078       /* If the next token is a `:', then we are looking at a
8079          labeled-statement.  */
8080       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8081       if (token->type == CPP_COLON)
8082         {
8083           /* Looks like a labeled-statement with an ordinary label.
8084              Parse the label, and then use tail recursion to parse
8085              the statement.  */
8086           cp_parser_label_for_labeled_statement (parser);
8087           goto restart;
8088         }
8089     }
8090   /* Anything that starts with a `{' must be a compound-statement.  */
8091   else if (token->type == CPP_OPEN_BRACE)
8092     statement = cp_parser_compound_statement (parser, NULL, false);
8093   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8094      a statement all its own.  */
8095   else if (token->type == CPP_PRAGMA)
8096     {
8097       /* Only certain OpenMP pragmas are attached to statements, and thus
8098          are considered statements themselves.  All others are not.  In
8099          the context of a compound, accept the pragma as a "statement" and
8100          return so that we can check for a close brace.  Otherwise we
8101          require a real statement and must go back and read one.  */
8102       if (in_compound)
8103         cp_parser_pragma (parser, pragma_compound);
8104       else if (!cp_parser_pragma (parser, pragma_stmt))
8105         goto restart;
8106       return;
8107     }
8108   else if (token->type == CPP_EOF)
8109     {
8110       cp_parser_error (parser, "expected statement");
8111       return;
8112     }
8113
8114   /* Everything else must be a declaration-statement or an
8115      expression-statement.  Try for the declaration-statement
8116      first, unless we are looking at a `;', in which case we know that
8117      we have an expression-statement.  */
8118   if (!statement)
8119     {
8120       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8121         {
8122           cp_parser_parse_tentatively (parser);
8123           /* Try to parse the declaration-statement.  */
8124           cp_parser_declaration_statement (parser);
8125           /* If that worked, we're done.  */
8126           if (cp_parser_parse_definitely (parser))
8127             return;
8128         }
8129       /* Look for an expression-statement instead.  */
8130       statement = cp_parser_expression_statement (parser, in_statement_expr);
8131     }
8132
8133   /* Set the line number for the statement.  */
8134   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8135     SET_EXPR_LOCATION (statement, statement_location);
8136 }
8137
8138 /* Parse the label for a labeled-statement, i.e.
8139
8140    identifier :
8141    case constant-expression :
8142    default :
8143
8144    GNU Extension:
8145    case constant-expression ... constant-expression : statement
8146
8147    When a label is parsed without errors, the label is added to the
8148    parse tree by the finish_* functions, so this function doesn't
8149    have to return the label.  */
8150
8151 static void
8152 cp_parser_label_for_labeled_statement (cp_parser* parser)
8153 {
8154   cp_token *token;
8155   tree label = NULL_TREE;
8156
8157   /* The next token should be an identifier.  */
8158   token = cp_lexer_peek_token (parser->lexer);
8159   if (token->type != CPP_NAME
8160       && token->type != CPP_KEYWORD)
8161     {
8162       cp_parser_error (parser, "expected labeled-statement");
8163       return;
8164     }
8165
8166   switch (token->keyword)
8167     {
8168     case RID_CASE:
8169       {
8170         tree expr, expr_hi;
8171         cp_token *ellipsis;
8172
8173         /* Consume the `case' token.  */
8174         cp_lexer_consume_token (parser->lexer);
8175         /* Parse the constant-expression.  */
8176         expr = cp_parser_constant_expression (parser,
8177                                               /*allow_non_constant_p=*/false,
8178                                               NULL);
8179
8180         ellipsis = cp_lexer_peek_token (parser->lexer);
8181         if (ellipsis->type == CPP_ELLIPSIS)
8182           {
8183             /* Consume the `...' token.  */
8184             cp_lexer_consume_token (parser->lexer);
8185             expr_hi =
8186               cp_parser_constant_expression (parser,
8187                                              /*allow_non_constant_p=*/false,
8188                                              NULL);
8189             /* We don't need to emit warnings here, as the common code
8190                will do this for us.  */
8191           }
8192         else
8193           expr_hi = NULL_TREE;
8194
8195         if (parser->in_switch_statement_p)
8196           finish_case_label (token->location, expr, expr_hi);
8197         else
8198           error_at (token->location,
8199                     "case label %qE not within a switch statement",
8200                     expr);
8201       }
8202       break;
8203
8204     case RID_DEFAULT:
8205       /* Consume the `default' token.  */
8206       cp_lexer_consume_token (parser->lexer);
8207
8208       if (parser->in_switch_statement_p)
8209         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8210       else
8211         error_at (token->location, "case label not within a switch statement");
8212       break;
8213
8214     default:
8215       /* Anything else must be an ordinary label.  */
8216       label = finish_label_stmt (cp_parser_identifier (parser));
8217       break;
8218     }
8219
8220   /* Require the `:' token.  */
8221   cp_parser_require (parser, CPP_COLON, RT_COLON);
8222
8223   /* An ordinary label may optionally be followed by attributes.
8224      However, this is only permitted if the attributes are then
8225      followed by a semicolon.  This is because, for backward
8226      compatibility, when parsing
8227        lab: __attribute__ ((unused)) int i;
8228      we want the attribute to attach to "i", not "lab".  */
8229   if (label != NULL_TREE
8230       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8231     {
8232       tree attrs;
8233
8234       cp_parser_parse_tentatively (parser);
8235       attrs = cp_parser_attributes_opt (parser);
8236       if (attrs == NULL_TREE
8237           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8238         cp_parser_abort_tentative_parse (parser);
8239       else if (!cp_parser_parse_definitely (parser))
8240         ;
8241       else
8242         cplus_decl_attributes (&label, attrs, 0);
8243     }
8244 }
8245
8246 /* Parse an expression-statement.
8247
8248    expression-statement:
8249      expression [opt] ;
8250
8251    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8252    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8253    indicates whether this expression-statement is part of an
8254    expression statement.  */
8255
8256 static tree
8257 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8258 {
8259   tree statement = NULL_TREE;
8260   cp_token *token = cp_lexer_peek_token (parser->lexer);
8261
8262   /* If the next token is a ';', then there is no expression
8263      statement.  */
8264   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8265     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8266
8267   /* Give a helpful message for "A<T>::type t;" and the like.  */
8268   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8269       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8270     {
8271       if (TREE_CODE (statement) == SCOPE_REF)
8272         error_at (token->location, "need %<typename%> before %qE because "
8273                   "%qT is a dependent scope",
8274                   statement, TREE_OPERAND (statement, 0));
8275       else if (is_overloaded_fn (statement)
8276                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8277         {
8278           /* A::A a; */
8279           tree fn = get_first_fn (statement);
8280           error_at (token->location,
8281                     "%<%T::%D%> names the constructor, not the type",
8282                     DECL_CONTEXT (fn), DECL_NAME (fn));
8283         }
8284     }
8285
8286   /* Consume the final `;'.  */
8287   cp_parser_consume_semicolon_at_end_of_statement (parser);
8288
8289   if (in_statement_expr
8290       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8291     /* This is the final expression statement of a statement
8292        expression.  */
8293     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8294   else if (statement)
8295     statement = finish_expr_stmt (statement);
8296   else
8297     finish_stmt ();
8298
8299   return statement;
8300 }
8301
8302 /* Parse a compound-statement.
8303
8304    compound-statement:
8305      { statement-seq [opt] }
8306
8307    GNU extension:
8308
8309    compound-statement:
8310      { label-declaration-seq [opt] statement-seq [opt] }
8311
8312    label-declaration-seq:
8313      label-declaration
8314      label-declaration-seq label-declaration
8315
8316    Returns a tree representing the statement.  */
8317
8318 static tree
8319 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8320                               bool in_try)
8321 {
8322   tree compound_stmt;
8323
8324   /* Consume the `{'.  */
8325   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8326     return error_mark_node;
8327   /* Begin the compound-statement.  */
8328   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8329   /* If the next keyword is `__label__' we have a label declaration.  */
8330   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8331     cp_parser_label_declaration (parser);
8332   /* Parse an (optional) statement-seq.  */
8333   cp_parser_statement_seq_opt (parser, in_statement_expr);
8334   /* Finish the compound-statement.  */
8335   finish_compound_stmt (compound_stmt);
8336   /* Consume the `}'.  */
8337   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8338
8339   return compound_stmt;
8340 }
8341
8342 /* Parse an (optional) statement-seq.
8343
8344    statement-seq:
8345      statement
8346      statement-seq [opt] statement  */
8347
8348 static void
8349 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8350 {
8351   /* Scan statements until there aren't any more.  */
8352   while (true)
8353     {
8354       cp_token *token = cp_lexer_peek_token (parser->lexer);
8355
8356       /* If we are looking at a `}', then we have run out of
8357          statements; the same is true if we have reached the end
8358          of file, or have stumbled upon a stray '@end'.  */
8359       if (token->type == CPP_CLOSE_BRACE
8360           || token->type == CPP_EOF
8361           || token->type == CPP_PRAGMA_EOL
8362           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8363         break;
8364       
8365       /* If we are in a compound statement and find 'else' then
8366          something went wrong.  */
8367       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8368         {
8369           if (parser->in_statement & IN_IF_STMT) 
8370             break;
8371           else
8372             {
8373               token = cp_lexer_consume_token (parser->lexer);
8374               error_at (token->location, "%<else%> without a previous %<if%>");
8375             }
8376         }
8377
8378       /* Parse the statement.  */
8379       cp_parser_statement (parser, in_statement_expr, true, NULL);
8380     }
8381 }
8382
8383 /* Parse a selection-statement.
8384
8385    selection-statement:
8386      if ( condition ) statement
8387      if ( condition ) statement else statement
8388      switch ( condition ) statement
8389
8390    Returns the new IF_STMT or SWITCH_STMT.
8391
8392    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8393    is a (possibly labeled) if statement which is not enclosed in
8394    braces and has an else clause.  This is used to implement
8395    -Wparentheses.  */
8396
8397 static tree
8398 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8399 {
8400   cp_token *token;
8401   enum rid keyword;
8402
8403   if (if_p != NULL)
8404     *if_p = false;
8405
8406   /* Peek at the next token.  */
8407   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8408
8409   /* See what kind of keyword it is.  */
8410   keyword = token->keyword;
8411   switch (keyword)
8412     {
8413     case RID_IF:
8414     case RID_SWITCH:
8415       {
8416         tree statement;
8417         tree condition;
8418
8419         /* Look for the `('.  */
8420         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8421           {
8422             cp_parser_skip_to_end_of_statement (parser);
8423             return error_mark_node;
8424           }
8425
8426         /* Begin the selection-statement.  */
8427         if (keyword == RID_IF)
8428           statement = begin_if_stmt ();
8429         else
8430           statement = begin_switch_stmt ();
8431
8432         /* Parse the condition.  */
8433         condition = cp_parser_condition (parser);
8434         /* Look for the `)'.  */
8435         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8436           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8437                                                  /*consume_paren=*/true);
8438
8439         if (keyword == RID_IF)
8440           {
8441             bool nested_if;
8442             unsigned char in_statement;
8443
8444             /* Add the condition.  */
8445             finish_if_stmt_cond (condition, statement);
8446
8447             /* Parse the then-clause.  */
8448             in_statement = parser->in_statement;
8449             parser->in_statement |= IN_IF_STMT;
8450             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8451               {
8452                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8453                 add_stmt (build_empty_stmt (loc));
8454                 cp_lexer_consume_token (parser->lexer);
8455                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8456                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8457                               "empty body in an %<if%> statement");
8458                 nested_if = false;
8459               }
8460             else
8461               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8462             parser->in_statement = in_statement;
8463
8464             finish_then_clause (statement);
8465
8466             /* If the next token is `else', parse the else-clause.  */
8467             if (cp_lexer_next_token_is_keyword (parser->lexer,
8468                                                 RID_ELSE))
8469               {
8470                 /* Consume the `else' keyword.  */
8471                 cp_lexer_consume_token (parser->lexer);
8472                 begin_else_clause (statement);
8473                 /* Parse the else-clause.  */
8474                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8475                   {
8476                     location_t loc;
8477                     loc = cp_lexer_peek_token (parser->lexer)->location;
8478                     warning_at (loc,
8479                                 OPT_Wempty_body, "suggest braces around "
8480                                 "empty body in an %<else%> statement");
8481                     add_stmt (build_empty_stmt (loc));
8482                     cp_lexer_consume_token (parser->lexer);
8483                   }
8484                 else
8485                   cp_parser_implicitly_scoped_statement (parser, NULL);
8486
8487                 finish_else_clause (statement);
8488
8489                 /* If we are currently parsing a then-clause, then
8490                    IF_P will not be NULL.  We set it to true to
8491                    indicate that this if statement has an else clause.
8492                    This may trigger the Wparentheses warning below
8493                    when we get back up to the parent if statement.  */
8494                 if (if_p != NULL)
8495                   *if_p = true;
8496               }
8497             else
8498               {
8499                 /* This if statement does not have an else clause.  If
8500                    NESTED_IF is true, then the then-clause is an if
8501                    statement which does have an else clause.  We warn
8502                    about the potential ambiguity.  */
8503                 if (nested_if)
8504                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8505                               "suggest explicit braces to avoid ambiguous"
8506                               " %<else%>");
8507               }
8508
8509             /* Now we're all done with the if-statement.  */
8510             finish_if_stmt (statement);
8511           }
8512         else
8513           {
8514             bool in_switch_statement_p;
8515             unsigned char in_statement;
8516
8517             /* Add the condition.  */
8518             finish_switch_cond (condition, statement);
8519
8520             /* Parse the body of the switch-statement.  */
8521             in_switch_statement_p = parser->in_switch_statement_p;
8522             in_statement = parser->in_statement;
8523             parser->in_switch_statement_p = true;
8524             parser->in_statement |= IN_SWITCH_STMT;
8525             cp_parser_implicitly_scoped_statement (parser, NULL);
8526             parser->in_switch_statement_p = in_switch_statement_p;
8527             parser->in_statement = in_statement;
8528
8529             /* Now we're all done with the switch-statement.  */
8530             finish_switch_stmt (statement);
8531           }
8532
8533         return statement;
8534       }
8535       break;
8536
8537     default:
8538       cp_parser_error (parser, "expected selection-statement");
8539       return error_mark_node;
8540     }
8541 }
8542
8543 /* Parse a condition.
8544
8545    condition:
8546      expression
8547      type-specifier-seq declarator = initializer-clause
8548      type-specifier-seq declarator braced-init-list
8549
8550    GNU Extension:
8551
8552    condition:
8553      type-specifier-seq declarator asm-specification [opt]
8554        attributes [opt] = assignment-expression
8555
8556    Returns the expression that should be tested.  */
8557
8558 static tree
8559 cp_parser_condition (cp_parser* parser)
8560 {
8561   cp_decl_specifier_seq type_specifiers;
8562   const char *saved_message;
8563   int declares_class_or_enum;
8564
8565   /* Try the declaration first.  */
8566   cp_parser_parse_tentatively (parser);
8567   /* New types are not allowed in the type-specifier-seq for a
8568      condition.  */
8569   saved_message = parser->type_definition_forbidden_message;
8570   parser->type_definition_forbidden_message
8571     = G_("types may not be defined in conditions");
8572   /* Parse the type-specifier-seq.  */
8573   cp_parser_decl_specifier_seq (parser,
8574                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8575                                 &type_specifiers,
8576                                 &declares_class_or_enum);
8577   /* Restore the saved message.  */
8578   parser->type_definition_forbidden_message = saved_message;
8579   /* If all is well, we might be looking at a declaration.  */
8580   if (!cp_parser_error_occurred (parser))
8581     {
8582       tree decl;
8583       tree asm_specification;
8584       tree attributes;
8585       cp_declarator *declarator;
8586       tree initializer = NULL_TREE;
8587
8588       /* Parse the declarator.  */
8589       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8590                                          /*ctor_dtor_or_conv_p=*/NULL,
8591                                          /*parenthesized_p=*/NULL,
8592                                          /*member_p=*/false);
8593       /* Parse the attributes.  */
8594       attributes = cp_parser_attributes_opt (parser);
8595       /* Parse the asm-specification.  */
8596       asm_specification = cp_parser_asm_specification_opt (parser);
8597       /* If the next token is not an `=' or '{', then we might still be
8598          looking at an expression.  For example:
8599
8600            if (A(a).x)
8601
8602          looks like a decl-specifier-seq and a declarator -- but then
8603          there is no `=', so this is an expression.  */
8604       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8605           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8606         cp_parser_simulate_error (parser);
8607         
8608       /* If we did see an `=' or '{', then we are looking at a declaration
8609          for sure.  */
8610       if (cp_parser_parse_definitely (parser))
8611         {
8612           tree pushed_scope;
8613           bool non_constant_p;
8614           bool flags = LOOKUP_ONLYCONVERTING;
8615
8616           /* Create the declaration.  */
8617           decl = start_decl (declarator, &type_specifiers,
8618                              /*initialized_p=*/true,
8619                              attributes, /*prefix_attributes=*/NULL_TREE,
8620                              &pushed_scope);
8621
8622           /* Parse the initializer.  */
8623           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8624             {
8625               initializer = cp_parser_braced_list (parser, &non_constant_p);
8626               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8627               flags = 0;
8628             }
8629           else
8630             {
8631               /* Consume the `='.  */
8632               cp_parser_require (parser, CPP_EQ, RT_EQ);
8633               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8634             }
8635           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8636             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8637
8638           if (!non_constant_p)
8639             initializer = fold_non_dependent_expr (initializer);
8640
8641           /* Process the initializer.  */
8642           cp_finish_decl (decl,
8643                           initializer, !non_constant_p,
8644                           asm_specification,
8645                           flags);
8646
8647           if (pushed_scope)
8648             pop_scope (pushed_scope);
8649
8650           return convert_from_reference (decl);
8651         }
8652     }
8653   /* If we didn't even get past the declarator successfully, we are
8654      definitely not looking at a declaration.  */
8655   else
8656     cp_parser_abort_tentative_parse (parser);
8657
8658   /* Otherwise, we are looking at an expression.  */
8659   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8660 }
8661
8662 /* Parses a traditional for-statement until the closing ')', not included. */
8663
8664 static tree
8665 cp_parser_c_for (cp_parser *parser)
8666 {
8667   /* Normal for loop */
8668   tree stmt;
8669   tree condition = NULL_TREE;
8670   tree expression = NULL_TREE;
8671
8672   /* Begin the for-statement.  */
8673   stmt = begin_for_stmt ();
8674
8675   /* Parse the initialization.  */
8676   cp_parser_for_init_statement (parser);
8677   finish_for_init_stmt (stmt);
8678
8679   /* If there's a condition, process it.  */
8680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8681     condition = cp_parser_condition (parser);
8682   finish_for_cond (condition, stmt);
8683   /* Look for the `;'.  */
8684   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8685
8686   /* If there's an expression, process it.  */
8687   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8688     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8689   finish_for_expr (expression, stmt);
8690
8691   return stmt;
8692 }
8693
8694 /* Tries to parse a range-based for-statement:
8695
8696   range-based-for:
8697     type-specifier-seq declarator : expression
8698
8699   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8700   expression. Note that the *DECL is returned unfinished, so
8701   later you should call cp_finish_decl().
8702
8703   Returns TRUE iff a range-based for is parsed. */
8704
8705 static tree
8706 cp_parser_range_for (cp_parser *parser)
8707 {
8708   tree stmt, range_decl, range_expr;
8709   cp_decl_specifier_seq type_specifiers;
8710   cp_declarator *declarator;
8711   const char *saved_message;
8712   tree attributes, pushed_scope;
8713
8714   cp_parser_parse_tentatively (parser);
8715   /* New types are not allowed in the type-specifier-seq for a
8716      range-based for loop.  */
8717   saved_message = parser->type_definition_forbidden_message;
8718   parser->type_definition_forbidden_message
8719     = G_("types may not be defined in range-based for loops");
8720   /* Parse the type-specifier-seq.  */
8721   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8722                                 /*is_trailing_return=*/false,
8723                                 &type_specifiers);
8724   /* Restore the saved message.  */
8725   parser->type_definition_forbidden_message = saved_message;
8726   /* If all is well, we might be looking at a declaration.  */
8727   if (cp_parser_error_occurred (parser))
8728     {
8729       cp_parser_abort_tentative_parse (parser);
8730       return NULL_TREE;
8731     }
8732   /* Parse the declarator.  */
8733   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8734                                      /*ctor_dtor_or_conv_p=*/NULL,
8735                                      /*parenthesized_p=*/NULL,
8736                                      /*member_p=*/false);
8737   /* Parse the attributes.  */
8738   attributes = cp_parser_attributes_opt (parser);
8739   /* The next token should be `:'. */
8740   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8741     cp_parser_simulate_error (parser);
8742
8743   /* Check if it is a range-based for */
8744   if (!cp_parser_parse_definitely (parser))
8745     return NULL_TREE;
8746
8747   cp_parser_require (parser, CPP_COLON, RT_COLON);
8748   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8749     {
8750       bool expr_non_constant_p;
8751       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8752     }
8753   else
8754     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8755
8756   /* If in template, STMT is converted to a normal for-statements
8757      at instantiation. If not, it is done just ahead. */
8758   if (processing_template_decl)
8759     stmt = begin_range_for_stmt ();
8760   else
8761     stmt = begin_for_stmt ();
8762
8763   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8764   range_decl = start_decl (declarator, &type_specifiers,
8765                            /*initialized_p=*/SD_INITIALIZED,
8766                            attributes, /*prefix_attributes=*/NULL_TREE,
8767                            &pushed_scope);
8768   /* No scope allowed here */
8769   pop_scope (pushed_scope);
8770
8771   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8772     finish_range_for_decl (stmt, range_decl, range_expr);
8773   else
8774     /* Convert the range-based for loop into a normal for-statement. */
8775     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8776
8777   return stmt;
8778 }
8779
8780 /* Converts a range-based for-statement into a normal
8781    for-statement, as per the definition.
8782
8783       for (RANGE_DECL : RANGE_EXPR)
8784         BLOCK
8785
8786    should be equivalent to:
8787
8788       {
8789         auto &&__range = RANGE_EXPR;
8790         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8791               __begin != __end;
8792               ++__begin)
8793           {
8794               RANGE_DECL = *__begin;
8795               BLOCK
8796           }
8797       }
8798
8799    If RANGE_EXPR is an array:
8800        BEGIN_EXPR = __range
8801        END_EXPR = __range + ARRAY_SIZE(__range)
8802    Else:
8803         BEGIN_EXPR = begin(__range)
8804         END_EXPR = end(__range);
8805
8806    When calling begin()/end() we must use argument dependent
8807    lookup, but always considering 'std' as an associated namespace.  */
8808
8809 tree
8810 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8811 {
8812   tree range_type, range_temp;
8813   tree begin, end;
8814   tree iter_type, begin_expr, end_expr;
8815   tree condition, expression;
8816
8817   /* Find out the type deduced by the declaration
8818    * `auto &&__range = range_expr' */
8819   range_type = cp_build_reference_type (make_auto (), true);
8820   range_type = do_auto_deduction (range_type, range_expr,
8821                                   type_uses_auto (range_type));
8822
8823   /* Create the __range variable */
8824   range_temp = build_decl (input_location, VAR_DECL,
8825                            get_identifier ("__for_range"), range_type);
8826   TREE_USED (range_temp) = 1;
8827   DECL_ARTIFICIAL (range_temp) = 1;
8828   pushdecl (range_temp);
8829   cp_finish_decl (range_temp, range_expr,
8830                   /*is_constant_init*/false, NULL_TREE,
8831                   LOOKUP_ONLYCONVERTING);
8832
8833   range_temp = convert_from_reference (range_temp);
8834
8835   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8836     {
8837       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8838       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8839       begin_expr = range_temp;
8840       end_expr
8841         = build_binary_op (input_location, PLUS_EXPR,
8842                            range_temp,
8843                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8844     }
8845   else
8846     {
8847       /* If it is not an array, we must call begin(__range)/end__range() */
8848       VEC(tree,gc) *vec;
8849
8850       begin_expr = get_identifier ("begin");
8851       vec = make_tree_vector ();
8852       VEC_safe_push (tree, gc, vec, range_temp);
8853       begin_expr = perform_koenig_lookup (begin_expr, vec,
8854                                           /*include_std=*/true);
8855       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8856                                      tf_warning_or_error);
8857       release_tree_vector (vec);
8858
8859       end_expr = get_identifier ("end");
8860       vec = make_tree_vector ();
8861       VEC_safe_push (tree, gc, vec, range_temp);
8862       end_expr = perform_koenig_lookup (end_expr, vec,
8863                                         /*include_std=*/true);
8864       end_expr = finish_call_expr (end_expr, &vec, false, true,
8865                                    tf_warning_or_error);
8866       release_tree_vector (vec);
8867
8868       /* The unqualified type of the __begin and __end temporaries should
8869       * be the same as required by the multiple auto declaration */
8870       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8871       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8872         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8873                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8874     }
8875
8876   /* The new for initialization statement */
8877   begin = build_decl (input_location, VAR_DECL,
8878                       get_identifier ("__for_begin"), iter_type);
8879   TREE_USED (begin) = 1;
8880   DECL_ARTIFICIAL (begin) = 1;
8881   pushdecl (begin);
8882   cp_finish_decl (begin, begin_expr,
8883                   /*is_constant_init*/false, NULL_TREE,
8884                   LOOKUP_ONLYCONVERTING);
8885
8886   end = build_decl (input_location, VAR_DECL,
8887                     get_identifier ("__for_end"), iter_type);
8888   TREE_USED (end) = 1;
8889   DECL_ARTIFICIAL (end) = 1;
8890   pushdecl (end);
8891   cp_finish_decl (end, end_expr,
8892                   /*is_constant_init*/false, NULL_TREE,
8893                   LOOKUP_ONLYCONVERTING);
8894
8895   finish_for_init_stmt (statement);
8896
8897 /* The new for condition */
8898   condition = build_x_binary_op (NE_EXPR,
8899                                  begin, ERROR_MARK,
8900                                  end, ERROR_MARK,
8901                                  NULL, tf_warning_or_error);
8902   finish_for_cond (condition, statement);
8903
8904   /* The new increment expression */
8905   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8906   finish_for_expr (expression, statement);
8907
8908   /* The declaration is initialized with *__begin inside the loop body */
8909   cp_finish_decl (range_decl,
8910                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8911                   /*is_constant_init*/false, NULL_TREE,
8912                   LOOKUP_ONLYCONVERTING);
8913
8914   return statement;
8915 }
8916
8917
8918 /* Parse an iteration-statement.
8919
8920    iteration-statement:
8921      while ( condition ) statement
8922      do statement while ( expression ) ;
8923      for ( for-init-statement condition [opt] ; expression [opt] )
8924        statement
8925
8926    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8927
8928 static tree
8929 cp_parser_iteration_statement (cp_parser* parser)
8930 {
8931   cp_token *token;
8932   enum rid keyword;
8933   tree statement;
8934   unsigned char in_statement;
8935
8936   /* Peek at the next token.  */
8937   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8938   if (!token)
8939     return error_mark_node;
8940
8941   /* Remember whether or not we are already within an iteration
8942      statement.  */
8943   in_statement = parser->in_statement;
8944
8945   /* See what kind of keyword it is.  */
8946   keyword = token->keyword;
8947   switch (keyword)
8948     {
8949     case RID_WHILE:
8950       {
8951         tree condition;
8952
8953         /* Begin the while-statement.  */
8954         statement = begin_while_stmt ();
8955         /* Look for the `('.  */
8956         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8957         /* Parse the condition.  */
8958         condition = cp_parser_condition (parser);
8959         finish_while_stmt_cond (condition, statement);
8960         /* Look for the `)'.  */
8961         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8962         /* Parse the dependent statement.  */
8963         parser->in_statement = IN_ITERATION_STMT;
8964         cp_parser_already_scoped_statement (parser);
8965         parser->in_statement = in_statement;
8966         /* We're done with the while-statement.  */
8967         finish_while_stmt (statement);
8968       }
8969       break;
8970
8971     case RID_DO:
8972       {
8973         tree expression;
8974
8975         /* Begin the do-statement.  */
8976         statement = begin_do_stmt ();
8977         /* Parse the body of the do-statement.  */
8978         parser->in_statement = IN_ITERATION_STMT;
8979         cp_parser_implicitly_scoped_statement (parser, NULL);
8980         parser->in_statement = in_statement;
8981         finish_do_body (statement);
8982         /* Look for the `while' keyword.  */
8983         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8984         /* Look for the `('.  */
8985         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8986         /* Parse the expression.  */
8987         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8988         /* We're done with the do-statement.  */
8989         finish_do_stmt (expression, statement);
8990         /* Look for the `)'.  */
8991         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8992         /* Look for the `;'.  */
8993         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8994       }
8995       break;
8996
8997     case RID_FOR:
8998       {
8999         /* Look for the `('.  */
9000         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9001
9002         if (cxx_dialect == cxx0x)
9003           statement = cp_parser_range_for (parser);
9004         else
9005           statement = NULL_TREE;
9006         if (statement == NULL_TREE)
9007           statement = cp_parser_c_for (parser);
9008
9009         /* Look for the `)'.  */
9010         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9011
9012         /* Parse the body of the for-statement.  */
9013         parser->in_statement = IN_ITERATION_STMT;
9014         cp_parser_already_scoped_statement (parser);
9015         parser->in_statement = in_statement;
9016
9017         /* We're done with the for-statement.  */
9018         finish_for_stmt (statement);
9019       }
9020       break;
9021
9022     default:
9023       cp_parser_error (parser, "expected iteration-statement");
9024       statement = error_mark_node;
9025       break;
9026     }
9027
9028   return statement;
9029 }
9030
9031 /* Parse a for-init-statement.
9032
9033    for-init-statement:
9034      expression-statement
9035      simple-declaration  */
9036
9037 static void
9038 cp_parser_for_init_statement (cp_parser* parser)
9039 {
9040   /* If the next token is a `;', then we have an empty
9041      expression-statement.  Grammatically, this is also a
9042      simple-declaration, but an invalid one, because it does not
9043      declare anything.  Therefore, if we did not handle this case
9044      specially, we would issue an error message about an invalid
9045      declaration.  */
9046   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9047     {
9048       /* We're going to speculatively look for a declaration, falling back
9049          to an expression, if necessary.  */
9050       cp_parser_parse_tentatively (parser);
9051       /* Parse the declaration.  */
9052       cp_parser_simple_declaration (parser,
9053                                     /*function_definition_allowed_p=*/false);
9054       /* If the tentative parse failed, then we shall need to look for an
9055          expression-statement.  */
9056       if (cp_parser_parse_definitely (parser))
9057         return;
9058     }
9059
9060   cp_parser_expression_statement (parser, NULL_TREE);
9061 }
9062
9063 /* Parse a jump-statement.
9064
9065    jump-statement:
9066      break ;
9067      continue ;
9068      return expression [opt] ;
9069      return braced-init-list ;
9070      goto identifier ;
9071
9072    GNU extension:
9073
9074    jump-statement:
9075      goto * expression ;
9076
9077    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9078
9079 static tree
9080 cp_parser_jump_statement (cp_parser* parser)
9081 {
9082   tree statement = error_mark_node;
9083   cp_token *token;
9084   enum rid keyword;
9085   unsigned char in_statement;
9086
9087   /* Peek at the next token.  */
9088   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9089   if (!token)
9090     return error_mark_node;
9091
9092   /* See what kind of keyword it is.  */
9093   keyword = token->keyword;
9094   switch (keyword)
9095     {
9096     case RID_BREAK:
9097       in_statement = parser->in_statement & ~IN_IF_STMT;      
9098       switch (in_statement)
9099         {
9100         case 0:
9101           error_at (token->location, "break statement not within loop or switch");
9102           break;
9103         default:
9104           gcc_assert ((in_statement & IN_SWITCH_STMT)
9105                       || in_statement == IN_ITERATION_STMT);
9106           statement = finish_break_stmt ();
9107           break;
9108         case IN_OMP_BLOCK:
9109           error_at (token->location, "invalid exit from OpenMP structured block");
9110           break;
9111         case IN_OMP_FOR:
9112           error_at (token->location, "break statement used with OpenMP for loop");
9113           break;
9114         }
9115       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9116       break;
9117
9118     case RID_CONTINUE:
9119       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9120         {
9121         case 0:
9122           error_at (token->location, "continue statement not within a loop");
9123           break;
9124         case IN_ITERATION_STMT:
9125         case IN_OMP_FOR:
9126           statement = finish_continue_stmt ();
9127           break;
9128         case IN_OMP_BLOCK:
9129           error_at (token->location, "invalid exit from OpenMP structured block");
9130           break;
9131         default:
9132           gcc_unreachable ();
9133         }
9134       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9135       break;
9136
9137     case RID_RETURN:
9138       {
9139         tree expr;
9140         bool expr_non_constant_p;
9141
9142         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9143           {
9144             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9145             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9146           }
9147         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9148           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9149         else
9150           /* If the next token is a `;', then there is no
9151              expression.  */
9152           expr = NULL_TREE;
9153         /* Build the return-statement.  */
9154         statement = finish_return_stmt (expr);
9155         /* Look for the final `;'.  */
9156         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9157       }
9158       break;
9159
9160     case RID_GOTO:
9161       /* Create the goto-statement.  */
9162       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9163         {
9164           /* Issue a warning about this use of a GNU extension.  */
9165           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9166           /* Consume the '*' token.  */
9167           cp_lexer_consume_token (parser->lexer);
9168           /* Parse the dependent expression.  */
9169           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9170         }
9171       else
9172         finish_goto_stmt (cp_parser_identifier (parser));
9173       /* Look for the final `;'.  */
9174       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9175       break;
9176
9177     default:
9178       cp_parser_error (parser, "expected jump-statement");
9179       break;
9180     }
9181
9182   return statement;
9183 }
9184
9185 /* Parse a declaration-statement.
9186
9187    declaration-statement:
9188      block-declaration  */
9189
9190 static void
9191 cp_parser_declaration_statement (cp_parser* parser)
9192 {
9193   void *p;
9194
9195   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9196   p = obstack_alloc (&declarator_obstack, 0);
9197
9198  /* Parse the block-declaration.  */
9199   cp_parser_block_declaration (parser, /*statement_p=*/true);
9200
9201   /* Free any declarators allocated.  */
9202   obstack_free (&declarator_obstack, p);
9203
9204   /* Finish off the statement.  */
9205   finish_stmt ();
9206 }
9207
9208 /* Some dependent statements (like `if (cond) statement'), are
9209    implicitly in their own scope.  In other words, if the statement is
9210    a single statement (as opposed to a compound-statement), it is
9211    none-the-less treated as if it were enclosed in braces.  Any
9212    declarations appearing in the dependent statement are out of scope
9213    after control passes that point.  This function parses a statement,
9214    but ensures that is in its own scope, even if it is not a
9215    compound-statement.
9216
9217    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9218    is a (possibly labeled) if statement which is not enclosed in
9219    braces and has an else clause.  This is used to implement
9220    -Wparentheses.
9221
9222    Returns the new statement.  */
9223
9224 static tree
9225 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9226 {
9227   tree statement;
9228
9229   if (if_p != NULL)
9230     *if_p = false;
9231
9232   /* Mark if () ; with a special NOP_EXPR.  */
9233   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9234     {
9235       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9236       cp_lexer_consume_token (parser->lexer);
9237       statement = add_stmt (build_empty_stmt (loc));
9238     }
9239   /* if a compound is opened, we simply parse the statement directly.  */
9240   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9241     statement = cp_parser_compound_statement (parser, NULL, false);
9242   /* If the token is not a `{', then we must take special action.  */
9243   else
9244     {
9245       /* Create a compound-statement.  */
9246       statement = begin_compound_stmt (0);
9247       /* Parse the dependent-statement.  */
9248       cp_parser_statement (parser, NULL_TREE, false, if_p);
9249       /* Finish the dummy compound-statement.  */
9250       finish_compound_stmt (statement);
9251     }
9252
9253   /* Return the statement.  */
9254   return statement;
9255 }
9256
9257 /* For some dependent statements (like `while (cond) statement'), we
9258    have already created a scope.  Therefore, even if the dependent
9259    statement is a compound-statement, we do not want to create another
9260    scope.  */
9261
9262 static void
9263 cp_parser_already_scoped_statement (cp_parser* parser)
9264 {
9265   /* If the token is a `{', then we must take special action.  */
9266   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9267     cp_parser_statement (parser, NULL_TREE, false, NULL);
9268   else
9269     {
9270       /* Avoid calling cp_parser_compound_statement, so that we
9271          don't create a new scope.  Do everything else by hand.  */
9272       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9273       /* If the next keyword is `__label__' we have a label declaration.  */
9274       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9275         cp_parser_label_declaration (parser);
9276       /* Parse an (optional) statement-seq.  */
9277       cp_parser_statement_seq_opt (parser, NULL_TREE);
9278       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9279     }
9280 }
9281
9282 /* Declarations [gram.dcl.dcl] */
9283
9284 /* Parse an optional declaration-sequence.
9285
9286    declaration-seq:
9287      declaration
9288      declaration-seq declaration  */
9289
9290 static void
9291 cp_parser_declaration_seq_opt (cp_parser* parser)
9292 {
9293   while (true)
9294     {
9295       cp_token *token;
9296
9297       token = cp_lexer_peek_token (parser->lexer);
9298
9299       if (token->type == CPP_CLOSE_BRACE
9300           || token->type == CPP_EOF
9301           || token->type == CPP_PRAGMA_EOL)
9302         break;
9303
9304       if (token->type == CPP_SEMICOLON)
9305         {
9306           /* A declaration consisting of a single semicolon is
9307              invalid.  Allow it unless we're being pedantic.  */
9308           cp_lexer_consume_token (parser->lexer);
9309           if (!in_system_header)
9310             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9311           continue;
9312         }
9313
9314       /* If we're entering or exiting a region that's implicitly
9315          extern "C", modify the lang context appropriately.  */
9316       if (!parser->implicit_extern_c && token->implicit_extern_c)
9317         {
9318           push_lang_context (lang_name_c);
9319           parser->implicit_extern_c = true;
9320         }
9321       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9322         {
9323           pop_lang_context ();
9324           parser->implicit_extern_c = false;
9325         }
9326
9327       if (token->type == CPP_PRAGMA)
9328         {
9329           /* A top-level declaration can consist solely of a #pragma.
9330              A nested declaration cannot, so this is done here and not
9331              in cp_parser_declaration.  (A #pragma at block scope is
9332              handled in cp_parser_statement.)  */
9333           cp_parser_pragma (parser, pragma_external);
9334           continue;
9335         }
9336
9337       /* Parse the declaration itself.  */
9338       cp_parser_declaration (parser);
9339     }
9340 }
9341
9342 /* Parse a declaration.
9343
9344    declaration:
9345      block-declaration
9346      function-definition
9347      template-declaration
9348      explicit-instantiation
9349      explicit-specialization
9350      linkage-specification
9351      namespace-definition
9352
9353    GNU extension:
9354
9355    declaration:
9356       __extension__ declaration */
9357
9358 static void
9359 cp_parser_declaration (cp_parser* parser)
9360 {
9361   cp_token token1;
9362   cp_token token2;
9363   int saved_pedantic;
9364   void *p;
9365   tree attributes = NULL_TREE;
9366
9367   /* Check for the `__extension__' keyword.  */
9368   if (cp_parser_extension_opt (parser, &saved_pedantic))
9369     {
9370       /* Parse the qualified declaration.  */
9371       cp_parser_declaration (parser);
9372       /* Restore the PEDANTIC flag.  */
9373       pedantic = saved_pedantic;
9374
9375       return;
9376     }
9377
9378   /* Try to figure out what kind of declaration is present.  */
9379   token1 = *cp_lexer_peek_token (parser->lexer);
9380
9381   if (token1.type != CPP_EOF)
9382     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9383   else
9384     {
9385       token2.type = CPP_EOF;
9386       token2.keyword = RID_MAX;
9387     }
9388
9389   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9390   p = obstack_alloc (&declarator_obstack, 0);
9391
9392   /* If the next token is `extern' and the following token is a string
9393      literal, then we have a linkage specification.  */
9394   if (token1.keyword == RID_EXTERN
9395       && cp_parser_is_string_literal (&token2))
9396     cp_parser_linkage_specification (parser);
9397   /* If the next token is `template', then we have either a template
9398      declaration, an explicit instantiation, or an explicit
9399      specialization.  */
9400   else if (token1.keyword == RID_TEMPLATE)
9401     {
9402       /* `template <>' indicates a template specialization.  */
9403       if (token2.type == CPP_LESS
9404           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9405         cp_parser_explicit_specialization (parser);
9406       /* `template <' indicates a template declaration.  */
9407       else if (token2.type == CPP_LESS)
9408         cp_parser_template_declaration (parser, /*member_p=*/false);
9409       /* Anything else must be an explicit instantiation.  */
9410       else
9411         cp_parser_explicit_instantiation (parser);
9412     }
9413   /* If the next token is `export', then we have a template
9414      declaration.  */
9415   else if (token1.keyword == RID_EXPORT)
9416     cp_parser_template_declaration (parser, /*member_p=*/false);
9417   /* If the next token is `extern', 'static' or 'inline' and the one
9418      after that is `template', we have a GNU extended explicit
9419      instantiation directive.  */
9420   else if (cp_parser_allow_gnu_extensions_p (parser)
9421            && (token1.keyword == RID_EXTERN
9422                || token1.keyword == RID_STATIC
9423                || token1.keyword == RID_INLINE)
9424            && token2.keyword == RID_TEMPLATE)
9425     cp_parser_explicit_instantiation (parser);
9426   /* If the next token is `namespace', check for a named or unnamed
9427      namespace definition.  */
9428   else if (token1.keyword == RID_NAMESPACE
9429            && (/* A named namespace definition.  */
9430                (token2.type == CPP_NAME
9431                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9432                     != CPP_EQ))
9433                /* An unnamed namespace definition.  */
9434                || token2.type == CPP_OPEN_BRACE
9435                || token2.keyword == RID_ATTRIBUTE))
9436     cp_parser_namespace_definition (parser);
9437   /* An inline (associated) namespace definition.  */
9438   else if (token1.keyword == RID_INLINE
9439            && token2.keyword == RID_NAMESPACE)
9440     cp_parser_namespace_definition (parser);
9441   /* Objective-C++ declaration/definition.  */
9442   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9443     cp_parser_objc_declaration (parser, NULL_TREE);
9444   else if (c_dialect_objc ()
9445            && token1.keyword == RID_ATTRIBUTE
9446            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9447     cp_parser_objc_declaration (parser, attributes);
9448   /* We must have either a block declaration or a function
9449      definition.  */
9450   else
9451     /* Try to parse a block-declaration, or a function-definition.  */
9452     cp_parser_block_declaration (parser, /*statement_p=*/false);
9453
9454   /* Free any declarators allocated.  */
9455   obstack_free (&declarator_obstack, p);
9456 }
9457
9458 /* Parse a block-declaration.
9459
9460    block-declaration:
9461      simple-declaration
9462      asm-definition
9463      namespace-alias-definition
9464      using-declaration
9465      using-directive
9466
9467    GNU Extension:
9468
9469    block-declaration:
9470      __extension__ block-declaration
9471
9472    C++0x Extension:
9473
9474    block-declaration:
9475      static_assert-declaration
9476
9477    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9478    part of a declaration-statement.  */
9479
9480 static void
9481 cp_parser_block_declaration (cp_parser *parser,
9482                              bool      statement_p)
9483 {
9484   cp_token *token1;
9485   int saved_pedantic;
9486
9487   /* Check for the `__extension__' keyword.  */
9488   if (cp_parser_extension_opt (parser, &saved_pedantic))
9489     {
9490       /* Parse the qualified declaration.  */
9491       cp_parser_block_declaration (parser, statement_p);
9492       /* Restore the PEDANTIC flag.  */
9493       pedantic = saved_pedantic;
9494
9495       return;
9496     }
9497
9498   /* Peek at the next token to figure out which kind of declaration is
9499      present.  */
9500   token1 = cp_lexer_peek_token (parser->lexer);
9501
9502   /* If the next keyword is `asm', we have an asm-definition.  */
9503   if (token1->keyword == RID_ASM)
9504     {
9505       if (statement_p)
9506         cp_parser_commit_to_tentative_parse (parser);
9507       cp_parser_asm_definition (parser);
9508     }
9509   /* If the next keyword is `namespace', we have a
9510      namespace-alias-definition.  */
9511   else if (token1->keyword == RID_NAMESPACE)
9512     cp_parser_namespace_alias_definition (parser);
9513   /* If the next keyword is `using', we have either a
9514      using-declaration or a using-directive.  */
9515   else if (token1->keyword == RID_USING)
9516     {
9517       cp_token *token2;
9518
9519       if (statement_p)
9520         cp_parser_commit_to_tentative_parse (parser);
9521       /* If the token after `using' is `namespace', then we have a
9522          using-directive.  */
9523       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9524       if (token2->keyword == RID_NAMESPACE)
9525         cp_parser_using_directive (parser);
9526       /* Otherwise, it's a using-declaration.  */
9527       else
9528         cp_parser_using_declaration (parser,
9529                                      /*access_declaration_p=*/false);
9530     }
9531   /* If the next keyword is `__label__' we have a misplaced label
9532      declaration.  */
9533   else if (token1->keyword == RID_LABEL)
9534     {
9535       cp_lexer_consume_token (parser->lexer);
9536       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9537       cp_parser_skip_to_end_of_statement (parser);
9538       /* If the next token is now a `;', consume it.  */
9539       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9540         cp_lexer_consume_token (parser->lexer);
9541     }
9542   /* If the next token is `static_assert' we have a static assertion.  */
9543   else if (token1->keyword == RID_STATIC_ASSERT)
9544     cp_parser_static_assert (parser, /*member_p=*/false);
9545   /* Anything else must be a simple-declaration.  */
9546   else
9547     cp_parser_simple_declaration (parser, !statement_p);
9548 }
9549
9550 /* Parse a simple-declaration.
9551
9552    simple-declaration:
9553      decl-specifier-seq [opt] init-declarator-list [opt] ;
9554
9555    init-declarator-list:
9556      init-declarator
9557      init-declarator-list , init-declarator
9558
9559    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9560    function-definition as a simple-declaration.  */
9561
9562 static void
9563 cp_parser_simple_declaration (cp_parser* parser,
9564                               bool function_definition_allowed_p)
9565 {
9566   cp_decl_specifier_seq decl_specifiers;
9567   int declares_class_or_enum;
9568   bool saw_declarator;
9569
9570   /* Defer access checks until we know what is being declared; the
9571      checks for names appearing in the decl-specifier-seq should be
9572      done as if we were in the scope of the thing being declared.  */
9573   push_deferring_access_checks (dk_deferred);
9574
9575   /* Parse the decl-specifier-seq.  We have to keep track of whether
9576      or not the decl-specifier-seq declares a named class or
9577      enumeration type, since that is the only case in which the
9578      init-declarator-list is allowed to be empty.
9579
9580      [dcl.dcl]
9581
9582      In a simple-declaration, the optional init-declarator-list can be
9583      omitted only when declaring a class or enumeration, that is when
9584      the decl-specifier-seq contains either a class-specifier, an
9585      elaborated-type-specifier, or an enum-specifier.  */
9586   cp_parser_decl_specifier_seq (parser,
9587                                 CP_PARSER_FLAGS_OPTIONAL,
9588                                 &decl_specifiers,
9589                                 &declares_class_or_enum);
9590   /* We no longer need to defer access checks.  */
9591   stop_deferring_access_checks ();
9592
9593   /* In a block scope, a valid declaration must always have a
9594      decl-specifier-seq.  By not trying to parse declarators, we can
9595      resolve the declaration/expression ambiguity more quickly.  */
9596   if (!function_definition_allowed_p
9597       && !decl_specifiers.any_specifiers_p)
9598     {
9599       cp_parser_error (parser, "expected declaration");
9600       goto done;
9601     }
9602
9603   /* If the next two tokens are both identifiers, the code is
9604      erroneous. The usual cause of this situation is code like:
9605
9606        T t;
9607
9608      where "T" should name a type -- but does not.  */
9609   if (!decl_specifiers.any_type_specifiers_p
9610       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9611     {
9612       /* If parsing tentatively, we should commit; we really are
9613          looking at a declaration.  */
9614       cp_parser_commit_to_tentative_parse (parser);
9615       /* Give up.  */
9616       goto done;
9617     }
9618
9619   /* If we have seen at least one decl-specifier, and the next token
9620      is not a parenthesis, then we must be looking at a declaration.
9621      (After "int (" we might be looking at a functional cast.)  */
9622   if (decl_specifiers.any_specifiers_p
9623       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9624       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9625       && !cp_parser_error_occurred (parser))
9626     cp_parser_commit_to_tentative_parse (parser);
9627
9628   /* Keep going until we hit the `;' at the end of the simple
9629      declaration.  */
9630   saw_declarator = false;
9631   while (cp_lexer_next_token_is_not (parser->lexer,
9632                                      CPP_SEMICOLON))
9633     {
9634       cp_token *token;
9635       bool function_definition_p;
9636       tree decl;
9637
9638       if (saw_declarator)
9639         {
9640           /* If we are processing next declarator, coma is expected */
9641           token = cp_lexer_peek_token (parser->lexer);
9642           gcc_assert (token->type == CPP_COMMA);
9643           cp_lexer_consume_token (parser->lexer);
9644         }
9645       else
9646         saw_declarator = true;
9647
9648       /* Parse the init-declarator.  */
9649       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9650                                         /*checks=*/NULL,
9651                                         function_definition_allowed_p,
9652                                         /*member_p=*/false,
9653                                         declares_class_or_enum,
9654                                         &function_definition_p);
9655       /* If an error occurred while parsing tentatively, exit quickly.
9656          (That usually happens when in the body of a function; each
9657          statement is treated as a declaration-statement until proven
9658          otherwise.)  */
9659       if (cp_parser_error_occurred (parser))
9660         goto done;
9661       /* Handle function definitions specially.  */
9662       if (function_definition_p)
9663         {
9664           /* If the next token is a `,', then we are probably
9665              processing something like:
9666
9667                void f() {}, *p;
9668
9669              which is erroneous.  */
9670           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9671             {
9672               cp_token *token = cp_lexer_peek_token (parser->lexer);
9673               error_at (token->location,
9674                         "mixing"
9675                         " declarations and function-definitions is forbidden");
9676             }
9677           /* Otherwise, we're done with the list of declarators.  */
9678           else
9679             {
9680               pop_deferring_access_checks ();
9681               return;
9682             }
9683         }
9684       /* The next token should be either a `,' or a `;'.  */
9685       token = cp_lexer_peek_token (parser->lexer);
9686       /* If it's a `,', there are more declarators to come.  */
9687       if (token->type == CPP_COMMA)
9688         /* will be consumed next time around */;
9689       /* If it's a `;', we are done.  */
9690       else if (token->type == CPP_SEMICOLON)
9691         break;
9692       /* Anything else is an error.  */
9693       else
9694         {
9695           /* If we have already issued an error message we don't need
9696              to issue another one.  */
9697           if (decl != error_mark_node
9698               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9699             cp_parser_error (parser, "expected %<,%> or %<;%>");
9700           /* Skip tokens until we reach the end of the statement.  */
9701           cp_parser_skip_to_end_of_statement (parser);
9702           /* If the next token is now a `;', consume it.  */
9703           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9704             cp_lexer_consume_token (parser->lexer);
9705           goto done;
9706         }
9707       /* After the first time around, a function-definition is not
9708          allowed -- even if it was OK at first.  For example:
9709
9710            int i, f() {}
9711
9712          is not valid.  */
9713       function_definition_allowed_p = false;
9714     }
9715
9716   /* Issue an error message if no declarators are present, and the
9717      decl-specifier-seq does not itself declare a class or
9718      enumeration.  */
9719   if (!saw_declarator)
9720     {
9721       if (cp_parser_declares_only_class_p (parser))
9722         shadow_tag (&decl_specifiers);
9723       /* Perform any deferred access checks.  */
9724       perform_deferred_access_checks ();
9725     }
9726
9727   /* Consume the `;'.  */
9728   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9729
9730  done:
9731   pop_deferring_access_checks ();
9732 }
9733
9734 /* Parse a decl-specifier-seq.
9735
9736    decl-specifier-seq:
9737      decl-specifier-seq [opt] decl-specifier
9738
9739    decl-specifier:
9740      storage-class-specifier
9741      type-specifier
9742      function-specifier
9743      friend
9744      typedef
9745
9746    GNU Extension:
9747
9748    decl-specifier:
9749      attributes
9750
9751    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9752
9753    The parser flags FLAGS is used to control type-specifier parsing.
9754
9755    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9756    flags:
9757
9758      1: one of the decl-specifiers is an elaborated-type-specifier
9759         (i.e., a type declaration)
9760      2: one of the decl-specifiers is an enum-specifier or a
9761         class-specifier (i.e., a type definition)
9762
9763    */
9764
9765 static void
9766 cp_parser_decl_specifier_seq (cp_parser* parser,
9767                               cp_parser_flags flags,
9768                               cp_decl_specifier_seq *decl_specs,
9769                               int* declares_class_or_enum)
9770 {
9771   bool constructor_possible_p = !parser->in_declarator_p;
9772   cp_token *start_token = NULL;
9773
9774   /* Clear DECL_SPECS.  */
9775   clear_decl_specs (decl_specs);
9776
9777   /* Assume no class or enumeration type is declared.  */
9778   *declares_class_or_enum = 0;
9779
9780   /* Keep reading specifiers until there are no more to read.  */
9781   while (true)
9782     {
9783       bool constructor_p;
9784       bool found_decl_spec;
9785       cp_token *token;
9786
9787       /* Peek at the next token.  */
9788       token = cp_lexer_peek_token (parser->lexer);
9789
9790       /* Save the first token of the decl spec list for error
9791          reporting.  */
9792       if (!start_token)
9793         start_token = token;
9794       /* Handle attributes.  */
9795       if (token->keyword == RID_ATTRIBUTE)
9796         {
9797           /* Parse the attributes.  */
9798           decl_specs->attributes
9799             = chainon (decl_specs->attributes,
9800                        cp_parser_attributes_opt (parser));
9801           continue;
9802         }
9803       /* Assume we will find a decl-specifier keyword.  */
9804       found_decl_spec = true;
9805       /* If the next token is an appropriate keyword, we can simply
9806          add it to the list.  */
9807       switch (token->keyword)
9808         {
9809           /* decl-specifier:
9810                friend
9811                constexpr */
9812         case RID_FRIEND:
9813           if (!at_class_scope_p ())
9814             {
9815               error_at (token->location, "%<friend%> used outside of class");
9816               cp_lexer_purge_token (parser->lexer);
9817             }
9818           else
9819             {
9820               ++decl_specs->specs[(int) ds_friend];
9821               /* Consume the token.  */
9822               cp_lexer_consume_token (parser->lexer);
9823             }
9824           break;
9825
9826         case RID_CONSTEXPR:
9827           ++decl_specs->specs[(int) ds_constexpr];
9828           cp_lexer_consume_token (parser->lexer);
9829           break;
9830
9831           /* function-specifier:
9832                inline
9833                virtual
9834                explicit  */
9835         case RID_INLINE:
9836         case RID_VIRTUAL:
9837         case RID_EXPLICIT:
9838           cp_parser_function_specifier_opt (parser, decl_specs);
9839           break;
9840
9841           /* decl-specifier:
9842                typedef  */
9843         case RID_TYPEDEF:
9844           ++decl_specs->specs[(int) ds_typedef];
9845           /* Consume the token.  */
9846           cp_lexer_consume_token (parser->lexer);
9847           /* A constructor declarator cannot appear in a typedef.  */
9848           constructor_possible_p = false;
9849           /* The "typedef" keyword can only occur in a declaration; we
9850              may as well commit at this point.  */
9851           cp_parser_commit_to_tentative_parse (parser);
9852
9853           if (decl_specs->storage_class != sc_none)
9854             decl_specs->conflicting_specifiers_p = true;
9855           break;
9856
9857           /* storage-class-specifier:
9858                auto
9859                register
9860                static
9861                extern
9862                mutable
9863
9864              GNU Extension:
9865                thread  */
9866         case RID_AUTO:
9867           if (cxx_dialect == cxx98) 
9868             {
9869               /* Consume the token.  */
9870               cp_lexer_consume_token (parser->lexer);
9871
9872               /* Complain about `auto' as a storage specifier, if
9873                  we're complaining about C++0x compatibility.  */
9874               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9875                           " will change meaning in C++0x; please remove it");
9876
9877               /* Set the storage class anyway.  */
9878               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9879                                            token->location);
9880             }
9881           else
9882             /* C++0x auto type-specifier.  */
9883             found_decl_spec = false;
9884           break;
9885
9886         case RID_REGISTER:
9887         case RID_STATIC:
9888         case RID_EXTERN:
9889         case RID_MUTABLE:
9890           /* Consume the token.  */
9891           cp_lexer_consume_token (parser->lexer);
9892           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9893                                        token->location);
9894           break;
9895         case RID_THREAD:
9896           /* Consume the token.  */
9897           cp_lexer_consume_token (parser->lexer);
9898           ++decl_specs->specs[(int) ds_thread];
9899           break;
9900
9901         default:
9902           /* We did not yet find a decl-specifier yet.  */
9903           found_decl_spec = false;
9904           break;
9905         }
9906
9907       if (found_decl_spec
9908           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9909           && token->keyword != RID_CONSTEXPR)
9910         error ("decl-specifier invalid in condition");
9911
9912       /* Constructors are a special case.  The `S' in `S()' is not a
9913          decl-specifier; it is the beginning of the declarator.  */
9914       constructor_p
9915         = (!found_decl_spec
9916            && constructor_possible_p
9917            && (cp_parser_constructor_declarator_p
9918                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9919
9920       /* If we don't have a DECL_SPEC yet, then we must be looking at
9921          a type-specifier.  */
9922       if (!found_decl_spec && !constructor_p)
9923         {
9924           int decl_spec_declares_class_or_enum;
9925           bool is_cv_qualifier;
9926           tree type_spec;
9927
9928           type_spec
9929             = cp_parser_type_specifier (parser, flags,
9930                                         decl_specs,
9931                                         /*is_declaration=*/true,
9932                                         &decl_spec_declares_class_or_enum,
9933                                         &is_cv_qualifier);
9934           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9935
9936           /* If this type-specifier referenced a user-defined type
9937              (a typedef, class-name, etc.), then we can't allow any
9938              more such type-specifiers henceforth.
9939
9940              [dcl.spec]
9941
9942              The longest sequence of decl-specifiers that could
9943              possibly be a type name is taken as the
9944              decl-specifier-seq of a declaration.  The sequence shall
9945              be self-consistent as described below.
9946
9947              [dcl.type]
9948
9949              As a general rule, at most one type-specifier is allowed
9950              in the complete decl-specifier-seq of a declaration.  The
9951              only exceptions are the following:
9952
9953              -- const or volatile can be combined with any other
9954                 type-specifier.
9955
9956              -- signed or unsigned can be combined with char, long,
9957                 short, or int.
9958
9959              -- ..
9960
9961              Example:
9962
9963                typedef char* Pc;
9964                void g (const int Pc);
9965
9966              Here, Pc is *not* part of the decl-specifier seq; it's
9967              the declarator.  Therefore, once we see a type-specifier
9968              (other than a cv-qualifier), we forbid any additional
9969              user-defined types.  We *do* still allow things like `int
9970              int' to be considered a decl-specifier-seq, and issue the
9971              error message later.  */
9972           if (type_spec && !is_cv_qualifier)
9973             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9974           /* A constructor declarator cannot follow a type-specifier.  */
9975           if (type_spec)
9976             {
9977               constructor_possible_p = false;
9978               found_decl_spec = true;
9979               if (!is_cv_qualifier)
9980                 decl_specs->any_type_specifiers_p = true;
9981             }
9982         }
9983
9984       /* If we still do not have a DECL_SPEC, then there are no more
9985          decl-specifiers.  */
9986       if (!found_decl_spec)
9987         break;
9988
9989       decl_specs->any_specifiers_p = true;
9990       /* After we see one decl-specifier, further decl-specifiers are
9991          always optional.  */
9992       flags |= CP_PARSER_FLAGS_OPTIONAL;
9993     }
9994
9995   cp_parser_check_decl_spec (decl_specs, start_token->location);
9996
9997   /* Don't allow a friend specifier with a class definition.  */
9998   if (decl_specs->specs[(int) ds_friend] != 0
9999       && (*declares_class_or_enum & 2))
10000     error_at (start_token->location,
10001               "class definition may not be declared a friend");
10002 }
10003
10004 /* Parse an (optional) storage-class-specifier.
10005
10006    storage-class-specifier:
10007      auto
10008      register
10009      static
10010      extern
10011      mutable
10012
10013    GNU Extension:
10014
10015    storage-class-specifier:
10016      thread
10017
10018    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10019
10020 static tree
10021 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10022 {
10023   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10024     {
10025     case RID_AUTO:
10026       if (cxx_dialect != cxx98)
10027         return NULL_TREE;
10028       /* Fall through for C++98.  */
10029
10030     case RID_REGISTER:
10031     case RID_STATIC:
10032     case RID_EXTERN:
10033     case RID_MUTABLE:
10034     case RID_THREAD:
10035       /* Consume the token.  */
10036       return cp_lexer_consume_token (parser->lexer)->u.value;
10037
10038     default:
10039       return NULL_TREE;
10040     }
10041 }
10042
10043 /* Parse an (optional) function-specifier.
10044
10045    function-specifier:
10046      inline
10047      virtual
10048      explicit
10049
10050    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10051    Updates DECL_SPECS, if it is non-NULL.  */
10052
10053 static tree
10054 cp_parser_function_specifier_opt (cp_parser* parser,
10055                                   cp_decl_specifier_seq *decl_specs)
10056 {
10057   cp_token *token = cp_lexer_peek_token (parser->lexer);
10058   switch (token->keyword)
10059     {
10060     case RID_INLINE:
10061       if (decl_specs)
10062         ++decl_specs->specs[(int) ds_inline];
10063       break;
10064
10065     case RID_VIRTUAL:
10066       /* 14.5.2.3 [temp.mem]
10067
10068          A member function template shall not be virtual.  */
10069       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10070         error_at (token->location, "templates may not be %<virtual%>");
10071       else if (decl_specs)
10072         ++decl_specs->specs[(int) ds_virtual];
10073       break;
10074
10075     case RID_EXPLICIT:
10076       if (decl_specs)
10077         ++decl_specs->specs[(int) ds_explicit];
10078       break;
10079
10080     default:
10081       return NULL_TREE;
10082     }
10083
10084   /* Consume the token.  */
10085   return cp_lexer_consume_token (parser->lexer)->u.value;
10086 }
10087
10088 /* Parse a linkage-specification.
10089
10090    linkage-specification:
10091      extern string-literal { declaration-seq [opt] }
10092      extern string-literal declaration  */
10093
10094 static void
10095 cp_parser_linkage_specification (cp_parser* parser)
10096 {
10097   tree linkage;
10098
10099   /* Look for the `extern' keyword.  */
10100   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10101
10102   /* Look for the string-literal.  */
10103   linkage = cp_parser_string_literal (parser, false, false);
10104
10105   /* Transform the literal into an identifier.  If the literal is a
10106      wide-character string, or contains embedded NULs, then we can't
10107      handle it as the user wants.  */
10108   if (strlen (TREE_STRING_POINTER (linkage))
10109       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10110     {
10111       cp_parser_error (parser, "invalid linkage-specification");
10112       /* Assume C++ linkage.  */
10113       linkage = lang_name_cplusplus;
10114     }
10115   else
10116     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10117
10118   /* We're now using the new linkage.  */
10119   push_lang_context (linkage);
10120
10121   /* If the next token is a `{', then we're using the first
10122      production.  */
10123   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10124     {
10125       /* Consume the `{' token.  */
10126       cp_lexer_consume_token (parser->lexer);
10127       /* Parse the declarations.  */
10128       cp_parser_declaration_seq_opt (parser);
10129       /* Look for the closing `}'.  */
10130       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10131     }
10132   /* Otherwise, there's just one declaration.  */
10133   else
10134     {
10135       bool saved_in_unbraced_linkage_specification_p;
10136
10137       saved_in_unbraced_linkage_specification_p
10138         = parser->in_unbraced_linkage_specification_p;
10139       parser->in_unbraced_linkage_specification_p = true;
10140       cp_parser_declaration (parser);
10141       parser->in_unbraced_linkage_specification_p
10142         = saved_in_unbraced_linkage_specification_p;
10143     }
10144
10145   /* We're done with the linkage-specification.  */
10146   pop_lang_context ();
10147 }
10148
10149 /* Parse a static_assert-declaration.
10150
10151    static_assert-declaration:
10152      static_assert ( constant-expression , string-literal ) ; 
10153
10154    If MEMBER_P, this static_assert is a class member.  */
10155
10156 static void 
10157 cp_parser_static_assert(cp_parser *parser, bool member_p)
10158 {
10159   tree condition;
10160   tree message;
10161   cp_token *token;
10162   location_t saved_loc;
10163
10164   /* Peek at the `static_assert' token so we can keep track of exactly
10165      where the static assertion started.  */
10166   token = cp_lexer_peek_token (parser->lexer);
10167   saved_loc = token->location;
10168
10169   /* Look for the `static_assert' keyword.  */
10170   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10171                                   RT_STATIC_ASSERT))
10172     return;
10173
10174   /*  We know we are in a static assertion; commit to any tentative
10175       parse.  */
10176   if (cp_parser_parsing_tentatively (parser))
10177     cp_parser_commit_to_tentative_parse (parser);
10178
10179   /* Parse the `(' starting the static assertion condition.  */
10180   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10181
10182   /* Parse the constant-expression.  */
10183   condition = 
10184     cp_parser_constant_expression (parser,
10185                                    /*allow_non_constant_p=*/false,
10186                                    /*non_constant_p=*/NULL);
10187
10188   /* Parse the separating `,'.  */
10189   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10190
10191   /* Parse the string-literal message.  */
10192   message = cp_parser_string_literal (parser, 
10193                                       /*translate=*/false,
10194                                       /*wide_ok=*/true);
10195
10196   /* A `)' completes the static assertion.  */
10197   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10198     cp_parser_skip_to_closing_parenthesis (parser, 
10199                                            /*recovering=*/true, 
10200                                            /*or_comma=*/false,
10201                                            /*consume_paren=*/true);
10202
10203   /* A semicolon terminates the declaration.  */
10204   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10205
10206   /* Complete the static assertion, which may mean either processing 
10207      the static assert now or saving it for template instantiation.  */
10208   finish_static_assert (condition, message, saved_loc, member_p);
10209 }
10210
10211 /* Parse a `decltype' type. Returns the type. 
10212
10213    simple-type-specifier:
10214      decltype ( expression )  */
10215
10216 static tree
10217 cp_parser_decltype (cp_parser *parser)
10218 {
10219   tree expr;
10220   bool id_expression_or_member_access_p = false;
10221   const char *saved_message;
10222   bool saved_integral_constant_expression_p;
10223   bool saved_non_integral_constant_expression_p;
10224   cp_token *id_expr_start_token;
10225
10226   /* Look for the `decltype' token.  */
10227   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10228     return error_mark_node;
10229
10230   /* Types cannot be defined in a `decltype' expression.  Save away the
10231      old message.  */
10232   saved_message = parser->type_definition_forbidden_message;
10233
10234   /* And create the new one.  */
10235   parser->type_definition_forbidden_message
10236     = G_("types may not be defined in %<decltype%> expressions");
10237
10238   /* The restrictions on constant-expressions do not apply inside
10239      decltype expressions.  */
10240   saved_integral_constant_expression_p
10241     = parser->integral_constant_expression_p;
10242   saved_non_integral_constant_expression_p
10243     = parser->non_integral_constant_expression_p;
10244   parser->integral_constant_expression_p = false;
10245
10246   /* Do not actually evaluate the expression.  */
10247   ++cp_unevaluated_operand;
10248
10249   /* Do not warn about problems with the expression.  */
10250   ++c_inhibit_evaluation_warnings;
10251
10252   /* Parse the opening `('.  */
10253   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10254     return error_mark_node;
10255   
10256   /* First, try parsing an id-expression.  */
10257   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10258   cp_parser_parse_tentatively (parser);
10259   expr = cp_parser_id_expression (parser,
10260                                   /*template_keyword_p=*/false,
10261                                   /*check_dependency_p=*/true,
10262                                   /*template_p=*/NULL,
10263                                   /*declarator_p=*/false,
10264                                   /*optional_p=*/false);
10265
10266   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10267     {
10268       bool non_integral_constant_expression_p = false;
10269       tree id_expression = expr;
10270       cp_id_kind idk;
10271       const char *error_msg;
10272
10273       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10274         /* Lookup the name we got back from the id-expression.  */
10275         expr = cp_parser_lookup_name (parser, expr,
10276                                       none_type,
10277                                       /*is_template=*/false,
10278                                       /*is_namespace=*/false,
10279                                       /*check_dependency=*/true,
10280                                       /*ambiguous_decls=*/NULL,
10281                                       id_expr_start_token->location);
10282
10283       if (expr
10284           && expr != error_mark_node
10285           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10286           && TREE_CODE (expr) != TYPE_DECL
10287           && (TREE_CODE (expr) != BIT_NOT_EXPR
10288               || !TYPE_P (TREE_OPERAND (expr, 0)))
10289           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10290         {
10291           /* Complete lookup of the id-expression.  */
10292           expr = (finish_id_expression
10293                   (id_expression, expr, parser->scope, &idk,
10294                    /*integral_constant_expression_p=*/false,
10295                    /*allow_non_integral_constant_expression_p=*/true,
10296                    &non_integral_constant_expression_p,
10297                    /*template_p=*/false,
10298                    /*done=*/true,
10299                    /*address_p=*/false,
10300                    /*template_arg_p=*/false,
10301                    &error_msg,
10302                    id_expr_start_token->location));
10303
10304           if (expr == error_mark_node)
10305             /* We found an id-expression, but it was something that we
10306                should not have found. This is an error, not something
10307                we can recover from, so note that we found an
10308                id-expression and we'll recover as gracefully as
10309                possible.  */
10310             id_expression_or_member_access_p = true;
10311         }
10312
10313       if (expr 
10314           && expr != error_mark_node
10315           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10316         /* We have an id-expression.  */
10317         id_expression_or_member_access_p = true;
10318     }
10319
10320   if (!id_expression_or_member_access_p)
10321     {
10322       /* Abort the id-expression parse.  */
10323       cp_parser_abort_tentative_parse (parser);
10324
10325       /* Parsing tentatively, again.  */
10326       cp_parser_parse_tentatively (parser);
10327
10328       /* Parse a class member access.  */
10329       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10330                                            /*cast_p=*/false,
10331                                            /*member_access_only_p=*/true, NULL);
10332
10333       if (expr 
10334           && expr != error_mark_node
10335           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10336         /* We have an id-expression.  */
10337         id_expression_or_member_access_p = true;
10338     }
10339
10340   if (id_expression_or_member_access_p)
10341     /* We have parsed the complete id-expression or member access.  */
10342     cp_parser_parse_definitely (parser);
10343   else
10344     {
10345       bool saved_greater_than_is_operator_p;
10346
10347       /* Abort our attempt to parse an id-expression or member access
10348          expression.  */
10349       cp_parser_abort_tentative_parse (parser);
10350
10351       /* Within a parenthesized expression, a `>' token is always
10352          the greater-than operator.  */
10353       saved_greater_than_is_operator_p
10354         = parser->greater_than_is_operator_p;
10355       parser->greater_than_is_operator_p = true;
10356
10357       /* Parse a full expression.  */
10358       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10359
10360       /* The `>' token might be the end of a template-id or
10361          template-parameter-list now.  */
10362       parser->greater_than_is_operator_p
10363         = saved_greater_than_is_operator_p;
10364     }
10365
10366   /* Go back to evaluating expressions.  */
10367   --cp_unevaluated_operand;
10368   --c_inhibit_evaluation_warnings;
10369
10370   /* Restore the old message and the integral constant expression
10371      flags.  */
10372   parser->type_definition_forbidden_message = saved_message;
10373   parser->integral_constant_expression_p
10374     = saved_integral_constant_expression_p;
10375   parser->non_integral_constant_expression_p
10376     = saved_non_integral_constant_expression_p;
10377
10378   if (expr == error_mark_node)
10379     {
10380       /* Skip everything up to the closing `)'.  */
10381       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10382                                              /*consume_paren=*/true);
10383       return error_mark_node;
10384     }
10385   
10386   /* Parse to the closing `)'.  */
10387   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10388     {
10389       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10390                                              /*consume_paren=*/true);
10391       return error_mark_node;
10392     }
10393
10394   return finish_decltype_type (expr, id_expression_or_member_access_p);
10395 }
10396
10397 /* Special member functions [gram.special] */
10398
10399 /* Parse a conversion-function-id.
10400
10401    conversion-function-id:
10402      operator conversion-type-id
10403
10404    Returns an IDENTIFIER_NODE representing the operator.  */
10405
10406 static tree
10407 cp_parser_conversion_function_id (cp_parser* parser)
10408 {
10409   tree type;
10410   tree saved_scope;
10411   tree saved_qualifying_scope;
10412   tree saved_object_scope;
10413   tree pushed_scope = NULL_TREE;
10414
10415   /* Look for the `operator' token.  */
10416   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10417     return error_mark_node;
10418   /* When we parse the conversion-type-id, the current scope will be
10419      reset.  However, we need that information in able to look up the
10420      conversion function later, so we save it here.  */
10421   saved_scope = parser->scope;
10422   saved_qualifying_scope = parser->qualifying_scope;
10423   saved_object_scope = parser->object_scope;
10424   /* We must enter the scope of the class so that the names of
10425      entities declared within the class are available in the
10426      conversion-type-id.  For example, consider:
10427
10428        struct S {
10429          typedef int I;
10430          operator I();
10431        };
10432
10433        S::operator I() { ... }
10434
10435      In order to see that `I' is a type-name in the definition, we
10436      must be in the scope of `S'.  */
10437   if (saved_scope)
10438     pushed_scope = push_scope (saved_scope);
10439   /* Parse the conversion-type-id.  */
10440   type = cp_parser_conversion_type_id (parser);
10441   /* Leave the scope of the class, if any.  */
10442   if (pushed_scope)
10443     pop_scope (pushed_scope);
10444   /* Restore the saved scope.  */
10445   parser->scope = saved_scope;
10446   parser->qualifying_scope = saved_qualifying_scope;
10447   parser->object_scope = saved_object_scope;
10448   /* If the TYPE is invalid, indicate failure.  */
10449   if (type == error_mark_node)
10450     return error_mark_node;
10451   return mangle_conv_op_name_for_type (type);
10452 }
10453
10454 /* Parse a conversion-type-id:
10455
10456    conversion-type-id:
10457      type-specifier-seq conversion-declarator [opt]
10458
10459    Returns the TYPE specified.  */
10460
10461 static tree
10462 cp_parser_conversion_type_id (cp_parser* parser)
10463 {
10464   tree attributes;
10465   cp_decl_specifier_seq type_specifiers;
10466   cp_declarator *declarator;
10467   tree type_specified;
10468
10469   /* Parse the attributes.  */
10470   attributes = cp_parser_attributes_opt (parser);
10471   /* Parse the type-specifiers.  */
10472   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10473                                 /*is_trailing_return=*/false,
10474                                 &type_specifiers);
10475   /* If that didn't work, stop.  */
10476   if (type_specifiers.type == error_mark_node)
10477     return error_mark_node;
10478   /* Parse the conversion-declarator.  */
10479   declarator = cp_parser_conversion_declarator_opt (parser);
10480
10481   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10482                                     /*initialized=*/0, &attributes);
10483   if (attributes)
10484     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10485
10486   /* Don't give this error when parsing tentatively.  This happens to
10487      work because we always parse this definitively once.  */
10488   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10489       && type_uses_auto (type_specified))
10490     {
10491       error ("invalid use of %<auto%> in conversion operator");
10492       return error_mark_node;
10493     }
10494
10495   return type_specified;
10496 }
10497
10498 /* Parse an (optional) conversion-declarator.
10499
10500    conversion-declarator:
10501      ptr-operator conversion-declarator [opt]
10502
10503    */
10504
10505 static cp_declarator *
10506 cp_parser_conversion_declarator_opt (cp_parser* parser)
10507 {
10508   enum tree_code code;
10509   tree class_type;
10510   cp_cv_quals cv_quals;
10511
10512   /* We don't know if there's a ptr-operator next, or not.  */
10513   cp_parser_parse_tentatively (parser);
10514   /* Try the ptr-operator.  */
10515   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10516   /* If it worked, look for more conversion-declarators.  */
10517   if (cp_parser_parse_definitely (parser))
10518     {
10519       cp_declarator *declarator;
10520
10521       /* Parse another optional declarator.  */
10522       declarator = cp_parser_conversion_declarator_opt (parser);
10523
10524       return cp_parser_make_indirect_declarator
10525         (code, class_type, cv_quals, declarator);
10526    }
10527
10528   return NULL;
10529 }
10530
10531 /* Parse an (optional) ctor-initializer.
10532
10533    ctor-initializer:
10534      : mem-initializer-list
10535
10536    Returns TRUE iff the ctor-initializer was actually present.  */
10537
10538 static bool
10539 cp_parser_ctor_initializer_opt (cp_parser* parser)
10540 {
10541   /* If the next token is not a `:', then there is no
10542      ctor-initializer.  */
10543   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10544     {
10545       /* Do default initialization of any bases and members.  */
10546       if (DECL_CONSTRUCTOR_P (current_function_decl))
10547         finish_mem_initializers (NULL_TREE);
10548
10549       return false;
10550     }
10551
10552   /* Consume the `:' token.  */
10553   cp_lexer_consume_token (parser->lexer);
10554   /* And the mem-initializer-list.  */
10555   cp_parser_mem_initializer_list (parser);
10556
10557   return true;
10558 }
10559
10560 /* Parse a mem-initializer-list.
10561
10562    mem-initializer-list:
10563      mem-initializer ... [opt]
10564      mem-initializer ... [opt] , mem-initializer-list  */
10565
10566 static void
10567 cp_parser_mem_initializer_list (cp_parser* parser)
10568 {
10569   tree mem_initializer_list = NULL_TREE;
10570   cp_token *token = cp_lexer_peek_token (parser->lexer);
10571
10572   /* Let the semantic analysis code know that we are starting the
10573      mem-initializer-list.  */
10574   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10575     error_at (token->location,
10576               "only constructors take member initializers");
10577
10578   /* Loop through the list.  */
10579   while (true)
10580     {
10581       tree mem_initializer;
10582
10583       token = cp_lexer_peek_token (parser->lexer);
10584       /* Parse the mem-initializer.  */
10585       mem_initializer = cp_parser_mem_initializer (parser);
10586       /* If the next token is a `...', we're expanding member initializers. */
10587       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10588         {
10589           /* Consume the `...'. */
10590           cp_lexer_consume_token (parser->lexer);
10591
10592           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10593              can be expanded but members cannot. */
10594           if (mem_initializer != error_mark_node
10595               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10596             {
10597               error_at (token->location,
10598                         "cannot expand initializer for member %<%D%>",
10599                         TREE_PURPOSE (mem_initializer));
10600               mem_initializer = error_mark_node;
10601             }
10602
10603           /* Construct the pack expansion type. */
10604           if (mem_initializer != error_mark_node)
10605             mem_initializer = make_pack_expansion (mem_initializer);
10606         }
10607       /* Add it to the list, unless it was erroneous.  */
10608       if (mem_initializer != error_mark_node)
10609         {
10610           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10611           mem_initializer_list = mem_initializer;
10612         }
10613       /* If the next token is not a `,', we're done.  */
10614       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10615         break;
10616       /* Consume the `,' token.  */
10617       cp_lexer_consume_token (parser->lexer);
10618     }
10619
10620   /* Perform semantic analysis.  */
10621   if (DECL_CONSTRUCTOR_P (current_function_decl))
10622     finish_mem_initializers (mem_initializer_list);
10623 }
10624
10625 /* Parse a mem-initializer.
10626
10627    mem-initializer:
10628      mem-initializer-id ( expression-list [opt] )
10629      mem-initializer-id braced-init-list
10630
10631    GNU extension:
10632
10633    mem-initializer:
10634      ( expression-list [opt] )
10635
10636    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10637    class) or FIELD_DECL (for a non-static data member) to initialize;
10638    the TREE_VALUE is the expression-list.  An empty initialization
10639    list is represented by void_list_node.  */
10640
10641 static tree
10642 cp_parser_mem_initializer (cp_parser* parser)
10643 {
10644   tree mem_initializer_id;
10645   tree expression_list;
10646   tree member;
10647   cp_token *token = cp_lexer_peek_token (parser->lexer);
10648
10649   /* Find out what is being initialized.  */
10650   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10651     {
10652       permerror (token->location,
10653                  "anachronistic old-style base class initializer");
10654       mem_initializer_id = NULL_TREE;
10655     }
10656   else
10657     {
10658       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10659       if (mem_initializer_id == error_mark_node)
10660         return mem_initializer_id;
10661     }
10662   member = expand_member_init (mem_initializer_id);
10663   if (member && !DECL_P (member))
10664     in_base_initializer = 1;
10665
10666   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10667     {
10668       bool expr_non_constant_p;
10669       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10670       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10671       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10672       expression_list = build_tree_list (NULL_TREE, expression_list);
10673     }
10674   else
10675     {
10676       VEC(tree,gc)* vec;
10677       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10678                                                      /*cast_p=*/false,
10679                                                      /*allow_expansion_p=*/true,
10680                                                      /*non_constant_p=*/NULL);
10681       if (vec == NULL)
10682         return error_mark_node;
10683       expression_list = build_tree_list_vec (vec);
10684       release_tree_vector (vec);
10685     }
10686
10687   if (expression_list == error_mark_node)
10688     return error_mark_node;
10689   if (!expression_list)
10690     expression_list = void_type_node;
10691
10692   in_base_initializer = 0;
10693
10694   return member ? build_tree_list (member, expression_list) : error_mark_node;
10695 }
10696
10697 /* Parse a mem-initializer-id.
10698
10699    mem-initializer-id:
10700      :: [opt] nested-name-specifier [opt] class-name
10701      identifier
10702
10703    Returns a TYPE indicating the class to be initializer for the first
10704    production.  Returns an IDENTIFIER_NODE indicating the data member
10705    to be initialized for the second production.  */
10706
10707 static tree
10708 cp_parser_mem_initializer_id (cp_parser* parser)
10709 {
10710   bool global_scope_p;
10711   bool nested_name_specifier_p;
10712   bool template_p = false;
10713   tree id;
10714
10715   cp_token *token = cp_lexer_peek_token (parser->lexer);
10716
10717   /* `typename' is not allowed in this context ([temp.res]).  */
10718   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10719     {
10720       error_at (token->location, 
10721                 "keyword %<typename%> not allowed in this context (a qualified "
10722                 "member initializer is implicitly a type)");
10723       cp_lexer_consume_token (parser->lexer);
10724     }
10725   /* Look for the optional `::' operator.  */
10726   global_scope_p
10727     = (cp_parser_global_scope_opt (parser,
10728                                    /*current_scope_valid_p=*/false)
10729        != NULL_TREE);
10730   /* Look for the optional nested-name-specifier.  The simplest way to
10731      implement:
10732
10733        [temp.res]
10734
10735        The keyword `typename' is not permitted in a base-specifier or
10736        mem-initializer; in these contexts a qualified name that
10737        depends on a template-parameter is implicitly assumed to be a
10738        type name.
10739
10740      is to assume that we have seen the `typename' keyword at this
10741      point.  */
10742   nested_name_specifier_p
10743     = (cp_parser_nested_name_specifier_opt (parser,
10744                                             /*typename_keyword_p=*/true,
10745                                             /*check_dependency_p=*/true,
10746                                             /*type_p=*/true,
10747                                             /*is_declaration=*/true)
10748        != NULL_TREE);
10749   if (nested_name_specifier_p)
10750     template_p = cp_parser_optional_template_keyword (parser);
10751   /* If there is a `::' operator or a nested-name-specifier, then we
10752      are definitely looking for a class-name.  */
10753   if (global_scope_p || nested_name_specifier_p)
10754     return cp_parser_class_name (parser,
10755                                  /*typename_keyword_p=*/true,
10756                                  /*template_keyword_p=*/template_p,
10757                                  typename_type,
10758                                  /*check_dependency_p=*/true,
10759                                  /*class_head_p=*/false,
10760                                  /*is_declaration=*/true);
10761   /* Otherwise, we could also be looking for an ordinary identifier.  */
10762   cp_parser_parse_tentatively (parser);
10763   /* Try a class-name.  */
10764   id = cp_parser_class_name (parser,
10765                              /*typename_keyword_p=*/true,
10766                              /*template_keyword_p=*/false,
10767                              none_type,
10768                              /*check_dependency_p=*/true,
10769                              /*class_head_p=*/false,
10770                              /*is_declaration=*/true);
10771   /* If we found one, we're done.  */
10772   if (cp_parser_parse_definitely (parser))
10773     return id;
10774   /* Otherwise, look for an ordinary identifier.  */
10775   return cp_parser_identifier (parser);
10776 }
10777
10778 /* Overloading [gram.over] */
10779
10780 /* Parse an operator-function-id.
10781
10782    operator-function-id:
10783      operator operator
10784
10785    Returns an IDENTIFIER_NODE for the operator which is a
10786    human-readable spelling of the identifier, e.g., `operator +'.  */
10787
10788 static tree
10789 cp_parser_operator_function_id (cp_parser* parser)
10790 {
10791   /* Look for the `operator' keyword.  */
10792   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10793     return error_mark_node;
10794   /* And then the name of the operator itself.  */
10795   return cp_parser_operator (parser);
10796 }
10797
10798 /* Parse an operator.
10799
10800    operator:
10801      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10802      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10803      || ++ -- , ->* -> () []
10804
10805    GNU Extensions:
10806
10807    operator:
10808      <? >? <?= >?=
10809
10810    Returns an IDENTIFIER_NODE for the operator which is a
10811    human-readable spelling of the identifier, e.g., `operator +'.  */
10812
10813 static tree
10814 cp_parser_operator (cp_parser* parser)
10815 {
10816   tree id = NULL_TREE;
10817   cp_token *token;
10818
10819   /* Peek at the next token.  */
10820   token = cp_lexer_peek_token (parser->lexer);
10821   /* Figure out which operator we have.  */
10822   switch (token->type)
10823     {
10824     case CPP_KEYWORD:
10825       {
10826         enum tree_code op;
10827
10828         /* The keyword should be either `new' or `delete'.  */
10829         if (token->keyword == RID_NEW)
10830           op = NEW_EXPR;
10831         else if (token->keyword == RID_DELETE)
10832           op = DELETE_EXPR;
10833         else
10834           break;
10835
10836         /* Consume the `new' or `delete' token.  */
10837         cp_lexer_consume_token (parser->lexer);
10838
10839         /* Peek at the next token.  */
10840         token = cp_lexer_peek_token (parser->lexer);
10841         /* If it's a `[' token then this is the array variant of the
10842            operator.  */
10843         if (token->type == CPP_OPEN_SQUARE)
10844           {
10845             /* Consume the `[' token.  */
10846             cp_lexer_consume_token (parser->lexer);
10847             /* Look for the `]' token.  */
10848             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10849             id = ansi_opname (op == NEW_EXPR
10850                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10851           }
10852         /* Otherwise, we have the non-array variant.  */
10853         else
10854           id = ansi_opname (op);
10855
10856         return id;
10857       }
10858
10859     case CPP_PLUS:
10860       id = ansi_opname (PLUS_EXPR);
10861       break;
10862
10863     case CPP_MINUS:
10864       id = ansi_opname (MINUS_EXPR);
10865       break;
10866
10867     case CPP_MULT:
10868       id = ansi_opname (MULT_EXPR);
10869       break;
10870
10871     case CPP_DIV:
10872       id = ansi_opname (TRUNC_DIV_EXPR);
10873       break;
10874
10875     case CPP_MOD:
10876       id = ansi_opname (TRUNC_MOD_EXPR);
10877       break;
10878
10879     case CPP_XOR:
10880       id = ansi_opname (BIT_XOR_EXPR);
10881       break;
10882
10883     case CPP_AND:
10884       id = ansi_opname (BIT_AND_EXPR);
10885       break;
10886
10887     case CPP_OR:
10888       id = ansi_opname (BIT_IOR_EXPR);
10889       break;
10890
10891     case CPP_COMPL:
10892       id = ansi_opname (BIT_NOT_EXPR);
10893       break;
10894
10895     case CPP_NOT:
10896       id = ansi_opname (TRUTH_NOT_EXPR);
10897       break;
10898
10899     case CPP_EQ:
10900       id = ansi_assopname (NOP_EXPR);
10901       break;
10902
10903     case CPP_LESS:
10904       id = ansi_opname (LT_EXPR);
10905       break;
10906
10907     case CPP_GREATER:
10908       id = ansi_opname (GT_EXPR);
10909       break;
10910
10911     case CPP_PLUS_EQ:
10912       id = ansi_assopname (PLUS_EXPR);
10913       break;
10914
10915     case CPP_MINUS_EQ:
10916       id = ansi_assopname (MINUS_EXPR);
10917       break;
10918
10919     case CPP_MULT_EQ:
10920       id = ansi_assopname (MULT_EXPR);
10921       break;
10922
10923     case CPP_DIV_EQ:
10924       id = ansi_assopname (TRUNC_DIV_EXPR);
10925       break;
10926
10927     case CPP_MOD_EQ:
10928       id = ansi_assopname (TRUNC_MOD_EXPR);
10929       break;
10930
10931     case CPP_XOR_EQ:
10932       id = ansi_assopname (BIT_XOR_EXPR);
10933       break;
10934
10935     case CPP_AND_EQ:
10936       id = ansi_assopname (BIT_AND_EXPR);
10937       break;
10938
10939     case CPP_OR_EQ:
10940       id = ansi_assopname (BIT_IOR_EXPR);
10941       break;
10942
10943     case CPP_LSHIFT:
10944       id = ansi_opname (LSHIFT_EXPR);
10945       break;
10946
10947     case CPP_RSHIFT:
10948       id = ansi_opname (RSHIFT_EXPR);
10949       break;
10950
10951     case CPP_LSHIFT_EQ:
10952       id = ansi_assopname (LSHIFT_EXPR);
10953       break;
10954
10955     case CPP_RSHIFT_EQ:
10956       id = ansi_assopname (RSHIFT_EXPR);
10957       break;
10958
10959     case CPP_EQ_EQ:
10960       id = ansi_opname (EQ_EXPR);
10961       break;
10962
10963     case CPP_NOT_EQ:
10964       id = ansi_opname (NE_EXPR);
10965       break;
10966
10967     case CPP_LESS_EQ:
10968       id = ansi_opname (LE_EXPR);
10969       break;
10970
10971     case CPP_GREATER_EQ:
10972       id = ansi_opname (GE_EXPR);
10973       break;
10974
10975     case CPP_AND_AND:
10976       id = ansi_opname (TRUTH_ANDIF_EXPR);
10977       break;
10978
10979     case CPP_OR_OR:
10980       id = ansi_opname (TRUTH_ORIF_EXPR);
10981       break;
10982
10983     case CPP_PLUS_PLUS:
10984       id = ansi_opname (POSTINCREMENT_EXPR);
10985       break;
10986
10987     case CPP_MINUS_MINUS:
10988       id = ansi_opname (PREDECREMENT_EXPR);
10989       break;
10990
10991     case CPP_COMMA:
10992       id = ansi_opname (COMPOUND_EXPR);
10993       break;
10994
10995     case CPP_DEREF_STAR:
10996       id = ansi_opname (MEMBER_REF);
10997       break;
10998
10999     case CPP_DEREF:
11000       id = ansi_opname (COMPONENT_REF);
11001       break;
11002
11003     case CPP_OPEN_PAREN:
11004       /* Consume the `('.  */
11005       cp_lexer_consume_token (parser->lexer);
11006       /* Look for the matching `)'.  */
11007       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11008       return ansi_opname (CALL_EXPR);
11009
11010     case CPP_OPEN_SQUARE:
11011       /* Consume the `['.  */
11012       cp_lexer_consume_token (parser->lexer);
11013       /* Look for the matching `]'.  */
11014       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11015       return ansi_opname (ARRAY_REF);
11016
11017     default:
11018       /* Anything else is an error.  */
11019       break;
11020     }
11021
11022   /* If we have selected an identifier, we need to consume the
11023      operator token.  */
11024   if (id)
11025     cp_lexer_consume_token (parser->lexer);
11026   /* Otherwise, no valid operator name was present.  */
11027   else
11028     {
11029       cp_parser_error (parser, "expected operator");
11030       id = error_mark_node;
11031     }
11032
11033   return id;
11034 }
11035
11036 /* Parse a template-declaration.
11037
11038    template-declaration:
11039      export [opt] template < template-parameter-list > declaration
11040
11041    If MEMBER_P is TRUE, this template-declaration occurs within a
11042    class-specifier.
11043
11044    The grammar rule given by the standard isn't correct.  What
11045    is really meant is:
11046
11047    template-declaration:
11048      export [opt] template-parameter-list-seq
11049        decl-specifier-seq [opt] init-declarator [opt] ;
11050      export [opt] template-parameter-list-seq
11051        function-definition
11052
11053    template-parameter-list-seq:
11054      template-parameter-list-seq [opt]
11055      template < template-parameter-list >  */
11056
11057 static void
11058 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11059 {
11060   /* Check for `export'.  */
11061   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11062     {
11063       /* Consume the `export' token.  */
11064       cp_lexer_consume_token (parser->lexer);
11065       /* Warn that we do not support `export'.  */
11066       warning (0, "keyword %<export%> not implemented, and will be ignored");
11067     }
11068
11069   cp_parser_template_declaration_after_export (parser, member_p);
11070 }
11071
11072 /* Parse a template-parameter-list.
11073
11074    template-parameter-list:
11075      template-parameter
11076      template-parameter-list , template-parameter
11077
11078    Returns a TREE_LIST.  Each node represents a template parameter.
11079    The nodes are connected via their TREE_CHAINs.  */
11080
11081 static tree
11082 cp_parser_template_parameter_list (cp_parser* parser)
11083 {
11084   tree parameter_list = NULL_TREE;
11085
11086   begin_template_parm_list ();
11087
11088   /* The loop below parses the template parms.  We first need to know
11089      the total number of template parms to be able to compute proper
11090      canonical types of each dependent type. So after the loop, when
11091      we know the total number of template parms,
11092      end_template_parm_list computes the proper canonical types and
11093      fixes up the dependent types accordingly.  */
11094   while (true)
11095     {
11096       tree parameter;
11097       bool is_non_type;
11098       bool is_parameter_pack;
11099       location_t parm_loc;
11100
11101       /* Parse the template-parameter.  */
11102       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11103       parameter = cp_parser_template_parameter (parser, 
11104                                                 &is_non_type,
11105                                                 &is_parameter_pack);
11106       /* Add it to the list.  */
11107       if (parameter != error_mark_node)
11108         parameter_list = process_template_parm (parameter_list,
11109                                                 parm_loc,
11110                                                 parameter,
11111                                                 is_non_type,
11112                                                 is_parameter_pack,
11113                                                 0);
11114       else
11115        {
11116          tree err_parm = build_tree_list (parameter, parameter);
11117          parameter_list = chainon (parameter_list, err_parm);
11118        }
11119
11120       /* If the next token is not a `,', we're done.  */
11121       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11122         break;
11123       /* Otherwise, consume the `,' token.  */
11124       cp_lexer_consume_token (parser->lexer);
11125     }
11126
11127   return end_template_parm_list (parameter_list);
11128 }
11129
11130 /* Parse a template-parameter.
11131
11132    template-parameter:
11133      type-parameter
11134      parameter-declaration
11135
11136    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11137    the parameter.  The TREE_PURPOSE is the default value, if any.
11138    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11139    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11140    set to true iff this parameter is a parameter pack. */
11141
11142 static tree
11143 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11144                               bool *is_parameter_pack)
11145 {
11146   cp_token *token;
11147   cp_parameter_declarator *parameter_declarator;
11148   cp_declarator *id_declarator;
11149   tree parm;
11150
11151   /* Assume it is a type parameter or a template parameter.  */
11152   *is_non_type = false;
11153   /* Assume it not a parameter pack. */
11154   *is_parameter_pack = false;
11155   /* Peek at the next token.  */
11156   token = cp_lexer_peek_token (parser->lexer);
11157   /* If it is `class' or `template', we have a type-parameter.  */
11158   if (token->keyword == RID_TEMPLATE)
11159     return cp_parser_type_parameter (parser, is_parameter_pack);
11160   /* If it is `class' or `typename' we do not know yet whether it is a
11161      type parameter or a non-type parameter.  Consider:
11162
11163        template <typename T, typename T::X X> ...
11164
11165      or:
11166
11167        template <class C, class D*> ...
11168
11169      Here, the first parameter is a type parameter, and the second is
11170      a non-type parameter.  We can tell by looking at the token after
11171      the identifier -- if it is a `,', `=', or `>' then we have a type
11172      parameter.  */
11173   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11174     {
11175       /* Peek at the token after `class' or `typename'.  */
11176       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11177       /* If it's an ellipsis, we have a template type parameter
11178          pack. */
11179       if (token->type == CPP_ELLIPSIS)
11180         return cp_parser_type_parameter (parser, is_parameter_pack);
11181       /* If it's an identifier, skip it.  */
11182       if (token->type == CPP_NAME)
11183         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11184       /* Now, see if the token looks like the end of a template
11185          parameter.  */
11186       if (token->type == CPP_COMMA
11187           || token->type == CPP_EQ
11188           || token->type == CPP_GREATER)
11189         return cp_parser_type_parameter (parser, is_parameter_pack);
11190     }
11191
11192   /* Otherwise, it is a non-type parameter.
11193
11194      [temp.param]
11195
11196      When parsing a default template-argument for a non-type
11197      template-parameter, the first non-nested `>' is taken as the end
11198      of the template parameter-list rather than a greater-than
11199      operator.  */
11200   *is_non_type = true;
11201   parameter_declarator
11202      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11203                                         /*parenthesized_p=*/NULL);
11204
11205   /* If the parameter declaration is marked as a parameter pack, set
11206      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11207      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11208      grokdeclarator. */
11209   if (parameter_declarator
11210       && parameter_declarator->declarator
11211       && parameter_declarator->declarator->parameter_pack_p)
11212     {
11213       *is_parameter_pack = true;
11214       parameter_declarator->declarator->parameter_pack_p = false;
11215     }
11216
11217   /* If the next token is an ellipsis, and we don't already have it
11218      marked as a parameter pack, then we have a parameter pack (that
11219      has no declarator).  */
11220   if (!*is_parameter_pack
11221       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11222       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11223     {
11224       /* Consume the `...'.  */
11225       cp_lexer_consume_token (parser->lexer);
11226       maybe_warn_variadic_templates ();
11227       
11228       *is_parameter_pack = true;
11229     }
11230   /* We might end up with a pack expansion as the type of the non-type
11231      template parameter, in which case this is a non-type template
11232      parameter pack.  */
11233   else if (parameter_declarator
11234            && parameter_declarator->decl_specifiers.type
11235            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11236     {
11237       *is_parameter_pack = true;
11238       parameter_declarator->decl_specifiers.type = 
11239         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11240     }
11241
11242   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11243     {
11244       /* Parameter packs cannot have default arguments.  However, a
11245          user may try to do so, so we'll parse them and give an
11246          appropriate diagnostic here.  */
11247
11248       /* Consume the `='.  */
11249       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11250       cp_lexer_consume_token (parser->lexer);
11251       
11252       /* Find the name of the parameter pack.  */     
11253       id_declarator = parameter_declarator->declarator;
11254       while (id_declarator && id_declarator->kind != cdk_id)
11255         id_declarator = id_declarator->declarator;
11256       
11257       if (id_declarator && id_declarator->kind == cdk_id)
11258         error_at (start_token->location,
11259                   "template parameter pack %qD cannot have a default argument",
11260                   id_declarator->u.id.unqualified_name);
11261       else
11262         error_at (start_token->location,
11263                   "template parameter pack cannot have a default argument");
11264       
11265       /* Parse the default argument, but throw away the result.  */
11266       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11267     }
11268
11269   parm = grokdeclarator (parameter_declarator->declarator,
11270                          &parameter_declarator->decl_specifiers,
11271                          TPARM, /*initialized=*/0,
11272                          /*attrlist=*/NULL);
11273   if (parm == error_mark_node)
11274     return error_mark_node;
11275
11276   return build_tree_list (parameter_declarator->default_argument, parm);
11277 }
11278
11279 /* Parse a type-parameter.
11280
11281    type-parameter:
11282      class identifier [opt]
11283      class identifier [opt] = type-id
11284      typename identifier [opt]
11285      typename identifier [opt] = type-id
11286      template < template-parameter-list > class identifier [opt]
11287      template < template-parameter-list > class identifier [opt]
11288        = id-expression
11289
11290    GNU Extension (variadic templates):
11291
11292    type-parameter:
11293      class ... identifier [opt]
11294      typename ... identifier [opt]
11295
11296    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11297    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11298    the declaration of the parameter.
11299
11300    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11301
11302 static tree
11303 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11304 {
11305   cp_token *token;
11306   tree parameter;
11307
11308   /* Look for a keyword to tell us what kind of parameter this is.  */
11309   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11310   if (!token)
11311     return error_mark_node;
11312
11313   switch (token->keyword)
11314     {
11315     case RID_CLASS:
11316     case RID_TYPENAME:
11317       {
11318         tree identifier;
11319         tree default_argument;
11320
11321         /* If the next token is an ellipsis, we have a template
11322            argument pack. */
11323         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11324           {
11325             /* Consume the `...' token. */
11326             cp_lexer_consume_token (parser->lexer);
11327             maybe_warn_variadic_templates ();
11328
11329             *is_parameter_pack = true;
11330           }
11331
11332         /* If the next token is an identifier, then it names the
11333            parameter.  */
11334         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11335           identifier = cp_parser_identifier (parser);
11336         else
11337           identifier = NULL_TREE;
11338
11339         /* Create the parameter.  */
11340         parameter = finish_template_type_parm (class_type_node, identifier);
11341
11342         /* If the next token is an `=', we have a default argument.  */
11343         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11344           {
11345             /* Consume the `=' token.  */
11346             cp_lexer_consume_token (parser->lexer);
11347             /* Parse the default-argument.  */
11348             push_deferring_access_checks (dk_no_deferred);
11349             default_argument = cp_parser_type_id (parser);
11350
11351             /* Template parameter packs cannot have default
11352                arguments. */
11353             if (*is_parameter_pack)
11354               {
11355                 if (identifier)
11356                   error_at (token->location,
11357                             "template parameter pack %qD cannot have a "
11358                             "default argument", identifier);
11359                 else
11360                   error_at (token->location,
11361                             "template parameter packs cannot have "
11362                             "default arguments");
11363                 default_argument = NULL_TREE;
11364               }
11365             pop_deferring_access_checks ();
11366           }
11367         else
11368           default_argument = NULL_TREE;
11369
11370         /* Create the combined representation of the parameter and the
11371            default argument.  */
11372         parameter = build_tree_list (default_argument, parameter);
11373       }
11374       break;
11375
11376     case RID_TEMPLATE:
11377       {
11378         tree identifier;
11379         tree default_argument;
11380
11381         /* Look for the `<'.  */
11382         cp_parser_require (parser, CPP_LESS, RT_LESS);
11383         /* Parse the template-parameter-list.  */
11384         cp_parser_template_parameter_list (parser);
11385         /* Look for the `>'.  */
11386         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11387         /* Look for the `class' keyword.  */
11388         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11389         /* If the next token is an ellipsis, we have a template
11390            argument pack. */
11391         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11392           {
11393             /* Consume the `...' token. */
11394             cp_lexer_consume_token (parser->lexer);
11395             maybe_warn_variadic_templates ();
11396
11397             *is_parameter_pack = true;
11398           }
11399         /* If the next token is an `=', then there is a
11400            default-argument.  If the next token is a `>', we are at
11401            the end of the parameter-list.  If the next token is a `,',
11402            then we are at the end of this parameter.  */
11403         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11404             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11405             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11406           {
11407             identifier = cp_parser_identifier (parser);
11408             /* Treat invalid names as if the parameter were nameless.  */
11409             if (identifier == error_mark_node)
11410               identifier = NULL_TREE;
11411           }
11412         else
11413           identifier = NULL_TREE;
11414
11415         /* Create the template parameter.  */
11416         parameter = finish_template_template_parm (class_type_node,
11417                                                    identifier);
11418
11419         /* If the next token is an `=', then there is a
11420            default-argument.  */
11421         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11422           {
11423             bool is_template;
11424
11425             /* Consume the `='.  */
11426             cp_lexer_consume_token (parser->lexer);
11427             /* Parse the id-expression.  */
11428             push_deferring_access_checks (dk_no_deferred);
11429             /* save token before parsing the id-expression, for error
11430                reporting */
11431             token = cp_lexer_peek_token (parser->lexer);
11432             default_argument
11433               = cp_parser_id_expression (parser,
11434                                          /*template_keyword_p=*/false,
11435                                          /*check_dependency_p=*/true,
11436                                          /*template_p=*/&is_template,
11437                                          /*declarator_p=*/false,
11438                                          /*optional_p=*/false);
11439             if (TREE_CODE (default_argument) == TYPE_DECL)
11440               /* If the id-expression was a template-id that refers to
11441                  a template-class, we already have the declaration here,
11442                  so no further lookup is needed.  */
11443                  ;
11444             else
11445               /* Look up the name.  */
11446               default_argument
11447                 = cp_parser_lookup_name (parser, default_argument,
11448                                          none_type,
11449                                          /*is_template=*/is_template,
11450                                          /*is_namespace=*/false,
11451                                          /*check_dependency=*/true,
11452                                          /*ambiguous_decls=*/NULL,
11453                                          token->location);
11454             /* See if the default argument is valid.  */
11455             default_argument
11456               = check_template_template_default_arg (default_argument);
11457
11458             /* Template parameter packs cannot have default
11459                arguments. */
11460             if (*is_parameter_pack)
11461               {
11462                 if (identifier)
11463                   error_at (token->location,
11464                             "template parameter pack %qD cannot "
11465                             "have a default argument",
11466                             identifier);
11467                 else
11468                   error_at (token->location, "template parameter packs cannot "
11469                             "have default arguments");
11470                 default_argument = NULL_TREE;
11471               }
11472             pop_deferring_access_checks ();
11473           }
11474         else
11475           default_argument = NULL_TREE;
11476
11477         /* Create the combined representation of the parameter and the
11478            default argument.  */
11479         parameter = build_tree_list (default_argument, parameter);
11480       }
11481       break;
11482
11483     default:
11484       gcc_unreachable ();
11485       break;
11486     }
11487
11488   return parameter;
11489 }
11490
11491 /* Parse a template-id.
11492
11493    template-id:
11494      template-name < template-argument-list [opt] >
11495
11496    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11497    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11498    returned.  Otherwise, if the template-name names a function, or set
11499    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11500    names a class, returns a TYPE_DECL for the specialization.
11501
11502    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11503    uninstantiated templates.  */
11504
11505 static tree
11506 cp_parser_template_id (cp_parser *parser,
11507                        bool template_keyword_p,
11508                        bool check_dependency_p,
11509                        bool is_declaration)
11510 {
11511   int i;
11512   tree templ;
11513   tree arguments;
11514   tree template_id;
11515   cp_token_position start_of_id = 0;
11516   deferred_access_check *chk;
11517   VEC (deferred_access_check,gc) *access_check;
11518   cp_token *next_token = NULL, *next_token_2 = NULL;
11519   bool is_identifier;
11520
11521   /* If the next token corresponds to a template-id, there is no need
11522      to reparse it.  */
11523   next_token = cp_lexer_peek_token (parser->lexer);
11524   if (next_token->type == CPP_TEMPLATE_ID)
11525     {
11526       struct tree_check *check_value;
11527
11528       /* Get the stored value.  */
11529       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11530       /* Perform any access checks that were deferred.  */
11531       access_check = check_value->checks;
11532       if (access_check)
11533         {
11534           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11535             perform_or_defer_access_check (chk->binfo,
11536                                            chk->decl,
11537                                            chk->diag_decl);
11538         }
11539       /* Return the stored value.  */
11540       return check_value->value;
11541     }
11542
11543   /* Avoid performing name lookup if there is no possibility of
11544      finding a template-id.  */
11545   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11546       || (next_token->type == CPP_NAME
11547           && !cp_parser_nth_token_starts_template_argument_list_p
11548                (parser, 2)))
11549     {
11550       cp_parser_error (parser, "expected template-id");
11551       return error_mark_node;
11552     }
11553
11554   /* Remember where the template-id starts.  */
11555   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11556     start_of_id = cp_lexer_token_position (parser->lexer, false);
11557
11558   push_deferring_access_checks (dk_deferred);
11559
11560   /* Parse the template-name.  */
11561   is_identifier = false;
11562   templ = cp_parser_template_name (parser, template_keyword_p,
11563                                    check_dependency_p,
11564                                    is_declaration,
11565                                    &is_identifier);
11566   if (templ == error_mark_node || is_identifier)
11567     {
11568       pop_deferring_access_checks ();
11569       return templ;
11570     }
11571
11572   /* If we find the sequence `[:' after a template-name, it's probably
11573      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11574      parse correctly the argument list.  */
11575   next_token = cp_lexer_peek_token (parser->lexer);
11576   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11577   if (next_token->type == CPP_OPEN_SQUARE
11578       && next_token->flags & DIGRAPH
11579       && next_token_2->type == CPP_COLON
11580       && !(next_token_2->flags & PREV_WHITE))
11581     {
11582       cp_parser_parse_tentatively (parser);
11583       /* Change `:' into `::'.  */
11584       next_token_2->type = CPP_SCOPE;
11585       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11586          CPP_LESS.  */
11587       cp_lexer_consume_token (parser->lexer);
11588
11589       /* Parse the arguments.  */
11590       arguments = cp_parser_enclosed_template_argument_list (parser);
11591       if (!cp_parser_parse_definitely (parser))
11592         {
11593           /* If we couldn't parse an argument list, then we revert our changes
11594              and return simply an error. Maybe this is not a template-id
11595              after all.  */
11596           next_token_2->type = CPP_COLON;
11597           cp_parser_error (parser, "expected %<<%>");
11598           pop_deferring_access_checks ();
11599           return error_mark_node;
11600         }
11601       /* Otherwise, emit an error about the invalid digraph, but continue
11602          parsing because we got our argument list.  */
11603       if (permerror (next_token->location,
11604                      "%<<::%> cannot begin a template-argument list"))
11605         {
11606           static bool hint = false;
11607           inform (next_token->location,
11608                   "%<<:%> is an alternate spelling for %<[%>."
11609                   " Insert whitespace between %<<%> and %<::%>");
11610           if (!hint && !flag_permissive)
11611             {
11612               inform (next_token->location, "(if you use %<-fpermissive%>"
11613                       " G++ will accept your code)");
11614               hint = true;
11615             }
11616         }
11617     }
11618   else
11619     {
11620       /* Look for the `<' that starts the template-argument-list.  */
11621       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11622         {
11623           pop_deferring_access_checks ();
11624           return error_mark_node;
11625         }
11626       /* Parse the arguments.  */
11627       arguments = cp_parser_enclosed_template_argument_list (parser);
11628     }
11629
11630   /* Build a representation of the specialization.  */
11631   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11632     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11633   else if (DECL_CLASS_TEMPLATE_P (templ)
11634            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11635     {
11636       bool entering_scope;
11637       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11638          template (rather than some instantiation thereof) only if
11639          is not nested within some other construct.  For example, in
11640          "template <typename T> void f(T) { A<T>::", A<T> is just an
11641          instantiation of A.  */
11642       entering_scope = (template_parm_scope_p ()
11643                         && cp_lexer_next_token_is (parser->lexer,
11644                                                    CPP_SCOPE));
11645       template_id
11646         = finish_template_type (templ, arguments, entering_scope);
11647     }
11648   else
11649     {
11650       /* If it's not a class-template or a template-template, it should be
11651          a function-template.  */
11652       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11653                    || TREE_CODE (templ) == OVERLOAD
11654                    || BASELINK_P (templ)));
11655
11656       template_id = lookup_template_function (templ, arguments);
11657     }
11658
11659   /* If parsing tentatively, replace the sequence of tokens that makes
11660      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11661      should we re-parse the token stream, we will not have to repeat
11662      the effort required to do the parse, nor will we issue duplicate
11663      error messages about problems during instantiation of the
11664      template.  */
11665   if (start_of_id)
11666     {
11667       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11668
11669       /* Reset the contents of the START_OF_ID token.  */
11670       token->type = CPP_TEMPLATE_ID;
11671       /* Retrieve any deferred checks.  Do not pop this access checks yet
11672          so the memory will not be reclaimed during token replacing below.  */
11673       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11674       token->u.tree_check_value->value = template_id;
11675       token->u.tree_check_value->checks = get_deferred_access_checks ();
11676       token->keyword = RID_MAX;
11677
11678       /* Purge all subsequent tokens.  */
11679       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11680
11681       /* ??? Can we actually assume that, if template_id ==
11682          error_mark_node, we will have issued a diagnostic to the
11683          user, as opposed to simply marking the tentative parse as
11684          failed?  */
11685       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11686         error_at (token->location, "parse error in template argument list");
11687     }
11688
11689   pop_deferring_access_checks ();
11690   return template_id;
11691 }
11692
11693 /* Parse a template-name.
11694
11695    template-name:
11696      identifier
11697
11698    The standard should actually say:
11699
11700    template-name:
11701      identifier
11702      operator-function-id
11703
11704    A defect report has been filed about this issue.
11705
11706    A conversion-function-id cannot be a template name because they cannot
11707    be part of a template-id. In fact, looking at this code:
11708
11709    a.operator K<int>()
11710
11711    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11712    It is impossible to call a templated conversion-function-id with an
11713    explicit argument list, since the only allowed template parameter is
11714    the type to which it is converting.
11715
11716    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11717    `template' keyword, in a construction like:
11718
11719      T::template f<3>()
11720
11721    In that case `f' is taken to be a template-name, even though there
11722    is no way of knowing for sure.
11723
11724    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11725    name refers to a set of overloaded functions, at least one of which
11726    is a template, or an IDENTIFIER_NODE with the name of the template,
11727    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11728    names are looked up inside uninstantiated templates.  */
11729
11730 static tree
11731 cp_parser_template_name (cp_parser* parser,
11732                          bool template_keyword_p,
11733                          bool check_dependency_p,
11734                          bool is_declaration,
11735                          bool *is_identifier)
11736 {
11737   tree identifier;
11738   tree decl;
11739   tree fns;
11740   cp_token *token = cp_lexer_peek_token (parser->lexer);
11741
11742   /* If the next token is `operator', then we have either an
11743      operator-function-id or a conversion-function-id.  */
11744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11745     {
11746       /* We don't know whether we're looking at an
11747          operator-function-id or a conversion-function-id.  */
11748       cp_parser_parse_tentatively (parser);
11749       /* Try an operator-function-id.  */
11750       identifier = cp_parser_operator_function_id (parser);
11751       /* If that didn't work, try a conversion-function-id.  */
11752       if (!cp_parser_parse_definitely (parser))
11753         {
11754           cp_parser_error (parser, "expected template-name");
11755           return error_mark_node;
11756         }
11757     }
11758   /* Look for the identifier.  */
11759   else
11760     identifier = cp_parser_identifier (parser);
11761
11762   /* If we didn't find an identifier, we don't have a template-id.  */
11763   if (identifier == error_mark_node)
11764     return error_mark_node;
11765
11766   /* If the name immediately followed the `template' keyword, then it
11767      is a template-name.  However, if the next token is not `<', then
11768      we do not treat it as a template-name, since it is not being used
11769      as part of a template-id.  This enables us to handle constructs
11770      like:
11771
11772        template <typename T> struct S { S(); };
11773        template <typename T> S<T>::S();
11774
11775      correctly.  We would treat `S' as a template -- if it were `S<T>'
11776      -- but we do not if there is no `<'.  */
11777
11778   if (processing_template_decl
11779       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11780     {
11781       /* In a declaration, in a dependent context, we pretend that the
11782          "template" keyword was present in order to improve error
11783          recovery.  For example, given:
11784
11785            template <typename T> void f(T::X<int>);
11786
11787          we want to treat "X<int>" as a template-id.  */
11788       if (is_declaration
11789           && !template_keyword_p
11790           && parser->scope && TYPE_P (parser->scope)
11791           && check_dependency_p
11792           && dependent_scope_p (parser->scope)
11793           /* Do not do this for dtors (or ctors), since they never
11794              need the template keyword before their name.  */
11795           && !constructor_name_p (identifier, parser->scope))
11796         {
11797           cp_token_position start = 0;
11798
11799           /* Explain what went wrong.  */
11800           error_at (token->location, "non-template %qD used as template",
11801                     identifier);
11802           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11803                   parser->scope, identifier);
11804           /* If parsing tentatively, find the location of the "<" token.  */
11805           if (cp_parser_simulate_error (parser))
11806             start = cp_lexer_token_position (parser->lexer, true);
11807           /* Parse the template arguments so that we can issue error
11808              messages about them.  */
11809           cp_lexer_consume_token (parser->lexer);
11810           cp_parser_enclosed_template_argument_list (parser);
11811           /* Skip tokens until we find a good place from which to
11812              continue parsing.  */
11813           cp_parser_skip_to_closing_parenthesis (parser,
11814                                                  /*recovering=*/true,
11815                                                  /*or_comma=*/true,
11816                                                  /*consume_paren=*/false);
11817           /* If parsing tentatively, permanently remove the
11818              template argument list.  That will prevent duplicate
11819              error messages from being issued about the missing
11820              "template" keyword.  */
11821           if (start)
11822             cp_lexer_purge_tokens_after (parser->lexer, start);
11823           if (is_identifier)
11824             *is_identifier = true;
11825           return identifier;
11826         }
11827
11828       /* If the "template" keyword is present, then there is generally
11829          no point in doing name-lookup, so we just return IDENTIFIER.
11830          But, if the qualifying scope is non-dependent then we can
11831          (and must) do name-lookup normally.  */
11832       if (template_keyword_p
11833           && (!parser->scope
11834               || (TYPE_P (parser->scope)
11835                   && dependent_type_p (parser->scope))))
11836         return identifier;
11837     }
11838
11839   /* Look up the name.  */
11840   decl = cp_parser_lookup_name (parser, identifier,
11841                                 none_type,
11842                                 /*is_template=*/true,
11843                                 /*is_namespace=*/false,
11844                                 check_dependency_p,
11845                                 /*ambiguous_decls=*/NULL,
11846                                 token->location);
11847
11848   /* If DECL is a template, then the name was a template-name.  */
11849   if (TREE_CODE (decl) == TEMPLATE_DECL)
11850     ;
11851   else
11852     {
11853       tree fn = NULL_TREE;
11854
11855       /* The standard does not explicitly indicate whether a name that
11856          names a set of overloaded declarations, some of which are
11857          templates, is a template-name.  However, such a name should
11858          be a template-name; otherwise, there is no way to form a
11859          template-id for the overloaded templates.  */
11860       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11861       if (TREE_CODE (fns) == OVERLOAD)
11862         for (fn = fns; fn; fn = OVL_NEXT (fn))
11863           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11864             break;
11865
11866       if (!fn)
11867         {
11868           /* The name does not name a template.  */
11869           cp_parser_error (parser, "expected template-name");
11870           return error_mark_node;
11871         }
11872     }
11873
11874   /* If DECL is dependent, and refers to a function, then just return
11875      its name; we will look it up again during template instantiation.  */
11876   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11877     {
11878       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11879       if (TYPE_P (scope) && dependent_type_p (scope))
11880         return identifier;
11881     }
11882
11883   return decl;
11884 }
11885
11886 /* Parse a template-argument-list.
11887
11888    template-argument-list:
11889      template-argument ... [opt]
11890      template-argument-list , template-argument ... [opt]
11891
11892    Returns a TREE_VEC containing the arguments.  */
11893
11894 static tree
11895 cp_parser_template_argument_list (cp_parser* parser)
11896 {
11897   tree fixed_args[10];
11898   unsigned n_args = 0;
11899   unsigned alloced = 10;
11900   tree *arg_ary = fixed_args;
11901   tree vec;
11902   bool saved_in_template_argument_list_p;
11903   bool saved_ice_p;
11904   bool saved_non_ice_p;
11905
11906   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11907   parser->in_template_argument_list_p = true;
11908   /* Even if the template-id appears in an integral
11909      constant-expression, the contents of the argument list do
11910      not.  */
11911   saved_ice_p = parser->integral_constant_expression_p;
11912   parser->integral_constant_expression_p = false;
11913   saved_non_ice_p = parser->non_integral_constant_expression_p;
11914   parser->non_integral_constant_expression_p = false;
11915   /* Parse the arguments.  */
11916   do
11917     {
11918       tree argument;
11919
11920       if (n_args)
11921         /* Consume the comma.  */
11922         cp_lexer_consume_token (parser->lexer);
11923
11924       /* Parse the template-argument.  */
11925       argument = cp_parser_template_argument (parser);
11926
11927       /* If the next token is an ellipsis, we're expanding a template
11928          argument pack. */
11929       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11930         {
11931           if (argument == error_mark_node)
11932             {
11933               cp_token *token = cp_lexer_peek_token (parser->lexer);
11934               error_at (token->location,
11935                         "expected parameter pack before %<...%>");
11936             }
11937           /* Consume the `...' token. */
11938           cp_lexer_consume_token (parser->lexer);
11939
11940           /* Make the argument into a TYPE_PACK_EXPANSION or
11941              EXPR_PACK_EXPANSION. */
11942           argument = make_pack_expansion (argument);
11943         }
11944
11945       if (n_args == alloced)
11946         {
11947           alloced *= 2;
11948
11949           if (arg_ary == fixed_args)
11950             {
11951               arg_ary = XNEWVEC (tree, alloced);
11952               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11953             }
11954           else
11955             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11956         }
11957       arg_ary[n_args++] = argument;
11958     }
11959   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11960
11961   vec = make_tree_vec (n_args);
11962
11963   while (n_args--)
11964     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11965
11966   if (arg_ary != fixed_args)
11967     free (arg_ary);
11968   parser->non_integral_constant_expression_p = saved_non_ice_p;
11969   parser->integral_constant_expression_p = saved_ice_p;
11970   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11971 #ifdef ENABLE_CHECKING
11972   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11973 #endif
11974   return vec;
11975 }
11976
11977 /* Parse a template-argument.
11978
11979    template-argument:
11980      assignment-expression
11981      type-id
11982      id-expression
11983
11984    The representation is that of an assignment-expression, type-id, or
11985    id-expression -- except that the qualified id-expression is
11986    evaluated, so that the value returned is either a DECL or an
11987    OVERLOAD.
11988
11989    Although the standard says "assignment-expression", it forbids
11990    throw-expressions or assignments in the template argument.
11991    Therefore, we use "conditional-expression" instead.  */
11992
11993 static tree
11994 cp_parser_template_argument (cp_parser* parser)
11995 {
11996   tree argument;
11997   bool template_p;
11998   bool address_p;
11999   bool maybe_type_id = false;
12000   cp_token *token = NULL, *argument_start_token = NULL;
12001   cp_id_kind idk;
12002
12003   /* There's really no way to know what we're looking at, so we just
12004      try each alternative in order.
12005
12006        [temp.arg]
12007
12008        In a template-argument, an ambiguity between a type-id and an
12009        expression is resolved to a type-id, regardless of the form of
12010        the corresponding template-parameter.
12011
12012      Therefore, we try a type-id first.  */
12013   cp_parser_parse_tentatively (parser);
12014   argument = cp_parser_template_type_arg (parser);
12015   /* If there was no error parsing the type-id but the next token is a
12016      '>>', our behavior depends on which dialect of C++ we're
12017      parsing. In C++98, we probably found a typo for '> >'. But there
12018      are type-id which are also valid expressions. For instance:
12019
12020      struct X { int operator >> (int); };
12021      template <int V> struct Foo {};
12022      Foo<X () >> 5> r;
12023
12024      Here 'X()' is a valid type-id of a function type, but the user just
12025      wanted to write the expression "X() >> 5". Thus, we remember that we
12026      found a valid type-id, but we still try to parse the argument as an
12027      expression to see what happens. 
12028
12029      In C++0x, the '>>' will be considered two separate '>'
12030      tokens.  */
12031   if (!cp_parser_error_occurred (parser)
12032       && cxx_dialect == cxx98
12033       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12034     {
12035       maybe_type_id = true;
12036       cp_parser_abort_tentative_parse (parser);
12037     }
12038   else
12039     {
12040       /* If the next token isn't a `,' or a `>', then this argument wasn't
12041       really finished. This means that the argument is not a valid
12042       type-id.  */
12043       if (!cp_parser_next_token_ends_template_argument_p (parser))
12044         cp_parser_error (parser, "expected template-argument");
12045       /* If that worked, we're done.  */
12046       if (cp_parser_parse_definitely (parser))
12047         return argument;
12048     }
12049   /* We're still not sure what the argument will be.  */
12050   cp_parser_parse_tentatively (parser);
12051   /* Try a template.  */
12052   argument_start_token = cp_lexer_peek_token (parser->lexer);
12053   argument = cp_parser_id_expression (parser,
12054                                       /*template_keyword_p=*/false,
12055                                       /*check_dependency_p=*/true,
12056                                       &template_p,
12057                                       /*declarator_p=*/false,
12058                                       /*optional_p=*/false);
12059   /* If the next token isn't a `,' or a `>', then this argument wasn't
12060      really finished.  */
12061   if (!cp_parser_next_token_ends_template_argument_p (parser))
12062     cp_parser_error (parser, "expected template-argument");
12063   if (!cp_parser_error_occurred (parser))
12064     {
12065       /* Figure out what is being referred to.  If the id-expression
12066          was for a class template specialization, then we will have a
12067          TYPE_DECL at this point.  There is no need to do name lookup
12068          at this point in that case.  */
12069       if (TREE_CODE (argument) != TYPE_DECL)
12070         argument = cp_parser_lookup_name (parser, argument,
12071                                           none_type,
12072                                           /*is_template=*/template_p,
12073                                           /*is_namespace=*/false,
12074                                           /*check_dependency=*/true,
12075                                           /*ambiguous_decls=*/NULL,
12076                                           argument_start_token->location);
12077       if (TREE_CODE (argument) != TEMPLATE_DECL
12078           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12079         cp_parser_error (parser, "expected template-name");
12080     }
12081   if (cp_parser_parse_definitely (parser))
12082     return argument;
12083   /* It must be a non-type argument.  There permitted cases are given
12084      in [temp.arg.nontype]:
12085
12086      -- an integral constant-expression of integral or enumeration
12087         type; or
12088
12089      -- the name of a non-type template-parameter; or
12090
12091      -- the name of an object or function with external linkage...
12092
12093      -- the address of an object or function with external linkage...
12094
12095      -- a pointer to member...  */
12096   /* Look for a non-type template parameter.  */
12097   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12098     {
12099       cp_parser_parse_tentatively (parser);
12100       argument = cp_parser_primary_expression (parser,
12101                                                /*address_p=*/false,
12102                                                /*cast_p=*/false,
12103                                                /*template_arg_p=*/true,
12104                                                &idk);
12105       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12106           || !cp_parser_next_token_ends_template_argument_p (parser))
12107         cp_parser_simulate_error (parser);
12108       if (cp_parser_parse_definitely (parser))
12109         return argument;
12110     }
12111
12112   /* If the next token is "&", the argument must be the address of an
12113      object or function with external linkage.  */
12114   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12115   if (address_p)
12116     cp_lexer_consume_token (parser->lexer);
12117   /* See if we might have an id-expression.  */
12118   token = cp_lexer_peek_token (parser->lexer);
12119   if (token->type == CPP_NAME
12120       || token->keyword == RID_OPERATOR
12121       || token->type == CPP_SCOPE
12122       || token->type == CPP_TEMPLATE_ID
12123       || token->type == CPP_NESTED_NAME_SPECIFIER)
12124     {
12125       cp_parser_parse_tentatively (parser);
12126       argument = cp_parser_primary_expression (parser,
12127                                                address_p,
12128                                                /*cast_p=*/false,
12129                                                /*template_arg_p=*/true,
12130                                                &idk);
12131       if (cp_parser_error_occurred (parser)
12132           || !cp_parser_next_token_ends_template_argument_p (parser))
12133         cp_parser_abort_tentative_parse (parser);
12134       else
12135         {
12136           tree probe;
12137
12138           if (TREE_CODE (argument) == INDIRECT_REF)
12139             {
12140               gcc_assert (REFERENCE_REF_P (argument));
12141               argument = TREE_OPERAND (argument, 0);
12142             }
12143
12144           /* If we're in a template, we represent a qualified-id referring
12145              to a static data member as a SCOPE_REF even if the scope isn't
12146              dependent so that we can check access control later.  */
12147           probe = argument;
12148           if (TREE_CODE (probe) == SCOPE_REF)
12149             probe = TREE_OPERAND (probe, 1);
12150           if (TREE_CODE (probe) == VAR_DECL)
12151             {
12152               /* A variable without external linkage might still be a
12153                  valid constant-expression, so no error is issued here
12154                  if the external-linkage check fails.  */
12155               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12156                 cp_parser_simulate_error (parser);
12157             }
12158           else if (is_overloaded_fn (argument))
12159             /* All overloaded functions are allowed; if the external
12160                linkage test does not pass, an error will be issued
12161                later.  */
12162             ;
12163           else if (address_p
12164                    && (TREE_CODE (argument) == OFFSET_REF
12165                        || TREE_CODE (argument) == SCOPE_REF))
12166             /* A pointer-to-member.  */
12167             ;
12168           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12169             ;
12170           else
12171             cp_parser_simulate_error (parser);
12172
12173           if (cp_parser_parse_definitely (parser))
12174             {
12175               if (address_p)
12176                 argument = build_x_unary_op (ADDR_EXPR, argument,
12177                                              tf_warning_or_error);
12178               return argument;
12179             }
12180         }
12181     }
12182   /* If the argument started with "&", there are no other valid
12183      alternatives at this point.  */
12184   if (address_p)
12185     {
12186       cp_parser_error (parser, "invalid non-type template argument");
12187       return error_mark_node;
12188     }
12189
12190   /* If the argument wasn't successfully parsed as a type-id followed
12191      by '>>', the argument can only be a constant expression now.
12192      Otherwise, we try parsing the constant-expression tentatively,
12193      because the argument could really be a type-id.  */
12194   if (maybe_type_id)
12195     cp_parser_parse_tentatively (parser);
12196   argument = cp_parser_constant_expression (parser,
12197                                             /*allow_non_constant_p=*/false,
12198                                             /*non_constant_p=*/NULL);
12199   argument = fold_non_dependent_expr (argument);
12200   if (!maybe_type_id)
12201     return argument;
12202   if (!cp_parser_next_token_ends_template_argument_p (parser))
12203     cp_parser_error (parser, "expected template-argument");
12204   if (cp_parser_parse_definitely (parser))
12205     return argument;
12206   /* We did our best to parse the argument as a non type-id, but that
12207      was the only alternative that matched (albeit with a '>' after
12208      it). We can assume it's just a typo from the user, and a
12209      diagnostic will then be issued.  */
12210   return cp_parser_template_type_arg (parser);
12211 }
12212
12213 /* Parse an explicit-instantiation.
12214
12215    explicit-instantiation:
12216      template declaration
12217
12218    Although the standard says `declaration', what it really means is:
12219
12220    explicit-instantiation:
12221      template decl-specifier-seq [opt] declarator [opt] ;
12222
12223    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12224    supposed to be allowed.  A defect report has been filed about this
12225    issue.
12226
12227    GNU Extension:
12228
12229    explicit-instantiation:
12230      storage-class-specifier template
12231        decl-specifier-seq [opt] declarator [opt] ;
12232      function-specifier template
12233        decl-specifier-seq [opt] declarator [opt] ;  */
12234
12235 static void
12236 cp_parser_explicit_instantiation (cp_parser* parser)
12237 {
12238   int declares_class_or_enum;
12239   cp_decl_specifier_seq decl_specifiers;
12240   tree extension_specifier = NULL_TREE;
12241
12242   /* Look for an (optional) storage-class-specifier or
12243      function-specifier.  */
12244   if (cp_parser_allow_gnu_extensions_p (parser))
12245     {
12246       extension_specifier
12247         = cp_parser_storage_class_specifier_opt (parser);
12248       if (!extension_specifier)
12249         extension_specifier
12250           = cp_parser_function_specifier_opt (parser,
12251                                               /*decl_specs=*/NULL);
12252     }
12253
12254   /* Look for the `template' keyword.  */
12255   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12256   /* Let the front end know that we are processing an explicit
12257      instantiation.  */
12258   begin_explicit_instantiation ();
12259   /* [temp.explicit] says that we are supposed to ignore access
12260      control while processing explicit instantiation directives.  */
12261   push_deferring_access_checks (dk_no_check);
12262   /* Parse a decl-specifier-seq.  */
12263   cp_parser_decl_specifier_seq (parser,
12264                                 CP_PARSER_FLAGS_OPTIONAL,
12265                                 &decl_specifiers,
12266                                 &declares_class_or_enum);
12267   /* If there was exactly one decl-specifier, and it declared a class,
12268      and there's no declarator, then we have an explicit type
12269      instantiation.  */
12270   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12271     {
12272       tree type;
12273
12274       type = check_tag_decl (&decl_specifiers);
12275       /* Turn access control back on for names used during
12276          template instantiation.  */
12277       pop_deferring_access_checks ();
12278       if (type)
12279         do_type_instantiation (type, extension_specifier,
12280                                /*complain=*/tf_error);
12281     }
12282   else
12283     {
12284       cp_declarator *declarator;
12285       tree decl;
12286
12287       /* Parse the declarator.  */
12288       declarator
12289         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12290                                 /*ctor_dtor_or_conv_p=*/NULL,
12291                                 /*parenthesized_p=*/NULL,
12292                                 /*member_p=*/false);
12293       if (declares_class_or_enum & 2)
12294         cp_parser_check_for_definition_in_return_type (declarator,
12295                                                        decl_specifiers.type,
12296                                                        decl_specifiers.type_location);
12297       if (declarator != cp_error_declarator)
12298         {
12299           if (decl_specifiers.specs[(int)ds_inline])
12300             permerror (input_location, "explicit instantiation shall not use"
12301                        " %<inline%> specifier");
12302           if (decl_specifiers.specs[(int)ds_constexpr])
12303             permerror (input_location, "explicit instantiation shall not use"
12304                        " %<constexpr%> specifier");
12305
12306           decl = grokdeclarator (declarator, &decl_specifiers,
12307                                  NORMAL, 0, &decl_specifiers.attributes);
12308           /* Turn access control back on for names used during
12309              template instantiation.  */
12310           pop_deferring_access_checks ();
12311           /* Do the explicit instantiation.  */
12312           do_decl_instantiation (decl, extension_specifier);
12313         }
12314       else
12315         {
12316           pop_deferring_access_checks ();
12317           /* Skip the body of the explicit instantiation.  */
12318           cp_parser_skip_to_end_of_statement (parser);
12319         }
12320     }
12321   /* We're done with the instantiation.  */
12322   end_explicit_instantiation ();
12323
12324   cp_parser_consume_semicolon_at_end_of_statement (parser);
12325 }
12326
12327 /* Parse an explicit-specialization.
12328
12329    explicit-specialization:
12330      template < > declaration
12331
12332    Although the standard says `declaration', what it really means is:
12333
12334    explicit-specialization:
12335      template <> decl-specifier [opt] init-declarator [opt] ;
12336      template <> function-definition
12337      template <> explicit-specialization
12338      template <> template-declaration  */
12339
12340 static void
12341 cp_parser_explicit_specialization (cp_parser* parser)
12342 {
12343   bool need_lang_pop;
12344   cp_token *token = cp_lexer_peek_token (parser->lexer);
12345
12346   /* Look for the `template' keyword.  */
12347   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12348   /* Look for the `<'.  */
12349   cp_parser_require (parser, CPP_LESS, RT_LESS);
12350   /* Look for the `>'.  */
12351   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12352   /* We have processed another parameter list.  */
12353   ++parser->num_template_parameter_lists;
12354   /* [temp]
12355
12356      A template ... explicit specialization ... shall not have C
12357      linkage.  */
12358   if (current_lang_name == lang_name_c)
12359     {
12360       error_at (token->location, "template specialization with C linkage");
12361       /* Give it C++ linkage to avoid confusing other parts of the
12362          front end.  */
12363       push_lang_context (lang_name_cplusplus);
12364       need_lang_pop = true;
12365     }
12366   else
12367     need_lang_pop = false;
12368   /* Let the front end know that we are beginning a specialization.  */
12369   if (!begin_specialization ())
12370     {
12371       end_specialization ();
12372       return;
12373     }
12374
12375   /* If the next keyword is `template', we need to figure out whether
12376      or not we're looking a template-declaration.  */
12377   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12378     {
12379       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12380           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12381         cp_parser_template_declaration_after_export (parser,
12382                                                      /*member_p=*/false);
12383       else
12384         cp_parser_explicit_specialization (parser);
12385     }
12386   else
12387     /* Parse the dependent declaration.  */
12388     cp_parser_single_declaration (parser,
12389                                   /*checks=*/NULL,
12390                                   /*member_p=*/false,
12391                                   /*explicit_specialization_p=*/true,
12392                                   /*friend_p=*/NULL);
12393   /* We're done with the specialization.  */
12394   end_specialization ();
12395   /* For the erroneous case of a template with C linkage, we pushed an
12396      implicit C++ linkage scope; exit that scope now.  */
12397   if (need_lang_pop)
12398     pop_lang_context ();
12399   /* We're done with this parameter list.  */
12400   --parser->num_template_parameter_lists;
12401 }
12402
12403 /* Parse a type-specifier.
12404
12405    type-specifier:
12406      simple-type-specifier
12407      class-specifier
12408      enum-specifier
12409      elaborated-type-specifier
12410      cv-qualifier
12411
12412    GNU Extension:
12413
12414    type-specifier:
12415      __complex__
12416
12417    Returns a representation of the type-specifier.  For a
12418    class-specifier, enum-specifier, or elaborated-type-specifier, a
12419    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12420
12421    The parser flags FLAGS is used to control type-specifier parsing.
12422
12423    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12424    in a decl-specifier-seq.
12425
12426    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12427    class-specifier, enum-specifier, or elaborated-type-specifier, then
12428    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12429    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12430    zero.
12431
12432    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12433    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12434    is set to FALSE.  */
12435
12436 static tree
12437 cp_parser_type_specifier (cp_parser* parser,
12438                           cp_parser_flags flags,
12439                           cp_decl_specifier_seq *decl_specs,
12440                           bool is_declaration,
12441                           int* declares_class_or_enum,
12442                           bool* is_cv_qualifier)
12443 {
12444   tree type_spec = NULL_TREE;
12445   cp_token *token;
12446   enum rid keyword;
12447   cp_decl_spec ds = ds_last;
12448
12449   /* Assume this type-specifier does not declare a new type.  */
12450   if (declares_class_or_enum)
12451     *declares_class_or_enum = 0;
12452   /* And that it does not specify a cv-qualifier.  */
12453   if (is_cv_qualifier)
12454     *is_cv_qualifier = false;
12455   /* Peek at the next token.  */
12456   token = cp_lexer_peek_token (parser->lexer);
12457
12458   /* If we're looking at a keyword, we can use that to guide the
12459      production we choose.  */
12460   keyword = token->keyword;
12461   switch (keyword)
12462     {
12463     case RID_ENUM:
12464       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12465         goto elaborated_type_specifier;
12466
12467       /* Look for the enum-specifier.  */
12468       type_spec = cp_parser_enum_specifier (parser);
12469       /* If that worked, we're done.  */
12470       if (type_spec)
12471         {
12472           if (declares_class_or_enum)
12473             *declares_class_or_enum = 2;
12474           if (decl_specs)
12475             cp_parser_set_decl_spec_type (decl_specs,
12476                                           type_spec,
12477                                           token->location,
12478                                           /*user_defined_p=*/true);
12479           return type_spec;
12480         }
12481       else
12482         goto elaborated_type_specifier;
12483
12484       /* Any of these indicate either a class-specifier, or an
12485          elaborated-type-specifier.  */
12486     case RID_CLASS:
12487     case RID_STRUCT:
12488     case RID_UNION:
12489       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12490         goto elaborated_type_specifier;
12491
12492       /* Parse tentatively so that we can back up if we don't find a
12493          class-specifier.  */
12494       cp_parser_parse_tentatively (parser);
12495       /* Look for the class-specifier.  */
12496       type_spec = cp_parser_class_specifier (parser);
12497       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12498       /* If that worked, we're done.  */
12499       if (cp_parser_parse_definitely (parser))
12500         {
12501           if (declares_class_or_enum)
12502             *declares_class_or_enum = 2;
12503           if (decl_specs)
12504             cp_parser_set_decl_spec_type (decl_specs,
12505                                           type_spec,
12506                                           token->location,
12507                                           /*user_defined_p=*/true);
12508           return type_spec;
12509         }
12510
12511       /* Fall through.  */
12512     elaborated_type_specifier:
12513       /* We're declaring (not defining) a class or enum.  */
12514       if (declares_class_or_enum)
12515         *declares_class_or_enum = 1;
12516
12517       /* Fall through.  */
12518     case RID_TYPENAME:
12519       /* Look for an elaborated-type-specifier.  */
12520       type_spec
12521         = (cp_parser_elaborated_type_specifier
12522            (parser,
12523             decl_specs && decl_specs->specs[(int) ds_friend],
12524             is_declaration));
12525       if (decl_specs)
12526         cp_parser_set_decl_spec_type (decl_specs,
12527                                       type_spec,
12528                                       token->location,
12529                                       /*user_defined_p=*/true);
12530       return type_spec;
12531
12532     case RID_CONST:
12533       ds = ds_const;
12534       if (is_cv_qualifier)
12535         *is_cv_qualifier = true;
12536       break;
12537
12538     case RID_VOLATILE:
12539       ds = ds_volatile;
12540       if (is_cv_qualifier)
12541         *is_cv_qualifier = true;
12542       break;
12543
12544     case RID_RESTRICT:
12545       ds = ds_restrict;
12546       if (is_cv_qualifier)
12547         *is_cv_qualifier = true;
12548       break;
12549
12550     case RID_COMPLEX:
12551       /* The `__complex__' keyword is a GNU extension.  */
12552       ds = ds_complex;
12553       break;
12554
12555     default:
12556       break;
12557     }
12558
12559   /* Handle simple keywords.  */
12560   if (ds != ds_last)
12561     {
12562       if (decl_specs)
12563         {
12564           ++decl_specs->specs[(int)ds];
12565           decl_specs->any_specifiers_p = true;
12566         }
12567       return cp_lexer_consume_token (parser->lexer)->u.value;
12568     }
12569
12570   /* If we do not already have a type-specifier, assume we are looking
12571      at a simple-type-specifier.  */
12572   type_spec = cp_parser_simple_type_specifier (parser,
12573                                                decl_specs,
12574                                                flags);
12575
12576   /* If we didn't find a type-specifier, and a type-specifier was not
12577      optional in this context, issue an error message.  */
12578   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12579     {
12580       cp_parser_error (parser, "expected type specifier");
12581       return error_mark_node;
12582     }
12583
12584   return type_spec;
12585 }
12586
12587 /* Parse a simple-type-specifier.
12588
12589    simple-type-specifier:
12590      :: [opt] nested-name-specifier [opt] type-name
12591      :: [opt] nested-name-specifier template template-id
12592      char
12593      wchar_t
12594      bool
12595      short
12596      int
12597      long
12598      signed
12599      unsigned
12600      float
12601      double
12602      void
12603
12604    C++0x Extension:
12605
12606    simple-type-specifier:
12607      auto
12608      decltype ( expression )   
12609      char16_t
12610      char32_t
12611
12612    GNU Extension:
12613
12614    simple-type-specifier:
12615      __int128
12616      __typeof__ unary-expression
12617      __typeof__ ( type-id )
12618
12619    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12620    appropriately updated.  */
12621
12622 static tree
12623 cp_parser_simple_type_specifier (cp_parser* parser,
12624                                  cp_decl_specifier_seq *decl_specs,
12625                                  cp_parser_flags flags)
12626 {
12627   tree type = NULL_TREE;
12628   cp_token *token;
12629
12630   /* Peek at the next token.  */
12631   token = cp_lexer_peek_token (parser->lexer);
12632
12633   /* If we're looking at a keyword, things are easy.  */
12634   switch (token->keyword)
12635     {
12636     case RID_CHAR:
12637       if (decl_specs)
12638         decl_specs->explicit_char_p = true;
12639       type = char_type_node;
12640       break;
12641     case RID_CHAR16:
12642       type = char16_type_node;
12643       break;
12644     case RID_CHAR32:
12645       type = char32_type_node;
12646       break;
12647     case RID_WCHAR:
12648       type = wchar_type_node;
12649       break;
12650     case RID_BOOL:
12651       type = boolean_type_node;
12652       break;
12653     case RID_SHORT:
12654       if (decl_specs)
12655         ++decl_specs->specs[(int) ds_short];
12656       type = short_integer_type_node;
12657       break;
12658     case RID_INT:
12659       if (decl_specs)
12660         decl_specs->explicit_int_p = true;
12661       type = integer_type_node;
12662       break;
12663     case RID_INT128:
12664       if (!int128_integer_type_node)
12665         break;
12666       if (decl_specs)
12667         decl_specs->explicit_int128_p = true;
12668       type = int128_integer_type_node;
12669       break;
12670     case RID_LONG:
12671       if (decl_specs)
12672         ++decl_specs->specs[(int) ds_long];
12673       type = long_integer_type_node;
12674       break;
12675     case RID_SIGNED:
12676       if (decl_specs)
12677         ++decl_specs->specs[(int) ds_signed];
12678       type = integer_type_node;
12679       break;
12680     case RID_UNSIGNED:
12681       if (decl_specs)
12682         ++decl_specs->specs[(int) ds_unsigned];
12683       type = unsigned_type_node;
12684       break;
12685     case RID_FLOAT:
12686       type = float_type_node;
12687       break;
12688     case RID_DOUBLE:
12689       type = double_type_node;
12690       break;
12691     case RID_VOID:
12692       type = void_type_node;
12693       break;
12694       
12695     case RID_AUTO:
12696       maybe_warn_cpp0x (CPP0X_AUTO);
12697       type = make_auto ();
12698       break;
12699
12700     case RID_DECLTYPE:
12701       /* Parse the `decltype' type.  */
12702       type = cp_parser_decltype (parser);
12703
12704       if (decl_specs)
12705         cp_parser_set_decl_spec_type (decl_specs, type,
12706                                       token->location,
12707                                       /*user_defined_p=*/true);
12708
12709       return type;
12710
12711     case RID_TYPEOF:
12712       /* Consume the `typeof' token.  */
12713       cp_lexer_consume_token (parser->lexer);
12714       /* Parse the operand to `typeof'.  */
12715       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12716       /* If it is not already a TYPE, take its type.  */
12717       if (!TYPE_P (type))
12718         type = finish_typeof (type);
12719
12720       if (decl_specs)
12721         cp_parser_set_decl_spec_type (decl_specs, type,
12722                                       token->location,
12723                                       /*user_defined_p=*/true);
12724
12725       return type;
12726
12727     default:
12728       break;
12729     }
12730
12731   /* If the type-specifier was for a built-in type, we're done.  */
12732   if (type)
12733     {
12734       /* Record the type.  */
12735       if (decl_specs
12736           && (token->keyword != RID_SIGNED
12737               && token->keyword != RID_UNSIGNED
12738               && token->keyword != RID_SHORT
12739               && token->keyword != RID_LONG))
12740         cp_parser_set_decl_spec_type (decl_specs,
12741                                       type,
12742                                       token->location,
12743                                       /*user_defined=*/false);
12744       if (decl_specs)
12745         decl_specs->any_specifiers_p = true;
12746
12747       /* Consume the token.  */
12748       cp_lexer_consume_token (parser->lexer);
12749
12750       /* There is no valid C++ program where a non-template type is
12751          followed by a "<".  That usually indicates that the user thought
12752          that the type was a template.  */
12753       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12754
12755       return TYPE_NAME (type);
12756     }
12757
12758   /* The type-specifier must be a user-defined type.  */
12759   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12760     {
12761       bool qualified_p;
12762       bool global_p;
12763
12764       /* Don't gobble tokens or issue error messages if this is an
12765          optional type-specifier.  */
12766       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12767         cp_parser_parse_tentatively (parser);
12768
12769       /* Look for the optional `::' operator.  */
12770       global_p
12771         = (cp_parser_global_scope_opt (parser,
12772                                        /*current_scope_valid_p=*/false)
12773            != NULL_TREE);
12774       /* Look for the nested-name specifier.  */
12775       qualified_p
12776         = (cp_parser_nested_name_specifier_opt (parser,
12777                                                 /*typename_keyword_p=*/false,
12778                                                 /*check_dependency_p=*/true,
12779                                                 /*type_p=*/false,
12780                                                 /*is_declaration=*/false)
12781            != NULL_TREE);
12782       token = cp_lexer_peek_token (parser->lexer);
12783       /* If we have seen a nested-name-specifier, and the next token
12784          is `template', then we are using the template-id production.  */
12785       if (parser->scope
12786           && cp_parser_optional_template_keyword (parser))
12787         {
12788           /* Look for the template-id.  */
12789           type = cp_parser_template_id (parser,
12790                                         /*template_keyword_p=*/true,
12791                                         /*check_dependency_p=*/true,
12792                                         /*is_declaration=*/false);
12793           /* If the template-id did not name a type, we are out of
12794              luck.  */
12795           if (TREE_CODE (type) != TYPE_DECL)
12796             {
12797               cp_parser_error (parser, "expected template-id for type");
12798               type = NULL_TREE;
12799             }
12800         }
12801       /* Otherwise, look for a type-name.  */
12802       else
12803         type = cp_parser_type_name (parser);
12804       /* Keep track of all name-lookups performed in class scopes.  */
12805       if (type
12806           && !global_p
12807           && !qualified_p
12808           && TREE_CODE (type) == TYPE_DECL
12809           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12810         maybe_note_name_used_in_class (DECL_NAME (type), type);
12811       /* If it didn't work out, we don't have a TYPE.  */
12812       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12813           && !cp_parser_parse_definitely (parser))
12814         type = NULL_TREE;
12815       if (type && decl_specs)
12816         cp_parser_set_decl_spec_type (decl_specs, type,
12817                                       token->location,
12818                                       /*user_defined=*/true);
12819     }
12820
12821   /* If we didn't get a type-name, issue an error message.  */
12822   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12823     {
12824       cp_parser_error (parser, "expected type-name");
12825       return error_mark_node;
12826     }
12827
12828   if (type && type != error_mark_node)
12829     {
12830       /* See if TYPE is an Objective-C type, and if so, parse and
12831          accept any protocol references following it.  Do this before
12832          the cp_parser_check_for_invalid_template_id() call, because
12833          Objective-C types can be followed by '<...>' which would
12834          enclose protocol names rather than template arguments, and so
12835          everything is fine.  */
12836       if (c_dialect_objc () && !parser->scope
12837           && (objc_is_id (type) || objc_is_class_name (type)))
12838         {
12839           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12840           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12841
12842           /* Clobber the "unqualified" type previously entered into
12843              DECL_SPECS with the new, improved protocol-qualified version.  */
12844           if (decl_specs)
12845             decl_specs->type = qual_type;
12846
12847           return qual_type;
12848         }
12849
12850       /* There is no valid C++ program where a non-template type is
12851          followed by a "<".  That usually indicates that the user
12852          thought that the type was a template.  */
12853       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12854                                                token->location);
12855     }
12856
12857   return type;
12858 }
12859
12860 /* Parse a type-name.
12861
12862    type-name:
12863      class-name
12864      enum-name
12865      typedef-name
12866
12867    enum-name:
12868      identifier
12869
12870    typedef-name:
12871      identifier
12872
12873    Returns a TYPE_DECL for the type.  */
12874
12875 static tree
12876 cp_parser_type_name (cp_parser* parser)
12877 {
12878   tree type_decl;
12879
12880   /* We can't know yet whether it is a class-name or not.  */
12881   cp_parser_parse_tentatively (parser);
12882   /* Try a class-name.  */
12883   type_decl = cp_parser_class_name (parser,
12884                                     /*typename_keyword_p=*/false,
12885                                     /*template_keyword_p=*/false,
12886                                     none_type,
12887                                     /*check_dependency_p=*/true,
12888                                     /*class_head_p=*/false,
12889                                     /*is_declaration=*/false);
12890   /* If it's not a class-name, keep looking.  */
12891   if (!cp_parser_parse_definitely (parser))
12892     {
12893       /* It must be a typedef-name or an enum-name.  */
12894       return cp_parser_nonclass_name (parser);
12895     }
12896
12897   return type_decl;
12898 }
12899
12900 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12901
12902    enum-name:
12903      identifier
12904
12905    typedef-name:
12906      identifier
12907
12908    Returns a TYPE_DECL for the type.  */
12909
12910 static tree
12911 cp_parser_nonclass_name (cp_parser* parser)
12912 {
12913   tree type_decl;
12914   tree identifier;
12915
12916   cp_token *token = cp_lexer_peek_token (parser->lexer);
12917   identifier = cp_parser_identifier (parser);
12918   if (identifier == error_mark_node)
12919     return error_mark_node;
12920
12921   /* Look up the type-name.  */
12922   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12923
12924   if (TREE_CODE (type_decl) != TYPE_DECL
12925       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12926     {
12927       /* See if this is an Objective-C type.  */
12928       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12929       tree type = objc_get_protocol_qualified_type (identifier, protos);
12930       if (type)
12931         type_decl = TYPE_NAME (type);
12932     }
12933
12934   /* Issue an error if we did not find a type-name.  */
12935   if (TREE_CODE (type_decl) != TYPE_DECL
12936       /* In Objective-C, we have the complication that class names are
12937          normally type names and start declarations (eg, the
12938          "NSObject" in "NSObject *object;"), but can be used in an
12939          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12940          is an expression.  So, a classname followed by a dot is not a
12941          valid type-name.  */
12942       || (objc_is_class_name (TREE_TYPE (type_decl))
12943           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12944     {
12945       if (!cp_parser_simulate_error (parser))
12946         cp_parser_name_lookup_error (parser, identifier, type_decl,
12947                                      NLE_TYPE, token->location);
12948       return error_mark_node;
12949     }
12950   /* Remember that the name was used in the definition of the
12951      current class so that we can check later to see if the
12952      meaning would have been different after the class was
12953      entirely defined.  */
12954   else if (type_decl != error_mark_node
12955            && !parser->scope)
12956     maybe_note_name_used_in_class (identifier, type_decl);
12957   
12958   return type_decl;
12959 }
12960
12961 /* Parse an elaborated-type-specifier.  Note that the grammar given
12962    here incorporates the resolution to DR68.
12963
12964    elaborated-type-specifier:
12965      class-key :: [opt] nested-name-specifier [opt] identifier
12966      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12967      enum-key :: [opt] nested-name-specifier [opt] identifier
12968      typename :: [opt] nested-name-specifier identifier
12969      typename :: [opt] nested-name-specifier template [opt]
12970        template-id
12971
12972    GNU extension:
12973
12974    elaborated-type-specifier:
12975      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12976      class-key attributes :: [opt] nested-name-specifier [opt]
12977                template [opt] template-id
12978      enum attributes :: [opt] nested-name-specifier [opt] identifier
12979
12980    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12981    declared `friend'.  If IS_DECLARATION is TRUE, then this
12982    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12983    something is being declared.
12984
12985    Returns the TYPE specified.  */
12986
12987 static tree
12988 cp_parser_elaborated_type_specifier (cp_parser* parser,
12989                                      bool is_friend,
12990                                      bool is_declaration)
12991 {
12992   enum tag_types tag_type;
12993   tree identifier;
12994   tree type = NULL_TREE;
12995   tree attributes = NULL_TREE;
12996   tree globalscope;
12997   cp_token *token = NULL;
12998
12999   /* See if we're looking at the `enum' keyword.  */
13000   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13001     {
13002       /* Consume the `enum' token.  */
13003       cp_lexer_consume_token (parser->lexer);
13004       /* Remember that it's an enumeration type.  */
13005       tag_type = enum_type;
13006       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13007          enums) is used here.  */
13008       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13009           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13010         {
13011             pedwarn (input_location, 0, "elaborated-type-specifier "
13012                       "for a scoped enum must not use the %<%D%> keyword",
13013                       cp_lexer_peek_token (parser->lexer)->u.value);
13014           /* Consume the `struct' or `class' and parse it anyway.  */
13015           cp_lexer_consume_token (parser->lexer);
13016         }
13017       /* Parse the attributes.  */
13018       attributes = cp_parser_attributes_opt (parser);
13019     }
13020   /* Or, it might be `typename'.  */
13021   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13022                                            RID_TYPENAME))
13023     {
13024       /* Consume the `typename' token.  */
13025       cp_lexer_consume_token (parser->lexer);
13026       /* Remember that it's a `typename' type.  */
13027       tag_type = typename_type;
13028     }
13029   /* Otherwise it must be a class-key.  */
13030   else
13031     {
13032       tag_type = cp_parser_class_key (parser);
13033       if (tag_type == none_type)
13034         return error_mark_node;
13035       /* Parse the attributes.  */
13036       attributes = cp_parser_attributes_opt (parser);
13037     }
13038
13039   /* Look for the `::' operator.  */
13040   globalscope =  cp_parser_global_scope_opt (parser,
13041                                              /*current_scope_valid_p=*/false);
13042   /* Look for the nested-name-specifier.  */
13043   if (tag_type == typename_type && !globalscope)
13044     {
13045       if (!cp_parser_nested_name_specifier (parser,
13046                                            /*typename_keyword_p=*/true,
13047                                            /*check_dependency_p=*/true,
13048                                            /*type_p=*/true,
13049                                             is_declaration))
13050         return error_mark_node;
13051     }
13052   else
13053     /* Even though `typename' is not present, the proposed resolution
13054        to Core Issue 180 says that in `class A<T>::B', `B' should be
13055        considered a type-name, even if `A<T>' is dependent.  */
13056     cp_parser_nested_name_specifier_opt (parser,
13057                                          /*typename_keyword_p=*/true,
13058                                          /*check_dependency_p=*/true,
13059                                          /*type_p=*/true,
13060                                          is_declaration);
13061  /* For everything but enumeration types, consider a template-id.
13062     For an enumeration type, consider only a plain identifier.  */
13063   if (tag_type != enum_type)
13064     {
13065       bool template_p = false;
13066       tree decl;
13067
13068       /* Allow the `template' keyword.  */
13069       template_p = cp_parser_optional_template_keyword (parser);
13070       /* If we didn't see `template', we don't know if there's a
13071          template-id or not.  */
13072       if (!template_p)
13073         cp_parser_parse_tentatively (parser);
13074       /* Parse the template-id.  */
13075       token = cp_lexer_peek_token (parser->lexer);
13076       decl = cp_parser_template_id (parser, template_p,
13077                                     /*check_dependency_p=*/true,
13078                                     is_declaration);
13079       /* If we didn't find a template-id, look for an ordinary
13080          identifier.  */
13081       if (!template_p && !cp_parser_parse_definitely (parser))
13082         ;
13083       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13084          in effect, then we must assume that, upon instantiation, the
13085          template will correspond to a class.  */
13086       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13087                && tag_type == typename_type)
13088         type = make_typename_type (parser->scope, decl,
13089                                    typename_type,
13090                                    /*complain=*/tf_error);
13091       /* If the `typename' keyword is in effect and DECL is not a type
13092          decl. Then type is non existant.   */
13093       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13094         type = NULL_TREE; 
13095       else 
13096         type = TREE_TYPE (decl);
13097     }
13098
13099   if (!type)
13100     {
13101       token = cp_lexer_peek_token (parser->lexer);
13102       identifier = cp_parser_identifier (parser);
13103
13104       if (identifier == error_mark_node)
13105         {
13106           parser->scope = NULL_TREE;
13107           return error_mark_node;
13108         }
13109
13110       /* For a `typename', we needn't call xref_tag.  */
13111       if (tag_type == typename_type
13112           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13113         return cp_parser_make_typename_type (parser, parser->scope,
13114                                              identifier,
13115                                              token->location);
13116       /* Look up a qualified name in the usual way.  */
13117       if (parser->scope)
13118         {
13119           tree decl;
13120           tree ambiguous_decls;
13121
13122           decl = cp_parser_lookup_name (parser, identifier,
13123                                         tag_type,
13124                                         /*is_template=*/false,
13125                                         /*is_namespace=*/false,
13126                                         /*check_dependency=*/true,
13127                                         &ambiguous_decls,
13128                                         token->location);
13129
13130           /* If the lookup was ambiguous, an error will already have been
13131              issued.  */
13132           if (ambiguous_decls)
13133             return error_mark_node;
13134
13135           /* If we are parsing friend declaration, DECL may be a
13136              TEMPLATE_DECL tree node here.  However, we need to check
13137              whether this TEMPLATE_DECL results in valid code.  Consider
13138              the following example:
13139
13140                namespace N {
13141                  template <class T> class C {};
13142                }
13143                class X {
13144                  template <class T> friend class N::C; // #1, valid code
13145                };
13146                template <class T> class Y {
13147                  friend class N::C;                    // #2, invalid code
13148                };
13149
13150              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13151              name lookup of `N::C'.  We see that friend declaration must
13152              be template for the code to be valid.  Note that
13153              processing_template_decl does not work here since it is
13154              always 1 for the above two cases.  */
13155
13156           decl = (cp_parser_maybe_treat_template_as_class
13157                   (decl, /*tag_name_p=*/is_friend
13158                          && parser->num_template_parameter_lists));
13159
13160           if (TREE_CODE (decl) != TYPE_DECL)
13161             {
13162               cp_parser_diagnose_invalid_type_name (parser,
13163                                                     parser->scope,
13164                                                     identifier,
13165                                                     token->location);
13166               return error_mark_node;
13167             }
13168
13169           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13170             {
13171               bool allow_template = (parser->num_template_parameter_lists
13172                                       || DECL_SELF_REFERENCE_P (decl));
13173               type = check_elaborated_type_specifier (tag_type, decl, 
13174                                                       allow_template);
13175
13176               if (type == error_mark_node)
13177                 return error_mark_node;
13178             }
13179
13180           /* Forward declarations of nested types, such as
13181
13182                class C1::C2;
13183                class C1::C2::C3;
13184
13185              are invalid unless all components preceding the final '::'
13186              are complete.  If all enclosing types are complete, these
13187              declarations become merely pointless.
13188
13189              Invalid forward declarations of nested types are errors
13190              caught elsewhere in parsing.  Those that are pointless arrive
13191              here.  */
13192
13193           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13194               && !is_friend && !processing_explicit_instantiation)
13195             warning (0, "declaration %qD does not declare anything", decl);
13196
13197           type = TREE_TYPE (decl);
13198         }
13199       else
13200         {
13201           /* An elaborated-type-specifier sometimes introduces a new type and
13202              sometimes names an existing type.  Normally, the rule is that it
13203              introduces a new type only if there is not an existing type of
13204              the same name already in scope.  For example, given:
13205
13206                struct S {};
13207                void f() { struct S s; }
13208
13209              the `struct S' in the body of `f' is the same `struct S' as in
13210              the global scope; the existing definition is used.  However, if
13211              there were no global declaration, this would introduce a new
13212              local class named `S'.
13213
13214              An exception to this rule applies to the following code:
13215
13216                namespace N { struct S; }
13217
13218              Here, the elaborated-type-specifier names a new type
13219              unconditionally; even if there is already an `S' in the
13220              containing scope this declaration names a new type.
13221              This exception only applies if the elaborated-type-specifier
13222              forms the complete declaration:
13223
13224                [class.name]
13225
13226                A declaration consisting solely of `class-key identifier ;' is
13227                either a redeclaration of the name in the current scope or a
13228                forward declaration of the identifier as a class name.  It
13229                introduces the name into the current scope.
13230
13231              We are in this situation precisely when the next token is a `;'.
13232
13233              An exception to the exception is that a `friend' declaration does
13234              *not* name a new type; i.e., given:
13235
13236                struct S { friend struct T; };
13237
13238              `T' is not a new type in the scope of `S'.
13239
13240              Also, `new struct S' or `sizeof (struct S)' never results in the
13241              definition of a new type; a new type can only be declared in a
13242              declaration context.  */
13243
13244           tag_scope ts;
13245           bool template_p;
13246
13247           if (is_friend)
13248             /* Friends have special name lookup rules.  */
13249             ts = ts_within_enclosing_non_class;
13250           else if (is_declaration
13251                    && cp_lexer_next_token_is (parser->lexer,
13252                                               CPP_SEMICOLON))
13253             /* This is a `class-key identifier ;' */
13254             ts = ts_current;
13255           else
13256             ts = ts_global;
13257
13258           template_p =
13259             (parser->num_template_parameter_lists
13260              && (cp_parser_next_token_starts_class_definition_p (parser)
13261                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13262           /* An unqualified name was used to reference this type, so
13263              there were no qualifying templates.  */
13264           if (!cp_parser_check_template_parameters (parser,
13265                                                     /*num_templates=*/0,
13266                                                     token->location,
13267                                                     /*declarator=*/NULL))
13268             return error_mark_node;
13269           type = xref_tag (tag_type, identifier, ts, template_p);
13270         }
13271     }
13272
13273   if (type == error_mark_node)
13274     return error_mark_node;
13275
13276   /* Allow attributes on forward declarations of classes.  */
13277   if (attributes)
13278     {
13279       if (TREE_CODE (type) == TYPENAME_TYPE)
13280         warning (OPT_Wattributes,
13281                  "attributes ignored on uninstantiated type");
13282       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13283                && ! processing_explicit_instantiation)
13284         warning (OPT_Wattributes,
13285                  "attributes ignored on template instantiation");
13286       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13287         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13288       else
13289         warning (OPT_Wattributes,
13290                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13291     }
13292
13293   if (tag_type != enum_type)
13294     cp_parser_check_class_key (tag_type, type);
13295
13296   /* A "<" cannot follow an elaborated type specifier.  If that
13297      happens, the user was probably trying to form a template-id.  */
13298   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13299
13300   return type;
13301 }
13302
13303 /* Parse an enum-specifier.
13304
13305    enum-specifier:
13306      enum-head { enumerator-list [opt] }
13307
13308    enum-head:
13309      enum-key identifier [opt] enum-base [opt]
13310      enum-key nested-name-specifier identifier enum-base [opt]
13311
13312    enum-key:
13313      enum
13314      enum class   [C++0x]
13315      enum struct  [C++0x]
13316
13317    enum-base:   [C++0x]
13318      : type-specifier-seq
13319
13320    opaque-enum-specifier:
13321      enum-key identifier enum-base [opt] ;
13322
13323    GNU Extensions:
13324      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13325        { enumerator-list [opt] }attributes[opt]
13326
13327    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13328    if the token stream isn't an enum-specifier after all.  */
13329
13330 static tree
13331 cp_parser_enum_specifier (cp_parser* parser)
13332 {
13333   tree identifier;
13334   tree type = NULL_TREE;
13335   tree prev_scope;
13336   tree nested_name_specifier = NULL_TREE;
13337   tree attributes;
13338   bool scoped_enum_p = false;
13339   bool has_underlying_type = false;
13340   bool nested_being_defined = false;
13341   bool new_value_list = false;
13342   bool is_new_type = false;
13343   bool is_anonymous = false;
13344   tree underlying_type = NULL_TREE;
13345   cp_token *type_start_token = NULL;
13346
13347   /* Parse tentatively so that we can back up if we don't find a
13348      enum-specifier.  */
13349   cp_parser_parse_tentatively (parser);
13350
13351   /* Caller guarantees that the current token is 'enum', an identifier
13352      possibly follows, and the token after that is an opening brace.
13353      If we don't have an identifier, fabricate an anonymous name for
13354      the enumeration being defined.  */
13355   cp_lexer_consume_token (parser->lexer);
13356
13357   /* Parse the "class" or "struct", which indicates a scoped
13358      enumeration type in C++0x.  */
13359   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13360       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13361     {
13362       if (cxx_dialect < cxx0x)
13363         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13364
13365       /* Consume the `struct' or `class' token.  */
13366       cp_lexer_consume_token (parser->lexer);
13367
13368       scoped_enum_p = true;
13369     }
13370
13371   attributes = cp_parser_attributes_opt (parser);
13372
13373   /* Clear the qualification.  */
13374   parser->scope = NULL_TREE;
13375   parser->qualifying_scope = NULL_TREE;
13376   parser->object_scope = NULL_TREE;
13377
13378   /* Figure out in what scope the declaration is being placed.  */
13379   prev_scope = current_scope ();
13380
13381   type_start_token = cp_lexer_peek_token (parser->lexer);
13382
13383   push_deferring_access_checks (dk_no_check);
13384   nested_name_specifier
13385       = cp_parser_nested_name_specifier_opt (parser,
13386                                              /*typename_keyword_p=*/true,
13387                                              /*check_dependency_p=*/false,
13388                                              /*type_p=*/false,
13389                                              /*is_declaration=*/false);
13390
13391   if (nested_name_specifier)
13392     {
13393       tree name;
13394
13395       identifier = cp_parser_identifier (parser);
13396       name =  cp_parser_lookup_name (parser, identifier,
13397                                      enum_type,
13398                                      /*is_template=*/false,
13399                                      /*is_namespace=*/false,
13400                                      /*check_dependency=*/true,
13401                                      /*ambiguous_decls=*/NULL,
13402                                      input_location);
13403       if (name)
13404         {
13405           type = TREE_TYPE (name);
13406           if (TREE_CODE (type) == TYPENAME_TYPE)
13407             {
13408               /* Are template enums allowed in ISO? */
13409               if (template_parm_scope_p ())
13410                 pedwarn (type_start_token->location, OPT_pedantic,
13411                          "%qD is an enumeration template", name);
13412               /* ignore a typename reference, for it will be solved by name
13413                  in start_enum.  */
13414               type = NULL_TREE;
13415             }
13416         }
13417       else
13418         error_at (type_start_token->location,
13419                   "%qD is not an enumerator-name", identifier);
13420     }
13421   else
13422     {
13423       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13424         identifier = cp_parser_identifier (parser);
13425       else
13426         {
13427           identifier = make_anon_name ();
13428           is_anonymous = true;
13429         }
13430     }
13431   pop_deferring_access_checks ();
13432
13433   /* Check for the `:' that denotes a specified underlying type in C++0x.
13434      Note that a ':' could also indicate a bitfield width, however.  */
13435   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13436     {
13437       cp_decl_specifier_seq type_specifiers;
13438
13439       /* Consume the `:'.  */
13440       cp_lexer_consume_token (parser->lexer);
13441
13442       /* Parse the type-specifier-seq.  */
13443       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13444                                     /*is_trailing_return=*/false,
13445                                     &type_specifiers);
13446
13447       /* At this point this is surely not elaborated type specifier.  */
13448       if (!cp_parser_parse_definitely (parser))
13449         return NULL_TREE;
13450
13451       if (cxx_dialect < cxx0x)
13452         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13453
13454       has_underlying_type = true;
13455
13456       /* If that didn't work, stop.  */
13457       if (type_specifiers.type != error_mark_node)
13458         {
13459           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13460                                             /*initialized=*/0, NULL);
13461           if (underlying_type == error_mark_node)
13462             underlying_type = NULL_TREE;
13463         }
13464     }
13465
13466   /* Look for the `{' but don't consume it yet.  */
13467   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13468     {
13469       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13470         {
13471           cp_parser_error (parser, "expected %<{%>");
13472           if (has_underlying_type)
13473             return NULL_TREE;
13474         }
13475       /* An opaque-enum-specifier must have a ';' here.  */
13476       if ((scoped_enum_p || underlying_type)
13477           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13478         {
13479           cp_parser_error (parser, "expected %<;%> or %<{%>");
13480           if (has_underlying_type)
13481             return NULL_TREE;
13482         }
13483     }
13484
13485   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13486     return NULL_TREE;
13487
13488   if (nested_name_specifier)
13489     {
13490       if (CLASS_TYPE_P (nested_name_specifier))
13491         {
13492           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13493           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13494           push_scope (nested_name_specifier);
13495         }
13496       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13497         {
13498           push_nested_namespace (nested_name_specifier);
13499         }
13500     }
13501
13502   /* Issue an error message if type-definitions are forbidden here.  */
13503   if (!cp_parser_check_type_definition (parser))
13504     type = error_mark_node;
13505   else
13506     /* Create the new type.  We do this before consuming the opening
13507        brace so the enum will be recorded as being on the line of its
13508        tag (or the 'enum' keyword, if there is no tag).  */
13509     type = start_enum (identifier, type, underlying_type,
13510                        scoped_enum_p, &is_new_type);
13511
13512   /* If the next token is not '{' it is an opaque-enum-specifier or an
13513      elaborated-type-specifier.  */
13514   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13515     {
13516       if (nested_name_specifier)
13517         {
13518           /* The following catches invalid code such as:
13519              enum class S<int>::E { A, B, C }; */
13520           if (!processing_specialization
13521               && CLASS_TYPE_P (nested_name_specifier)
13522               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13523             error_at (type_start_token->location, "cannot add an enumerator "
13524                       "list to a template instantiation");
13525
13526           /* If that scope does not contain the scope in which the
13527              class was originally declared, the program is invalid.  */
13528           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13529             {
13530               if (at_namespace_scope_p ())
13531                 error_at (type_start_token->location,
13532                           "declaration of %qD in namespace %qD which does not "
13533                           "enclose %qD",
13534                           type, prev_scope, nested_name_specifier);
13535               else
13536                 error_at (type_start_token->location,
13537                           "declaration of %qD in %qD which does not enclose %qD",
13538                           type, prev_scope, nested_name_specifier);
13539               type = error_mark_node;
13540             }
13541         }
13542
13543       if (scoped_enum_p)
13544         begin_scope (sk_scoped_enum, type);
13545
13546       /* Consume the opening brace.  */
13547       cp_lexer_consume_token (parser->lexer);
13548
13549       if (type == error_mark_node)
13550         ; /* Nothing to add */
13551       else if (OPAQUE_ENUM_P (type)
13552                || (cxx_dialect > cxx98 && processing_specialization))
13553         {
13554           new_value_list = true;
13555           SET_OPAQUE_ENUM_P (type, false);
13556           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13557         }
13558       else
13559         {
13560           error_at (type_start_token->location, "multiple definition of %q#T", type);
13561           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13562                     "previous definition here");
13563           type = error_mark_node;
13564         }
13565
13566       if (type == error_mark_node)
13567         cp_parser_skip_to_end_of_block_or_statement (parser);
13568       /* If the next token is not '}', then there are some enumerators.  */
13569       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13570         cp_parser_enumerator_list (parser, type);
13571
13572       /* Consume the final '}'.  */
13573       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13574
13575       if (scoped_enum_p)
13576         finish_scope ();
13577     }
13578   else
13579     {
13580       /* If a ';' follows, then it is an opaque-enum-specifier
13581         and additional restrictions apply.  */
13582       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13583         {
13584           if (is_anonymous)
13585             error_at (type_start_token->location,
13586                       "opaque-enum-specifier without name");
13587           else if (nested_name_specifier)
13588             error_at (type_start_token->location,
13589                       "opaque-enum-specifier must use a simple identifier");
13590         }
13591     }
13592
13593   /* Look for trailing attributes to apply to this enumeration, and
13594      apply them if appropriate.  */
13595   if (cp_parser_allow_gnu_extensions_p (parser))
13596     {
13597       tree trailing_attr = cp_parser_attributes_opt (parser);
13598       trailing_attr = chainon (trailing_attr, attributes);
13599       cplus_decl_attributes (&type,
13600                              trailing_attr,
13601                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13602     }
13603
13604   /* Finish up the enumeration.  */
13605   if (type != error_mark_node)
13606     {
13607       if (new_value_list)
13608         finish_enum_value_list (type);
13609       if (is_new_type)
13610         finish_enum (type);
13611     }
13612
13613   if (nested_name_specifier)
13614     {
13615       if (CLASS_TYPE_P (nested_name_specifier))
13616         {
13617           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13618           pop_scope (nested_name_specifier);
13619         }
13620       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13621         {
13622           pop_nested_namespace (nested_name_specifier);
13623         }
13624     }
13625   return type;
13626 }
13627
13628 /* Parse an enumerator-list.  The enumerators all have the indicated
13629    TYPE.
13630
13631    enumerator-list:
13632      enumerator-definition
13633      enumerator-list , enumerator-definition  */
13634
13635 static void
13636 cp_parser_enumerator_list (cp_parser* parser, tree type)
13637 {
13638   while (true)
13639     {
13640       /* Parse an enumerator-definition.  */
13641       cp_parser_enumerator_definition (parser, type);
13642
13643       /* If the next token is not a ',', we've reached the end of
13644          the list.  */
13645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13646         break;
13647       /* Otherwise, consume the `,' and keep going.  */
13648       cp_lexer_consume_token (parser->lexer);
13649       /* If the next token is a `}', there is a trailing comma.  */
13650       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13651         {
13652           if (!in_system_header)
13653             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13654           break;
13655         }
13656     }
13657 }
13658
13659 /* Parse an enumerator-definition.  The enumerator has the indicated
13660    TYPE.
13661
13662    enumerator-definition:
13663      enumerator
13664      enumerator = constant-expression
13665
13666    enumerator:
13667      identifier  */
13668
13669 static void
13670 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13671 {
13672   tree identifier;
13673   tree value;
13674   location_t loc;
13675
13676   /* Save the input location because we are interested in the location
13677      of the identifier and not the location of the explicit value.  */
13678   loc = cp_lexer_peek_token (parser->lexer)->location;
13679
13680   /* Look for the identifier.  */
13681   identifier = cp_parser_identifier (parser);
13682   if (identifier == error_mark_node)
13683     return;
13684
13685   /* If the next token is an '=', then there is an explicit value.  */
13686   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13687     {
13688       /* Consume the `=' token.  */
13689       cp_lexer_consume_token (parser->lexer);
13690       /* Parse the value.  */
13691       value = cp_parser_constant_expression (parser,
13692                                              /*allow_non_constant_p=*/false,
13693                                              NULL);
13694     }
13695   else
13696     value = NULL_TREE;
13697
13698   /* If we are processing a template, make sure the initializer of the
13699      enumerator doesn't contain any bare template parameter pack.  */
13700   if (check_for_bare_parameter_packs (value))
13701     value = error_mark_node;
13702
13703   /* Create the enumerator.  */
13704   build_enumerator (identifier, value, type, loc);
13705 }
13706
13707 /* Parse a namespace-name.
13708
13709    namespace-name:
13710      original-namespace-name
13711      namespace-alias
13712
13713    Returns the NAMESPACE_DECL for the namespace.  */
13714
13715 static tree
13716 cp_parser_namespace_name (cp_parser* parser)
13717 {
13718   tree identifier;
13719   tree namespace_decl;
13720
13721   cp_token *token = cp_lexer_peek_token (parser->lexer);
13722
13723   /* Get the name of the namespace.  */
13724   identifier = cp_parser_identifier (parser);
13725   if (identifier == error_mark_node)
13726     return error_mark_node;
13727
13728   /* Look up the identifier in the currently active scope.  Look only
13729      for namespaces, due to:
13730
13731        [basic.lookup.udir]
13732
13733        When looking up a namespace-name in a using-directive or alias
13734        definition, only namespace names are considered.
13735
13736      And:
13737
13738        [basic.lookup.qual]
13739
13740        During the lookup of a name preceding the :: scope resolution
13741        operator, object, function, and enumerator names are ignored.
13742
13743      (Note that cp_parser_qualifying_entity only calls this
13744      function if the token after the name is the scope resolution
13745      operator.)  */
13746   namespace_decl = cp_parser_lookup_name (parser, identifier,
13747                                           none_type,
13748                                           /*is_template=*/false,
13749                                           /*is_namespace=*/true,
13750                                           /*check_dependency=*/true,
13751                                           /*ambiguous_decls=*/NULL,
13752                                           token->location);
13753   /* If it's not a namespace, issue an error.  */
13754   if (namespace_decl == error_mark_node
13755       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13756     {
13757       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13758         error_at (token->location, "%qD is not a namespace-name", identifier);
13759       cp_parser_error (parser, "expected namespace-name");
13760       namespace_decl = error_mark_node;
13761     }
13762
13763   return namespace_decl;
13764 }
13765
13766 /* Parse a namespace-definition.
13767
13768    namespace-definition:
13769      named-namespace-definition
13770      unnamed-namespace-definition
13771
13772    named-namespace-definition:
13773      original-namespace-definition
13774      extension-namespace-definition
13775
13776    original-namespace-definition:
13777      namespace identifier { namespace-body }
13778
13779    extension-namespace-definition:
13780      namespace original-namespace-name { namespace-body }
13781
13782    unnamed-namespace-definition:
13783      namespace { namespace-body } */
13784
13785 static void
13786 cp_parser_namespace_definition (cp_parser* parser)
13787 {
13788   tree identifier, attribs;
13789   bool has_visibility;
13790   bool is_inline;
13791
13792   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13793     {
13794       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13795       is_inline = true;
13796       cp_lexer_consume_token (parser->lexer);
13797     }
13798   else
13799     is_inline = false;
13800
13801   /* Look for the `namespace' keyword.  */
13802   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13803
13804   /* Get the name of the namespace.  We do not attempt to distinguish
13805      between an original-namespace-definition and an
13806      extension-namespace-definition at this point.  The semantic
13807      analysis routines are responsible for that.  */
13808   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13809     identifier = cp_parser_identifier (parser);
13810   else
13811     identifier = NULL_TREE;
13812
13813   /* Parse any specified attributes.  */
13814   attribs = cp_parser_attributes_opt (parser);
13815
13816   /* Look for the `{' to start the namespace.  */
13817   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13818   /* Start the namespace.  */
13819   push_namespace (identifier);
13820
13821   /* "inline namespace" is equivalent to a stub namespace definition
13822      followed by a strong using directive.  */
13823   if (is_inline)
13824     {
13825       tree name_space = current_namespace;
13826       /* Set up namespace association.  */
13827       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13828         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13829                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13830       /* Import the contents of the inline namespace.  */
13831       pop_namespace ();
13832       do_using_directive (name_space);
13833       push_namespace (identifier);
13834     }
13835
13836   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13837
13838   /* Parse the body of the namespace.  */
13839   cp_parser_namespace_body (parser);
13840
13841   if (has_visibility)
13842     pop_visibility (1);
13843
13844   /* Finish the namespace.  */
13845   pop_namespace ();
13846   /* Look for the final `}'.  */
13847   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13848 }
13849
13850 /* Parse a namespace-body.
13851
13852    namespace-body:
13853      declaration-seq [opt]  */
13854
13855 static void
13856 cp_parser_namespace_body (cp_parser* parser)
13857 {
13858   cp_parser_declaration_seq_opt (parser);
13859 }
13860
13861 /* Parse a namespace-alias-definition.
13862
13863    namespace-alias-definition:
13864      namespace identifier = qualified-namespace-specifier ;  */
13865
13866 static void
13867 cp_parser_namespace_alias_definition (cp_parser* parser)
13868 {
13869   tree identifier;
13870   tree namespace_specifier;
13871
13872   cp_token *token = cp_lexer_peek_token (parser->lexer);
13873
13874   /* Look for the `namespace' keyword.  */
13875   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13876   /* Look for the identifier.  */
13877   identifier = cp_parser_identifier (parser);
13878   if (identifier == error_mark_node)
13879     return;
13880   /* Look for the `=' token.  */
13881   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13882       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13883     {
13884       error_at (token->location, "%<namespace%> definition is not allowed here");
13885       /* Skip the definition.  */
13886       cp_lexer_consume_token (parser->lexer);
13887       if (cp_parser_skip_to_closing_brace (parser))
13888         cp_lexer_consume_token (parser->lexer);
13889       return;
13890     }
13891   cp_parser_require (parser, CPP_EQ, RT_EQ);
13892   /* Look for the qualified-namespace-specifier.  */
13893   namespace_specifier
13894     = cp_parser_qualified_namespace_specifier (parser);
13895   /* Look for the `;' token.  */
13896   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13897
13898   /* Register the alias in the symbol table.  */
13899   do_namespace_alias (identifier, namespace_specifier);
13900 }
13901
13902 /* Parse a qualified-namespace-specifier.
13903
13904    qualified-namespace-specifier:
13905      :: [opt] nested-name-specifier [opt] namespace-name
13906
13907    Returns a NAMESPACE_DECL corresponding to the specified
13908    namespace.  */
13909
13910 static tree
13911 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13912 {
13913   /* Look for the optional `::'.  */
13914   cp_parser_global_scope_opt (parser,
13915                               /*current_scope_valid_p=*/false);
13916
13917   /* Look for the optional nested-name-specifier.  */
13918   cp_parser_nested_name_specifier_opt (parser,
13919                                        /*typename_keyword_p=*/false,
13920                                        /*check_dependency_p=*/true,
13921                                        /*type_p=*/false,
13922                                        /*is_declaration=*/true);
13923
13924   return cp_parser_namespace_name (parser);
13925 }
13926
13927 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13928    access declaration.
13929
13930    using-declaration:
13931      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13932      using :: unqualified-id ;  
13933
13934    access-declaration:
13935      qualified-id ;  
13936
13937    */
13938
13939 static bool
13940 cp_parser_using_declaration (cp_parser* parser, 
13941                              bool access_declaration_p)
13942 {
13943   cp_token *token;
13944   bool typename_p = false;
13945   bool global_scope_p;
13946   tree decl;
13947   tree identifier;
13948   tree qscope;
13949
13950   if (access_declaration_p)
13951     cp_parser_parse_tentatively (parser);
13952   else
13953     {
13954       /* Look for the `using' keyword.  */
13955       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13956       
13957       /* Peek at the next token.  */
13958       token = cp_lexer_peek_token (parser->lexer);
13959       /* See if it's `typename'.  */
13960       if (token->keyword == RID_TYPENAME)
13961         {
13962           /* Remember that we've seen it.  */
13963           typename_p = true;
13964           /* Consume the `typename' token.  */
13965           cp_lexer_consume_token (parser->lexer);
13966         }
13967     }
13968
13969   /* Look for the optional global scope qualification.  */
13970   global_scope_p
13971     = (cp_parser_global_scope_opt (parser,
13972                                    /*current_scope_valid_p=*/false)
13973        != NULL_TREE);
13974
13975   /* If we saw `typename', or didn't see `::', then there must be a
13976      nested-name-specifier present.  */
13977   if (typename_p || !global_scope_p)
13978     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13979                                               /*check_dependency_p=*/true,
13980                                               /*type_p=*/false,
13981                                               /*is_declaration=*/true);
13982   /* Otherwise, we could be in either of the two productions.  In that
13983      case, treat the nested-name-specifier as optional.  */
13984   else
13985     qscope = cp_parser_nested_name_specifier_opt (parser,
13986                                                   /*typename_keyword_p=*/false,
13987                                                   /*check_dependency_p=*/true,
13988                                                   /*type_p=*/false,
13989                                                   /*is_declaration=*/true);
13990   if (!qscope)
13991     qscope = global_namespace;
13992
13993   if (access_declaration_p && cp_parser_error_occurred (parser))
13994     /* Something has already gone wrong; there's no need to parse
13995        further.  Since an error has occurred, the return value of
13996        cp_parser_parse_definitely will be false, as required.  */
13997     return cp_parser_parse_definitely (parser);
13998
13999   token = cp_lexer_peek_token (parser->lexer);
14000   /* Parse the unqualified-id.  */
14001   identifier = cp_parser_unqualified_id (parser,
14002                                          /*template_keyword_p=*/false,
14003                                          /*check_dependency_p=*/true,
14004                                          /*declarator_p=*/true,
14005                                          /*optional_p=*/false);
14006
14007   if (access_declaration_p)
14008     {
14009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14010         cp_parser_simulate_error (parser);
14011       if (!cp_parser_parse_definitely (parser))
14012         return false;
14013     }
14014
14015   /* The function we call to handle a using-declaration is different
14016      depending on what scope we are in.  */
14017   if (qscope == error_mark_node || identifier == error_mark_node)
14018     ;
14019   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14020            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14021     /* [namespace.udecl]
14022
14023        A using declaration shall not name a template-id.  */
14024     error_at (token->location,
14025               "a template-id may not appear in a using-declaration");
14026   else
14027     {
14028       if (at_class_scope_p ())
14029         {
14030           /* Create the USING_DECL.  */
14031           decl = do_class_using_decl (parser->scope, identifier);
14032
14033           if (check_for_bare_parameter_packs (decl))
14034             return false;
14035           else
14036             /* Add it to the list of members in this class.  */
14037             finish_member_declaration (decl);
14038         }
14039       else
14040         {
14041           decl = cp_parser_lookup_name_simple (parser,
14042                                                identifier,
14043                                                token->location);
14044           if (decl == error_mark_node)
14045             cp_parser_name_lookup_error (parser, identifier,
14046                                          decl, NLE_NULL,
14047                                          token->location);
14048           else if (check_for_bare_parameter_packs (decl))
14049             return false;
14050           else if (!at_namespace_scope_p ())
14051             do_local_using_decl (decl, qscope, identifier);
14052           else
14053             do_toplevel_using_decl (decl, qscope, identifier);
14054         }
14055     }
14056
14057   /* Look for the final `;'.  */
14058   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14059   
14060   return true;
14061 }
14062
14063 /* Parse a using-directive.
14064
14065    using-directive:
14066      using namespace :: [opt] nested-name-specifier [opt]
14067        namespace-name ;  */
14068
14069 static void
14070 cp_parser_using_directive (cp_parser* parser)
14071 {
14072   tree namespace_decl;
14073   tree attribs;
14074
14075   /* Look for the `using' keyword.  */
14076   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14077   /* And the `namespace' keyword.  */
14078   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14079   /* Look for the optional `::' operator.  */
14080   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14081   /* And the optional nested-name-specifier.  */
14082   cp_parser_nested_name_specifier_opt (parser,
14083                                        /*typename_keyword_p=*/false,
14084                                        /*check_dependency_p=*/true,
14085                                        /*type_p=*/false,
14086                                        /*is_declaration=*/true);
14087   /* Get the namespace being used.  */
14088   namespace_decl = cp_parser_namespace_name (parser);
14089   /* And any specified attributes.  */
14090   attribs = cp_parser_attributes_opt (parser);
14091   /* Update the symbol table.  */
14092   parse_using_directive (namespace_decl, attribs);
14093   /* Look for the final `;'.  */
14094   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14095 }
14096
14097 /* Parse an asm-definition.
14098
14099    asm-definition:
14100      asm ( string-literal ) ;
14101
14102    GNU Extension:
14103
14104    asm-definition:
14105      asm volatile [opt] ( string-literal ) ;
14106      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14107      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14108                           : asm-operand-list [opt] ) ;
14109      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14110                           : asm-operand-list [opt]
14111                           : asm-clobber-list [opt] ) ;
14112      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14113                                : asm-clobber-list [opt]
14114                                : asm-goto-list ) ;  */
14115
14116 static void
14117 cp_parser_asm_definition (cp_parser* parser)
14118 {
14119   tree string;
14120   tree outputs = NULL_TREE;
14121   tree inputs = NULL_TREE;
14122   tree clobbers = NULL_TREE;
14123   tree labels = NULL_TREE;
14124   tree asm_stmt;
14125   bool volatile_p = false;
14126   bool extended_p = false;
14127   bool invalid_inputs_p = false;
14128   bool invalid_outputs_p = false;
14129   bool goto_p = false;
14130   required_token missing = RT_NONE;
14131
14132   /* Look for the `asm' keyword.  */
14133   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14134   /* See if the next token is `volatile'.  */
14135   if (cp_parser_allow_gnu_extensions_p (parser)
14136       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14137     {
14138       /* Remember that we saw the `volatile' keyword.  */
14139       volatile_p = true;
14140       /* Consume the token.  */
14141       cp_lexer_consume_token (parser->lexer);
14142     }
14143   if (cp_parser_allow_gnu_extensions_p (parser)
14144       && parser->in_function_body
14145       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14146     {
14147       /* Remember that we saw the `goto' keyword.  */
14148       goto_p = true;
14149       /* Consume the token.  */
14150       cp_lexer_consume_token (parser->lexer);
14151     }
14152   /* Look for the opening `('.  */
14153   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14154     return;
14155   /* Look for the string.  */
14156   string = cp_parser_string_literal (parser, false, false);
14157   if (string == error_mark_node)
14158     {
14159       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160                                              /*consume_paren=*/true);
14161       return;
14162     }
14163
14164   /* If we're allowing GNU extensions, check for the extended assembly
14165      syntax.  Unfortunately, the `:' tokens need not be separated by
14166      a space in C, and so, for compatibility, we tolerate that here
14167      too.  Doing that means that we have to treat the `::' operator as
14168      two `:' tokens.  */
14169   if (cp_parser_allow_gnu_extensions_p (parser)
14170       && parser->in_function_body
14171       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14172           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14173     {
14174       bool inputs_p = false;
14175       bool clobbers_p = false;
14176       bool labels_p = false;
14177
14178       /* The extended syntax was used.  */
14179       extended_p = true;
14180
14181       /* Look for outputs.  */
14182       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14183         {
14184           /* Consume the `:'.  */
14185           cp_lexer_consume_token (parser->lexer);
14186           /* Parse the output-operands.  */
14187           if (cp_lexer_next_token_is_not (parser->lexer,
14188                                           CPP_COLON)
14189               && cp_lexer_next_token_is_not (parser->lexer,
14190                                              CPP_SCOPE)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_CLOSE_PAREN)
14193               && !goto_p)
14194             outputs = cp_parser_asm_operand_list (parser);
14195
14196             if (outputs == error_mark_node)
14197               invalid_outputs_p = true;
14198         }
14199       /* If the next token is `::', there are no outputs, and the
14200          next token is the beginning of the inputs.  */
14201       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14202         /* The inputs are coming next.  */
14203         inputs_p = true;
14204
14205       /* Look for inputs.  */
14206       if (inputs_p
14207           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14208         {
14209           /* Consume the `:' or `::'.  */
14210           cp_lexer_consume_token (parser->lexer);
14211           /* Parse the output-operands.  */
14212           if (cp_lexer_next_token_is_not (parser->lexer,
14213                                           CPP_COLON)
14214               && cp_lexer_next_token_is_not (parser->lexer,
14215                                              CPP_SCOPE)
14216               && cp_lexer_next_token_is_not (parser->lexer,
14217                                              CPP_CLOSE_PAREN))
14218             inputs = cp_parser_asm_operand_list (parser);
14219
14220             if (inputs == error_mark_node)
14221               invalid_inputs_p = true;
14222         }
14223       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14224         /* The clobbers are coming next.  */
14225         clobbers_p = true;
14226
14227       /* Look for clobbers.  */
14228       if (clobbers_p
14229           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14230         {
14231           clobbers_p = true;
14232           /* Consume the `:' or `::'.  */
14233           cp_lexer_consume_token (parser->lexer);
14234           /* Parse the clobbers.  */
14235           if (cp_lexer_next_token_is_not (parser->lexer,
14236                                           CPP_COLON)
14237               && cp_lexer_next_token_is_not (parser->lexer,
14238                                              CPP_CLOSE_PAREN))
14239             clobbers = cp_parser_asm_clobber_list (parser);
14240         }
14241       else if (goto_p
14242                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14243         /* The labels are coming next.  */
14244         labels_p = true;
14245
14246       /* Look for labels.  */
14247       if (labels_p
14248           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14249         {
14250           labels_p = true;
14251           /* Consume the `:' or `::'.  */
14252           cp_lexer_consume_token (parser->lexer);
14253           /* Parse the labels.  */
14254           labels = cp_parser_asm_label_list (parser);
14255         }
14256
14257       if (goto_p && !labels_p)
14258         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14259     }
14260   else if (goto_p)
14261     missing = RT_COLON_SCOPE;
14262
14263   /* Look for the closing `)'.  */
14264   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14265                           missing ? missing : RT_CLOSE_PAREN))
14266     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14267                                            /*consume_paren=*/true);
14268   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14269
14270   if (!invalid_inputs_p && !invalid_outputs_p)
14271     {
14272       /* Create the ASM_EXPR.  */
14273       if (parser->in_function_body)
14274         {
14275           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14276                                       inputs, clobbers, labels);
14277           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14278           if (!extended_p)
14279             {
14280               tree temp = asm_stmt;
14281               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14282                 temp = TREE_OPERAND (temp, 0);
14283
14284               ASM_INPUT_P (temp) = 1;
14285             }
14286         }
14287       else
14288         cgraph_add_asm_node (string);
14289     }
14290 }
14291
14292 /* Declarators [gram.dcl.decl] */
14293
14294 /* Parse an init-declarator.
14295
14296    init-declarator:
14297      declarator initializer [opt]
14298
14299    GNU Extension:
14300
14301    init-declarator:
14302      declarator asm-specification [opt] attributes [opt] initializer [opt]
14303
14304    function-definition:
14305      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14306        function-body
14307      decl-specifier-seq [opt] declarator function-try-block
14308
14309    GNU Extension:
14310
14311    function-definition:
14312      __extension__ function-definition
14313
14314    The DECL_SPECIFIERS apply to this declarator.  Returns a
14315    representation of the entity declared.  If MEMBER_P is TRUE, then
14316    this declarator appears in a class scope.  The new DECL created by
14317    this declarator is returned.
14318
14319    The CHECKS are access checks that should be performed once we know
14320    what entity is being declared (and, therefore, what classes have
14321    befriended it).
14322
14323    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14324    for a function-definition here as well.  If the declarator is a
14325    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14326    be TRUE upon return.  By that point, the function-definition will
14327    have been completely parsed.
14328
14329    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14330    is FALSE.  */
14331
14332 static tree
14333 cp_parser_init_declarator (cp_parser* parser,
14334                            cp_decl_specifier_seq *decl_specifiers,
14335                            VEC (deferred_access_check,gc)* checks,
14336                            bool function_definition_allowed_p,
14337                            bool member_p,
14338                            int declares_class_or_enum,
14339                            bool* function_definition_p)
14340 {
14341   cp_token *token = NULL, *asm_spec_start_token = NULL,
14342            *attributes_start_token = NULL;
14343   cp_declarator *declarator;
14344   tree prefix_attributes;
14345   tree attributes;
14346   tree asm_specification;
14347   tree initializer;
14348   tree decl = NULL_TREE;
14349   tree scope;
14350   int is_initialized;
14351   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14352      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14353      "(...)".  */
14354   enum cpp_ttype initialization_kind;
14355   bool is_direct_init = false;
14356   bool is_non_constant_init;
14357   int ctor_dtor_or_conv_p;
14358   bool friend_p;
14359   tree pushed_scope = NULL;
14360
14361   /* Gather the attributes that were provided with the
14362      decl-specifiers.  */
14363   prefix_attributes = decl_specifiers->attributes;
14364
14365   /* Assume that this is not the declarator for a function
14366      definition.  */
14367   if (function_definition_p)
14368     *function_definition_p = false;
14369
14370   /* Defer access checks while parsing the declarator; we cannot know
14371      what names are accessible until we know what is being
14372      declared.  */
14373   resume_deferring_access_checks ();
14374
14375   /* Parse the declarator.  */
14376   token = cp_lexer_peek_token (parser->lexer);
14377   declarator
14378     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14379                             &ctor_dtor_or_conv_p,
14380                             /*parenthesized_p=*/NULL,
14381                             /*member_p=*/false);
14382   /* Gather up the deferred checks.  */
14383   stop_deferring_access_checks ();
14384
14385   /* If the DECLARATOR was erroneous, there's no need to go
14386      further.  */
14387   if (declarator == cp_error_declarator)
14388     return error_mark_node;
14389
14390   /* Check that the number of template-parameter-lists is OK.  */
14391   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14392                                                        token->location))
14393     return error_mark_node;
14394
14395   if (declares_class_or_enum & 2)
14396     cp_parser_check_for_definition_in_return_type (declarator,
14397                                                    decl_specifiers->type,
14398                                                    decl_specifiers->type_location);
14399
14400   /* Figure out what scope the entity declared by the DECLARATOR is
14401      located in.  `grokdeclarator' sometimes changes the scope, so
14402      we compute it now.  */
14403   scope = get_scope_of_declarator (declarator);
14404
14405   /* Perform any lookups in the declared type which were thought to be
14406      dependent, but are not in the scope of the declarator.  */
14407   decl_specifiers->type
14408     = maybe_update_decl_type (decl_specifiers->type, scope);
14409
14410   /* If we're allowing GNU extensions, look for an asm-specification
14411      and attributes.  */
14412   if (cp_parser_allow_gnu_extensions_p (parser))
14413     {
14414       /* Look for an asm-specification.  */
14415       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14416       asm_specification = cp_parser_asm_specification_opt (parser);
14417       /* And attributes.  */
14418       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14419       attributes = cp_parser_attributes_opt (parser);
14420     }
14421   else
14422     {
14423       asm_specification = NULL_TREE;
14424       attributes = NULL_TREE;
14425     }
14426
14427   /* Peek at the next token.  */
14428   token = cp_lexer_peek_token (parser->lexer);
14429   /* Check to see if the token indicates the start of a
14430      function-definition.  */
14431   if (function_declarator_p (declarator)
14432       && cp_parser_token_starts_function_definition_p (token))
14433     {
14434       if (!function_definition_allowed_p)
14435         {
14436           /* If a function-definition should not appear here, issue an
14437              error message.  */
14438           cp_parser_error (parser,
14439                            "a function-definition is not allowed here");
14440           return error_mark_node;
14441         }
14442       else
14443         {
14444           location_t func_brace_location
14445             = cp_lexer_peek_token (parser->lexer)->location;
14446
14447           /* Neither attributes nor an asm-specification are allowed
14448              on a function-definition.  */
14449           if (asm_specification)
14450             error_at (asm_spec_start_token->location,
14451                       "an asm-specification is not allowed "
14452                       "on a function-definition");
14453           if (attributes)
14454             error_at (attributes_start_token->location,
14455                       "attributes are not allowed on a function-definition");
14456           /* This is a function-definition.  */
14457           *function_definition_p = true;
14458
14459           /* Parse the function definition.  */
14460           if (member_p)
14461             decl = cp_parser_save_member_function_body (parser,
14462                                                         decl_specifiers,
14463                                                         declarator,
14464                                                         prefix_attributes);
14465           else
14466             decl
14467               = (cp_parser_function_definition_from_specifiers_and_declarator
14468                  (parser, decl_specifiers, prefix_attributes, declarator));
14469
14470           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14471             {
14472               /* This is where the prologue starts...  */
14473               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14474                 = func_brace_location;
14475             }
14476
14477           return decl;
14478         }
14479     }
14480
14481   /* [dcl.dcl]
14482
14483      Only in function declarations for constructors, destructors, and
14484      type conversions can the decl-specifier-seq be omitted.
14485
14486      We explicitly postpone this check past the point where we handle
14487      function-definitions because we tolerate function-definitions
14488      that are missing their return types in some modes.  */
14489   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14490     {
14491       cp_parser_error (parser,
14492                        "expected constructor, destructor, or type conversion");
14493       return error_mark_node;
14494     }
14495
14496   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14497   if (token->type == CPP_EQ
14498       || token->type == CPP_OPEN_PAREN
14499       || token->type == CPP_OPEN_BRACE)
14500     {
14501       is_initialized = SD_INITIALIZED;
14502       initialization_kind = token->type;
14503
14504       if (token->type == CPP_EQ
14505           && function_declarator_p (declarator))
14506         {
14507           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14508           if (t2->keyword == RID_DEFAULT)
14509             is_initialized = SD_DEFAULTED;
14510           else if (t2->keyword == RID_DELETE)
14511             is_initialized = SD_DELETED;
14512         }
14513     }
14514   else
14515     {
14516       /* If the init-declarator isn't initialized and isn't followed by a
14517          `,' or `;', it's not a valid init-declarator.  */
14518       if (token->type != CPP_COMMA
14519           && token->type != CPP_SEMICOLON)
14520         {
14521           cp_parser_error (parser, "expected initializer");
14522           return error_mark_node;
14523         }
14524       is_initialized = SD_UNINITIALIZED;
14525       initialization_kind = CPP_EOF;
14526     }
14527
14528   /* Because start_decl has side-effects, we should only call it if we
14529      know we're going ahead.  By this point, we know that we cannot
14530      possibly be looking at any other construct.  */
14531   cp_parser_commit_to_tentative_parse (parser);
14532
14533   /* If the decl specifiers were bad, issue an error now that we're
14534      sure this was intended to be a declarator.  Then continue
14535      declaring the variable(s), as int, to try to cut down on further
14536      errors.  */
14537   if (decl_specifiers->any_specifiers_p
14538       && decl_specifiers->type == error_mark_node)
14539     {
14540       cp_parser_error (parser, "invalid type in declaration");
14541       decl_specifiers->type = integer_type_node;
14542     }
14543
14544   /* Check to see whether or not this declaration is a friend.  */
14545   friend_p = cp_parser_friend_p (decl_specifiers);
14546
14547   /* Enter the newly declared entry in the symbol table.  If we're
14548      processing a declaration in a class-specifier, we wait until
14549      after processing the initializer.  */
14550   if (!member_p)
14551     {
14552       if (parser->in_unbraced_linkage_specification_p)
14553         decl_specifiers->storage_class = sc_extern;
14554       decl = start_decl (declarator, decl_specifiers,
14555                          is_initialized, attributes, prefix_attributes,
14556                          &pushed_scope);
14557       /* Adjust location of decl if declarator->id_loc is more appropriate:
14558          set, and decl wasn't merged with another decl, in which case its
14559          location would be different from input_location, and more accurate.  */
14560       if (DECL_P (decl)
14561           && declarator->id_loc != UNKNOWN_LOCATION
14562           && DECL_SOURCE_LOCATION (decl) == input_location)
14563         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14564     }
14565   else if (scope)
14566     /* Enter the SCOPE.  That way unqualified names appearing in the
14567        initializer will be looked up in SCOPE.  */
14568     pushed_scope = push_scope (scope);
14569
14570   /* Perform deferred access control checks, now that we know in which
14571      SCOPE the declared entity resides.  */
14572   if (!member_p && decl)
14573     {
14574       tree saved_current_function_decl = NULL_TREE;
14575
14576       /* If the entity being declared is a function, pretend that we
14577          are in its scope.  If it is a `friend', it may have access to
14578          things that would not otherwise be accessible.  */
14579       if (TREE_CODE (decl) == FUNCTION_DECL)
14580         {
14581           saved_current_function_decl = current_function_decl;
14582           current_function_decl = decl;
14583         }
14584
14585       /* Perform access checks for template parameters.  */
14586       cp_parser_perform_template_parameter_access_checks (checks);
14587
14588       /* Perform the access control checks for the declarator and the
14589          decl-specifiers.  */
14590       perform_deferred_access_checks ();
14591
14592       /* Restore the saved value.  */
14593       if (TREE_CODE (decl) == FUNCTION_DECL)
14594         current_function_decl = saved_current_function_decl;
14595     }
14596
14597   /* Parse the initializer.  */
14598   initializer = NULL_TREE;
14599   is_direct_init = false;
14600   is_non_constant_init = true;
14601   if (is_initialized)
14602     {
14603       if (function_declarator_p (declarator))
14604         {
14605           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14606            if (initialization_kind == CPP_EQ)
14607              initializer = cp_parser_pure_specifier (parser);
14608            else
14609              {
14610                /* If the declaration was erroneous, we don't really
14611                   know what the user intended, so just silently
14612                   consume the initializer.  */
14613                if (decl != error_mark_node)
14614                  error_at (initializer_start_token->location,
14615                            "initializer provided for function");
14616                cp_parser_skip_to_closing_parenthesis (parser,
14617                                                       /*recovering=*/true,
14618                                                       /*or_comma=*/false,
14619                                                       /*consume_paren=*/true);
14620              }
14621         }
14622       else
14623         {
14624           /* We want to record the extra mangling scope for in-class
14625              initializers of class members and initializers of static data
14626              member templates.  The former is a C++0x feature which isn't
14627              implemented yet, and I expect it will involve deferring
14628              parsing of the initializer until end of class as with default
14629              arguments.  So right here we only handle the latter.  */
14630           if (!member_p && processing_template_decl)
14631             start_lambda_scope (decl);
14632           initializer = cp_parser_initializer (parser,
14633                                                &is_direct_init,
14634                                                &is_non_constant_init);
14635           if (!member_p && processing_template_decl)
14636             finish_lambda_scope ();
14637         }
14638     }
14639
14640   /* The old parser allows attributes to appear after a parenthesized
14641      initializer.  Mark Mitchell proposed removing this functionality
14642      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14643      attributes -- but ignores them.  */
14644   if (cp_parser_allow_gnu_extensions_p (parser)
14645       && initialization_kind == CPP_OPEN_PAREN)
14646     if (cp_parser_attributes_opt (parser))
14647       warning (OPT_Wattributes,
14648                "attributes after parenthesized initializer ignored");
14649
14650   /* For an in-class declaration, use `grokfield' to create the
14651      declaration.  */
14652   if (member_p)
14653     {
14654       if (pushed_scope)
14655         {
14656           pop_scope (pushed_scope);
14657           pushed_scope = false;
14658         }
14659       decl = grokfield (declarator, decl_specifiers,
14660                         initializer, !is_non_constant_init,
14661                         /*asmspec=*/NULL_TREE,
14662                         prefix_attributes);
14663       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14664         cp_parser_save_default_args (parser, decl);
14665     }
14666
14667   /* Finish processing the declaration.  But, skip friend
14668      declarations.  */
14669   if (!friend_p && decl && decl != error_mark_node)
14670     {
14671       cp_finish_decl (decl,
14672                       initializer, !is_non_constant_init,
14673                       asm_specification,
14674                       /* If the initializer is in parentheses, then this is
14675                          a direct-initialization, which means that an
14676                          `explicit' constructor is OK.  Otherwise, an
14677                          `explicit' constructor cannot be used.  */
14678                       ((is_direct_init || !is_initialized)
14679                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14680     }
14681   else if ((cxx_dialect != cxx98) && friend_p
14682            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14683     /* Core issue #226 (C++0x only): A default template-argument
14684        shall not be specified in a friend class template
14685        declaration. */
14686     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14687                              /*is_partial=*/0, /*is_friend_decl=*/1);
14688
14689   if (!friend_p && pushed_scope)
14690     pop_scope (pushed_scope);
14691
14692   return decl;
14693 }
14694
14695 /* Parse a declarator.
14696
14697    declarator:
14698      direct-declarator
14699      ptr-operator declarator
14700
14701    abstract-declarator:
14702      ptr-operator abstract-declarator [opt]
14703      direct-abstract-declarator
14704
14705    GNU Extensions:
14706
14707    declarator:
14708      attributes [opt] direct-declarator
14709      attributes [opt] ptr-operator declarator
14710
14711    abstract-declarator:
14712      attributes [opt] ptr-operator abstract-declarator [opt]
14713      attributes [opt] direct-abstract-declarator
14714
14715    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14716    detect constructor, destructor or conversion operators. It is set
14717    to -1 if the declarator is a name, and +1 if it is a
14718    function. Otherwise it is set to zero. Usually you just want to
14719    test for >0, but internally the negative value is used.
14720
14721    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14722    a decl-specifier-seq unless it declares a constructor, destructor,
14723    or conversion.  It might seem that we could check this condition in
14724    semantic analysis, rather than parsing, but that makes it difficult
14725    to handle something like `f()'.  We want to notice that there are
14726    no decl-specifiers, and therefore realize that this is an
14727    expression, not a declaration.)
14728
14729    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14730    the declarator is a direct-declarator of the form "(...)".
14731
14732    MEMBER_P is true iff this declarator is a member-declarator.  */
14733
14734 static cp_declarator *
14735 cp_parser_declarator (cp_parser* parser,
14736                       cp_parser_declarator_kind dcl_kind,
14737                       int* ctor_dtor_or_conv_p,
14738                       bool* parenthesized_p,
14739                       bool member_p)
14740 {
14741   cp_declarator *declarator;
14742   enum tree_code code;
14743   cp_cv_quals cv_quals;
14744   tree class_type;
14745   tree attributes = NULL_TREE;
14746
14747   /* Assume this is not a constructor, destructor, or type-conversion
14748      operator.  */
14749   if (ctor_dtor_or_conv_p)
14750     *ctor_dtor_or_conv_p = 0;
14751
14752   if (cp_parser_allow_gnu_extensions_p (parser))
14753     attributes = cp_parser_attributes_opt (parser);
14754
14755   /* Check for the ptr-operator production.  */
14756   cp_parser_parse_tentatively (parser);
14757   /* Parse the ptr-operator.  */
14758   code = cp_parser_ptr_operator (parser,
14759                                  &class_type,
14760                                  &cv_quals);
14761   /* If that worked, then we have a ptr-operator.  */
14762   if (cp_parser_parse_definitely (parser))
14763     {
14764       /* If a ptr-operator was found, then this declarator was not
14765          parenthesized.  */
14766       if (parenthesized_p)
14767         *parenthesized_p = true;
14768       /* The dependent declarator is optional if we are parsing an
14769          abstract-declarator.  */
14770       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14771         cp_parser_parse_tentatively (parser);
14772
14773       /* Parse the dependent declarator.  */
14774       declarator = cp_parser_declarator (parser, dcl_kind,
14775                                          /*ctor_dtor_or_conv_p=*/NULL,
14776                                          /*parenthesized_p=*/NULL,
14777                                          /*member_p=*/false);
14778
14779       /* If we are parsing an abstract-declarator, we must handle the
14780          case where the dependent declarator is absent.  */
14781       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14782           && !cp_parser_parse_definitely (parser))
14783         declarator = NULL;
14784
14785       declarator = cp_parser_make_indirect_declarator
14786         (code, class_type, cv_quals, declarator);
14787     }
14788   /* Everything else is a direct-declarator.  */
14789   else
14790     {
14791       if (parenthesized_p)
14792         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14793                                                    CPP_OPEN_PAREN);
14794       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14795                                                 ctor_dtor_or_conv_p,
14796                                                 member_p);
14797     }
14798
14799   if (attributes && declarator && declarator != cp_error_declarator)
14800     declarator->attributes = attributes;
14801
14802   return declarator;
14803 }
14804
14805 /* Parse a direct-declarator or direct-abstract-declarator.
14806
14807    direct-declarator:
14808      declarator-id
14809      direct-declarator ( parameter-declaration-clause )
14810        cv-qualifier-seq [opt]
14811        exception-specification [opt]
14812      direct-declarator [ constant-expression [opt] ]
14813      ( declarator )
14814
14815    direct-abstract-declarator:
14816      direct-abstract-declarator [opt]
14817        ( parameter-declaration-clause )
14818        cv-qualifier-seq [opt]
14819        exception-specification [opt]
14820      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14821      ( abstract-declarator )
14822
14823    Returns a representation of the declarator.  DCL_KIND is
14824    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14825    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14826    we are parsing a direct-declarator.  It is
14827    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14828    of ambiguity we prefer an abstract declarator, as per
14829    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14830    cp_parser_declarator.  */
14831
14832 static cp_declarator *
14833 cp_parser_direct_declarator (cp_parser* parser,
14834                              cp_parser_declarator_kind dcl_kind,
14835                              int* ctor_dtor_or_conv_p,
14836                              bool member_p)
14837 {
14838   cp_token *token;
14839   cp_declarator *declarator = NULL;
14840   tree scope = NULL_TREE;
14841   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14842   bool saved_in_declarator_p = parser->in_declarator_p;
14843   bool first = true;
14844   tree pushed_scope = NULL_TREE;
14845
14846   while (true)
14847     {
14848       /* Peek at the next token.  */
14849       token = cp_lexer_peek_token (parser->lexer);
14850       if (token->type == CPP_OPEN_PAREN)
14851         {
14852           /* This is either a parameter-declaration-clause, or a
14853              parenthesized declarator. When we know we are parsing a
14854              named declarator, it must be a parenthesized declarator
14855              if FIRST is true. For instance, `(int)' is a
14856              parameter-declaration-clause, with an omitted
14857              direct-abstract-declarator. But `((*))', is a
14858              parenthesized abstract declarator. Finally, when T is a
14859              template parameter `(T)' is a
14860              parameter-declaration-clause, and not a parenthesized
14861              named declarator.
14862
14863              We first try and parse a parameter-declaration-clause,
14864              and then try a nested declarator (if FIRST is true).
14865
14866              It is not an error for it not to be a
14867              parameter-declaration-clause, even when FIRST is
14868              false. Consider,
14869
14870                int i (int);
14871                int i (3);
14872
14873              The first is the declaration of a function while the
14874              second is the definition of a variable, including its
14875              initializer.
14876
14877              Having seen only the parenthesis, we cannot know which of
14878              these two alternatives should be selected.  Even more
14879              complex are examples like:
14880
14881                int i (int (a));
14882                int i (int (3));
14883
14884              The former is a function-declaration; the latter is a
14885              variable initialization.
14886
14887              Thus again, we try a parameter-declaration-clause, and if
14888              that fails, we back out and return.  */
14889
14890           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14891             {
14892               tree params;
14893               unsigned saved_num_template_parameter_lists;
14894               bool is_declarator = false;
14895               tree t;
14896
14897               /* In a member-declarator, the only valid interpretation
14898                  of a parenthesis is the start of a
14899                  parameter-declaration-clause.  (It is invalid to
14900                  initialize a static data member with a parenthesized
14901                  initializer; only the "=" form of initialization is
14902                  permitted.)  */
14903               if (!member_p)
14904                 cp_parser_parse_tentatively (parser);
14905
14906               /* Consume the `('.  */
14907               cp_lexer_consume_token (parser->lexer);
14908               if (first)
14909                 {
14910                   /* If this is going to be an abstract declarator, we're
14911                      in a declarator and we can't have default args.  */
14912                   parser->default_arg_ok_p = false;
14913                   parser->in_declarator_p = true;
14914                 }
14915
14916               /* Inside the function parameter list, surrounding
14917                  template-parameter-lists do not apply.  */
14918               saved_num_template_parameter_lists
14919                 = parser->num_template_parameter_lists;
14920               parser->num_template_parameter_lists = 0;
14921
14922               begin_scope (sk_function_parms, NULL_TREE);
14923
14924               /* Parse the parameter-declaration-clause.  */
14925               params = cp_parser_parameter_declaration_clause (parser);
14926
14927               parser->num_template_parameter_lists
14928                 = saved_num_template_parameter_lists;
14929
14930               /* If all went well, parse the cv-qualifier-seq and the
14931                  exception-specification.  */
14932               if (member_p || cp_parser_parse_definitely (parser))
14933                 {
14934                   cp_cv_quals cv_quals;
14935                   tree exception_specification;
14936                   tree late_return;
14937
14938                   is_declarator = true;
14939
14940                   if (ctor_dtor_or_conv_p)
14941                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14942                   first = false;
14943                   /* Consume the `)'.  */
14944                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14945
14946                   /* Parse the cv-qualifier-seq.  */
14947                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14948                   /* And the exception-specification.  */
14949                   exception_specification
14950                     = cp_parser_exception_specification_opt (parser);
14951
14952                   late_return
14953                     = cp_parser_late_return_type_opt (parser);
14954
14955                   /* Create the function-declarator.  */
14956                   declarator = make_call_declarator (declarator,
14957                                                      params,
14958                                                      cv_quals,
14959                                                      exception_specification,
14960                                                      late_return);
14961                   /* Any subsequent parameter lists are to do with
14962                      return type, so are not those of the declared
14963                      function.  */
14964                   parser->default_arg_ok_p = false;
14965                 }
14966
14967               /* Remove the function parms from scope.  */
14968               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14969                 pop_binding (DECL_NAME (t), t);
14970               leave_scope();
14971
14972               if (is_declarator)
14973                 /* Repeat the main loop.  */
14974                 continue;
14975             }
14976
14977           /* If this is the first, we can try a parenthesized
14978              declarator.  */
14979           if (first)
14980             {
14981               bool saved_in_type_id_in_expr_p;
14982
14983               parser->default_arg_ok_p = saved_default_arg_ok_p;
14984               parser->in_declarator_p = saved_in_declarator_p;
14985
14986               /* Consume the `('.  */
14987               cp_lexer_consume_token (parser->lexer);
14988               /* Parse the nested declarator.  */
14989               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14990               parser->in_type_id_in_expr_p = true;
14991               declarator
14992                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14993                                         /*parenthesized_p=*/NULL,
14994                                         member_p);
14995               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14996               first = false;
14997               /* Expect a `)'.  */
14998               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14999                 declarator = cp_error_declarator;
15000               if (declarator == cp_error_declarator)
15001                 break;
15002
15003               goto handle_declarator;
15004             }
15005           /* Otherwise, we must be done.  */
15006           else
15007             break;
15008         }
15009       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15010                && token->type == CPP_OPEN_SQUARE)
15011         {
15012           /* Parse an array-declarator.  */
15013           tree bounds;
15014
15015           if (ctor_dtor_or_conv_p)
15016             *ctor_dtor_or_conv_p = 0;
15017
15018           first = false;
15019           parser->default_arg_ok_p = false;
15020           parser->in_declarator_p = true;
15021           /* Consume the `['.  */
15022           cp_lexer_consume_token (parser->lexer);
15023           /* Peek at the next token.  */
15024           token = cp_lexer_peek_token (parser->lexer);
15025           /* If the next token is `]', then there is no
15026              constant-expression.  */
15027           if (token->type != CPP_CLOSE_SQUARE)
15028             {
15029               bool non_constant_p;
15030
15031               bounds
15032                 = cp_parser_constant_expression (parser,
15033                                                  /*allow_non_constant=*/true,
15034                                                  &non_constant_p);
15035               if (!non_constant_p || cxx_dialect >= cxx0x)
15036                 /* OK */;
15037               /* Normally, the array bound must be an integral constant
15038                  expression.  However, as an extension, we allow VLAs
15039                  in function scopes as long as they aren't part of a
15040                  parameter declaration.  */
15041               else if (!parser->in_function_body
15042                        || current_binding_level->kind == sk_function_parms)
15043                 {
15044                   cp_parser_error (parser,
15045                                    "array bound is not an integer constant");
15046                   bounds = error_mark_node;
15047                 }
15048               else if (processing_template_decl && !error_operand_p (bounds))
15049                 {
15050                   /* Remember this wasn't a constant-expression.  */
15051                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15052                   TREE_SIDE_EFFECTS (bounds) = 1;
15053                 }
15054             }
15055           else
15056             bounds = NULL_TREE;
15057           /* Look for the closing `]'.  */
15058           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15059             {
15060               declarator = cp_error_declarator;
15061               break;
15062             }
15063
15064           declarator = make_array_declarator (declarator, bounds);
15065         }
15066       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15067         {
15068           {
15069             tree qualifying_scope;
15070             tree unqualified_name;
15071             special_function_kind sfk;
15072             bool abstract_ok;
15073             bool pack_expansion_p = false;
15074             cp_token *declarator_id_start_token;
15075
15076             /* Parse a declarator-id */
15077             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15078             if (abstract_ok)
15079               {
15080                 cp_parser_parse_tentatively (parser);
15081
15082                 /* If we see an ellipsis, we should be looking at a
15083                    parameter pack. */
15084                 if (token->type == CPP_ELLIPSIS)
15085                   {
15086                     /* Consume the `...' */
15087                     cp_lexer_consume_token (parser->lexer);
15088
15089                     pack_expansion_p = true;
15090                   }
15091               }
15092
15093             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15094             unqualified_name
15095               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15096             qualifying_scope = parser->scope;
15097             if (abstract_ok)
15098               {
15099                 bool okay = false;
15100
15101                 if (!unqualified_name && pack_expansion_p)
15102                   {
15103                     /* Check whether an error occurred. */
15104                     okay = !cp_parser_error_occurred (parser);
15105
15106                     /* We already consumed the ellipsis to mark a
15107                        parameter pack, but we have no way to report it,
15108                        so abort the tentative parse. We will be exiting
15109                        immediately anyway. */
15110                     cp_parser_abort_tentative_parse (parser);
15111                   }
15112                 else
15113                   okay = cp_parser_parse_definitely (parser);
15114
15115                 if (!okay)
15116                   unqualified_name = error_mark_node;
15117                 else if (unqualified_name
15118                          && (qualifying_scope
15119                              || (TREE_CODE (unqualified_name)
15120                                  != IDENTIFIER_NODE)))
15121                   {
15122                     cp_parser_error (parser, "expected unqualified-id");
15123                     unqualified_name = error_mark_node;
15124                   }
15125               }
15126
15127             if (!unqualified_name)
15128               return NULL;
15129             if (unqualified_name == error_mark_node)
15130               {
15131                 declarator = cp_error_declarator;
15132                 pack_expansion_p = false;
15133                 declarator->parameter_pack_p = false;
15134                 break;
15135               }
15136
15137             if (qualifying_scope && at_namespace_scope_p ()
15138                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15139               {
15140                 /* In the declaration of a member of a template class
15141                    outside of the class itself, the SCOPE will sometimes
15142                    be a TYPENAME_TYPE.  For example, given:
15143
15144                    template <typename T>
15145                    int S<T>::R::i = 3;
15146
15147                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15148                    this context, we must resolve S<T>::R to an ordinary
15149                    type, rather than a typename type.
15150
15151                    The reason we normally avoid resolving TYPENAME_TYPEs
15152                    is that a specialization of `S' might render
15153                    `S<T>::R' not a type.  However, if `S' is
15154                    specialized, then this `i' will not be used, so there
15155                    is no harm in resolving the types here.  */
15156                 tree type;
15157
15158                 /* Resolve the TYPENAME_TYPE.  */
15159                 type = resolve_typename_type (qualifying_scope,
15160                                               /*only_current_p=*/false);
15161                 /* If that failed, the declarator is invalid.  */
15162                 if (TREE_CODE (type) == TYPENAME_TYPE)
15163                   {
15164                     if (typedef_variant_p (type))
15165                       error_at (declarator_id_start_token->location,
15166                                 "cannot define member of dependent typedef "
15167                                 "%qT", type);
15168                     else
15169                       error_at (declarator_id_start_token->location,
15170                                 "%<%T::%E%> is not a type",
15171                                 TYPE_CONTEXT (qualifying_scope),
15172                                 TYPE_IDENTIFIER (qualifying_scope));
15173                   }
15174                 qualifying_scope = type;
15175               }
15176
15177             sfk = sfk_none;
15178
15179             if (unqualified_name)
15180               {
15181                 tree class_type;
15182
15183                 if (qualifying_scope
15184                     && CLASS_TYPE_P (qualifying_scope))
15185                   class_type = qualifying_scope;
15186                 else
15187                   class_type = current_class_type;
15188
15189                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15190                   {
15191                     tree name_type = TREE_TYPE (unqualified_name);
15192                     if (class_type && same_type_p (name_type, class_type))
15193                       {
15194                         if (qualifying_scope
15195                             && CLASSTYPE_USE_TEMPLATE (name_type))
15196                           {
15197                             error_at (declarator_id_start_token->location,
15198                                       "invalid use of constructor as a template");
15199                             inform (declarator_id_start_token->location,
15200                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15201                                     "name the constructor in a qualified name",
15202                                     class_type,
15203                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15204                                     class_type, name_type);
15205                             declarator = cp_error_declarator;
15206                             break;
15207                           }
15208                         else
15209                           unqualified_name = constructor_name (class_type);
15210                       }
15211                     else
15212                       {
15213                         /* We do not attempt to print the declarator
15214                            here because we do not have enough
15215                            information about its original syntactic
15216                            form.  */
15217                         cp_parser_error (parser, "invalid declarator");
15218                         declarator = cp_error_declarator;
15219                         break;
15220                       }
15221                   }
15222
15223                 if (class_type)
15224                   {
15225                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15226                       sfk = sfk_destructor;
15227                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15228                       sfk = sfk_conversion;
15229                     else if (/* There's no way to declare a constructor
15230                                 for an anonymous type, even if the type
15231                                 got a name for linkage purposes.  */
15232                              !TYPE_WAS_ANONYMOUS (class_type)
15233                              && constructor_name_p (unqualified_name,
15234                                                     class_type))
15235                       {
15236                         unqualified_name = constructor_name (class_type);
15237                         sfk = sfk_constructor;
15238                       }
15239                     else if (is_overloaded_fn (unqualified_name)
15240                              && DECL_CONSTRUCTOR_P (get_first_fn
15241                                                     (unqualified_name)))
15242                       sfk = sfk_constructor;
15243
15244                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15245                       *ctor_dtor_or_conv_p = -1;
15246                   }
15247               }
15248             declarator = make_id_declarator (qualifying_scope,
15249                                              unqualified_name,
15250                                              sfk);
15251             declarator->id_loc = token->location;
15252             declarator->parameter_pack_p = pack_expansion_p;
15253
15254             if (pack_expansion_p)
15255               maybe_warn_variadic_templates ();
15256           }
15257
15258         handle_declarator:;
15259           scope = get_scope_of_declarator (declarator);
15260           if (scope)
15261             /* Any names that appear after the declarator-id for a
15262                member are looked up in the containing scope.  */
15263             pushed_scope = push_scope (scope);
15264           parser->in_declarator_p = true;
15265           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15266               || (declarator && declarator->kind == cdk_id))
15267             /* Default args are only allowed on function
15268                declarations.  */
15269             parser->default_arg_ok_p = saved_default_arg_ok_p;
15270           else
15271             parser->default_arg_ok_p = false;
15272
15273           first = false;
15274         }
15275       /* We're done.  */
15276       else
15277         break;
15278     }
15279
15280   /* For an abstract declarator, we might wind up with nothing at this
15281      point.  That's an error; the declarator is not optional.  */
15282   if (!declarator)
15283     cp_parser_error (parser, "expected declarator");
15284
15285   /* If we entered a scope, we must exit it now.  */
15286   if (pushed_scope)
15287     pop_scope (pushed_scope);
15288
15289   parser->default_arg_ok_p = saved_default_arg_ok_p;
15290   parser->in_declarator_p = saved_in_declarator_p;
15291
15292   return declarator;
15293 }
15294
15295 /* Parse a ptr-operator.
15296
15297    ptr-operator:
15298      * cv-qualifier-seq [opt]
15299      &
15300      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15301
15302    GNU Extension:
15303
15304    ptr-operator:
15305      & cv-qualifier-seq [opt]
15306
15307    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15308    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15309    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15310    filled in with the TYPE containing the member.  *CV_QUALS is
15311    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15312    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15313    Note that the tree codes returned by this function have nothing
15314    to do with the types of trees that will be eventually be created
15315    to represent the pointer or reference type being parsed. They are
15316    just constants with suggestive names. */
15317 static enum tree_code
15318 cp_parser_ptr_operator (cp_parser* parser,
15319                         tree* type,
15320                         cp_cv_quals *cv_quals)
15321 {
15322   enum tree_code code = ERROR_MARK;
15323   cp_token *token;
15324
15325   /* Assume that it's not a pointer-to-member.  */
15326   *type = NULL_TREE;
15327   /* And that there are no cv-qualifiers.  */
15328   *cv_quals = TYPE_UNQUALIFIED;
15329
15330   /* Peek at the next token.  */
15331   token = cp_lexer_peek_token (parser->lexer);
15332
15333   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15334   if (token->type == CPP_MULT)
15335     code = INDIRECT_REF;
15336   else if (token->type == CPP_AND)
15337     code = ADDR_EXPR;
15338   else if ((cxx_dialect != cxx98) &&
15339            token->type == CPP_AND_AND) /* C++0x only */
15340     code = NON_LVALUE_EXPR;
15341
15342   if (code != ERROR_MARK)
15343     {
15344       /* Consume the `*', `&' or `&&'.  */
15345       cp_lexer_consume_token (parser->lexer);
15346
15347       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15348          `&', if we are allowing GNU extensions.  (The only qualifier
15349          that can legally appear after `&' is `restrict', but that is
15350          enforced during semantic analysis.  */
15351       if (code == INDIRECT_REF
15352           || cp_parser_allow_gnu_extensions_p (parser))
15353         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15354     }
15355   else
15356     {
15357       /* Try the pointer-to-member case.  */
15358       cp_parser_parse_tentatively (parser);
15359       /* Look for the optional `::' operator.  */
15360       cp_parser_global_scope_opt (parser,
15361                                   /*current_scope_valid_p=*/false);
15362       /* Look for the nested-name specifier.  */
15363       token = cp_lexer_peek_token (parser->lexer);
15364       cp_parser_nested_name_specifier (parser,
15365                                        /*typename_keyword_p=*/false,
15366                                        /*check_dependency_p=*/true,
15367                                        /*type_p=*/false,
15368                                        /*is_declaration=*/false);
15369       /* If we found it, and the next token is a `*', then we are
15370          indeed looking at a pointer-to-member operator.  */
15371       if (!cp_parser_error_occurred (parser)
15372           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15373         {
15374           /* Indicate that the `*' operator was used.  */
15375           code = INDIRECT_REF;
15376
15377           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15378             error_at (token->location, "%qD is a namespace", parser->scope);
15379           else
15380             {
15381               /* The type of which the member is a member is given by the
15382                  current SCOPE.  */
15383               *type = parser->scope;
15384               /* The next name will not be qualified.  */
15385               parser->scope = NULL_TREE;
15386               parser->qualifying_scope = NULL_TREE;
15387               parser->object_scope = NULL_TREE;
15388               /* Look for the optional cv-qualifier-seq.  */
15389               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15390             }
15391         }
15392       /* If that didn't work we don't have a ptr-operator.  */
15393       if (!cp_parser_parse_definitely (parser))
15394         cp_parser_error (parser, "expected ptr-operator");
15395     }
15396
15397   return code;
15398 }
15399
15400 /* Parse an (optional) cv-qualifier-seq.
15401
15402    cv-qualifier-seq:
15403      cv-qualifier cv-qualifier-seq [opt]
15404
15405    cv-qualifier:
15406      const
15407      volatile
15408
15409    GNU Extension:
15410
15411    cv-qualifier:
15412      __restrict__
15413
15414    Returns a bitmask representing the cv-qualifiers.  */
15415
15416 static cp_cv_quals
15417 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15418 {
15419   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15420
15421   while (true)
15422     {
15423       cp_token *token;
15424       cp_cv_quals cv_qualifier;
15425
15426       /* Peek at the next token.  */
15427       token = cp_lexer_peek_token (parser->lexer);
15428       /* See if it's a cv-qualifier.  */
15429       switch (token->keyword)
15430         {
15431         case RID_CONST:
15432           cv_qualifier = TYPE_QUAL_CONST;
15433           break;
15434
15435         case RID_VOLATILE:
15436           cv_qualifier = TYPE_QUAL_VOLATILE;
15437           break;
15438
15439         case RID_RESTRICT:
15440           cv_qualifier = TYPE_QUAL_RESTRICT;
15441           break;
15442
15443         default:
15444           cv_qualifier = TYPE_UNQUALIFIED;
15445           break;
15446         }
15447
15448       if (!cv_qualifier)
15449         break;
15450
15451       if (cv_quals & cv_qualifier)
15452         {
15453           error_at (token->location, "duplicate cv-qualifier");
15454           cp_lexer_purge_token (parser->lexer);
15455         }
15456       else
15457         {
15458           cp_lexer_consume_token (parser->lexer);
15459           cv_quals |= cv_qualifier;
15460         }
15461     }
15462
15463   return cv_quals;
15464 }
15465
15466 /* Parse a late-specified return type, if any.  This is not a separate
15467    non-terminal, but part of a function declarator, which looks like
15468
15469    -> trailing-type-specifier-seq abstract-declarator(opt)
15470
15471    Returns the type indicated by the type-id.  */
15472
15473 static tree
15474 cp_parser_late_return_type_opt (cp_parser* parser)
15475 {
15476   cp_token *token;
15477
15478   /* Peek at the next token.  */
15479   token = cp_lexer_peek_token (parser->lexer);
15480   /* A late-specified return type is indicated by an initial '->'. */
15481   if (token->type != CPP_DEREF)
15482     return NULL_TREE;
15483
15484   /* Consume the ->.  */
15485   cp_lexer_consume_token (parser->lexer);
15486
15487   return cp_parser_trailing_type_id (parser);
15488 }
15489
15490 /* Parse a declarator-id.
15491
15492    declarator-id:
15493      id-expression
15494      :: [opt] nested-name-specifier [opt] type-name
15495
15496    In the `id-expression' case, the value returned is as for
15497    cp_parser_id_expression if the id-expression was an unqualified-id.
15498    If the id-expression was a qualified-id, then a SCOPE_REF is
15499    returned.  The first operand is the scope (either a NAMESPACE_DECL
15500    or TREE_TYPE), but the second is still just a representation of an
15501    unqualified-id.  */
15502
15503 static tree
15504 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15505 {
15506   tree id;
15507   /* The expression must be an id-expression.  Assume that qualified
15508      names are the names of types so that:
15509
15510        template <class T>
15511        int S<T>::R::i = 3;
15512
15513      will work; we must treat `S<T>::R' as the name of a type.
15514      Similarly, assume that qualified names are templates, where
15515      required, so that:
15516
15517        template <class T>
15518        int S<T>::R<T>::i = 3;
15519
15520      will work, too.  */
15521   id = cp_parser_id_expression (parser,
15522                                 /*template_keyword_p=*/false,
15523                                 /*check_dependency_p=*/false,
15524                                 /*template_p=*/NULL,
15525                                 /*declarator_p=*/true,
15526                                 optional_p);
15527   if (id && BASELINK_P (id))
15528     id = BASELINK_FUNCTIONS (id);
15529   return id;
15530 }
15531
15532 /* Parse a type-id.
15533
15534    type-id:
15535      type-specifier-seq abstract-declarator [opt]
15536
15537    Returns the TYPE specified.  */
15538
15539 static tree
15540 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15541                      bool is_trailing_return)
15542 {
15543   cp_decl_specifier_seq type_specifier_seq;
15544   cp_declarator *abstract_declarator;
15545
15546   /* Parse the type-specifier-seq.  */
15547   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15548                                 is_trailing_return,
15549                                 &type_specifier_seq);
15550   if (type_specifier_seq.type == error_mark_node)
15551     return error_mark_node;
15552
15553   /* There might or might not be an abstract declarator.  */
15554   cp_parser_parse_tentatively (parser);
15555   /* Look for the declarator.  */
15556   abstract_declarator
15557     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15558                             /*parenthesized_p=*/NULL,
15559                             /*member_p=*/false);
15560   /* Check to see if there really was a declarator.  */
15561   if (!cp_parser_parse_definitely (parser))
15562     abstract_declarator = NULL;
15563
15564   if (type_specifier_seq.type
15565       && type_uses_auto (type_specifier_seq.type))
15566     {
15567       /* A type-id with type 'auto' is only ok if the abstract declarator
15568          is a function declarator with a late-specified return type.  */
15569       if (abstract_declarator
15570           && abstract_declarator->kind == cdk_function
15571           && abstract_declarator->u.function.late_return_type)
15572         /* OK */;
15573       else
15574         {
15575           error ("invalid use of %<auto%>");
15576           return error_mark_node;
15577         }
15578     }
15579   
15580   return groktypename (&type_specifier_seq, abstract_declarator,
15581                        is_template_arg);
15582 }
15583
15584 static tree cp_parser_type_id (cp_parser *parser)
15585 {
15586   return cp_parser_type_id_1 (parser, false, false);
15587 }
15588
15589 static tree cp_parser_template_type_arg (cp_parser *parser)
15590 {
15591   return cp_parser_type_id_1 (parser, true, false);
15592 }
15593
15594 static tree cp_parser_trailing_type_id (cp_parser *parser)
15595 {
15596   return cp_parser_type_id_1 (parser, false, true);
15597 }
15598
15599 /* Parse a type-specifier-seq.
15600
15601    type-specifier-seq:
15602      type-specifier type-specifier-seq [opt]
15603
15604    GNU extension:
15605
15606    type-specifier-seq:
15607      attributes type-specifier-seq [opt]
15608
15609    If IS_DECLARATION is true, we are at the start of a "condition" or
15610    exception-declaration, so we might be followed by a declarator-id.
15611
15612    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15613    i.e. we've just seen "->".
15614
15615    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15616
15617 static void
15618 cp_parser_type_specifier_seq (cp_parser* parser,
15619                               bool is_declaration,
15620                               bool is_trailing_return,
15621                               cp_decl_specifier_seq *type_specifier_seq)
15622 {
15623   bool seen_type_specifier = false;
15624   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15625   cp_token *start_token = NULL;
15626
15627   /* Clear the TYPE_SPECIFIER_SEQ.  */
15628   clear_decl_specs (type_specifier_seq);
15629
15630   /* In the context of a trailing return type, enum E { } is an
15631      elaborated-type-specifier followed by a function-body, not an
15632      enum-specifier.  */
15633   if (is_trailing_return)
15634     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15635
15636   /* Parse the type-specifiers and attributes.  */
15637   while (true)
15638     {
15639       tree type_specifier;
15640       bool is_cv_qualifier;
15641
15642       /* Check for attributes first.  */
15643       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15644         {
15645           type_specifier_seq->attributes =
15646             chainon (type_specifier_seq->attributes,
15647                      cp_parser_attributes_opt (parser));
15648           continue;
15649         }
15650
15651       /* record the token of the beginning of the type specifier seq,
15652          for error reporting purposes*/
15653      if (!start_token)
15654        start_token = cp_lexer_peek_token (parser->lexer);
15655
15656       /* Look for the type-specifier.  */
15657       type_specifier = cp_parser_type_specifier (parser,
15658                                                  flags,
15659                                                  type_specifier_seq,
15660                                                  /*is_declaration=*/false,
15661                                                  NULL,
15662                                                  &is_cv_qualifier);
15663       if (!type_specifier)
15664         {
15665           /* If the first type-specifier could not be found, this is not a
15666              type-specifier-seq at all.  */
15667           if (!seen_type_specifier)
15668             {
15669               cp_parser_error (parser, "expected type-specifier");
15670               type_specifier_seq->type = error_mark_node;
15671               return;
15672             }
15673           /* If subsequent type-specifiers could not be found, the
15674              type-specifier-seq is complete.  */
15675           break;
15676         }
15677
15678       seen_type_specifier = true;
15679       /* The standard says that a condition can be:
15680
15681             type-specifier-seq declarator = assignment-expression
15682
15683          However, given:
15684
15685            struct S {};
15686            if (int S = ...)
15687
15688          we should treat the "S" as a declarator, not as a
15689          type-specifier.  The standard doesn't say that explicitly for
15690          type-specifier-seq, but it does say that for
15691          decl-specifier-seq in an ordinary declaration.  Perhaps it
15692          would be clearer just to allow a decl-specifier-seq here, and
15693          then add a semantic restriction that if any decl-specifiers
15694          that are not type-specifiers appear, the program is invalid.  */
15695       if (is_declaration && !is_cv_qualifier)
15696         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15697     }
15698
15699   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15700 }
15701
15702 /* Parse a parameter-declaration-clause.
15703
15704    parameter-declaration-clause:
15705      parameter-declaration-list [opt] ... [opt]
15706      parameter-declaration-list , ...
15707
15708    Returns a representation for the parameter declarations.  A return
15709    value of NULL indicates a parameter-declaration-clause consisting
15710    only of an ellipsis.  */
15711
15712 static tree
15713 cp_parser_parameter_declaration_clause (cp_parser* parser)
15714 {
15715   tree parameters;
15716   cp_token *token;
15717   bool ellipsis_p;
15718   bool is_error;
15719
15720   /* Peek at the next token.  */
15721   token = cp_lexer_peek_token (parser->lexer);
15722   /* Check for trivial parameter-declaration-clauses.  */
15723   if (token->type == CPP_ELLIPSIS)
15724     {
15725       /* Consume the `...' token.  */
15726       cp_lexer_consume_token (parser->lexer);
15727       return NULL_TREE;
15728     }
15729   else if (token->type == CPP_CLOSE_PAREN)
15730     /* There are no parameters.  */
15731     {
15732 #ifndef NO_IMPLICIT_EXTERN_C
15733       if (in_system_header && current_class_type == NULL
15734           && current_lang_name == lang_name_c)
15735         return NULL_TREE;
15736       else
15737 #endif
15738         return void_list_node;
15739     }
15740   /* Check for `(void)', too, which is a special case.  */
15741   else if (token->keyword == RID_VOID
15742            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15743                == CPP_CLOSE_PAREN))
15744     {
15745       /* Consume the `void' token.  */
15746       cp_lexer_consume_token (parser->lexer);
15747       /* There are no parameters.  */
15748       return void_list_node;
15749     }
15750
15751   /* Parse the parameter-declaration-list.  */
15752   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15753   /* If a parse error occurred while parsing the
15754      parameter-declaration-list, then the entire
15755      parameter-declaration-clause is erroneous.  */
15756   if (is_error)
15757     return NULL;
15758
15759   /* Peek at the next token.  */
15760   token = cp_lexer_peek_token (parser->lexer);
15761   /* If it's a `,', the clause should terminate with an ellipsis.  */
15762   if (token->type == CPP_COMMA)
15763     {
15764       /* Consume the `,'.  */
15765       cp_lexer_consume_token (parser->lexer);
15766       /* Expect an ellipsis.  */
15767       ellipsis_p
15768         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15769     }
15770   /* It might also be `...' if the optional trailing `,' was
15771      omitted.  */
15772   else if (token->type == CPP_ELLIPSIS)
15773     {
15774       /* Consume the `...' token.  */
15775       cp_lexer_consume_token (parser->lexer);
15776       /* And remember that we saw it.  */
15777       ellipsis_p = true;
15778     }
15779   else
15780     ellipsis_p = false;
15781
15782   /* Finish the parameter list.  */
15783   if (!ellipsis_p)
15784     parameters = chainon (parameters, void_list_node);
15785
15786   return parameters;
15787 }
15788
15789 /* Parse a parameter-declaration-list.
15790
15791    parameter-declaration-list:
15792      parameter-declaration
15793      parameter-declaration-list , parameter-declaration
15794
15795    Returns a representation of the parameter-declaration-list, as for
15796    cp_parser_parameter_declaration_clause.  However, the
15797    `void_list_node' is never appended to the list.  Upon return,
15798    *IS_ERROR will be true iff an error occurred.  */
15799
15800 static tree
15801 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15802 {
15803   tree parameters = NULL_TREE;
15804   tree *tail = &parameters; 
15805   bool saved_in_unbraced_linkage_specification_p;
15806   int index = 0;
15807
15808   /* Assume all will go well.  */
15809   *is_error = false;
15810   /* The special considerations that apply to a function within an
15811      unbraced linkage specifications do not apply to the parameters
15812      to the function.  */
15813   saved_in_unbraced_linkage_specification_p 
15814     = parser->in_unbraced_linkage_specification_p;
15815   parser->in_unbraced_linkage_specification_p = false;
15816
15817   /* Look for more parameters.  */
15818   while (true)
15819     {
15820       cp_parameter_declarator *parameter;
15821       tree decl = error_mark_node;
15822       bool parenthesized_p;
15823       /* Parse the parameter.  */
15824       parameter
15825         = cp_parser_parameter_declaration (parser,
15826                                            /*template_parm_p=*/false,
15827                                            &parenthesized_p);
15828
15829       /* We don't know yet if the enclosing context is deprecated, so wait
15830          and warn in grokparms if appropriate.  */
15831       deprecated_state = DEPRECATED_SUPPRESS;
15832
15833       if (parameter)
15834         decl = grokdeclarator (parameter->declarator,
15835                                &parameter->decl_specifiers,
15836                                PARM,
15837                                parameter->default_argument != NULL_TREE,
15838                                &parameter->decl_specifiers.attributes);
15839
15840       deprecated_state = DEPRECATED_NORMAL;
15841
15842       /* If a parse error occurred parsing the parameter declaration,
15843          then the entire parameter-declaration-list is erroneous.  */
15844       if (decl == error_mark_node)
15845         {
15846           *is_error = true;
15847           parameters = error_mark_node;
15848           break;
15849         }
15850
15851       if (parameter->decl_specifiers.attributes)
15852         cplus_decl_attributes (&decl,
15853                                parameter->decl_specifiers.attributes,
15854                                0);
15855       if (DECL_NAME (decl))
15856         decl = pushdecl (decl);
15857
15858       if (decl != error_mark_node)
15859         {
15860           retrofit_lang_decl (decl);
15861           DECL_PARM_INDEX (decl) = ++index;
15862         }
15863
15864       /* Add the new parameter to the list.  */
15865       *tail = build_tree_list (parameter->default_argument, decl);
15866       tail = &TREE_CHAIN (*tail);
15867
15868       /* Peek at the next token.  */
15869       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15870           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15871           /* These are for Objective-C++ */
15872           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15873           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15874         /* The parameter-declaration-list is complete.  */
15875         break;
15876       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15877         {
15878           cp_token *token;
15879
15880           /* Peek at the next token.  */
15881           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15882           /* If it's an ellipsis, then the list is complete.  */
15883           if (token->type == CPP_ELLIPSIS)
15884             break;
15885           /* Otherwise, there must be more parameters.  Consume the
15886              `,'.  */
15887           cp_lexer_consume_token (parser->lexer);
15888           /* When parsing something like:
15889
15890                 int i(float f, double d)
15891
15892              we can tell after seeing the declaration for "f" that we
15893              are not looking at an initialization of a variable "i",
15894              but rather at the declaration of a function "i".
15895
15896              Due to the fact that the parsing of template arguments
15897              (as specified to a template-id) requires backtracking we
15898              cannot use this technique when inside a template argument
15899              list.  */
15900           if (!parser->in_template_argument_list_p
15901               && !parser->in_type_id_in_expr_p
15902               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15903               /* However, a parameter-declaration of the form
15904                  "foat(f)" (which is a valid declaration of a
15905                  parameter "f") can also be interpreted as an
15906                  expression (the conversion of "f" to "float").  */
15907               && !parenthesized_p)
15908             cp_parser_commit_to_tentative_parse (parser);
15909         }
15910       else
15911         {
15912           cp_parser_error (parser, "expected %<,%> or %<...%>");
15913           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15914             cp_parser_skip_to_closing_parenthesis (parser,
15915                                                    /*recovering=*/true,
15916                                                    /*or_comma=*/false,
15917                                                    /*consume_paren=*/false);
15918           break;
15919         }
15920     }
15921
15922   parser->in_unbraced_linkage_specification_p
15923     = saved_in_unbraced_linkage_specification_p;
15924
15925   return parameters;
15926 }
15927
15928 /* Parse a parameter declaration.
15929
15930    parameter-declaration:
15931      decl-specifier-seq ... [opt] declarator
15932      decl-specifier-seq declarator = assignment-expression
15933      decl-specifier-seq ... [opt] abstract-declarator [opt]
15934      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15935
15936    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15937    declares a template parameter.  (In that case, a non-nested `>'
15938    token encountered during the parsing of the assignment-expression
15939    is not interpreted as a greater-than operator.)
15940
15941    Returns a representation of the parameter, or NULL if an error
15942    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15943    true iff the declarator is of the form "(p)".  */
15944
15945 static cp_parameter_declarator *
15946 cp_parser_parameter_declaration (cp_parser *parser,
15947                                  bool template_parm_p,
15948                                  bool *parenthesized_p)
15949 {
15950   int declares_class_or_enum;
15951   cp_decl_specifier_seq decl_specifiers;
15952   cp_declarator *declarator;
15953   tree default_argument;
15954   cp_token *token = NULL, *declarator_token_start = NULL;
15955   const char *saved_message;
15956
15957   /* In a template parameter, `>' is not an operator.
15958
15959      [temp.param]
15960
15961      When parsing a default template-argument for a non-type
15962      template-parameter, the first non-nested `>' is taken as the end
15963      of the template parameter-list rather than a greater-than
15964      operator.  */
15965
15966   /* Type definitions may not appear in parameter types.  */
15967   saved_message = parser->type_definition_forbidden_message;
15968   parser->type_definition_forbidden_message
15969     = G_("types may not be defined in parameter types");
15970
15971   /* Parse the declaration-specifiers.  */
15972   cp_parser_decl_specifier_seq (parser,
15973                                 CP_PARSER_FLAGS_NONE,
15974                                 &decl_specifiers,
15975                                 &declares_class_or_enum);
15976
15977   /* Complain about missing 'typename' or other invalid type names.  */
15978   if (!decl_specifiers.any_type_specifiers_p)
15979     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15980
15981   /* If an error occurred, there's no reason to attempt to parse the
15982      rest of the declaration.  */
15983   if (cp_parser_error_occurred (parser))
15984     {
15985       parser->type_definition_forbidden_message = saved_message;
15986       return NULL;
15987     }
15988
15989   /* Peek at the next token.  */
15990   token = cp_lexer_peek_token (parser->lexer);
15991
15992   /* If the next token is a `)', `,', `=', `>', or `...', then there
15993      is no declarator. However, when variadic templates are enabled,
15994      there may be a declarator following `...'.  */
15995   if (token->type == CPP_CLOSE_PAREN
15996       || token->type == CPP_COMMA
15997       || token->type == CPP_EQ
15998       || token->type == CPP_GREATER)
15999     {
16000       declarator = NULL;
16001       if (parenthesized_p)
16002         *parenthesized_p = false;
16003     }
16004   /* Otherwise, there should be a declarator.  */
16005   else
16006     {
16007       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16008       parser->default_arg_ok_p = false;
16009
16010       /* After seeing a decl-specifier-seq, if the next token is not a
16011          "(", there is no possibility that the code is a valid
16012          expression.  Therefore, if parsing tentatively, we commit at
16013          this point.  */
16014       if (!parser->in_template_argument_list_p
16015           /* In an expression context, having seen:
16016
16017                (int((char ...
16018
16019              we cannot be sure whether we are looking at a
16020              function-type (taking a "char" as a parameter) or a cast
16021              of some object of type "char" to "int".  */
16022           && !parser->in_type_id_in_expr_p
16023           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16024           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16025         cp_parser_commit_to_tentative_parse (parser);
16026       /* Parse the declarator.  */
16027       declarator_token_start = token;
16028       declarator = cp_parser_declarator (parser,
16029                                          CP_PARSER_DECLARATOR_EITHER,
16030                                          /*ctor_dtor_or_conv_p=*/NULL,
16031                                          parenthesized_p,
16032                                          /*member_p=*/false);
16033       parser->default_arg_ok_p = saved_default_arg_ok_p;
16034       /* After the declarator, allow more attributes.  */
16035       decl_specifiers.attributes
16036         = chainon (decl_specifiers.attributes,
16037                    cp_parser_attributes_opt (parser));
16038     }
16039
16040   /* If the next token is an ellipsis, and we have not seen a
16041      declarator name, and the type of the declarator contains parameter
16042      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16043      a parameter pack expansion expression. Otherwise, leave the
16044      ellipsis for a C-style variadic function. */
16045   token = cp_lexer_peek_token (parser->lexer);
16046   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16047     {
16048       tree type = decl_specifiers.type;
16049
16050       if (type && DECL_P (type))
16051         type = TREE_TYPE (type);
16052
16053       if (type
16054           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16055           && declarator_can_be_parameter_pack (declarator)
16056           && (!declarator || !declarator->parameter_pack_p)
16057           && uses_parameter_packs (type))
16058         {
16059           /* Consume the `...'. */
16060           cp_lexer_consume_token (parser->lexer);
16061           maybe_warn_variadic_templates ();
16062           
16063           /* Build a pack expansion type */
16064           if (declarator)
16065             declarator->parameter_pack_p = true;
16066           else
16067             decl_specifiers.type = make_pack_expansion (type);
16068         }
16069     }
16070
16071   /* The restriction on defining new types applies only to the type
16072      of the parameter, not to the default argument.  */
16073   parser->type_definition_forbidden_message = saved_message;
16074
16075   /* If the next token is `=', then process a default argument.  */
16076   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16077     {
16078       /* Consume the `='.  */
16079       cp_lexer_consume_token (parser->lexer);
16080
16081       /* If we are defining a class, then the tokens that make up the
16082          default argument must be saved and processed later.  */
16083       if (!template_parm_p && at_class_scope_p ()
16084           && TYPE_BEING_DEFINED (current_class_type)
16085           && !LAMBDA_TYPE_P (current_class_type))
16086         {
16087           unsigned depth = 0;
16088           int maybe_template_id = 0;
16089           cp_token *first_token;
16090           cp_token *token;
16091
16092           /* Add tokens until we have processed the entire default
16093              argument.  We add the range [first_token, token).  */
16094           first_token = cp_lexer_peek_token (parser->lexer);
16095           while (true)
16096             {
16097               bool done = false;
16098
16099               /* Peek at the next token.  */
16100               token = cp_lexer_peek_token (parser->lexer);
16101               /* What we do depends on what token we have.  */
16102               switch (token->type)
16103                 {
16104                   /* In valid code, a default argument must be
16105                      immediately followed by a `,' `)', or `...'.  */
16106                 case CPP_COMMA:
16107                   if (depth == 0 && maybe_template_id)
16108                     {
16109                       /* If we've seen a '<', we might be in a
16110                          template-argument-list.  Until Core issue 325 is
16111                          resolved, we don't know how this situation ought
16112                          to be handled, so try to DTRT.  We check whether
16113                          what comes after the comma is a valid parameter
16114                          declaration list.  If it is, then the comma ends
16115                          the default argument; otherwise the default
16116                          argument continues.  */
16117                       bool error = false;
16118                       tree t;
16119
16120                       /* Set ITALP so cp_parser_parameter_declaration_list
16121                          doesn't decide to commit to this parse.  */
16122                       bool saved_italp = parser->in_template_argument_list_p;
16123                       parser->in_template_argument_list_p = true;
16124
16125                       cp_parser_parse_tentatively (parser);
16126                       cp_lexer_consume_token (parser->lexer);
16127                       begin_scope (sk_function_parms, NULL_TREE);
16128                       cp_parser_parameter_declaration_list (parser, &error);
16129                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16130                         pop_binding (DECL_NAME (t), t);
16131                       leave_scope ();
16132                       if (!cp_parser_error_occurred (parser) && !error)
16133                         done = true;
16134                       cp_parser_abort_tentative_parse (parser);
16135
16136                       parser->in_template_argument_list_p = saved_italp;
16137                       break;
16138                     }
16139                 case CPP_CLOSE_PAREN:
16140                 case CPP_ELLIPSIS:
16141                   /* If we run into a non-nested `;', `}', or `]',
16142                      then the code is invalid -- but the default
16143                      argument is certainly over.  */
16144                 case CPP_SEMICOLON:
16145                 case CPP_CLOSE_BRACE:
16146                 case CPP_CLOSE_SQUARE:
16147                   if (depth == 0)
16148                     done = true;
16149                   /* Update DEPTH, if necessary.  */
16150                   else if (token->type == CPP_CLOSE_PAREN
16151                            || token->type == CPP_CLOSE_BRACE
16152                            || token->type == CPP_CLOSE_SQUARE)
16153                     --depth;
16154                   break;
16155
16156                 case CPP_OPEN_PAREN:
16157                 case CPP_OPEN_SQUARE:
16158                 case CPP_OPEN_BRACE:
16159                   ++depth;
16160                   break;
16161
16162                 case CPP_LESS:
16163                   if (depth == 0)
16164                     /* This might be the comparison operator, or it might
16165                        start a template argument list.  */
16166                     ++maybe_template_id;
16167                   break;
16168
16169                 case CPP_RSHIFT:
16170                   if (cxx_dialect == cxx98)
16171                     break;
16172                   /* Fall through for C++0x, which treats the `>>'
16173                      operator like two `>' tokens in certain
16174                      cases.  */
16175
16176                 case CPP_GREATER:
16177                   if (depth == 0)
16178                     {
16179                       /* This might be an operator, or it might close a
16180                          template argument list.  But if a previous '<'
16181                          started a template argument list, this will have
16182                          closed it, so we can't be in one anymore.  */
16183                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16184                       if (maybe_template_id < 0)
16185                         maybe_template_id = 0;
16186                     }
16187                   break;
16188
16189                   /* If we run out of tokens, issue an error message.  */
16190                 case CPP_EOF:
16191                 case CPP_PRAGMA_EOL:
16192                   error_at (token->location, "file ends in default argument");
16193                   done = true;
16194                   break;
16195
16196                 case CPP_NAME:
16197                 case CPP_SCOPE:
16198                   /* In these cases, we should look for template-ids.
16199                      For example, if the default argument is
16200                      `X<int, double>()', we need to do name lookup to
16201                      figure out whether or not `X' is a template; if
16202                      so, the `,' does not end the default argument.
16203
16204                      That is not yet done.  */
16205                   break;
16206
16207                 default:
16208                   break;
16209                 }
16210
16211               /* If we've reached the end, stop.  */
16212               if (done)
16213                 break;
16214
16215               /* Add the token to the token block.  */
16216               token = cp_lexer_consume_token (parser->lexer);
16217             }
16218
16219           /* Create a DEFAULT_ARG to represent the unparsed default
16220              argument.  */
16221           default_argument = make_node (DEFAULT_ARG);
16222           DEFARG_TOKENS (default_argument)
16223             = cp_token_cache_new (first_token, token);
16224           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16225         }
16226       /* Outside of a class definition, we can just parse the
16227          assignment-expression.  */
16228       else
16229         {
16230           token = cp_lexer_peek_token (parser->lexer);
16231           default_argument 
16232             = cp_parser_default_argument (parser, template_parm_p);
16233         }
16234
16235       if (!parser->default_arg_ok_p)
16236         {
16237           if (flag_permissive)
16238             warning (0, "deprecated use of default argument for parameter of non-function");
16239           else
16240             {
16241               error_at (token->location,
16242                         "default arguments are only "
16243                         "permitted for function parameters");
16244               default_argument = NULL_TREE;
16245             }
16246         }
16247       else if ((declarator && declarator->parameter_pack_p)
16248                || (decl_specifiers.type
16249                    && PACK_EXPANSION_P (decl_specifiers.type)))
16250         {
16251           /* Find the name of the parameter pack.  */     
16252           cp_declarator *id_declarator = declarator;
16253           while (id_declarator && id_declarator->kind != cdk_id)
16254             id_declarator = id_declarator->declarator;
16255           
16256           if (id_declarator && id_declarator->kind == cdk_id)
16257             error_at (declarator_token_start->location,
16258                       template_parm_p 
16259                       ? "template parameter pack %qD"
16260                       " cannot have a default argument"
16261                       : "parameter pack %qD cannot have a default argument",
16262                       id_declarator->u.id.unqualified_name);
16263           else
16264             error_at (declarator_token_start->location,
16265                       template_parm_p 
16266                       ? "template parameter pack cannot have a default argument"
16267                       : "parameter pack cannot have a default argument");
16268           
16269           default_argument = NULL_TREE;
16270         }
16271     }
16272   else
16273     default_argument = NULL_TREE;
16274
16275   return make_parameter_declarator (&decl_specifiers,
16276                                     declarator,
16277                                     default_argument);
16278 }
16279
16280 /* Parse a default argument and return it.
16281
16282    TEMPLATE_PARM_P is true if this is a default argument for a
16283    non-type template parameter.  */
16284 static tree
16285 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16286 {
16287   tree default_argument = NULL_TREE;
16288   bool saved_greater_than_is_operator_p;
16289   bool saved_local_variables_forbidden_p;
16290
16291   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16292      set correctly.  */
16293   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16294   parser->greater_than_is_operator_p = !template_parm_p;
16295   /* Local variable names (and the `this' keyword) may not
16296      appear in a default argument.  */
16297   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16298   parser->local_variables_forbidden_p = true;
16299   /* Parse the assignment-expression.  */
16300   if (template_parm_p)
16301     push_deferring_access_checks (dk_no_deferred);
16302   default_argument
16303     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16304   if (template_parm_p)
16305     pop_deferring_access_checks ();
16306   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16307   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16308
16309   return default_argument;
16310 }
16311
16312 /* Parse a function-body.
16313
16314    function-body:
16315      compound_statement  */
16316
16317 static void
16318 cp_parser_function_body (cp_parser *parser)
16319 {
16320   cp_parser_compound_statement (parser, NULL, false);
16321 }
16322
16323 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16324    true if a ctor-initializer was present.  */
16325
16326 static bool
16327 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16328 {
16329   tree body, list;
16330   bool ctor_initializer_p;
16331   const bool check_body_p =
16332      DECL_CONSTRUCTOR_P (current_function_decl)
16333      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16334   tree last = NULL;
16335
16336   /* Begin the function body.  */
16337   body = begin_function_body ();
16338   /* Parse the optional ctor-initializer.  */
16339   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16340
16341   /* If we're parsing a constexpr constructor definition, we need
16342      to check that the constructor body is indeed empty.  However,
16343      before we get to cp_parser_function_body lot of junk has been
16344      generated, so we can't just check that we have an empty block.
16345      Rather we take a snapshot of the outermost block, and check whether
16346      cp_parser_function_body changed its state.  */
16347   if (check_body_p)
16348     {
16349       list = body;
16350       if (TREE_CODE (list) == BIND_EXPR)
16351         list = BIND_EXPR_BODY (list);
16352       if (TREE_CODE (list) == STATEMENT_LIST
16353           && STATEMENT_LIST_TAIL (list) != NULL)
16354         last = STATEMENT_LIST_TAIL (list)->stmt;
16355     }
16356   /* Parse the function-body.  */
16357   cp_parser_function_body (parser);
16358   if (check_body_p)
16359     check_constexpr_ctor_body (last, list);
16360   /* Finish the function body.  */
16361   finish_function_body (body);
16362
16363   return ctor_initializer_p;
16364 }
16365
16366 /* Parse an initializer.
16367
16368    initializer:
16369      = initializer-clause
16370      ( expression-list )
16371
16372    Returns an expression representing the initializer.  If no
16373    initializer is present, NULL_TREE is returned.
16374
16375    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16376    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16377    set to TRUE if there is no initializer present.  If there is an
16378    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16379    is set to true; otherwise it is set to false.  */
16380
16381 static tree
16382 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16383                        bool* non_constant_p)
16384 {
16385   cp_token *token;
16386   tree init;
16387
16388   /* Peek at the next token.  */
16389   token = cp_lexer_peek_token (parser->lexer);
16390
16391   /* Let our caller know whether or not this initializer was
16392      parenthesized.  */
16393   *is_direct_init = (token->type != CPP_EQ);
16394   /* Assume that the initializer is constant.  */
16395   *non_constant_p = false;
16396
16397   if (token->type == CPP_EQ)
16398     {
16399       /* Consume the `='.  */
16400       cp_lexer_consume_token (parser->lexer);
16401       /* Parse the initializer-clause.  */
16402       init = cp_parser_initializer_clause (parser, non_constant_p);
16403     }
16404   else if (token->type == CPP_OPEN_PAREN)
16405     {
16406       VEC(tree,gc) *vec;
16407       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16408                                                      /*cast_p=*/false,
16409                                                      /*allow_expansion_p=*/true,
16410                                                      non_constant_p);
16411       if (vec == NULL)
16412         return error_mark_node;
16413       init = build_tree_list_vec (vec);
16414       release_tree_vector (vec);
16415     }
16416   else if (token->type == CPP_OPEN_BRACE)
16417     {
16418       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16419       init = cp_parser_braced_list (parser, non_constant_p);
16420       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16421     }
16422   else
16423     {
16424       /* Anything else is an error.  */
16425       cp_parser_error (parser, "expected initializer");
16426       init = error_mark_node;
16427     }
16428
16429   return init;
16430 }
16431
16432 /* Parse an initializer-clause.
16433
16434    initializer-clause:
16435      assignment-expression
16436      braced-init-list
16437
16438    Returns an expression representing the initializer.
16439
16440    If the `assignment-expression' production is used the value
16441    returned is simply a representation for the expression.
16442
16443    Otherwise, calls cp_parser_braced_list.  */
16444
16445 static tree
16446 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16447 {
16448   tree initializer;
16449
16450   /* Assume the expression is constant.  */
16451   *non_constant_p = false;
16452
16453   /* If it is not a `{', then we are looking at an
16454      assignment-expression.  */
16455   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16456     {
16457       initializer
16458         = cp_parser_constant_expression (parser,
16459                                         /*allow_non_constant_p=*/true,
16460                                         non_constant_p);
16461       if (!*non_constant_p)
16462         {
16463           /* We only want to fold if this is really a constant
16464              expression.  FIXME Actually, we don't want to fold here, but in
16465              cp_finish_decl.  */
16466           tree folded = fold_non_dependent_expr (initializer);
16467           folded = maybe_constant_value (folded);
16468           if (TREE_CONSTANT (folded))
16469             initializer = folded;
16470         }
16471     }
16472   else
16473     initializer = cp_parser_braced_list (parser, non_constant_p);
16474
16475   return initializer;
16476 }
16477
16478 /* Parse a brace-enclosed initializer list.
16479
16480    braced-init-list:
16481      { initializer-list , [opt] }
16482      { }
16483
16484    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16485    the elements of the initializer-list (or NULL, if the last
16486    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16487    NULL_TREE.  There is no way to detect whether or not the optional
16488    trailing `,' was provided.  NON_CONSTANT_P is as for
16489    cp_parser_initializer.  */     
16490
16491 static tree
16492 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16493 {
16494   tree initializer;
16495
16496   /* Consume the `{' token.  */
16497   cp_lexer_consume_token (parser->lexer);
16498   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16499   initializer = make_node (CONSTRUCTOR);
16500   /* If it's not a `}', then there is a non-trivial initializer.  */
16501   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16502     {
16503       /* Parse the initializer list.  */
16504       CONSTRUCTOR_ELTS (initializer)
16505         = cp_parser_initializer_list (parser, non_constant_p);
16506       /* A trailing `,' token is allowed.  */
16507       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16508         cp_lexer_consume_token (parser->lexer);
16509     }
16510   /* Now, there should be a trailing `}'.  */
16511   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16512   TREE_TYPE (initializer) = init_list_type_node;
16513   return initializer;
16514 }
16515
16516 /* Parse an initializer-list.
16517
16518    initializer-list:
16519      initializer-clause ... [opt]
16520      initializer-list , initializer-clause ... [opt]
16521
16522    GNU Extension:
16523
16524    initializer-list:
16525      identifier : initializer-clause
16526      initializer-list, identifier : initializer-clause
16527
16528    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16529    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16530    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16531    as for cp_parser_initializer.  */
16532
16533 static VEC(constructor_elt,gc) *
16534 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16535 {
16536   VEC(constructor_elt,gc) *v = NULL;
16537
16538   /* Assume all of the expressions are constant.  */
16539   *non_constant_p = false;
16540
16541   /* Parse the rest of the list.  */
16542   while (true)
16543     {
16544       cp_token *token;
16545       tree identifier;
16546       tree initializer;
16547       bool clause_non_constant_p;
16548
16549       /* If the next token is an identifier and the following one is a
16550          colon, we are looking at the GNU designated-initializer
16551          syntax.  */
16552       if (cp_parser_allow_gnu_extensions_p (parser)
16553           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16554           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16555         {
16556           /* Warn the user that they are using an extension.  */
16557           pedwarn (input_location, OPT_pedantic, 
16558                    "ISO C++ does not allow designated initializers");
16559           /* Consume the identifier.  */
16560           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16561           /* Consume the `:'.  */
16562           cp_lexer_consume_token (parser->lexer);
16563         }
16564       else
16565         identifier = NULL_TREE;
16566
16567       /* Parse the initializer.  */
16568       initializer = cp_parser_initializer_clause (parser,
16569                                                   &clause_non_constant_p);
16570       /* If any clause is non-constant, so is the entire initializer.  */
16571       if (clause_non_constant_p)
16572         *non_constant_p = true;
16573
16574       /* If we have an ellipsis, this is an initializer pack
16575          expansion.  */
16576       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16577         {
16578           /* Consume the `...'.  */
16579           cp_lexer_consume_token (parser->lexer);
16580
16581           /* Turn the initializer into an initializer expansion.  */
16582           initializer = make_pack_expansion (initializer);
16583         }
16584
16585       /* Add it to the vector.  */
16586       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16587
16588       /* If the next token is not a comma, we have reached the end of
16589          the list.  */
16590       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16591         break;
16592
16593       /* Peek at the next token.  */
16594       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16595       /* If the next token is a `}', then we're still done.  An
16596          initializer-clause can have a trailing `,' after the
16597          initializer-list and before the closing `}'.  */
16598       if (token->type == CPP_CLOSE_BRACE)
16599         break;
16600
16601       /* Consume the `,' token.  */
16602       cp_lexer_consume_token (parser->lexer);
16603     }
16604
16605   return v;
16606 }
16607
16608 /* Classes [gram.class] */
16609
16610 /* Parse a class-name.
16611
16612    class-name:
16613      identifier
16614      template-id
16615
16616    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16617    to indicate that names looked up in dependent types should be
16618    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16619    keyword has been used to indicate that the name that appears next
16620    is a template.  TAG_TYPE indicates the explicit tag given before
16621    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16622    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16623    is the class being defined in a class-head.
16624
16625    Returns the TYPE_DECL representing the class.  */
16626
16627 static tree
16628 cp_parser_class_name (cp_parser *parser,
16629                       bool typename_keyword_p,
16630                       bool template_keyword_p,
16631                       enum tag_types tag_type,
16632                       bool check_dependency_p,
16633                       bool class_head_p,
16634                       bool is_declaration)
16635 {
16636   tree decl;
16637   tree scope;
16638   bool typename_p;
16639   cp_token *token;
16640   tree identifier = NULL_TREE;
16641
16642   /* All class-names start with an identifier.  */
16643   token = cp_lexer_peek_token (parser->lexer);
16644   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16645     {
16646       cp_parser_error (parser, "expected class-name");
16647       return error_mark_node;
16648     }
16649
16650   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16651      to a template-id, so we save it here.  */
16652   scope = parser->scope;
16653   if (scope == error_mark_node)
16654     return error_mark_node;
16655
16656   /* Any name names a type if we're following the `typename' keyword
16657      in a qualified name where the enclosing scope is type-dependent.  */
16658   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16659                 && dependent_type_p (scope));
16660   /* Handle the common case (an identifier, but not a template-id)
16661      efficiently.  */
16662   if (token->type == CPP_NAME
16663       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16664     {
16665       cp_token *identifier_token;
16666       bool ambiguous_p;
16667
16668       /* Look for the identifier.  */
16669       identifier_token = cp_lexer_peek_token (parser->lexer);
16670       ambiguous_p = identifier_token->ambiguous_p;
16671       identifier = cp_parser_identifier (parser);
16672       /* If the next token isn't an identifier, we are certainly not
16673          looking at a class-name.  */
16674       if (identifier == error_mark_node)
16675         decl = error_mark_node;
16676       /* If we know this is a type-name, there's no need to look it
16677          up.  */
16678       else if (typename_p)
16679         decl = identifier;
16680       else
16681         {
16682           tree ambiguous_decls;
16683           /* If we already know that this lookup is ambiguous, then
16684              we've already issued an error message; there's no reason
16685              to check again.  */
16686           if (ambiguous_p)
16687             {
16688               cp_parser_simulate_error (parser);
16689               return error_mark_node;
16690             }
16691           /* If the next token is a `::', then the name must be a type
16692              name.
16693
16694              [basic.lookup.qual]
16695
16696              During the lookup for a name preceding the :: scope
16697              resolution operator, object, function, and enumerator
16698              names are ignored.  */
16699           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16700             tag_type = typename_type;
16701           /* Look up the name.  */
16702           decl = cp_parser_lookup_name (parser, identifier,
16703                                         tag_type,
16704                                         /*is_template=*/false,
16705                                         /*is_namespace=*/false,
16706                                         check_dependency_p,
16707                                         &ambiguous_decls,
16708                                         identifier_token->location);
16709           if (ambiguous_decls)
16710             {
16711               if (cp_parser_parsing_tentatively (parser))
16712                 cp_parser_simulate_error (parser);
16713               return error_mark_node;
16714             }
16715         }
16716     }
16717   else
16718     {
16719       /* Try a template-id.  */
16720       decl = cp_parser_template_id (parser, template_keyword_p,
16721                                     check_dependency_p,
16722                                     is_declaration);
16723       if (decl == error_mark_node)
16724         return error_mark_node;
16725     }
16726
16727   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16728
16729   /* If this is a typename, create a TYPENAME_TYPE.  */
16730   if (typename_p && decl != error_mark_node)
16731     {
16732       decl = make_typename_type (scope, decl, typename_type,
16733                                  /*complain=*/tf_error);
16734       if (decl != error_mark_node)
16735         decl = TYPE_NAME (decl);
16736     }
16737
16738   /* Check to see that it is really the name of a class.  */
16739   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16740       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16741       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16742     /* Situations like this:
16743
16744          template <typename T> struct A {
16745            typename T::template X<int>::I i;
16746          };
16747
16748        are problematic.  Is `T::template X<int>' a class-name?  The
16749        standard does not seem to be definitive, but there is no other
16750        valid interpretation of the following `::'.  Therefore, those
16751        names are considered class-names.  */
16752     {
16753       decl = make_typename_type (scope, decl, tag_type, tf_error);
16754       if (decl != error_mark_node)
16755         decl = TYPE_NAME (decl);
16756     }
16757   else if (TREE_CODE (decl) != TYPE_DECL
16758            || TREE_TYPE (decl) == error_mark_node
16759            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16760            /* In Objective-C 2.0, a classname followed by '.' starts a
16761               dot-syntax expression, and it's not a type-name.  */
16762            || (c_dialect_objc ()
16763                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16764                && objc_is_class_name (decl)))
16765     decl = error_mark_node;
16766
16767   if (decl == error_mark_node)
16768     cp_parser_error (parser, "expected class-name");
16769   else if (identifier && !parser->scope)
16770     maybe_note_name_used_in_class (identifier, decl);
16771
16772   return decl;
16773 }
16774
16775 /* Parse a class-specifier.
16776
16777    class-specifier:
16778      class-head { member-specification [opt] }
16779
16780    Returns the TREE_TYPE representing the class.  */
16781
16782 static tree
16783 cp_parser_class_specifier (cp_parser* parser)
16784 {
16785   tree type;
16786   tree attributes = NULL_TREE;
16787   bool nested_name_specifier_p;
16788   unsigned saved_num_template_parameter_lists;
16789   bool saved_in_function_body;
16790   bool saved_in_unbraced_linkage_specification_p;
16791   tree old_scope = NULL_TREE;
16792   tree scope = NULL_TREE;
16793   tree bases;
16794
16795   push_deferring_access_checks (dk_no_deferred);
16796
16797   /* Parse the class-head.  */
16798   type = cp_parser_class_head (parser,
16799                                &nested_name_specifier_p,
16800                                &attributes,
16801                                &bases);
16802   /* If the class-head was a semantic disaster, skip the entire body
16803      of the class.  */
16804   if (!type)
16805     {
16806       cp_parser_skip_to_end_of_block_or_statement (parser);
16807       pop_deferring_access_checks ();
16808       return error_mark_node;
16809     }
16810
16811   /* Look for the `{'.  */
16812   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16813     {
16814       pop_deferring_access_checks ();
16815       return error_mark_node;
16816     }
16817
16818   /* Process the base classes. If they're invalid, skip the 
16819      entire class body.  */
16820   if (!xref_basetypes (type, bases))
16821     {
16822       /* Consuming the closing brace yields better error messages
16823          later on.  */
16824       if (cp_parser_skip_to_closing_brace (parser))
16825         cp_lexer_consume_token (parser->lexer);
16826       pop_deferring_access_checks ();
16827       return error_mark_node;
16828     }
16829
16830   /* Issue an error message if type-definitions are forbidden here.  */
16831   cp_parser_check_type_definition (parser);
16832   /* Remember that we are defining one more class.  */
16833   ++parser->num_classes_being_defined;
16834   /* Inside the class, surrounding template-parameter-lists do not
16835      apply.  */
16836   saved_num_template_parameter_lists
16837     = parser->num_template_parameter_lists;
16838   parser->num_template_parameter_lists = 0;
16839   /* We are not in a function body.  */
16840   saved_in_function_body = parser->in_function_body;
16841   parser->in_function_body = false;
16842   /* We are not immediately inside an extern "lang" block.  */
16843   saved_in_unbraced_linkage_specification_p
16844     = parser->in_unbraced_linkage_specification_p;
16845   parser->in_unbraced_linkage_specification_p = false;
16846
16847   /* Start the class.  */
16848   if (nested_name_specifier_p)
16849     {
16850       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16851       old_scope = push_inner_scope (scope);
16852     }
16853   type = begin_class_definition (type, attributes);
16854
16855   if (type == error_mark_node)
16856     /* If the type is erroneous, skip the entire body of the class.  */
16857     cp_parser_skip_to_closing_brace (parser);
16858   else
16859     /* Parse the member-specification.  */
16860     cp_parser_member_specification_opt (parser);
16861
16862   /* Look for the trailing `}'.  */
16863   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16864   /* Look for trailing attributes to apply to this class.  */
16865   if (cp_parser_allow_gnu_extensions_p (parser))
16866     attributes = cp_parser_attributes_opt (parser);
16867   if (type != error_mark_node)
16868     type = finish_struct (type, attributes);
16869   if (nested_name_specifier_p)
16870     pop_inner_scope (old_scope, scope);
16871
16872   /* We've finished a type definition.  Check for the common syntax
16873      error of forgetting a semicolon after the definition.  We need to
16874      be careful, as we can't just check for not-a-semicolon and be done
16875      with it; the user might have typed:
16876
16877      class X { } c = ...;
16878      class X { } *p = ...;
16879
16880      and so forth.  Instead, enumerate all the possible tokens that
16881      might follow this production; if we don't see one of them, then
16882      complain and silently insert the semicolon.  */
16883   {
16884     cp_token *token = cp_lexer_peek_token (parser->lexer);
16885     bool want_semicolon = true;
16886
16887     switch (token->type)
16888       {
16889       case CPP_NAME:
16890       case CPP_SEMICOLON:
16891       case CPP_MULT:
16892       case CPP_AND:
16893       case CPP_OPEN_PAREN:
16894       case CPP_CLOSE_PAREN:
16895       case CPP_COMMA:
16896         want_semicolon = false;
16897         break;
16898
16899         /* While it's legal for type qualifiers and storage class
16900            specifiers to follow type definitions in the grammar, only
16901            compiler testsuites contain code like that.  Assume that if
16902            we see such code, then what we're really seeing is a case
16903            like:
16904
16905            class X { }
16906            const <type> var = ...;
16907
16908            or
16909
16910            class Y { }
16911            static <type> func (...) ...
16912
16913            i.e. the qualifier or specifier applies to the next
16914            declaration.  To do so, however, we need to look ahead one
16915            more token to see if *that* token is a type specifier.
16916
16917            This code could be improved to handle:
16918
16919            class Z { }
16920            static const <type> var = ...;  */
16921       case CPP_KEYWORD:
16922         if (keyword_is_storage_class_specifier (token->keyword)
16923             || keyword_is_type_qualifier (token->keyword))
16924           {
16925             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16926
16927             if (lookahead->type == CPP_KEYWORD
16928                 && !keyword_begins_type_specifier (lookahead->keyword))
16929               want_semicolon = false;
16930             else if (lookahead->type == CPP_NAME)
16931               /* Handling user-defined types here would be nice, but
16932                  very tricky.  */
16933               want_semicolon = false;
16934           }
16935         break;
16936       default:
16937         break;
16938       }
16939
16940     if (want_semicolon)
16941       {
16942         cp_token_position prev
16943           = cp_lexer_previous_token_position (parser->lexer);
16944         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16945         location_t loc = prev_token->location;
16946
16947         if (CLASSTYPE_DECLARED_CLASS (type))
16948           error_at (loc, "expected %<;%> after class definition");
16949         else if (TREE_CODE (type) == RECORD_TYPE)
16950           error_at (loc, "expected %<;%> after struct definition");
16951         else if (TREE_CODE (type) == UNION_TYPE)
16952           error_at (loc, "expected %<;%> after union definition");
16953         else
16954           gcc_unreachable ();
16955
16956         /* Unget one token and smash it to look as though we encountered
16957            a semicolon in the input stream.  */
16958         cp_lexer_set_token_position (parser->lexer, prev);
16959         token = cp_lexer_peek_token (parser->lexer);
16960         token->type = CPP_SEMICOLON;
16961         token->keyword = RID_MAX;
16962       }
16963   }
16964
16965   /* If this class is not itself within the scope of another class,
16966      then we need to parse the bodies of all of the queued function
16967      definitions.  Note that the queued functions defined in a class
16968      are not always processed immediately following the
16969      class-specifier for that class.  Consider:
16970
16971        struct A {
16972          struct B { void f() { sizeof (A); } };
16973        };
16974
16975      If `f' were processed before the processing of `A' were
16976      completed, there would be no way to compute the size of `A'.
16977      Note that the nesting we are interested in here is lexical --
16978      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16979      for:
16980
16981        struct A { struct B; };
16982        struct A::B { void f() { } };
16983
16984      there is no need to delay the parsing of `A::B::f'.  */
16985   if (--parser->num_classes_being_defined == 0)
16986     {
16987       tree fn;
16988       tree class_type = NULL_TREE;
16989       tree pushed_scope = NULL_TREE;
16990       unsigned ix;
16991       cp_default_arg_entry *e;
16992
16993       /* In a first pass, parse default arguments to the functions.
16994          Then, in a second pass, parse the bodies of the functions.
16995          This two-phased approach handles cases like:
16996
16997             struct S {
16998               void f() { g(); }
16999               void g(int i = 3);
17000             };
17001
17002          */
17003       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17004                         ix, e)
17005         {
17006           fn = e->decl;
17007           /* If there are default arguments that have not yet been processed,
17008              take care of them now.  */
17009           if (class_type != e->class_type)
17010             {
17011               if (pushed_scope)
17012                 pop_scope (pushed_scope);
17013               class_type = e->class_type;
17014               pushed_scope = push_scope (class_type);
17015             }
17016           /* Make sure that any template parameters are in scope.  */
17017           maybe_begin_member_template_processing (fn);
17018           /* Parse the default argument expressions.  */
17019           cp_parser_late_parsing_default_args (parser, fn);
17020           /* Remove any template parameters from the symbol table.  */
17021           maybe_end_member_template_processing ();
17022         }
17023       if (pushed_scope)
17024         pop_scope (pushed_scope);
17025       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17026       /* Now parse the body of the functions.  */
17027       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17028         cp_parser_late_parsing_for_member (parser, fn);
17029       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17030     }
17031
17032   /* Put back any saved access checks.  */
17033   pop_deferring_access_checks ();
17034
17035   /* Restore saved state.  */
17036   parser->in_function_body = saved_in_function_body;
17037   parser->num_template_parameter_lists
17038     = saved_num_template_parameter_lists;
17039   parser->in_unbraced_linkage_specification_p
17040     = saved_in_unbraced_linkage_specification_p;
17041
17042   return type;
17043 }
17044
17045 /* Parse a class-head.
17046
17047    class-head:
17048      class-key identifier [opt] base-clause [opt]
17049      class-key nested-name-specifier identifier base-clause [opt]
17050      class-key nested-name-specifier [opt] template-id
17051        base-clause [opt]
17052
17053    GNU Extensions:
17054      class-key attributes identifier [opt] base-clause [opt]
17055      class-key attributes nested-name-specifier identifier base-clause [opt]
17056      class-key attributes nested-name-specifier [opt] template-id
17057        base-clause [opt]
17058
17059    Upon return BASES is initialized to the list of base classes (or
17060    NULL, if there are none) in the same form returned by
17061    cp_parser_base_clause.
17062
17063    Returns the TYPE of the indicated class.  Sets
17064    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17065    involving a nested-name-specifier was used, and FALSE otherwise.
17066
17067    Returns error_mark_node if this is not a class-head.
17068
17069    Returns NULL_TREE if the class-head is syntactically valid, but
17070    semantically invalid in a way that means we should skip the entire
17071    body of the class.  */
17072
17073 static tree
17074 cp_parser_class_head (cp_parser* parser,
17075                       bool* nested_name_specifier_p,
17076                       tree *attributes_p,
17077                       tree *bases)
17078 {
17079   tree nested_name_specifier;
17080   enum tag_types class_key;
17081   tree id = NULL_TREE;
17082   tree type = NULL_TREE;
17083   tree attributes;
17084   bool template_id_p = false;
17085   bool qualified_p = false;
17086   bool invalid_nested_name_p = false;
17087   bool invalid_explicit_specialization_p = false;
17088   tree pushed_scope = NULL_TREE;
17089   unsigned num_templates;
17090   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17091   /* Assume no nested-name-specifier will be present.  */
17092   *nested_name_specifier_p = false;
17093   /* Assume no template parameter lists will be used in defining the
17094      type.  */
17095   num_templates = 0;
17096
17097   *bases = NULL_TREE;
17098
17099   /* Look for the class-key.  */
17100   class_key = cp_parser_class_key (parser);
17101   if (class_key == none_type)
17102     return error_mark_node;
17103
17104   /* Parse the attributes.  */
17105   attributes = cp_parser_attributes_opt (parser);
17106
17107   /* If the next token is `::', that is invalid -- but sometimes
17108      people do try to write:
17109
17110        struct ::S {};
17111
17112      Handle this gracefully by accepting the extra qualifier, and then
17113      issuing an error about it later if this really is a
17114      class-head.  If it turns out just to be an elaborated type
17115      specifier, remain silent.  */
17116   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17117     qualified_p = true;
17118
17119   push_deferring_access_checks (dk_no_check);
17120
17121   /* Determine the name of the class.  Begin by looking for an
17122      optional nested-name-specifier.  */
17123   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17124   nested_name_specifier
17125     = cp_parser_nested_name_specifier_opt (parser,
17126                                            /*typename_keyword_p=*/false,
17127                                            /*check_dependency_p=*/false,
17128                                            /*type_p=*/false,
17129                                            /*is_declaration=*/false);
17130   /* If there was a nested-name-specifier, then there *must* be an
17131      identifier.  */
17132   if (nested_name_specifier)
17133     {
17134       type_start_token = cp_lexer_peek_token (parser->lexer);
17135       /* Although the grammar says `identifier', it really means
17136          `class-name' or `template-name'.  You are only allowed to
17137          define a class that has already been declared with this
17138          syntax.
17139
17140          The proposed resolution for Core Issue 180 says that wherever
17141          you see `class T::X' you should treat `X' as a type-name.
17142
17143          It is OK to define an inaccessible class; for example:
17144
17145            class A { class B; };
17146            class A::B {};
17147
17148          We do not know if we will see a class-name, or a
17149          template-name.  We look for a class-name first, in case the
17150          class-name is a template-id; if we looked for the
17151          template-name first we would stop after the template-name.  */
17152       cp_parser_parse_tentatively (parser);
17153       type = cp_parser_class_name (parser,
17154                                    /*typename_keyword_p=*/false,
17155                                    /*template_keyword_p=*/false,
17156                                    class_type,
17157                                    /*check_dependency_p=*/false,
17158                                    /*class_head_p=*/true,
17159                                    /*is_declaration=*/false);
17160       /* If that didn't work, ignore the nested-name-specifier.  */
17161       if (!cp_parser_parse_definitely (parser))
17162         {
17163           invalid_nested_name_p = true;
17164           type_start_token = cp_lexer_peek_token (parser->lexer);
17165           id = cp_parser_identifier (parser);
17166           if (id == error_mark_node)
17167             id = NULL_TREE;
17168         }
17169       /* If we could not find a corresponding TYPE, treat this
17170          declaration like an unqualified declaration.  */
17171       if (type == error_mark_node)
17172         nested_name_specifier = NULL_TREE;
17173       /* Otherwise, count the number of templates used in TYPE and its
17174          containing scopes.  */
17175       else
17176         {
17177           tree scope;
17178
17179           for (scope = TREE_TYPE (type);
17180                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17181                scope = (TYPE_P (scope)
17182                         ? TYPE_CONTEXT (scope)
17183                         : DECL_CONTEXT (scope)))
17184             if (TYPE_P (scope)
17185                 && CLASS_TYPE_P (scope)
17186                 && CLASSTYPE_TEMPLATE_INFO (scope)
17187                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17188                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17189               ++num_templates;
17190         }
17191     }
17192   /* Otherwise, the identifier is optional.  */
17193   else
17194     {
17195       /* We don't know whether what comes next is a template-id,
17196          an identifier, or nothing at all.  */
17197       cp_parser_parse_tentatively (parser);
17198       /* Check for a template-id.  */
17199       type_start_token = cp_lexer_peek_token (parser->lexer);
17200       id = cp_parser_template_id (parser,
17201                                   /*template_keyword_p=*/false,
17202                                   /*check_dependency_p=*/true,
17203                                   /*is_declaration=*/true);
17204       /* If that didn't work, it could still be an identifier.  */
17205       if (!cp_parser_parse_definitely (parser))
17206         {
17207           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17208             {
17209               type_start_token = cp_lexer_peek_token (parser->lexer);
17210               id = cp_parser_identifier (parser);
17211             }
17212           else
17213             id = NULL_TREE;
17214         }
17215       else
17216         {
17217           template_id_p = true;
17218           ++num_templates;
17219         }
17220     }
17221
17222   pop_deferring_access_checks ();
17223
17224   if (id)
17225     cp_parser_check_for_invalid_template_id (parser, id,
17226                                              type_start_token->location);
17227
17228   /* If it's not a `:' or a `{' then we can't really be looking at a
17229      class-head, since a class-head only appears as part of a
17230      class-specifier.  We have to detect this situation before calling
17231      xref_tag, since that has irreversible side-effects.  */
17232   if (!cp_parser_next_token_starts_class_definition_p (parser))
17233     {
17234       cp_parser_error (parser, "expected %<{%> or %<:%>");
17235       return error_mark_node;
17236     }
17237
17238   /* At this point, we're going ahead with the class-specifier, even
17239      if some other problem occurs.  */
17240   cp_parser_commit_to_tentative_parse (parser);
17241   /* Issue the error about the overly-qualified name now.  */
17242   if (qualified_p)
17243     {
17244       cp_parser_error (parser,
17245                        "global qualification of class name is invalid");
17246       return error_mark_node;
17247     }
17248   else if (invalid_nested_name_p)
17249     {
17250       cp_parser_error (parser,
17251                        "qualified name does not name a class");
17252       return error_mark_node;
17253     }
17254   else if (nested_name_specifier)
17255     {
17256       tree scope;
17257
17258       /* Reject typedef-names in class heads.  */
17259       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17260         {
17261           error_at (type_start_token->location,
17262                     "invalid class name in declaration of %qD",
17263                     type);
17264           type = NULL_TREE;
17265           goto done;
17266         }
17267
17268       /* Figure out in what scope the declaration is being placed.  */
17269       scope = current_scope ();
17270       /* If that scope does not contain the scope in which the
17271          class was originally declared, the program is invalid.  */
17272       if (scope && !is_ancestor (scope, nested_name_specifier))
17273         {
17274           if (at_namespace_scope_p ())
17275             error_at (type_start_token->location,
17276                       "declaration of %qD in namespace %qD which does not "
17277                       "enclose %qD",
17278                       type, scope, nested_name_specifier);
17279           else
17280             error_at (type_start_token->location,
17281                       "declaration of %qD in %qD which does not enclose %qD",
17282                       type, scope, nested_name_specifier);
17283           type = NULL_TREE;
17284           goto done;
17285         }
17286       /* [dcl.meaning]
17287
17288          A declarator-id shall not be qualified except for the
17289          definition of a ... nested class outside of its class
17290          ... [or] the definition or explicit instantiation of a
17291          class member of a namespace outside of its namespace.  */
17292       if (scope == nested_name_specifier)
17293         {
17294           permerror (nested_name_specifier_token_start->location,
17295                      "extra qualification not allowed");
17296           nested_name_specifier = NULL_TREE;
17297           num_templates = 0;
17298         }
17299     }
17300   /* An explicit-specialization must be preceded by "template <>".  If
17301      it is not, try to recover gracefully.  */
17302   if (at_namespace_scope_p ()
17303       && parser->num_template_parameter_lists == 0
17304       && template_id_p)
17305     {
17306       error_at (type_start_token->location,
17307                 "an explicit specialization must be preceded by %<template <>%>");
17308       invalid_explicit_specialization_p = true;
17309       /* Take the same action that would have been taken by
17310          cp_parser_explicit_specialization.  */
17311       ++parser->num_template_parameter_lists;
17312       begin_specialization ();
17313     }
17314   /* There must be no "return" statements between this point and the
17315      end of this function; set "type "to the correct return value and
17316      use "goto done;" to return.  */
17317   /* Make sure that the right number of template parameters were
17318      present.  */
17319   if (!cp_parser_check_template_parameters (parser, num_templates,
17320                                             type_start_token->location,
17321                                             /*declarator=*/NULL))
17322     {
17323       /* If something went wrong, there is no point in even trying to
17324          process the class-definition.  */
17325       type = NULL_TREE;
17326       goto done;
17327     }
17328
17329   /* Look up the type.  */
17330   if (template_id_p)
17331     {
17332       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17333           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17334               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17335         {
17336           error_at (type_start_token->location,
17337                     "function template %qD redeclared as a class template", id);
17338           type = error_mark_node;
17339         }
17340       else
17341         {
17342           type = TREE_TYPE (id);
17343           type = maybe_process_partial_specialization (type);
17344         }
17345       if (nested_name_specifier)
17346         pushed_scope = push_scope (nested_name_specifier);
17347     }
17348   else if (nested_name_specifier)
17349     {
17350       tree class_type;
17351
17352       /* Given:
17353
17354             template <typename T> struct S { struct T };
17355             template <typename T> struct S<T>::T { };
17356
17357          we will get a TYPENAME_TYPE when processing the definition of
17358          `S::T'.  We need to resolve it to the actual type before we
17359          try to define it.  */
17360       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17361         {
17362           class_type = resolve_typename_type (TREE_TYPE (type),
17363                                               /*only_current_p=*/false);
17364           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17365             type = TYPE_NAME (class_type);
17366           else
17367             {
17368               cp_parser_error (parser, "could not resolve typename type");
17369               type = error_mark_node;
17370             }
17371         }
17372
17373       if (maybe_process_partial_specialization (TREE_TYPE (type))
17374           == error_mark_node)
17375         {
17376           type = NULL_TREE;
17377           goto done;
17378         }
17379
17380       class_type = current_class_type;
17381       /* Enter the scope indicated by the nested-name-specifier.  */
17382       pushed_scope = push_scope (nested_name_specifier);
17383       /* Get the canonical version of this type.  */
17384       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17385       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17386           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17387         {
17388           type = push_template_decl (type);
17389           if (type == error_mark_node)
17390             {
17391               type = NULL_TREE;
17392               goto done;
17393             }
17394         }
17395
17396       type = TREE_TYPE (type);
17397       *nested_name_specifier_p = true;
17398     }
17399   else      /* The name is not a nested name.  */
17400     {
17401       /* If the class was unnamed, create a dummy name.  */
17402       if (!id)
17403         id = make_anon_name ();
17404       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17405                        parser->num_template_parameter_lists);
17406     }
17407
17408   /* Indicate whether this class was declared as a `class' or as a
17409      `struct'.  */
17410   if (TREE_CODE (type) == RECORD_TYPE)
17411     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17412   cp_parser_check_class_key (class_key, type);
17413
17414   /* If this type was already complete, and we see another definition,
17415      that's an error.  */
17416   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17417     {
17418       error_at (type_start_token->location, "redefinition of %q#T",
17419                 type);
17420       error_at (type_start_token->location, "previous definition of %q+#T",
17421                 type);
17422       type = NULL_TREE;
17423       goto done;
17424     }
17425   else if (type == error_mark_node)
17426     type = NULL_TREE;
17427
17428   /* We will have entered the scope containing the class; the names of
17429      base classes should be looked up in that context.  For example:
17430
17431        struct A { struct B {}; struct C; };
17432        struct A::C : B {};
17433
17434      is valid.  */
17435
17436   /* Get the list of base-classes, if there is one.  */
17437   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17438     *bases = cp_parser_base_clause (parser);
17439
17440  done:
17441   /* Leave the scope given by the nested-name-specifier.  We will
17442      enter the class scope itself while processing the members.  */
17443   if (pushed_scope)
17444     pop_scope (pushed_scope);
17445
17446   if (invalid_explicit_specialization_p)
17447     {
17448       end_specialization ();
17449       --parser->num_template_parameter_lists;
17450     }
17451
17452   if (type)
17453     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17454   *attributes_p = attributes;
17455   return type;
17456 }
17457
17458 /* Parse a class-key.
17459
17460    class-key:
17461      class
17462      struct
17463      union
17464
17465    Returns the kind of class-key specified, or none_type to indicate
17466    error.  */
17467
17468 static enum tag_types
17469 cp_parser_class_key (cp_parser* parser)
17470 {
17471   cp_token *token;
17472   enum tag_types tag_type;
17473
17474   /* Look for the class-key.  */
17475   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17476   if (!token)
17477     return none_type;
17478
17479   /* Check to see if the TOKEN is a class-key.  */
17480   tag_type = cp_parser_token_is_class_key (token);
17481   if (!tag_type)
17482     cp_parser_error (parser, "expected class-key");
17483   return tag_type;
17484 }
17485
17486 /* Parse an (optional) member-specification.
17487
17488    member-specification:
17489      member-declaration member-specification [opt]
17490      access-specifier : member-specification [opt]  */
17491
17492 static void
17493 cp_parser_member_specification_opt (cp_parser* parser)
17494 {
17495   while (true)
17496     {
17497       cp_token *token;
17498       enum rid keyword;
17499
17500       /* Peek at the next token.  */
17501       token = cp_lexer_peek_token (parser->lexer);
17502       /* If it's a `}', or EOF then we've seen all the members.  */
17503       if (token->type == CPP_CLOSE_BRACE
17504           || token->type == CPP_EOF
17505           || token->type == CPP_PRAGMA_EOL)
17506         break;
17507
17508       /* See if this token is a keyword.  */
17509       keyword = token->keyword;
17510       switch (keyword)
17511         {
17512         case RID_PUBLIC:
17513         case RID_PROTECTED:
17514         case RID_PRIVATE:
17515           /* Consume the access-specifier.  */
17516           cp_lexer_consume_token (parser->lexer);
17517           /* Remember which access-specifier is active.  */
17518           current_access_specifier = token->u.value;
17519           /* Look for the `:'.  */
17520           cp_parser_require (parser, CPP_COLON, RT_COLON);
17521           break;
17522
17523         default:
17524           /* Accept #pragmas at class scope.  */
17525           if (token->type == CPP_PRAGMA)
17526             {
17527               cp_parser_pragma (parser, pragma_external);
17528               break;
17529             }
17530
17531           /* Otherwise, the next construction must be a
17532              member-declaration.  */
17533           cp_parser_member_declaration (parser);
17534         }
17535     }
17536 }
17537
17538 /* Parse a member-declaration.
17539
17540    member-declaration:
17541      decl-specifier-seq [opt] member-declarator-list [opt] ;
17542      function-definition ; [opt]
17543      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17544      using-declaration
17545      template-declaration
17546
17547    member-declarator-list:
17548      member-declarator
17549      member-declarator-list , member-declarator
17550
17551    member-declarator:
17552      declarator pure-specifier [opt]
17553      declarator constant-initializer [opt]
17554      identifier [opt] : constant-expression
17555
17556    GNU Extensions:
17557
17558    member-declaration:
17559      __extension__ member-declaration
17560
17561    member-declarator:
17562      declarator attributes [opt] pure-specifier [opt]
17563      declarator attributes [opt] constant-initializer [opt]
17564      identifier [opt] attributes [opt] : constant-expression  
17565
17566    C++0x Extensions:
17567
17568    member-declaration:
17569      static_assert-declaration  */
17570
17571 static void
17572 cp_parser_member_declaration (cp_parser* parser)
17573 {
17574   cp_decl_specifier_seq decl_specifiers;
17575   tree prefix_attributes;
17576   tree decl;
17577   int declares_class_or_enum;
17578   bool friend_p;
17579   cp_token *token = NULL;
17580   cp_token *decl_spec_token_start = NULL;
17581   cp_token *initializer_token_start = NULL;
17582   int saved_pedantic;
17583
17584   /* Check for the `__extension__' keyword.  */
17585   if (cp_parser_extension_opt (parser, &saved_pedantic))
17586     {
17587       /* Recurse.  */
17588       cp_parser_member_declaration (parser);
17589       /* Restore the old value of the PEDANTIC flag.  */
17590       pedantic = saved_pedantic;
17591
17592       return;
17593     }
17594
17595   /* Check for a template-declaration.  */
17596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17597     {
17598       /* An explicit specialization here is an error condition, and we
17599          expect the specialization handler to detect and report this.  */
17600       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17601           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17602         cp_parser_explicit_specialization (parser);
17603       else
17604         cp_parser_template_declaration (parser, /*member_p=*/true);
17605
17606       return;
17607     }
17608
17609   /* Check for a using-declaration.  */
17610   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17611     {
17612       /* Parse the using-declaration.  */
17613       cp_parser_using_declaration (parser,
17614                                    /*access_declaration_p=*/false);
17615       return;
17616     }
17617
17618   /* Check for @defs.  */
17619   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17620     {
17621       tree ivar, member;
17622       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17623       ivar = ivar_chains;
17624       while (ivar)
17625         {
17626           member = ivar;
17627           ivar = TREE_CHAIN (member);
17628           TREE_CHAIN (member) = NULL_TREE;
17629           finish_member_declaration (member);
17630         }
17631       return;
17632     }
17633
17634   /* If the next token is `static_assert' we have a static assertion.  */
17635   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17636     {
17637       cp_parser_static_assert (parser, /*member_p=*/true);
17638       return;
17639     }
17640
17641   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17642     return;
17643
17644   /* Parse the decl-specifier-seq.  */
17645   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17646   cp_parser_decl_specifier_seq (parser,
17647                                 CP_PARSER_FLAGS_OPTIONAL,
17648                                 &decl_specifiers,
17649                                 &declares_class_or_enum);
17650   prefix_attributes = decl_specifiers.attributes;
17651   decl_specifiers.attributes = NULL_TREE;
17652   /* Check for an invalid type-name.  */
17653   if (!decl_specifiers.any_type_specifiers_p
17654       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17655     return;
17656   /* If there is no declarator, then the decl-specifier-seq should
17657      specify a type.  */
17658   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17659     {
17660       /* If there was no decl-specifier-seq, and the next token is a
17661          `;', then we have something like:
17662
17663            struct S { ; };
17664
17665          [class.mem]
17666
17667          Each member-declaration shall declare at least one member
17668          name of the class.  */
17669       if (!decl_specifiers.any_specifiers_p)
17670         {
17671           cp_token *token = cp_lexer_peek_token (parser->lexer);
17672           if (!in_system_header_at (token->location))
17673             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17674         }
17675       else
17676         {
17677           tree type;
17678
17679           /* See if this declaration is a friend.  */
17680           friend_p = cp_parser_friend_p (&decl_specifiers);
17681           /* If there were decl-specifiers, check to see if there was
17682              a class-declaration.  */
17683           type = check_tag_decl (&decl_specifiers);
17684           /* Nested classes have already been added to the class, but
17685              a `friend' needs to be explicitly registered.  */
17686           if (friend_p)
17687             {
17688               /* If the `friend' keyword was present, the friend must
17689                  be introduced with a class-key.  */
17690                if (!declares_class_or_enum)
17691                  error_at (decl_spec_token_start->location,
17692                            "a class-key must be used when declaring a friend");
17693                /* In this case:
17694
17695                     template <typename T> struct A {
17696                       friend struct A<T>::B;
17697                     };
17698
17699                   A<T>::B will be represented by a TYPENAME_TYPE, and
17700                   therefore not recognized by check_tag_decl.  */
17701                if (!type
17702                    && decl_specifiers.type
17703                    && TYPE_P (decl_specifiers.type))
17704                  type = decl_specifiers.type;
17705                if (!type || !TYPE_P (type))
17706                  error_at (decl_spec_token_start->location,
17707                            "friend declaration does not name a class or "
17708                            "function");
17709                else
17710                  make_friend_class (current_class_type, type,
17711                                     /*complain=*/true);
17712             }
17713           /* If there is no TYPE, an error message will already have
17714              been issued.  */
17715           else if (!type || type == error_mark_node)
17716             ;
17717           /* An anonymous aggregate has to be handled specially; such
17718              a declaration really declares a data member (with a
17719              particular type), as opposed to a nested class.  */
17720           else if (ANON_AGGR_TYPE_P (type))
17721             {
17722               /* Remove constructors and such from TYPE, now that we
17723                  know it is an anonymous aggregate.  */
17724               fixup_anonymous_aggr (type);
17725               /* And make the corresponding data member.  */
17726               decl = build_decl (decl_spec_token_start->location,
17727                                  FIELD_DECL, NULL_TREE, type);
17728               /* Add it to the class.  */
17729               finish_member_declaration (decl);
17730             }
17731           else
17732             cp_parser_check_access_in_redeclaration
17733                                               (TYPE_NAME (type),
17734                                                decl_spec_token_start->location);
17735         }
17736     }
17737   else
17738     {
17739       bool assume_semicolon = false;
17740
17741       /* See if these declarations will be friends.  */
17742       friend_p = cp_parser_friend_p (&decl_specifiers);
17743
17744       /* Keep going until we hit the `;' at the end of the
17745          declaration.  */
17746       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17747         {
17748           tree attributes = NULL_TREE;
17749           tree first_attribute;
17750
17751           /* Peek at the next token.  */
17752           token = cp_lexer_peek_token (parser->lexer);
17753
17754           /* Check for a bitfield declaration.  */
17755           if (token->type == CPP_COLON
17756               || (token->type == CPP_NAME
17757                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17758                   == CPP_COLON))
17759             {
17760               tree identifier;
17761               tree width;
17762
17763               /* Get the name of the bitfield.  Note that we cannot just
17764                  check TOKEN here because it may have been invalidated by
17765                  the call to cp_lexer_peek_nth_token above.  */
17766               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17767                 identifier = cp_parser_identifier (parser);
17768               else
17769                 identifier = NULL_TREE;
17770
17771               /* Consume the `:' token.  */
17772               cp_lexer_consume_token (parser->lexer);
17773               /* Get the width of the bitfield.  */
17774               width
17775                 = cp_parser_constant_expression (parser,
17776                                                  /*allow_non_constant=*/false,
17777                                                  NULL);
17778
17779               /* Look for attributes that apply to the bitfield.  */
17780               attributes = cp_parser_attributes_opt (parser);
17781               /* Remember which attributes are prefix attributes and
17782                  which are not.  */
17783               first_attribute = attributes;
17784               /* Combine the attributes.  */
17785               attributes = chainon (prefix_attributes, attributes);
17786
17787               /* Create the bitfield declaration.  */
17788               decl = grokbitfield (identifier
17789                                    ? make_id_declarator (NULL_TREE,
17790                                                          identifier,
17791                                                          sfk_none)
17792                                    : NULL,
17793                                    &decl_specifiers,
17794                                    width,
17795                                    attributes);
17796             }
17797           else
17798             {
17799               cp_declarator *declarator;
17800               tree initializer;
17801               tree asm_specification;
17802               int ctor_dtor_or_conv_p;
17803
17804               /* Parse the declarator.  */
17805               declarator
17806                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17807                                         &ctor_dtor_or_conv_p,
17808                                         /*parenthesized_p=*/NULL,
17809                                         /*member_p=*/true);
17810
17811               /* If something went wrong parsing the declarator, make sure
17812                  that we at least consume some tokens.  */
17813               if (declarator == cp_error_declarator)
17814                 {
17815                   /* Skip to the end of the statement.  */
17816                   cp_parser_skip_to_end_of_statement (parser);
17817                   /* If the next token is not a semicolon, that is
17818                      probably because we just skipped over the body of
17819                      a function.  So, we consume a semicolon if
17820                      present, but do not issue an error message if it
17821                      is not present.  */
17822                   if (cp_lexer_next_token_is (parser->lexer,
17823                                               CPP_SEMICOLON))
17824                     cp_lexer_consume_token (parser->lexer);
17825                   return;
17826                 }
17827
17828               if (declares_class_or_enum & 2)
17829                 cp_parser_check_for_definition_in_return_type
17830                                             (declarator, decl_specifiers.type,
17831                                              decl_specifiers.type_location);
17832
17833               /* Look for an asm-specification.  */
17834               asm_specification = cp_parser_asm_specification_opt (parser);
17835               /* Look for attributes that apply to the declaration.  */
17836               attributes = cp_parser_attributes_opt (parser);
17837               /* Remember which attributes are prefix attributes and
17838                  which are not.  */
17839               first_attribute = attributes;
17840               /* Combine the attributes.  */
17841               attributes = chainon (prefix_attributes, attributes);
17842
17843               /* If it's an `=', then we have a constant-initializer or a
17844                  pure-specifier.  It is not correct to parse the
17845                  initializer before registering the member declaration
17846                  since the member declaration should be in scope while
17847                  its initializer is processed.  However, the rest of the
17848                  front end does not yet provide an interface that allows
17849                  us to handle this correctly.  */
17850               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17851                 {
17852                   /* In [class.mem]:
17853
17854                      A pure-specifier shall be used only in the declaration of
17855                      a virtual function.
17856
17857                      A member-declarator can contain a constant-initializer
17858                      only if it declares a static member of integral or
17859                      enumeration type.
17860
17861                      Therefore, if the DECLARATOR is for a function, we look
17862                      for a pure-specifier; otherwise, we look for a
17863                      constant-initializer.  When we call `grokfield', it will
17864                      perform more stringent semantics checks.  */
17865                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17866                   if (function_declarator_p (declarator))
17867                     initializer = cp_parser_pure_specifier (parser);
17868                   else
17869                     /* Parse the initializer.  */
17870                     initializer = cp_parser_constant_initializer (parser);
17871                 }
17872               /* Otherwise, there is no initializer.  */
17873               else
17874                 initializer = NULL_TREE;
17875
17876               /* See if we are probably looking at a function
17877                  definition.  We are certainly not looking at a
17878                  member-declarator.  Calling `grokfield' has
17879                  side-effects, so we must not do it unless we are sure
17880                  that we are looking at a member-declarator.  */
17881               if (cp_parser_token_starts_function_definition_p
17882                   (cp_lexer_peek_token (parser->lexer)))
17883                 {
17884                   /* The grammar does not allow a pure-specifier to be
17885                      used when a member function is defined.  (It is
17886                      possible that this fact is an oversight in the
17887                      standard, since a pure function may be defined
17888                      outside of the class-specifier.  */
17889                   if (initializer)
17890                     error_at (initializer_token_start->location,
17891                               "pure-specifier on function-definition");
17892                   decl = cp_parser_save_member_function_body (parser,
17893                                                               &decl_specifiers,
17894                                                               declarator,
17895                                                               attributes);
17896                   /* If the member was not a friend, declare it here.  */
17897                   if (!friend_p)
17898                     finish_member_declaration (decl);
17899                   /* Peek at the next token.  */
17900                   token = cp_lexer_peek_token (parser->lexer);
17901                   /* If the next token is a semicolon, consume it.  */
17902                   if (token->type == CPP_SEMICOLON)
17903                     cp_lexer_consume_token (parser->lexer);
17904                   return;
17905                 }
17906               else
17907                 if (declarator->kind == cdk_function)
17908                   declarator->id_loc = token->location;
17909                 /* Create the declaration.  */
17910                 decl = grokfield (declarator, &decl_specifiers,
17911                                   initializer, /*init_const_expr_p=*/true,
17912                                   asm_specification,
17913                                   attributes);
17914             }
17915
17916           /* Reset PREFIX_ATTRIBUTES.  */
17917           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17918             attributes = TREE_CHAIN (attributes);
17919           if (attributes)
17920             TREE_CHAIN (attributes) = NULL_TREE;
17921
17922           /* If there is any qualification still in effect, clear it
17923              now; we will be starting fresh with the next declarator.  */
17924           parser->scope = NULL_TREE;
17925           parser->qualifying_scope = NULL_TREE;
17926           parser->object_scope = NULL_TREE;
17927           /* If it's a `,', then there are more declarators.  */
17928           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17929             cp_lexer_consume_token (parser->lexer);
17930           /* If the next token isn't a `;', then we have a parse error.  */
17931           else if (cp_lexer_next_token_is_not (parser->lexer,
17932                                                CPP_SEMICOLON))
17933             {
17934               /* The next token might be a ways away from where the
17935                  actual semicolon is missing.  Find the previous token
17936                  and use that for our error position.  */
17937               cp_token *token = cp_lexer_previous_token (parser->lexer);
17938               error_at (token->location,
17939                         "expected %<;%> at end of member declaration");
17940
17941               /* Assume that the user meant to provide a semicolon.  If
17942                  we were to cp_parser_skip_to_end_of_statement, we might
17943                  skip to a semicolon inside a member function definition
17944                  and issue nonsensical error messages.  */
17945               assume_semicolon = true;
17946             }
17947
17948           if (decl)
17949             {
17950               /* Add DECL to the list of members.  */
17951               if (!friend_p)
17952                 finish_member_declaration (decl);
17953
17954               if (TREE_CODE (decl) == FUNCTION_DECL)
17955                 cp_parser_save_default_args (parser, decl);
17956             }
17957
17958           if (assume_semicolon)
17959             return;
17960         }
17961     }
17962
17963   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17964 }
17965
17966 /* Parse a pure-specifier.
17967
17968    pure-specifier:
17969      = 0
17970
17971    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17972    Otherwise, ERROR_MARK_NODE is returned.  */
17973
17974 static tree
17975 cp_parser_pure_specifier (cp_parser* parser)
17976 {
17977   cp_token *token;
17978
17979   /* Look for the `=' token.  */
17980   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17981     return error_mark_node;
17982   /* Look for the `0' token.  */
17983   token = cp_lexer_peek_token (parser->lexer);
17984
17985   if (token->type == CPP_EOF
17986       || token->type == CPP_PRAGMA_EOL)
17987     return error_mark_node;
17988
17989   cp_lexer_consume_token (parser->lexer);
17990
17991   /* Accept = default or = delete in c++0x mode.  */
17992   if (token->keyword == RID_DEFAULT
17993       || token->keyword == RID_DELETE)
17994     {
17995       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17996       return token->u.value;
17997     }
17998
17999   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18000   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18001     {
18002       cp_parser_error (parser,
18003                        "invalid pure specifier (only %<= 0%> is allowed)");
18004       cp_parser_skip_to_end_of_statement (parser);
18005       return error_mark_node;
18006     }
18007   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18008     {
18009       error_at (token->location, "templates may not be %<virtual%>");
18010       return error_mark_node;
18011     }
18012
18013   return integer_zero_node;
18014 }
18015
18016 /* Parse a constant-initializer.
18017
18018    constant-initializer:
18019      = constant-expression
18020
18021    Returns a representation of the constant-expression.  */
18022
18023 static tree
18024 cp_parser_constant_initializer (cp_parser* parser)
18025 {
18026   /* Look for the `=' token.  */
18027   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18028     return error_mark_node;
18029
18030   /* It is invalid to write:
18031
18032        struct S { static const int i = { 7 }; };
18033
18034      */
18035   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18036     {
18037       cp_parser_error (parser,
18038                        "a brace-enclosed initializer is not allowed here");
18039       /* Consume the opening brace.  */
18040       cp_lexer_consume_token (parser->lexer);
18041       /* Skip the initializer.  */
18042       cp_parser_skip_to_closing_brace (parser);
18043       /* Look for the trailing `}'.  */
18044       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18045
18046       return error_mark_node;
18047     }
18048
18049   return cp_parser_constant_expression (parser,
18050                                         /*allow_non_constant=*/false,
18051                                         NULL);
18052 }
18053
18054 /* Derived classes [gram.class.derived] */
18055
18056 /* Parse a base-clause.
18057
18058    base-clause:
18059      : base-specifier-list
18060
18061    base-specifier-list:
18062      base-specifier ... [opt]
18063      base-specifier-list , base-specifier ... [opt]
18064
18065    Returns a TREE_LIST representing the base-classes, in the order in
18066    which they were declared.  The representation of each node is as
18067    described by cp_parser_base_specifier.
18068
18069    In the case that no bases are specified, this function will return
18070    NULL_TREE, not ERROR_MARK_NODE.  */
18071
18072 static tree
18073 cp_parser_base_clause (cp_parser* parser)
18074 {
18075   tree bases = NULL_TREE;
18076
18077   /* Look for the `:' that begins the list.  */
18078   cp_parser_require (parser, CPP_COLON, RT_COLON);
18079
18080   /* Scan the base-specifier-list.  */
18081   while (true)
18082     {
18083       cp_token *token;
18084       tree base;
18085       bool pack_expansion_p = false;
18086
18087       /* Look for the base-specifier.  */
18088       base = cp_parser_base_specifier (parser);
18089       /* Look for the (optional) ellipsis. */
18090       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18091         {
18092           /* Consume the `...'. */
18093           cp_lexer_consume_token (parser->lexer);
18094
18095           pack_expansion_p = true;
18096         }
18097
18098       /* Add BASE to the front of the list.  */
18099       if (base != error_mark_node)
18100         {
18101           if (pack_expansion_p)
18102             /* Make this a pack expansion type. */
18103             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18104           
18105
18106           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18107             {
18108               TREE_CHAIN (base) = bases;
18109               bases = base;
18110             }
18111         }
18112       /* Peek at the next token.  */
18113       token = cp_lexer_peek_token (parser->lexer);
18114       /* If it's not a comma, then the list is complete.  */
18115       if (token->type != CPP_COMMA)
18116         break;
18117       /* Consume the `,'.  */
18118       cp_lexer_consume_token (parser->lexer);
18119     }
18120
18121   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18122      base class had a qualified name.  However, the next name that
18123      appears is certainly not qualified.  */
18124   parser->scope = NULL_TREE;
18125   parser->qualifying_scope = NULL_TREE;
18126   parser->object_scope = NULL_TREE;
18127
18128   return nreverse (bases);
18129 }
18130
18131 /* Parse a base-specifier.
18132
18133    base-specifier:
18134      :: [opt] nested-name-specifier [opt] class-name
18135      virtual access-specifier [opt] :: [opt] nested-name-specifier
18136        [opt] class-name
18137      access-specifier virtual [opt] :: [opt] nested-name-specifier
18138        [opt] class-name
18139
18140    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18141    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18142    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18143    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18144
18145 static tree
18146 cp_parser_base_specifier (cp_parser* parser)
18147 {
18148   cp_token *token;
18149   bool done = false;
18150   bool virtual_p = false;
18151   bool duplicate_virtual_error_issued_p = false;
18152   bool duplicate_access_error_issued_p = false;
18153   bool class_scope_p, template_p;
18154   tree access = access_default_node;
18155   tree type;
18156
18157   /* Process the optional `virtual' and `access-specifier'.  */
18158   while (!done)
18159     {
18160       /* Peek at the next token.  */
18161       token = cp_lexer_peek_token (parser->lexer);
18162       /* Process `virtual'.  */
18163       switch (token->keyword)
18164         {
18165         case RID_VIRTUAL:
18166           /* If `virtual' appears more than once, issue an error.  */
18167           if (virtual_p && !duplicate_virtual_error_issued_p)
18168             {
18169               cp_parser_error (parser,
18170                                "%<virtual%> specified more than once in base-specified");
18171               duplicate_virtual_error_issued_p = true;
18172             }
18173
18174           virtual_p = true;
18175
18176           /* Consume the `virtual' token.  */
18177           cp_lexer_consume_token (parser->lexer);
18178
18179           break;
18180
18181         case RID_PUBLIC:
18182         case RID_PROTECTED:
18183         case RID_PRIVATE:
18184           /* If more than one access specifier appears, issue an
18185              error.  */
18186           if (access != access_default_node
18187               && !duplicate_access_error_issued_p)
18188             {
18189               cp_parser_error (parser,
18190                                "more than one access specifier in base-specified");
18191               duplicate_access_error_issued_p = true;
18192             }
18193
18194           access = ridpointers[(int) token->keyword];
18195
18196           /* Consume the access-specifier.  */
18197           cp_lexer_consume_token (parser->lexer);
18198
18199           break;
18200
18201         default:
18202           done = true;
18203           break;
18204         }
18205     }
18206   /* It is not uncommon to see programs mechanically, erroneously, use
18207      the 'typename' keyword to denote (dependent) qualified types
18208      as base classes.  */
18209   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18210     {
18211       token = cp_lexer_peek_token (parser->lexer);
18212       if (!processing_template_decl)
18213         error_at (token->location,
18214                   "keyword %<typename%> not allowed outside of templates");
18215       else
18216         error_at (token->location,
18217                   "keyword %<typename%> not allowed in this context "
18218                   "(the base class is implicitly a type)");
18219       cp_lexer_consume_token (parser->lexer);
18220     }
18221
18222   /* Look for the optional `::' operator.  */
18223   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18224   /* Look for the nested-name-specifier.  The simplest way to
18225      implement:
18226
18227        [temp.res]
18228
18229        The keyword `typename' is not permitted in a base-specifier or
18230        mem-initializer; in these contexts a qualified name that
18231        depends on a template-parameter is implicitly assumed to be a
18232        type name.
18233
18234      is to pretend that we have seen the `typename' keyword at this
18235      point.  */
18236   cp_parser_nested_name_specifier_opt (parser,
18237                                        /*typename_keyword_p=*/true,
18238                                        /*check_dependency_p=*/true,
18239                                        typename_type,
18240                                        /*is_declaration=*/true);
18241   /* If the base class is given by a qualified name, assume that names
18242      we see are type names or templates, as appropriate.  */
18243   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18244   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18245
18246   /* Finally, look for the class-name.  */
18247   type = cp_parser_class_name (parser,
18248                                class_scope_p,
18249                                template_p,
18250                                typename_type,
18251                                /*check_dependency_p=*/true,
18252                                /*class_head_p=*/false,
18253                                /*is_declaration=*/true);
18254
18255   if (type == error_mark_node)
18256     return error_mark_node;
18257
18258   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18259 }
18260
18261 /* Exception handling [gram.exception] */
18262
18263 /* Parse an (optional) exception-specification.
18264
18265    exception-specification:
18266      throw ( type-id-list [opt] )
18267
18268    Returns a TREE_LIST representing the exception-specification.  The
18269    TREE_VALUE of each node is a type.  */
18270
18271 static tree
18272 cp_parser_exception_specification_opt (cp_parser* parser)
18273 {
18274   cp_token *token;
18275   tree type_id_list;
18276   const char *saved_message;
18277
18278   /* Peek at the next token.  */
18279   token = cp_lexer_peek_token (parser->lexer);
18280
18281   /* Is it a noexcept-specification?  */
18282   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18283     {
18284       tree expr;
18285       cp_lexer_consume_token (parser->lexer);
18286
18287       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18288         {
18289           cp_lexer_consume_token (parser->lexer);
18290
18291           /* Types may not be defined in an exception-specification.  */
18292           saved_message = parser->type_definition_forbidden_message;
18293           parser->type_definition_forbidden_message
18294             = G_("types may not be defined in an exception-specification");
18295
18296           expr = cp_parser_constant_expression (parser, false, NULL);
18297
18298           /* Restore the saved message.  */
18299           parser->type_definition_forbidden_message = saved_message;
18300
18301           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18302         }
18303       else
18304         expr = boolean_true_node;
18305
18306       return build_noexcept_spec (expr, tf_warning_or_error);
18307     }
18308
18309   /* If it's not `throw', then there's no exception-specification.  */
18310   if (!cp_parser_is_keyword (token, RID_THROW))
18311     return NULL_TREE;
18312
18313 #if 0
18314   /* Enable this once a lot of code has transitioned to noexcept?  */
18315   if (cxx_dialect == cxx0x && !in_system_header)
18316     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18317              "deprecated in C++0x; use %<noexcept%> instead");
18318 #endif
18319
18320   /* Consume the `throw'.  */
18321   cp_lexer_consume_token (parser->lexer);
18322
18323   /* Look for the `('.  */
18324   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18325
18326   /* Peek at the next token.  */
18327   token = cp_lexer_peek_token (parser->lexer);
18328   /* If it's not a `)', then there is a type-id-list.  */
18329   if (token->type != CPP_CLOSE_PAREN)
18330     {
18331       /* Types may not be defined in an exception-specification.  */
18332       saved_message = parser->type_definition_forbidden_message;
18333       parser->type_definition_forbidden_message
18334         = G_("types may not be defined in an exception-specification");
18335       /* Parse the type-id-list.  */
18336       type_id_list = cp_parser_type_id_list (parser);
18337       /* Restore the saved message.  */
18338       parser->type_definition_forbidden_message = saved_message;
18339     }
18340   else
18341     type_id_list = empty_except_spec;
18342
18343   /* Look for the `)'.  */
18344   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18345
18346   return type_id_list;
18347 }
18348
18349 /* Parse an (optional) type-id-list.
18350
18351    type-id-list:
18352      type-id ... [opt]
18353      type-id-list , type-id ... [opt]
18354
18355    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18356    in the order that the types were presented.  */
18357
18358 static tree
18359 cp_parser_type_id_list (cp_parser* parser)
18360 {
18361   tree types = NULL_TREE;
18362
18363   while (true)
18364     {
18365       cp_token *token;
18366       tree type;
18367
18368       /* Get the next type-id.  */
18369       type = cp_parser_type_id (parser);
18370       /* Parse the optional ellipsis. */
18371       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18372         {
18373           /* Consume the `...'. */
18374           cp_lexer_consume_token (parser->lexer);
18375
18376           /* Turn the type into a pack expansion expression. */
18377           type = make_pack_expansion (type);
18378         }
18379       /* Add it to the list.  */
18380       types = add_exception_specifier (types, type, /*complain=*/1);
18381       /* Peek at the next token.  */
18382       token = cp_lexer_peek_token (parser->lexer);
18383       /* If it is not a `,', we are done.  */
18384       if (token->type != CPP_COMMA)
18385         break;
18386       /* Consume the `,'.  */
18387       cp_lexer_consume_token (parser->lexer);
18388     }
18389
18390   return nreverse (types);
18391 }
18392
18393 /* Parse a try-block.
18394
18395    try-block:
18396      try compound-statement handler-seq  */
18397
18398 static tree
18399 cp_parser_try_block (cp_parser* parser)
18400 {
18401   tree try_block;
18402
18403   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18404   try_block = begin_try_block ();
18405   cp_parser_compound_statement (parser, NULL, true);
18406   finish_try_block (try_block);
18407   cp_parser_handler_seq (parser);
18408   finish_handler_sequence (try_block);
18409
18410   return try_block;
18411 }
18412
18413 /* Parse a function-try-block.
18414
18415    function-try-block:
18416      try ctor-initializer [opt] function-body handler-seq  */
18417
18418 static bool
18419 cp_parser_function_try_block (cp_parser* parser)
18420 {
18421   tree compound_stmt;
18422   tree try_block;
18423   bool ctor_initializer_p;
18424
18425   /* Look for the `try' keyword.  */
18426   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18427     return false;
18428   /* Let the rest of the front end know where we are.  */
18429   try_block = begin_function_try_block (&compound_stmt);
18430   /* Parse the function-body.  */
18431   ctor_initializer_p
18432     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18433   /* We're done with the `try' part.  */
18434   finish_function_try_block (try_block);
18435   /* Parse the handlers.  */
18436   cp_parser_handler_seq (parser);
18437   /* We're done with the handlers.  */
18438   finish_function_handler_sequence (try_block, compound_stmt);
18439
18440   return ctor_initializer_p;
18441 }
18442
18443 /* Parse a handler-seq.
18444
18445    handler-seq:
18446      handler handler-seq [opt]  */
18447
18448 static void
18449 cp_parser_handler_seq (cp_parser* parser)
18450 {
18451   while (true)
18452     {
18453       cp_token *token;
18454
18455       /* Parse the handler.  */
18456       cp_parser_handler (parser);
18457       /* Peek at the next token.  */
18458       token = cp_lexer_peek_token (parser->lexer);
18459       /* If it's not `catch' then there are no more handlers.  */
18460       if (!cp_parser_is_keyword (token, RID_CATCH))
18461         break;
18462     }
18463 }
18464
18465 /* Parse a handler.
18466
18467    handler:
18468      catch ( exception-declaration ) compound-statement  */
18469
18470 static void
18471 cp_parser_handler (cp_parser* parser)
18472 {
18473   tree handler;
18474   tree declaration;
18475
18476   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18477   handler = begin_handler ();
18478   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18479   declaration = cp_parser_exception_declaration (parser);
18480   finish_handler_parms (declaration, handler);
18481   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18482   cp_parser_compound_statement (parser, NULL, false);
18483   finish_handler (handler);
18484 }
18485
18486 /* Parse an exception-declaration.
18487
18488    exception-declaration:
18489      type-specifier-seq declarator
18490      type-specifier-seq abstract-declarator
18491      type-specifier-seq
18492      ...
18493
18494    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18495    ellipsis variant is used.  */
18496
18497 static tree
18498 cp_parser_exception_declaration (cp_parser* parser)
18499 {
18500   cp_decl_specifier_seq type_specifiers;
18501   cp_declarator *declarator;
18502   const char *saved_message;
18503
18504   /* If it's an ellipsis, it's easy to handle.  */
18505   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18506     {
18507       /* Consume the `...' token.  */
18508       cp_lexer_consume_token (parser->lexer);
18509       return NULL_TREE;
18510     }
18511
18512   /* Types may not be defined in exception-declarations.  */
18513   saved_message = parser->type_definition_forbidden_message;
18514   parser->type_definition_forbidden_message
18515     = G_("types may not be defined in exception-declarations");
18516
18517   /* Parse the type-specifier-seq.  */
18518   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18519                                 /*is_trailing_return=*/false,
18520                                 &type_specifiers);
18521   /* If it's a `)', then there is no declarator.  */
18522   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18523     declarator = NULL;
18524   else
18525     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18526                                        /*ctor_dtor_or_conv_p=*/NULL,
18527                                        /*parenthesized_p=*/NULL,
18528                                        /*member_p=*/false);
18529
18530   /* Restore the saved message.  */
18531   parser->type_definition_forbidden_message = saved_message;
18532
18533   if (!type_specifiers.any_specifiers_p)
18534     return error_mark_node;
18535
18536   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18537 }
18538
18539 /* Parse a throw-expression.
18540
18541    throw-expression:
18542      throw assignment-expression [opt]
18543
18544    Returns a THROW_EXPR representing the throw-expression.  */
18545
18546 static tree
18547 cp_parser_throw_expression (cp_parser* parser)
18548 {
18549   tree expression;
18550   cp_token* token;
18551
18552   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18553   token = cp_lexer_peek_token (parser->lexer);
18554   /* Figure out whether or not there is an assignment-expression
18555      following the "throw" keyword.  */
18556   if (token->type == CPP_COMMA
18557       || token->type == CPP_SEMICOLON
18558       || token->type == CPP_CLOSE_PAREN
18559       || token->type == CPP_CLOSE_SQUARE
18560       || token->type == CPP_CLOSE_BRACE
18561       || token->type == CPP_COLON)
18562     expression = NULL_TREE;
18563   else
18564     expression = cp_parser_assignment_expression (parser,
18565                                                   /*cast_p=*/false, NULL);
18566
18567   return build_throw (expression);
18568 }
18569
18570 /* GNU Extensions */
18571
18572 /* Parse an (optional) asm-specification.
18573
18574    asm-specification:
18575      asm ( string-literal )
18576
18577    If the asm-specification is present, returns a STRING_CST
18578    corresponding to the string-literal.  Otherwise, returns
18579    NULL_TREE.  */
18580
18581 static tree
18582 cp_parser_asm_specification_opt (cp_parser* parser)
18583 {
18584   cp_token *token;
18585   tree asm_specification;
18586
18587   /* Peek at the next token.  */
18588   token = cp_lexer_peek_token (parser->lexer);
18589   /* If the next token isn't the `asm' keyword, then there's no
18590      asm-specification.  */
18591   if (!cp_parser_is_keyword (token, RID_ASM))
18592     return NULL_TREE;
18593
18594   /* Consume the `asm' token.  */
18595   cp_lexer_consume_token (parser->lexer);
18596   /* Look for the `('.  */
18597   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18598
18599   /* Look for the string-literal.  */
18600   asm_specification = cp_parser_string_literal (parser, false, false);
18601
18602   /* Look for the `)'.  */
18603   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18604
18605   return asm_specification;
18606 }
18607
18608 /* Parse an asm-operand-list.
18609
18610    asm-operand-list:
18611      asm-operand
18612      asm-operand-list , asm-operand
18613
18614    asm-operand:
18615      string-literal ( expression )
18616      [ string-literal ] string-literal ( expression )
18617
18618    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18619    each node is the expression.  The TREE_PURPOSE is itself a
18620    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18621    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18622    is a STRING_CST for the string literal before the parenthesis. Returns
18623    ERROR_MARK_NODE if any of the operands are invalid.  */
18624
18625 static tree
18626 cp_parser_asm_operand_list (cp_parser* parser)
18627 {
18628   tree asm_operands = NULL_TREE;
18629   bool invalid_operands = false;
18630
18631   while (true)
18632     {
18633       tree string_literal;
18634       tree expression;
18635       tree name;
18636
18637       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18638         {
18639           /* Consume the `[' token.  */
18640           cp_lexer_consume_token (parser->lexer);
18641           /* Read the operand name.  */
18642           name = cp_parser_identifier (parser);
18643           if (name != error_mark_node)
18644             name = build_string (IDENTIFIER_LENGTH (name),
18645                                  IDENTIFIER_POINTER (name));
18646           /* Look for the closing `]'.  */
18647           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18648         }
18649       else
18650         name = NULL_TREE;
18651       /* Look for the string-literal.  */
18652       string_literal = cp_parser_string_literal (parser, false, false);
18653
18654       /* Look for the `('.  */
18655       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18656       /* Parse the expression.  */
18657       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18658       /* Look for the `)'.  */
18659       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18660
18661       if (name == error_mark_node 
18662           || string_literal == error_mark_node 
18663           || expression == error_mark_node)
18664         invalid_operands = true;
18665
18666       /* Add this operand to the list.  */
18667       asm_operands = tree_cons (build_tree_list (name, string_literal),
18668                                 expression,
18669                                 asm_operands);
18670       /* If the next token is not a `,', there are no more
18671          operands.  */
18672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18673         break;
18674       /* Consume the `,'.  */
18675       cp_lexer_consume_token (parser->lexer);
18676     }
18677
18678   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18679 }
18680
18681 /* Parse an asm-clobber-list.
18682
18683    asm-clobber-list:
18684      string-literal
18685      asm-clobber-list , string-literal
18686
18687    Returns a TREE_LIST, indicating the clobbers in the order that they
18688    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18689
18690 static tree
18691 cp_parser_asm_clobber_list (cp_parser* parser)
18692 {
18693   tree clobbers = NULL_TREE;
18694
18695   while (true)
18696     {
18697       tree string_literal;
18698
18699       /* Look for the string literal.  */
18700       string_literal = cp_parser_string_literal (parser, false, false);
18701       /* Add it to the list.  */
18702       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18703       /* If the next token is not a `,', then the list is
18704          complete.  */
18705       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18706         break;
18707       /* Consume the `,' token.  */
18708       cp_lexer_consume_token (parser->lexer);
18709     }
18710
18711   return clobbers;
18712 }
18713
18714 /* Parse an asm-label-list.
18715
18716    asm-label-list:
18717      identifier
18718      asm-label-list , identifier
18719
18720    Returns a TREE_LIST, indicating the labels in the order that they
18721    appeared.  The TREE_VALUE of each node is a label.  */
18722
18723 static tree
18724 cp_parser_asm_label_list (cp_parser* parser)
18725 {
18726   tree labels = NULL_TREE;
18727
18728   while (true)
18729     {
18730       tree identifier, label, name;
18731
18732       /* Look for the identifier.  */
18733       identifier = cp_parser_identifier (parser);
18734       if (!error_operand_p (identifier))
18735         {
18736           label = lookup_label (identifier);
18737           if (TREE_CODE (label) == LABEL_DECL)
18738             {
18739               TREE_USED (label) = 1;
18740               check_goto (label);
18741               name = build_string (IDENTIFIER_LENGTH (identifier),
18742                                    IDENTIFIER_POINTER (identifier));
18743               labels = tree_cons (name, label, labels);
18744             }
18745         }
18746       /* If the next token is not a `,', then the list is
18747          complete.  */
18748       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18749         break;
18750       /* Consume the `,' token.  */
18751       cp_lexer_consume_token (parser->lexer);
18752     }
18753
18754   return nreverse (labels);
18755 }
18756
18757 /* Parse an (optional) series of attributes.
18758
18759    attributes:
18760      attributes attribute
18761
18762    attribute:
18763      __attribute__ (( attribute-list [opt] ))
18764
18765    The return value is as for cp_parser_attribute_list.  */
18766
18767 static tree
18768 cp_parser_attributes_opt (cp_parser* parser)
18769 {
18770   tree attributes = NULL_TREE;
18771
18772   while (true)
18773     {
18774       cp_token *token;
18775       tree attribute_list;
18776
18777       /* Peek at the next token.  */
18778       token = cp_lexer_peek_token (parser->lexer);
18779       /* If it's not `__attribute__', then we're done.  */
18780       if (token->keyword != RID_ATTRIBUTE)
18781         break;
18782
18783       /* Consume the `__attribute__' keyword.  */
18784       cp_lexer_consume_token (parser->lexer);
18785       /* Look for the two `(' tokens.  */
18786       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18787       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18788
18789       /* Peek at the next token.  */
18790       token = cp_lexer_peek_token (parser->lexer);
18791       if (token->type != CPP_CLOSE_PAREN)
18792         /* Parse the attribute-list.  */
18793         attribute_list = cp_parser_attribute_list (parser);
18794       else
18795         /* If the next token is a `)', then there is no attribute
18796            list.  */
18797         attribute_list = NULL;
18798
18799       /* Look for the two `)' tokens.  */
18800       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18801       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18802
18803       /* Add these new attributes to the list.  */
18804       attributes = chainon (attributes, attribute_list);
18805     }
18806
18807   return attributes;
18808 }
18809
18810 /* Parse an attribute-list.
18811
18812    attribute-list:
18813      attribute
18814      attribute-list , attribute
18815
18816    attribute:
18817      identifier
18818      identifier ( identifier )
18819      identifier ( identifier , expression-list )
18820      identifier ( expression-list )
18821
18822    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18823    to an attribute.  The TREE_PURPOSE of each node is the identifier
18824    indicating which attribute is in use.  The TREE_VALUE represents
18825    the arguments, if any.  */
18826
18827 static tree
18828 cp_parser_attribute_list (cp_parser* parser)
18829 {
18830   tree attribute_list = NULL_TREE;
18831   bool save_translate_strings_p = parser->translate_strings_p;
18832
18833   parser->translate_strings_p = false;
18834   while (true)
18835     {
18836       cp_token *token;
18837       tree identifier;
18838       tree attribute;
18839
18840       /* Look for the identifier.  We also allow keywords here; for
18841          example `__attribute__ ((const))' is legal.  */
18842       token = cp_lexer_peek_token (parser->lexer);
18843       if (token->type == CPP_NAME
18844           || token->type == CPP_KEYWORD)
18845         {
18846           tree arguments = NULL_TREE;
18847
18848           /* Consume the token.  */
18849           token = cp_lexer_consume_token (parser->lexer);
18850
18851           /* Save away the identifier that indicates which attribute
18852              this is.  */
18853           identifier = (token->type == CPP_KEYWORD) 
18854             /* For keywords, use the canonical spelling, not the
18855                parsed identifier.  */
18856             ? ridpointers[(int) token->keyword]
18857             : token->u.value;
18858           
18859           attribute = build_tree_list (identifier, NULL_TREE);
18860
18861           /* Peek at the next token.  */
18862           token = cp_lexer_peek_token (parser->lexer);
18863           /* If it's an `(', then parse the attribute arguments.  */
18864           if (token->type == CPP_OPEN_PAREN)
18865             {
18866               VEC(tree,gc) *vec;
18867               int attr_flag = (attribute_takes_identifier_p (identifier)
18868                                ? id_attr : normal_attr);
18869               vec = cp_parser_parenthesized_expression_list
18870                     (parser, attr_flag, /*cast_p=*/false,
18871                      /*allow_expansion_p=*/false,
18872                      /*non_constant_p=*/NULL);
18873               if (vec == NULL)
18874                 arguments = error_mark_node;
18875               else
18876                 {
18877                   arguments = build_tree_list_vec (vec);
18878                   release_tree_vector (vec);
18879                 }
18880               /* Save the arguments away.  */
18881               TREE_VALUE (attribute) = arguments;
18882             }
18883
18884           if (arguments != error_mark_node)
18885             {
18886               /* Add this attribute to the list.  */
18887               TREE_CHAIN (attribute) = attribute_list;
18888               attribute_list = attribute;
18889             }
18890
18891           token = cp_lexer_peek_token (parser->lexer);
18892         }
18893       /* Now, look for more attributes.  If the next token isn't a
18894          `,', we're done.  */
18895       if (token->type != CPP_COMMA)
18896         break;
18897
18898       /* Consume the comma and keep going.  */
18899       cp_lexer_consume_token (parser->lexer);
18900     }
18901   parser->translate_strings_p = save_translate_strings_p;
18902
18903   /* We built up the list in reverse order.  */
18904   return nreverse (attribute_list);
18905 }
18906
18907 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18908    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18909    current value of the PEDANTIC flag, regardless of whether or not
18910    the `__extension__' keyword is present.  The caller is responsible
18911    for restoring the value of the PEDANTIC flag.  */
18912
18913 static bool
18914 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18915 {
18916   /* Save the old value of the PEDANTIC flag.  */
18917   *saved_pedantic = pedantic;
18918
18919   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18920     {
18921       /* Consume the `__extension__' token.  */
18922       cp_lexer_consume_token (parser->lexer);
18923       /* We're not being pedantic while the `__extension__' keyword is
18924          in effect.  */
18925       pedantic = 0;
18926
18927       return true;
18928     }
18929
18930   return false;
18931 }
18932
18933 /* Parse a label declaration.
18934
18935    label-declaration:
18936      __label__ label-declarator-seq ;
18937
18938    label-declarator-seq:
18939      identifier , label-declarator-seq
18940      identifier  */
18941
18942 static void
18943 cp_parser_label_declaration (cp_parser* parser)
18944 {
18945   /* Look for the `__label__' keyword.  */
18946   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18947
18948   while (true)
18949     {
18950       tree identifier;
18951
18952       /* Look for an identifier.  */
18953       identifier = cp_parser_identifier (parser);
18954       /* If we failed, stop.  */
18955       if (identifier == error_mark_node)
18956         break;
18957       /* Declare it as a label.  */
18958       finish_label_decl (identifier);
18959       /* If the next token is a `;', stop.  */
18960       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18961         break;
18962       /* Look for the `,' separating the label declarations.  */
18963       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18964     }
18965
18966   /* Look for the final `;'.  */
18967   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18968 }
18969
18970 /* Support Functions */
18971
18972 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18973    NAME should have one of the representations used for an
18974    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18975    is returned.  If PARSER->SCOPE is a dependent type, then a
18976    SCOPE_REF is returned.
18977
18978    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18979    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18980    was formed.  Abstractly, such entities should not be passed to this
18981    function, because they do not need to be looked up, but it is
18982    simpler to check for this special case here, rather than at the
18983    call-sites.
18984
18985    In cases not explicitly covered above, this function returns a
18986    DECL, OVERLOAD, or baselink representing the result of the lookup.
18987    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18988    is returned.
18989
18990    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18991    (e.g., "struct") that was used.  In that case bindings that do not
18992    refer to types are ignored.
18993
18994    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18995    ignored.
18996
18997    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18998    are ignored.
18999
19000    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19001    types.
19002
19003    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19004    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19005    NULL_TREE otherwise.  */
19006
19007 static tree
19008 cp_parser_lookup_name (cp_parser *parser, tree name,
19009                        enum tag_types tag_type,
19010                        bool is_template,
19011                        bool is_namespace,
19012                        bool check_dependency,
19013                        tree *ambiguous_decls,
19014                        location_t name_location)
19015 {
19016   int flags = 0;
19017   tree decl;
19018   tree object_type = parser->context->object_type;
19019
19020   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19021     flags |= LOOKUP_COMPLAIN;
19022
19023   /* Assume that the lookup will be unambiguous.  */
19024   if (ambiguous_decls)
19025     *ambiguous_decls = NULL_TREE;
19026
19027   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19028      no longer valid.  Note that if we are parsing tentatively, and
19029      the parse fails, OBJECT_TYPE will be automatically restored.  */
19030   parser->context->object_type = NULL_TREE;
19031
19032   if (name == error_mark_node)
19033     return error_mark_node;
19034
19035   /* A template-id has already been resolved; there is no lookup to
19036      do.  */
19037   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19038     return name;
19039   if (BASELINK_P (name))
19040     {
19041       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19042                   == TEMPLATE_ID_EXPR);
19043       return name;
19044     }
19045
19046   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19047      it should already have been checked to make sure that the name
19048      used matches the type being destroyed.  */
19049   if (TREE_CODE (name) == BIT_NOT_EXPR)
19050     {
19051       tree type;
19052
19053       /* Figure out to which type this destructor applies.  */
19054       if (parser->scope)
19055         type = parser->scope;
19056       else if (object_type)
19057         type = object_type;
19058       else
19059         type = current_class_type;
19060       /* If that's not a class type, there is no destructor.  */
19061       if (!type || !CLASS_TYPE_P (type))
19062         return error_mark_node;
19063       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19064         lazily_declare_fn (sfk_destructor, type);
19065       if (!CLASSTYPE_DESTRUCTORS (type))
19066           return error_mark_node;
19067       /* If it was a class type, return the destructor.  */
19068       return CLASSTYPE_DESTRUCTORS (type);
19069     }
19070
19071   /* By this point, the NAME should be an ordinary identifier.  If
19072      the id-expression was a qualified name, the qualifying scope is
19073      stored in PARSER->SCOPE at this point.  */
19074   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19075
19076   /* Perform the lookup.  */
19077   if (parser->scope)
19078     {
19079       bool dependent_p;
19080
19081       if (parser->scope == error_mark_node)
19082         return error_mark_node;
19083
19084       /* If the SCOPE is dependent, the lookup must be deferred until
19085          the template is instantiated -- unless we are explicitly
19086          looking up names in uninstantiated templates.  Even then, we
19087          cannot look up the name if the scope is not a class type; it
19088          might, for example, be a template type parameter.  */
19089       dependent_p = (TYPE_P (parser->scope)
19090                      && dependent_scope_p (parser->scope));
19091       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19092           && dependent_p)
19093         /* Defer lookup.  */
19094         decl = error_mark_node;
19095       else
19096         {
19097           tree pushed_scope = NULL_TREE;
19098
19099           /* If PARSER->SCOPE is a dependent type, then it must be a
19100              class type, and we must not be checking dependencies;
19101              otherwise, we would have processed this lookup above.  So
19102              that PARSER->SCOPE is not considered a dependent base by
19103              lookup_member, we must enter the scope here.  */
19104           if (dependent_p)
19105             pushed_scope = push_scope (parser->scope);
19106
19107           /* If the PARSER->SCOPE is a template specialization, it
19108              may be instantiated during name lookup.  In that case,
19109              errors may be issued.  Even if we rollback the current
19110              tentative parse, those errors are valid.  */
19111           decl = lookup_qualified_name (parser->scope, name,
19112                                         tag_type != none_type,
19113                                         /*complain=*/true);
19114
19115           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19116              lookup result and the nested-name-specifier nominates a class C:
19117                * if the name specified after the nested-name-specifier, when
19118                looked up in C, is the injected-class-name of C (Clause 9), or
19119                * if the name specified after the nested-name-specifier is the
19120                same as the identifier or the simple-template-id's template-
19121                name in the last component of the nested-name-specifier,
19122              the name is instead considered to name the constructor of
19123              class C. [ Note: for example, the constructor is not an
19124              acceptable lookup result in an elaborated-type-specifier so
19125              the constructor would not be used in place of the
19126              injected-class-name. --end note ] Such a constructor name
19127              shall be used only in the declarator-id of a declaration that
19128              names a constructor or in a using-declaration.  */
19129           if (tag_type == none_type
19130               && DECL_SELF_REFERENCE_P (decl)
19131               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19132             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19133                                           tag_type != none_type,
19134                                           /*complain=*/true);
19135
19136           /* If we have a single function from a using decl, pull it out.  */
19137           if (TREE_CODE (decl) == OVERLOAD
19138               && !really_overloaded_fn (decl))
19139             decl = OVL_FUNCTION (decl);
19140
19141           if (pushed_scope)
19142             pop_scope (pushed_scope);
19143         }
19144
19145       /* If the scope is a dependent type and either we deferred lookup or
19146          we did lookup but didn't find the name, rememeber the name.  */
19147       if (decl == error_mark_node && TYPE_P (parser->scope)
19148           && dependent_type_p (parser->scope))
19149         {
19150           if (tag_type)
19151             {
19152               tree type;
19153
19154               /* The resolution to Core Issue 180 says that `struct
19155                  A::B' should be considered a type-name, even if `A'
19156                  is dependent.  */
19157               type = make_typename_type (parser->scope, name, tag_type,
19158                                          /*complain=*/tf_error);
19159               decl = TYPE_NAME (type);
19160             }
19161           else if (is_template
19162                    && (cp_parser_next_token_ends_template_argument_p (parser)
19163                        || cp_lexer_next_token_is (parser->lexer,
19164                                                   CPP_CLOSE_PAREN)))
19165             decl = make_unbound_class_template (parser->scope,
19166                                                 name, NULL_TREE,
19167                                                 /*complain=*/tf_error);
19168           else
19169             decl = build_qualified_name (/*type=*/NULL_TREE,
19170                                          parser->scope, name,
19171                                          is_template);
19172         }
19173       parser->qualifying_scope = parser->scope;
19174       parser->object_scope = NULL_TREE;
19175     }
19176   else if (object_type)
19177     {
19178       tree object_decl = NULL_TREE;
19179       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19180          OBJECT_TYPE is not a class.  */
19181       if (CLASS_TYPE_P (object_type))
19182         /* If the OBJECT_TYPE is a template specialization, it may
19183            be instantiated during name lookup.  In that case, errors
19184            may be issued.  Even if we rollback the current tentative
19185            parse, those errors are valid.  */
19186         object_decl = lookup_member (object_type,
19187                                      name,
19188                                      /*protect=*/0,
19189                                      tag_type != none_type);
19190       /* Look it up in the enclosing context, too.  */
19191       decl = lookup_name_real (name, tag_type != none_type,
19192                                /*nonclass=*/0,
19193                                /*block_p=*/true, is_namespace, flags);
19194       parser->object_scope = object_type;
19195       parser->qualifying_scope = NULL_TREE;
19196       if (object_decl)
19197         decl = object_decl;
19198     }
19199   else
19200     {
19201       decl = lookup_name_real (name, tag_type != none_type,
19202                                /*nonclass=*/0,
19203                                /*block_p=*/true, is_namespace, flags);
19204       parser->qualifying_scope = NULL_TREE;
19205       parser->object_scope = NULL_TREE;
19206     }
19207
19208   /* If the lookup failed, let our caller know.  */
19209   if (!decl || decl == error_mark_node)
19210     return error_mark_node;
19211
19212   /* Pull out the template from an injected-class-name (or multiple).  */
19213   if (is_template)
19214     decl = maybe_get_template_decl_from_type_decl (decl);
19215
19216   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19217   if (TREE_CODE (decl) == TREE_LIST)
19218     {
19219       if (ambiguous_decls)
19220         *ambiguous_decls = decl;
19221       /* The error message we have to print is too complicated for
19222          cp_parser_error, so we incorporate its actions directly.  */
19223       if (!cp_parser_simulate_error (parser))
19224         {
19225           error_at (name_location, "reference to %qD is ambiguous",
19226                     name);
19227           print_candidates (decl);
19228         }
19229       return error_mark_node;
19230     }
19231
19232   gcc_assert (DECL_P (decl)
19233               || TREE_CODE (decl) == OVERLOAD
19234               || TREE_CODE (decl) == SCOPE_REF
19235               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19236               || BASELINK_P (decl));
19237
19238   /* If we have resolved the name of a member declaration, check to
19239      see if the declaration is accessible.  When the name resolves to
19240      set of overloaded functions, accessibility is checked when
19241      overload resolution is done.
19242
19243      During an explicit instantiation, access is not checked at all,
19244      as per [temp.explicit].  */
19245   if (DECL_P (decl))
19246     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19247
19248   return decl;
19249 }
19250
19251 /* Like cp_parser_lookup_name, but for use in the typical case where
19252    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19253    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19254
19255 static tree
19256 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19257 {
19258   return cp_parser_lookup_name (parser, name,
19259                                 none_type,
19260                                 /*is_template=*/false,
19261                                 /*is_namespace=*/false,
19262                                 /*check_dependency=*/true,
19263                                 /*ambiguous_decls=*/NULL,
19264                                 location);
19265 }
19266
19267 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19268    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19269    true, the DECL indicates the class being defined in a class-head,
19270    or declared in an elaborated-type-specifier.
19271
19272    Otherwise, return DECL.  */
19273
19274 static tree
19275 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19276 {
19277   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19278      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19279
19280        struct A {
19281          template <typename T> struct B;
19282        };
19283
19284        template <typename T> struct A::B {};
19285
19286      Similarly, in an elaborated-type-specifier:
19287
19288        namespace N { struct X{}; }
19289
19290        struct A {
19291          template <typename T> friend struct N::X;
19292        };
19293
19294      However, if the DECL refers to a class type, and we are in
19295      the scope of the class, then the name lookup automatically
19296      finds the TYPE_DECL created by build_self_reference rather
19297      than a TEMPLATE_DECL.  For example, in:
19298
19299        template <class T> struct S {
19300          S s;
19301        };
19302
19303      there is no need to handle such case.  */
19304
19305   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19306     return DECL_TEMPLATE_RESULT (decl);
19307
19308   return decl;
19309 }
19310
19311 /* If too many, or too few, template-parameter lists apply to the
19312    declarator, issue an error message.  Returns TRUE if all went well,
19313    and FALSE otherwise.  */
19314
19315 static bool
19316 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19317                                                 cp_declarator *declarator,
19318                                                 location_t declarator_location)
19319 {
19320   unsigned num_templates;
19321
19322   /* We haven't seen any classes that involve template parameters yet.  */
19323   num_templates = 0;
19324
19325   switch (declarator->kind)
19326     {
19327     case cdk_id:
19328       if (declarator->u.id.qualifying_scope)
19329         {
19330           tree scope;
19331
19332           scope = declarator->u.id.qualifying_scope;
19333
19334           while (scope && CLASS_TYPE_P (scope))
19335             {
19336               /* You're supposed to have one `template <...>'
19337                  for every template class, but you don't need one
19338                  for a full specialization.  For example:
19339
19340                  template <class T> struct S{};
19341                  template <> struct S<int> { void f(); };
19342                  void S<int>::f () {}
19343
19344                  is correct; there shouldn't be a `template <>' for
19345                  the definition of `S<int>::f'.  */
19346               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19347                 /* If SCOPE does not have template information of any
19348                    kind, then it is not a template, nor is it nested
19349                    within a template.  */
19350                 break;
19351               if (explicit_class_specialization_p (scope))
19352                 break;
19353               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19354                 ++num_templates;
19355
19356               scope = TYPE_CONTEXT (scope);
19357             }
19358         }
19359       else if (TREE_CODE (declarator->u.id.unqualified_name)
19360                == TEMPLATE_ID_EXPR)
19361         /* If the DECLARATOR has the form `X<y>' then it uses one
19362            additional level of template parameters.  */
19363         ++num_templates;
19364
19365       return cp_parser_check_template_parameters 
19366         (parser, num_templates, declarator_location, declarator);
19367
19368
19369     case cdk_function:
19370     case cdk_array:
19371     case cdk_pointer:
19372     case cdk_reference:
19373     case cdk_ptrmem:
19374       return (cp_parser_check_declarator_template_parameters
19375               (parser, declarator->declarator, declarator_location));
19376
19377     case cdk_error:
19378       return true;
19379
19380     default:
19381       gcc_unreachable ();
19382     }
19383   return false;
19384 }
19385
19386 /* NUM_TEMPLATES were used in the current declaration.  If that is
19387    invalid, return FALSE and issue an error messages.  Otherwise,
19388    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19389    declarator and we can print more accurate diagnostics.  */
19390
19391 static bool
19392 cp_parser_check_template_parameters (cp_parser* parser,
19393                                      unsigned num_templates,
19394                                      location_t location,
19395                                      cp_declarator *declarator)
19396 {
19397   /* If there are the same number of template classes and parameter
19398      lists, that's OK.  */
19399   if (parser->num_template_parameter_lists == num_templates)
19400     return true;
19401   /* If there are more, but only one more, then we are referring to a
19402      member template.  That's OK too.  */
19403   if (parser->num_template_parameter_lists == num_templates + 1)
19404     return true;
19405   /* If there are more template classes than parameter lists, we have
19406      something like:
19407
19408        template <class T> void S<T>::R<T>::f ();  */
19409   if (parser->num_template_parameter_lists < num_templates)
19410     {
19411       if (declarator && !current_function_decl)
19412         error_at (location, "specializing member %<%T::%E%> "
19413                   "requires %<template<>%> syntax", 
19414                   declarator->u.id.qualifying_scope,
19415                   declarator->u.id.unqualified_name);
19416       else if (declarator)
19417         error_at (location, "invalid declaration of %<%T::%E%>",
19418                   declarator->u.id.qualifying_scope,
19419                   declarator->u.id.unqualified_name);
19420       else 
19421         error_at (location, "too few template-parameter-lists");
19422       return false;
19423     }
19424   /* Otherwise, there are too many template parameter lists.  We have
19425      something like:
19426
19427      template <class T> template <class U> void S::f();  */
19428   error_at (location, "too many template-parameter-lists");
19429   return false;
19430 }
19431
19432 /* Parse an optional `::' token indicating that the following name is
19433    from the global namespace.  If so, PARSER->SCOPE is set to the
19434    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19435    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19436    Returns the new value of PARSER->SCOPE, if the `::' token is
19437    present, and NULL_TREE otherwise.  */
19438
19439 static tree
19440 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19441 {
19442   cp_token *token;
19443
19444   /* Peek at the next token.  */
19445   token = cp_lexer_peek_token (parser->lexer);
19446   /* If we're looking at a `::' token then we're starting from the
19447      global namespace, not our current location.  */
19448   if (token->type == CPP_SCOPE)
19449     {
19450       /* Consume the `::' token.  */
19451       cp_lexer_consume_token (parser->lexer);
19452       /* Set the SCOPE so that we know where to start the lookup.  */
19453       parser->scope = global_namespace;
19454       parser->qualifying_scope = global_namespace;
19455       parser->object_scope = NULL_TREE;
19456
19457       return parser->scope;
19458     }
19459   else if (!current_scope_valid_p)
19460     {
19461       parser->scope = NULL_TREE;
19462       parser->qualifying_scope = NULL_TREE;
19463       parser->object_scope = NULL_TREE;
19464     }
19465
19466   return NULL_TREE;
19467 }
19468
19469 /* Returns TRUE if the upcoming token sequence is the start of a
19470    constructor declarator.  If FRIEND_P is true, the declarator is
19471    preceded by the `friend' specifier.  */
19472
19473 static bool
19474 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19475 {
19476   bool constructor_p;
19477   tree nested_name_specifier;
19478   cp_token *next_token;
19479
19480   /* The common case is that this is not a constructor declarator, so
19481      try to avoid doing lots of work if at all possible.  It's not
19482      valid declare a constructor at function scope.  */
19483   if (parser->in_function_body)
19484     return false;
19485   /* And only certain tokens can begin a constructor declarator.  */
19486   next_token = cp_lexer_peek_token (parser->lexer);
19487   if (next_token->type != CPP_NAME
19488       && next_token->type != CPP_SCOPE
19489       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19490       && next_token->type != CPP_TEMPLATE_ID)
19491     return false;
19492
19493   /* Parse tentatively; we are going to roll back all of the tokens
19494      consumed here.  */
19495   cp_parser_parse_tentatively (parser);
19496   /* Assume that we are looking at a constructor declarator.  */
19497   constructor_p = true;
19498
19499   /* Look for the optional `::' operator.  */
19500   cp_parser_global_scope_opt (parser,
19501                               /*current_scope_valid_p=*/false);
19502   /* Look for the nested-name-specifier.  */
19503   nested_name_specifier
19504     = (cp_parser_nested_name_specifier_opt (parser,
19505                                             /*typename_keyword_p=*/false,
19506                                             /*check_dependency_p=*/false,
19507                                             /*type_p=*/false,
19508                                             /*is_declaration=*/false));
19509   /* Outside of a class-specifier, there must be a
19510      nested-name-specifier.  */
19511   if (!nested_name_specifier &&
19512       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19513        || friend_p))
19514     constructor_p = false;
19515   else if (nested_name_specifier == error_mark_node)
19516     constructor_p = false;
19517
19518   /* If we have a class scope, this is easy; DR 147 says that S::S always
19519      names the constructor, and no other qualified name could.  */
19520   if (constructor_p && nested_name_specifier
19521       && TYPE_P (nested_name_specifier))
19522     {
19523       tree id = cp_parser_unqualified_id (parser,
19524                                           /*template_keyword_p=*/false,
19525                                           /*check_dependency_p=*/false,
19526                                           /*declarator_p=*/true,
19527                                           /*optional_p=*/false);
19528       if (is_overloaded_fn (id))
19529         id = DECL_NAME (get_first_fn (id));
19530       if (!constructor_name_p (id, nested_name_specifier))
19531         constructor_p = false;
19532     }
19533   /* If we still think that this might be a constructor-declarator,
19534      look for a class-name.  */
19535   else if (constructor_p)
19536     {
19537       /* If we have:
19538
19539            template <typename T> struct S {
19540              S();
19541            };
19542
19543          we must recognize that the nested `S' names a class.  */
19544       tree type_decl;
19545       type_decl = cp_parser_class_name (parser,
19546                                         /*typename_keyword_p=*/false,
19547                                         /*template_keyword_p=*/false,
19548                                         none_type,
19549                                         /*check_dependency_p=*/false,
19550                                         /*class_head_p=*/false,
19551                                         /*is_declaration=*/false);
19552       /* If there was no class-name, then this is not a constructor.  */
19553       constructor_p = !cp_parser_error_occurred (parser);
19554
19555       /* If we're still considering a constructor, we have to see a `(',
19556          to begin the parameter-declaration-clause, followed by either a
19557          `)', an `...', or a decl-specifier.  We need to check for a
19558          type-specifier to avoid being fooled into thinking that:
19559
19560            S (f) (int);
19561
19562          is a constructor.  (It is actually a function named `f' that
19563          takes one parameter (of type `int') and returns a value of type
19564          `S'.  */
19565       if (constructor_p
19566           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19567         constructor_p = false;
19568
19569       if (constructor_p
19570           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19571           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19572           /* A parameter declaration begins with a decl-specifier,
19573              which is either the "attribute" keyword, a storage class
19574              specifier, or (usually) a type-specifier.  */
19575           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19576         {
19577           tree type;
19578           tree pushed_scope = NULL_TREE;
19579           unsigned saved_num_template_parameter_lists;
19580
19581           /* Names appearing in the type-specifier should be looked up
19582              in the scope of the class.  */
19583           if (current_class_type)
19584             type = NULL_TREE;
19585           else
19586             {
19587               type = TREE_TYPE (type_decl);
19588               if (TREE_CODE (type) == TYPENAME_TYPE)
19589                 {
19590                   type = resolve_typename_type (type,
19591                                                 /*only_current_p=*/false);
19592                   if (TREE_CODE (type) == TYPENAME_TYPE)
19593                     {
19594                       cp_parser_abort_tentative_parse (parser);
19595                       return false;
19596                     }
19597                 }
19598               pushed_scope = push_scope (type);
19599             }
19600
19601           /* Inside the constructor parameter list, surrounding
19602              template-parameter-lists do not apply.  */
19603           saved_num_template_parameter_lists
19604             = parser->num_template_parameter_lists;
19605           parser->num_template_parameter_lists = 0;
19606
19607           /* Look for the type-specifier.  */
19608           cp_parser_type_specifier (parser,
19609                                     CP_PARSER_FLAGS_NONE,
19610                                     /*decl_specs=*/NULL,
19611                                     /*is_declarator=*/true,
19612                                     /*declares_class_or_enum=*/NULL,
19613                                     /*is_cv_qualifier=*/NULL);
19614
19615           parser->num_template_parameter_lists
19616             = saved_num_template_parameter_lists;
19617
19618           /* Leave the scope of the class.  */
19619           if (pushed_scope)
19620             pop_scope (pushed_scope);
19621
19622           constructor_p = !cp_parser_error_occurred (parser);
19623         }
19624     }
19625
19626   /* We did not really want to consume any tokens.  */
19627   cp_parser_abort_tentative_parse (parser);
19628
19629   return constructor_p;
19630 }
19631
19632 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19633    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19634    they must be performed once we are in the scope of the function.
19635
19636    Returns the function defined.  */
19637
19638 static tree
19639 cp_parser_function_definition_from_specifiers_and_declarator
19640   (cp_parser* parser,
19641    cp_decl_specifier_seq *decl_specifiers,
19642    tree attributes,
19643    const cp_declarator *declarator)
19644 {
19645   tree fn;
19646   bool success_p;
19647
19648   /* Begin the function-definition.  */
19649   success_p = start_function (decl_specifiers, declarator, attributes);
19650
19651   /* The things we're about to see are not directly qualified by any
19652      template headers we've seen thus far.  */
19653   reset_specialization ();
19654
19655   /* If there were names looked up in the decl-specifier-seq that we
19656      did not check, check them now.  We must wait until we are in the
19657      scope of the function to perform the checks, since the function
19658      might be a friend.  */
19659   perform_deferred_access_checks ();
19660
19661   if (!success_p)
19662     {
19663       /* Skip the entire function.  */
19664       cp_parser_skip_to_end_of_block_or_statement (parser);
19665       fn = error_mark_node;
19666     }
19667   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19668     {
19669       /* Seen already, skip it.  An error message has already been output.  */
19670       cp_parser_skip_to_end_of_block_or_statement (parser);
19671       fn = current_function_decl;
19672       current_function_decl = NULL_TREE;
19673       /* If this is a function from a class, pop the nested class.  */
19674       if (current_class_name)
19675         pop_nested_class ();
19676     }
19677   else
19678     fn = cp_parser_function_definition_after_declarator (parser,
19679                                                          /*inline_p=*/false);
19680
19681   return fn;
19682 }
19683
19684 /* Parse the part of a function-definition that follows the
19685    declarator.  INLINE_P is TRUE iff this function is an inline
19686    function defined within a class-specifier.
19687
19688    Returns the function defined.  */
19689
19690 static tree
19691 cp_parser_function_definition_after_declarator (cp_parser* parser,
19692                                                 bool inline_p)
19693 {
19694   tree fn;
19695   bool ctor_initializer_p = false;
19696   bool saved_in_unbraced_linkage_specification_p;
19697   bool saved_in_function_body;
19698   unsigned saved_num_template_parameter_lists;
19699   cp_token *token;
19700
19701   saved_in_function_body = parser->in_function_body;
19702   parser->in_function_body = true;
19703   /* If the next token is `return', then the code may be trying to
19704      make use of the "named return value" extension that G++ used to
19705      support.  */
19706   token = cp_lexer_peek_token (parser->lexer);
19707   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19708     {
19709       /* Consume the `return' keyword.  */
19710       cp_lexer_consume_token (parser->lexer);
19711       /* Look for the identifier that indicates what value is to be
19712          returned.  */
19713       cp_parser_identifier (parser);
19714       /* Issue an error message.  */
19715       error_at (token->location,
19716                 "named return values are no longer supported");
19717       /* Skip tokens until we reach the start of the function body.  */
19718       while (true)
19719         {
19720           cp_token *token = cp_lexer_peek_token (parser->lexer);
19721           if (token->type == CPP_OPEN_BRACE
19722               || token->type == CPP_EOF
19723               || token->type == CPP_PRAGMA_EOL)
19724             break;
19725           cp_lexer_consume_token (parser->lexer);
19726         }
19727     }
19728   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19729      anything declared inside `f'.  */
19730   saved_in_unbraced_linkage_specification_p
19731     = parser->in_unbraced_linkage_specification_p;
19732   parser->in_unbraced_linkage_specification_p = false;
19733   /* Inside the function, surrounding template-parameter-lists do not
19734      apply.  */
19735   saved_num_template_parameter_lists
19736     = parser->num_template_parameter_lists;
19737   parser->num_template_parameter_lists = 0;
19738
19739   start_lambda_scope (current_function_decl);
19740
19741   /* If the next token is `try', then we are looking at a
19742      function-try-block.  */
19743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19744     ctor_initializer_p = cp_parser_function_try_block (parser);
19745   /* A function-try-block includes the function-body, so we only do
19746      this next part if we're not processing a function-try-block.  */
19747   else
19748     ctor_initializer_p
19749       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19750
19751   finish_lambda_scope ();
19752
19753   /* Finish the function.  */
19754   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19755                         (inline_p ? 2 : 0));
19756   /* Generate code for it, if necessary.  */
19757   expand_or_defer_fn (fn);
19758   /* Restore the saved values.  */
19759   parser->in_unbraced_linkage_specification_p
19760     = saved_in_unbraced_linkage_specification_p;
19761   parser->num_template_parameter_lists
19762     = saved_num_template_parameter_lists;
19763   parser->in_function_body = saved_in_function_body;
19764
19765   return fn;
19766 }
19767
19768 /* Parse a template-declaration, assuming that the `export' (and
19769    `extern') keywords, if present, has already been scanned.  MEMBER_P
19770    is as for cp_parser_template_declaration.  */
19771
19772 static void
19773 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19774 {
19775   tree decl = NULL_TREE;
19776   VEC (deferred_access_check,gc) *checks;
19777   tree parameter_list;
19778   bool friend_p = false;
19779   bool need_lang_pop;
19780   cp_token *token;
19781
19782   /* Look for the `template' keyword.  */
19783   token = cp_lexer_peek_token (parser->lexer);
19784   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19785     return;
19786
19787   /* And the `<'.  */
19788   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19789     return;
19790   if (at_class_scope_p () && current_function_decl)
19791     {
19792       /* 14.5.2.2 [temp.mem]
19793
19794          A local class shall not have member templates.  */
19795       error_at (token->location,
19796                 "invalid declaration of member template in local class");
19797       cp_parser_skip_to_end_of_block_or_statement (parser);
19798       return;
19799     }
19800   /* [temp]
19801
19802      A template ... shall not have C linkage.  */
19803   if (current_lang_name == lang_name_c)
19804     {
19805       error_at (token->location, "template with C linkage");
19806       /* Give it C++ linkage to avoid confusing other parts of the
19807          front end.  */
19808       push_lang_context (lang_name_cplusplus);
19809       need_lang_pop = true;
19810     }
19811   else
19812     need_lang_pop = false;
19813
19814   /* We cannot perform access checks on the template parameter
19815      declarations until we know what is being declared, just as we
19816      cannot check the decl-specifier list.  */
19817   push_deferring_access_checks (dk_deferred);
19818
19819   /* If the next token is `>', then we have an invalid
19820      specialization.  Rather than complain about an invalid template
19821      parameter, issue an error message here.  */
19822   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19823     {
19824       cp_parser_error (parser, "invalid explicit specialization");
19825       begin_specialization ();
19826       parameter_list = NULL_TREE;
19827     }
19828   else
19829     /* Parse the template parameters.  */
19830     parameter_list = cp_parser_template_parameter_list (parser);
19831
19832   /* Get the deferred access checks from the parameter list.  These
19833      will be checked once we know what is being declared, as for a
19834      member template the checks must be performed in the scope of the
19835      class containing the member.  */
19836   checks = get_deferred_access_checks ();
19837
19838   /* Look for the `>'.  */
19839   cp_parser_skip_to_end_of_template_parameter_list (parser);
19840   /* We just processed one more parameter list.  */
19841   ++parser->num_template_parameter_lists;
19842   /* If the next token is `template', there are more template
19843      parameters.  */
19844   if (cp_lexer_next_token_is_keyword (parser->lexer,
19845                                       RID_TEMPLATE))
19846     cp_parser_template_declaration_after_export (parser, member_p);
19847   else
19848     {
19849       /* There are no access checks when parsing a template, as we do not
19850          know if a specialization will be a friend.  */
19851       push_deferring_access_checks (dk_no_check);
19852       token = cp_lexer_peek_token (parser->lexer);
19853       decl = cp_parser_single_declaration (parser,
19854                                            checks,
19855                                            member_p,
19856                                            /*explicit_specialization_p=*/false,
19857                                            &friend_p);
19858       pop_deferring_access_checks ();
19859
19860       /* If this is a member template declaration, let the front
19861          end know.  */
19862       if (member_p && !friend_p && decl)
19863         {
19864           if (TREE_CODE (decl) == TYPE_DECL)
19865             cp_parser_check_access_in_redeclaration (decl, token->location);
19866
19867           decl = finish_member_template_decl (decl);
19868         }
19869       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19870         make_friend_class (current_class_type, TREE_TYPE (decl),
19871                            /*complain=*/true);
19872     }
19873   /* We are done with the current parameter list.  */
19874   --parser->num_template_parameter_lists;
19875
19876   pop_deferring_access_checks ();
19877
19878   /* Finish up.  */
19879   finish_template_decl (parameter_list);
19880
19881   /* Register member declarations.  */
19882   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19883     finish_member_declaration (decl);
19884   /* For the erroneous case of a template with C linkage, we pushed an
19885      implicit C++ linkage scope; exit that scope now.  */
19886   if (need_lang_pop)
19887     pop_lang_context ();
19888   /* If DECL is a function template, we must return to parse it later.
19889      (Even though there is no definition, there might be default
19890      arguments that need handling.)  */
19891   if (member_p && decl
19892       && (TREE_CODE (decl) == FUNCTION_DECL
19893           || DECL_FUNCTION_TEMPLATE_P (decl)))
19894     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19895 }
19896
19897 /* Perform the deferred access checks from a template-parameter-list.
19898    CHECKS is a TREE_LIST of access checks, as returned by
19899    get_deferred_access_checks.  */
19900
19901 static void
19902 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19903 {
19904   ++processing_template_parmlist;
19905   perform_access_checks (checks);
19906   --processing_template_parmlist;
19907 }
19908
19909 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19910    `function-definition' sequence.  MEMBER_P is true, this declaration
19911    appears in a class scope.
19912
19913    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19914    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19915
19916 static tree
19917 cp_parser_single_declaration (cp_parser* parser,
19918                               VEC (deferred_access_check,gc)* checks,
19919                               bool member_p,
19920                               bool explicit_specialization_p,
19921                               bool* friend_p)
19922 {
19923   int declares_class_or_enum;
19924   tree decl = NULL_TREE;
19925   cp_decl_specifier_seq decl_specifiers;
19926   bool function_definition_p = false;
19927   cp_token *decl_spec_token_start;
19928
19929   /* This function is only used when processing a template
19930      declaration.  */
19931   gcc_assert (innermost_scope_kind () == sk_template_parms
19932               || innermost_scope_kind () == sk_template_spec);
19933
19934   /* Defer access checks until we know what is being declared.  */
19935   push_deferring_access_checks (dk_deferred);
19936
19937   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19938      alternative.  */
19939   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19940   cp_parser_decl_specifier_seq (parser,
19941                                 CP_PARSER_FLAGS_OPTIONAL,
19942                                 &decl_specifiers,
19943                                 &declares_class_or_enum);
19944   if (friend_p)
19945     *friend_p = cp_parser_friend_p (&decl_specifiers);
19946
19947   /* There are no template typedefs.  */
19948   if (decl_specifiers.specs[(int) ds_typedef])
19949     {
19950       error_at (decl_spec_token_start->location,
19951                 "template declaration of %<typedef%>");
19952       decl = error_mark_node;
19953     }
19954
19955   /* Gather up the access checks that occurred the
19956      decl-specifier-seq.  */
19957   stop_deferring_access_checks ();
19958
19959   /* Check for the declaration of a template class.  */
19960   if (declares_class_or_enum)
19961     {
19962       if (cp_parser_declares_only_class_p (parser))
19963         {
19964           decl = shadow_tag (&decl_specifiers);
19965
19966           /* In this case:
19967
19968                struct C {
19969                  friend template <typename T> struct A<T>::B;
19970                };
19971
19972              A<T>::B will be represented by a TYPENAME_TYPE, and
19973              therefore not recognized by shadow_tag.  */
19974           if (friend_p && *friend_p
19975               && !decl
19976               && decl_specifiers.type
19977               && TYPE_P (decl_specifiers.type))
19978             decl = decl_specifiers.type;
19979
19980           if (decl && decl != error_mark_node)
19981             decl = TYPE_NAME (decl);
19982           else
19983             decl = error_mark_node;
19984
19985           /* Perform access checks for template parameters.  */
19986           cp_parser_perform_template_parameter_access_checks (checks);
19987         }
19988     }
19989
19990   /* Complain about missing 'typename' or other invalid type names.  */
19991   if (!decl_specifiers.any_type_specifiers_p)
19992     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19993
19994   /* If it's not a template class, try for a template function.  If
19995      the next token is a `;', then this declaration does not declare
19996      anything.  But, if there were errors in the decl-specifiers, then
19997      the error might well have come from an attempted class-specifier.
19998      In that case, there's no need to warn about a missing declarator.  */
19999   if (!decl
20000       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20001           || decl_specifiers.type != error_mark_node))
20002     {
20003       decl = cp_parser_init_declarator (parser,
20004                                         &decl_specifiers,
20005                                         checks,
20006                                         /*function_definition_allowed_p=*/true,
20007                                         member_p,
20008                                         declares_class_or_enum,
20009                                         &function_definition_p);
20010
20011     /* 7.1.1-1 [dcl.stc]
20012
20013        A storage-class-specifier shall not be specified in an explicit
20014        specialization...  */
20015     if (decl
20016         && explicit_specialization_p
20017         && decl_specifiers.storage_class != sc_none)
20018       {
20019         error_at (decl_spec_token_start->location,
20020                   "explicit template specialization cannot have a storage class");
20021         decl = error_mark_node;
20022       }
20023     }
20024
20025   pop_deferring_access_checks ();
20026
20027   /* Clear any current qualification; whatever comes next is the start
20028      of something new.  */
20029   parser->scope = NULL_TREE;
20030   parser->qualifying_scope = NULL_TREE;
20031   parser->object_scope = NULL_TREE;
20032   /* Look for a trailing `;' after the declaration.  */
20033   if (!function_definition_p
20034       && (decl == error_mark_node
20035           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20036     cp_parser_skip_to_end_of_block_or_statement (parser);
20037
20038   return decl;
20039 }
20040
20041 /* Parse a cast-expression that is not the operand of a unary "&".  */
20042
20043 static tree
20044 cp_parser_simple_cast_expression (cp_parser *parser)
20045 {
20046   return cp_parser_cast_expression (parser, /*address_p=*/false,
20047                                     /*cast_p=*/false, NULL);
20048 }
20049
20050 /* Parse a functional cast to TYPE.  Returns an expression
20051    representing the cast.  */
20052
20053 static tree
20054 cp_parser_functional_cast (cp_parser* parser, tree type)
20055 {
20056   VEC(tree,gc) *vec;
20057   tree expression_list;
20058   tree cast;
20059   bool nonconst_p;
20060
20061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20062     {
20063       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20064       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20065       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20066       if (TREE_CODE (type) == TYPE_DECL)
20067         type = TREE_TYPE (type);
20068       return finish_compound_literal (type, expression_list);
20069     }
20070
20071
20072   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20073                                                  /*cast_p=*/true,
20074                                                  /*allow_expansion_p=*/true,
20075                                                  /*non_constant_p=*/NULL);
20076   if (vec == NULL)
20077     expression_list = error_mark_node;
20078   else
20079     {
20080       expression_list = build_tree_list_vec (vec);
20081       release_tree_vector (vec);
20082     }
20083
20084   cast = build_functional_cast (type, expression_list,
20085                                 tf_warning_or_error);
20086   /* [expr.const]/1: In an integral constant expression "only type
20087      conversions to integral or enumeration type can be used".  */
20088   if (TREE_CODE (type) == TYPE_DECL)
20089     type = TREE_TYPE (type);
20090   if (cast != error_mark_node
20091       && !cast_valid_in_integral_constant_expression_p (type)
20092       && cp_parser_non_integral_constant_expression (parser,
20093                                                      NIC_CONSTRUCTOR))
20094     return error_mark_node;
20095   return cast;
20096 }
20097
20098 /* Save the tokens that make up the body of a member function defined
20099    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20100    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20101    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20102    for the member function.  */
20103
20104 static tree
20105 cp_parser_save_member_function_body (cp_parser* parser,
20106                                      cp_decl_specifier_seq *decl_specifiers,
20107                                      cp_declarator *declarator,
20108                                      tree attributes)
20109 {
20110   cp_token *first;
20111   cp_token *last;
20112   tree fn;
20113
20114   /* Create the FUNCTION_DECL.  */
20115   fn = grokmethod (decl_specifiers, declarator, attributes);
20116   /* If something went badly wrong, bail out now.  */
20117   if (fn == error_mark_node)
20118     {
20119       /* If there's a function-body, skip it.  */
20120       if (cp_parser_token_starts_function_definition_p
20121           (cp_lexer_peek_token (parser->lexer)))
20122         cp_parser_skip_to_end_of_block_or_statement (parser);
20123       return error_mark_node;
20124     }
20125
20126   /* Remember it, if there default args to post process.  */
20127   cp_parser_save_default_args (parser, fn);
20128
20129   /* Save away the tokens that make up the body of the
20130      function.  */
20131   first = parser->lexer->next_token;
20132   /* We can have braced-init-list mem-initializers before the fn body.  */
20133   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20134     {
20135       cp_lexer_consume_token (parser->lexer);
20136       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20137              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20138         {
20139           /* cache_group will stop after an un-nested { } pair, too.  */
20140           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20141             break;
20142
20143           /* variadic mem-inits have ... after the ')'.  */
20144           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20145             cp_lexer_consume_token (parser->lexer);
20146         }
20147     }
20148   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20149   /* Handle function try blocks.  */
20150   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20151     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20152   last = parser->lexer->next_token;
20153
20154   /* Save away the inline definition; we will process it when the
20155      class is complete.  */
20156   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20157   DECL_PENDING_INLINE_P (fn) = 1;
20158
20159   /* We need to know that this was defined in the class, so that
20160      friend templates are handled correctly.  */
20161   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20162
20163   /* Add FN to the queue of functions to be parsed later.  */
20164   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20165
20166   return fn;
20167 }
20168
20169 /* Parse a template-argument-list, as well as the trailing ">" (but
20170    not the opening ">").  See cp_parser_template_argument_list for the
20171    return value.  */
20172
20173 static tree
20174 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20175 {
20176   tree arguments;
20177   tree saved_scope;
20178   tree saved_qualifying_scope;
20179   tree saved_object_scope;
20180   bool saved_greater_than_is_operator_p;
20181   int saved_unevaluated_operand;
20182   int saved_inhibit_evaluation_warnings;
20183
20184   /* [temp.names]
20185
20186      When parsing a template-id, the first non-nested `>' is taken as
20187      the end of the template-argument-list rather than a greater-than
20188      operator.  */
20189   saved_greater_than_is_operator_p
20190     = parser->greater_than_is_operator_p;
20191   parser->greater_than_is_operator_p = false;
20192   /* Parsing the argument list may modify SCOPE, so we save it
20193      here.  */
20194   saved_scope = parser->scope;
20195   saved_qualifying_scope = parser->qualifying_scope;
20196   saved_object_scope = parser->object_scope;
20197   /* We need to evaluate the template arguments, even though this
20198      template-id may be nested within a "sizeof".  */
20199   saved_unevaluated_operand = cp_unevaluated_operand;
20200   cp_unevaluated_operand = 0;
20201   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20202   c_inhibit_evaluation_warnings = 0;
20203   /* Parse the template-argument-list itself.  */
20204   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20205       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20206     arguments = NULL_TREE;
20207   else
20208     arguments = cp_parser_template_argument_list (parser);
20209   /* Look for the `>' that ends the template-argument-list. If we find
20210      a '>>' instead, it's probably just a typo.  */
20211   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20212     {
20213       if (cxx_dialect != cxx98)
20214         {
20215           /* In C++0x, a `>>' in a template argument list or cast
20216              expression is considered to be two separate `>'
20217              tokens. So, change the current token to a `>', but don't
20218              consume it: it will be consumed later when the outer
20219              template argument list (or cast expression) is parsed.
20220              Note that this replacement of `>' for `>>' is necessary
20221              even if we are parsing tentatively: in the tentative
20222              case, after calling
20223              cp_parser_enclosed_template_argument_list we will always
20224              throw away all of the template arguments and the first
20225              closing `>', either because the template argument list
20226              was erroneous or because we are replacing those tokens
20227              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20228              not have been thrown away) is needed either to close an
20229              outer template argument list or to complete a new-style
20230              cast.  */
20231           cp_token *token = cp_lexer_peek_token (parser->lexer);
20232           token->type = CPP_GREATER;
20233         }
20234       else if (!saved_greater_than_is_operator_p)
20235         {
20236           /* If we're in a nested template argument list, the '>>' has
20237             to be a typo for '> >'. We emit the error message, but we
20238             continue parsing and we push a '>' as next token, so that
20239             the argument list will be parsed correctly.  Note that the
20240             global source location is still on the token before the
20241             '>>', so we need to say explicitly where we want it.  */
20242           cp_token *token = cp_lexer_peek_token (parser->lexer);
20243           error_at (token->location, "%<>>%> should be %<> >%> "
20244                     "within a nested template argument list");
20245
20246           token->type = CPP_GREATER;
20247         }
20248       else
20249         {
20250           /* If this is not a nested template argument list, the '>>'
20251             is a typo for '>'. Emit an error message and continue.
20252             Same deal about the token location, but here we can get it
20253             right by consuming the '>>' before issuing the diagnostic.  */
20254           cp_token *token = cp_lexer_consume_token (parser->lexer);
20255           error_at (token->location,
20256                     "spurious %<>>%>, use %<>%> to terminate "
20257                     "a template argument list");
20258         }
20259     }
20260   else
20261     cp_parser_skip_to_end_of_template_parameter_list (parser);
20262   /* The `>' token might be a greater-than operator again now.  */
20263   parser->greater_than_is_operator_p
20264     = saved_greater_than_is_operator_p;
20265   /* Restore the SAVED_SCOPE.  */
20266   parser->scope = saved_scope;
20267   parser->qualifying_scope = saved_qualifying_scope;
20268   parser->object_scope = saved_object_scope;
20269   cp_unevaluated_operand = saved_unevaluated_operand;
20270   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20271
20272   return arguments;
20273 }
20274
20275 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20276    arguments, or the body of the function have not yet been parsed,
20277    parse them now.  */
20278
20279 static void
20280 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20281 {
20282   /* If this member is a template, get the underlying
20283      FUNCTION_DECL.  */
20284   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20285     member_function = DECL_TEMPLATE_RESULT (member_function);
20286
20287   /* There should not be any class definitions in progress at this
20288      point; the bodies of members are only parsed outside of all class
20289      definitions.  */
20290   gcc_assert (parser->num_classes_being_defined == 0);
20291   /* While we're parsing the member functions we might encounter more
20292      classes.  We want to handle them right away, but we don't want
20293      them getting mixed up with functions that are currently in the
20294      queue.  */
20295   push_unparsed_function_queues (parser);
20296
20297   /* Make sure that any template parameters are in scope.  */
20298   maybe_begin_member_template_processing (member_function);
20299
20300   /* If the body of the function has not yet been parsed, parse it
20301      now.  */
20302   if (DECL_PENDING_INLINE_P (member_function))
20303     {
20304       tree function_scope;
20305       cp_token_cache *tokens;
20306
20307       /* The function is no longer pending; we are processing it.  */
20308       tokens = DECL_PENDING_INLINE_INFO (member_function);
20309       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20310       DECL_PENDING_INLINE_P (member_function) = 0;
20311
20312       /* If this is a local class, enter the scope of the containing
20313          function.  */
20314       function_scope = current_function_decl;
20315       if (function_scope)
20316         push_function_context ();
20317
20318       /* Push the body of the function onto the lexer stack.  */
20319       cp_parser_push_lexer_for_tokens (parser, tokens);
20320
20321       /* Let the front end know that we going to be defining this
20322          function.  */
20323       start_preparsed_function (member_function, NULL_TREE,
20324                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20325
20326       /* Don't do access checking if it is a templated function.  */
20327       if (processing_template_decl)
20328         push_deferring_access_checks (dk_no_check);
20329
20330       /* Now, parse the body of the function.  */
20331       cp_parser_function_definition_after_declarator (parser,
20332                                                       /*inline_p=*/true);
20333
20334       if (processing_template_decl)
20335         pop_deferring_access_checks ();
20336
20337       /* Leave the scope of the containing function.  */
20338       if (function_scope)
20339         pop_function_context ();
20340       cp_parser_pop_lexer (parser);
20341     }
20342
20343   /* Remove any template parameters from the symbol table.  */
20344   maybe_end_member_template_processing ();
20345
20346   /* Restore the queue.  */
20347   pop_unparsed_function_queues (parser);
20348 }
20349
20350 /* If DECL contains any default args, remember it on the unparsed
20351    functions queue.  */
20352
20353 static void
20354 cp_parser_save_default_args (cp_parser* parser, tree decl)
20355 {
20356   tree probe;
20357
20358   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20359        probe;
20360        probe = TREE_CHAIN (probe))
20361     if (TREE_PURPOSE (probe))
20362       {
20363         cp_default_arg_entry *entry
20364           = VEC_safe_push (cp_default_arg_entry, gc,
20365                            unparsed_funs_with_default_args, NULL);
20366         entry->class_type = current_class_type;
20367         entry->decl = decl;
20368         break;
20369       }
20370 }
20371
20372 /* FN is a FUNCTION_DECL which may contains a parameter with an
20373    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20374    assumes that the current scope is the scope in which the default
20375    argument should be processed.  */
20376
20377 static void
20378 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20379 {
20380   bool saved_local_variables_forbidden_p;
20381   tree parm, parmdecl;
20382
20383   /* While we're parsing the default args, we might (due to the
20384      statement expression extension) encounter more classes.  We want
20385      to handle them right away, but we don't want them getting mixed
20386      up with default args that are currently in the queue.  */
20387   push_unparsed_function_queues (parser);
20388
20389   /* Local variable names (and the `this' keyword) may not appear
20390      in a default argument.  */
20391   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20392   parser->local_variables_forbidden_p = true;
20393
20394   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20395          parmdecl = DECL_ARGUMENTS (fn);
20396        parm && parm != void_list_node;
20397        parm = TREE_CHAIN (parm),
20398          parmdecl = DECL_CHAIN (parmdecl))
20399     {
20400       cp_token_cache *tokens;
20401       tree default_arg = TREE_PURPOSE (parm);
20402       tree parsed_arg;
20403       VEC(tree,gc) *insts;
20404       tree copy;
20405       unsigned ix;
20406
20407       if (!default_arg)
20408         continue;
20409
20410       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20411         /* This can happen for a friend declaration for a function
20412            already declared with default arguments.  */
20413         continue;
20414
20415        /* Push the saved tokens for the default argument onto the parser's
20416           lexer stack.  */
20417       tokens = DEFARG_TOKENS (default_arg);
20418       cp_parser_push_lexer_for_tokens (parser, tokens);
20419
20420       start_lambda_scope (parmdecl);
20421
20422       /* Parse the assignment-expression.  */
20423       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20424       if (parsed_arg == error_mark_node)
20425         {
20426           cp_parser_pop_lexer (parser);
20427           continue;
20428         }
20429
20430       if (!processing_template_decl)
20431         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20432
20433       TREE_PURPOSE (parm) = parsed_arg;
20434
20435       /* Update any instantiations we've already created.  */
20436       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20437            VEC_iterate (tree, insts, ix, copy); ix++)
20438         TREE_PURPOSE (copy) = parsed_arg;
20439
20440       finish_lambda_scope ();
20441
20442       /* If the token stream has not been completely used up, then
20443          there was extra junk after the end of the default
20444          argument.  */
20445       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20446         cp_parser_error (parser, "expected %<,%>");
20447
20448       /* Revert to the main lexer.  */
20449       cp_parser_pop_lexer (parser);
20450     }
20451
20452   /* Make sure no default arg is missing.  */
20453   check_default_args (fn);
20454
20455   /* Restore the state of local_variables_forbidden_p.  */
20456   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20457
20458   /* Restore the queue.  */
20459   pop_unparsed_function_queues (parser);
20460 }
20461
20462 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20463    either a TYPE or an expression, depending on the form of the
20464    input.  The KEYWORD indicates which kind of expression we have
20465    encountered.  */
20466
20467 static tree
20468 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20469 {
20470   tree expr = NULL_TREE;
20471   const char *saved_message;
20472   char *tmp;
20473   bool saved_integral_constant_expression_p;
20474   bool saved_non_integral_constant_expression_p;
20475   bool pack_expansion_p = false;
20476
20477   /* Types cannot be defined in a `sizeof' expression.  Save away the
20478      old message.  */
20479   saved_message = parser->type_definition_forbidden_message;
20480   /* And create the new one.  */
20481   tmp = concat ("types may not be defined in %<",
20482                 IDENTIFIER_POINTER (ridpointers[keyword]),
20483                 "%> expressions", NULL);
20484   parser->type_definition_forbidden_message = tmp;
20485
20486   /* The restrictions on constant-expressions do not apply inside
20487      sizeof expressions.  */
20488   saved_integral_constant_expression_p
20489     = parser->integral_constant_expression_p;
20490   saved_non_integral_constant_expression_p
20491     = parser->non_integral_constant_expression_p;
20492   parser->integral_constant_expression_p = false;
20493
20494   /* If it's a `...', then we are computing the length of a parameter
20495      pack.  */
20496   if (keyword == RID_SIZEOF
20497       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20498     {
20499       /* Consume the `...'.  */
20500       cp_lexer_consume_token (parser->lexer);
20501       maybe_warn_variadic_templates ();
20502
20503       /* Note that this is an expansion.  */
20504       pack_expansion_p = true;
20505     }
20506
20507   /* Do not actually evaluate the expression.  */
20508   ++cp_unevaluated_operand;
20509   ++c_inhibit_evaluation_warnings;
20510   /* If it's a `(', then we might be looking at the type-id
20511      construction.  */
20512   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20513     {
20514       tree type;
20515       bool saved_in_type_id_in_expr_p;
20516
20517       /* We can't be sure yet whether we're looking at a type-id or an
20518          expression.  */
20519       cp_parser_parse_tentatively (parser);
20520       /* Consume the `('.  */
20521       cp_lexer_consume_token (parser->lexer);
20522       /* Parse the type-id.  */
20523       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20524       parser->in_type_id_in_expr_p = true;
20525       type = cp_parser_type_id (parser);
20526       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20527       /* Now, look for the trailing `)'.  */
20528       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20529       /* If all went well, then we're done.  */
20530       if (cp_parser_parse_definitely (parser))
20531         {
20532           cp_decl_specifier_seq decl_specs;
20533
20534           /* Build a trivial decl-specifier-seq.  */
20535           clear_decl_specs (&decl_specs);
20536           decl_specs.type = type;
20537
20538           /* Call grokdeclarator to figure out what type this is.  */
20539           expr = grokdeclarator (NULL,
20540                                  &decl_specs,
20541                                  TYPENAME,
20542                                  /*initialized=*/0,
20543                                  /*attrlist=*/NULL);
20544         }
20545     }
20546
20547   /* If the type-id production did not work out, then we must be
20548      looking at the unary-expression production.  */
20549   if (!expr)
20550     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20551                                        /*cast_p=*/false, NULL);
20552
20553   if (pack_expansion_p)
20554     /* Build a pack expansion. */
20555     expr = make_pack_expansion (expr);
20556
20557   /* Go back to evaluating expressions.  */
20558   --cp_unevaluated_operand;
20559   --c_inhibit_evaluation_warnings;
20560
20561   /* Free the message we created.  */
20562   free (tmp);
20563   /* And restore the old one.  */
20564   parser->type_definition_forbidden_message = saved_message;
20565   parser->integral_constant_expression_p
20566     = saved_integral_constant_expression_p;
20567   parser->non_integral_constant_expression_p
20568     = saved_non_integral_constant_expression_p;
20569
20570   return expr;
20571 }
20572
20573 /* If the current declaration has no declarator, return true.  */
20574
20575 static bool
20576 cp_parser_declares_only_class_p (cp_parser *parser)
20577 {
20578   /* If the next token is a `;' or a `,' then there is no
20579      declarator.  */
20580   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20581           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20582 }
20583
20584 /* Update the DECL_SPECS to reflect the storage class indicated by
20585    KEYWORD.  */
20586
20587 static void
20588 cp_parser_set_storage_class (cp_parser *parser,
20589                              cp_decl_specifier_seq *decl_specs,
20590                              enum rid keyword,
20591                              location_t location)
20592 {
20593   cp_storage_class storage_class;
20594
20595   if (parser->in_unbraced_linkage_specification_p)
20596     {
20597       error_at (location, "invalid use of %qD in linkage specification",
20598                 ridpointers[keyword]);
20599       return;
20600     }
20601   else if (decl_specs->storage_class != sc_none)
20602     {
20603       decl_specs->conflicting_specifiers_p = true;
20604       return;
20605     }
20606
20607   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20608       && decl_specs->specs[(int) ds_thread])
20609     {
20610       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20611       decl_specs->specs[(int) ds_thread] = 0;
20612     }
20613
20614   switch (keyword)
20615     {
20616     case RID_AUTO:
20617       storage_class = sc_auto;
20618       break;
20619     case RID_REGISTER:
20620       storage_class = sc_register;
20621       break;
20622     case RID_STATIC:
20623       storage_class = sc_static;
20624       break;
20625     case RID_EXTERN:
20626       storage_class = sc_extern;
20627       break;
20628     case RID_MUTABLE:
20629       storage_class = sc_mutable;
20630       break;
20631     default:
20632       gcc_unreachable ();
20633     }
20634   decl_specs->storage_class = storage_class;
20635
20636   /* A storage class specifier cannot be applied alongside a typedef 
20637      specifier. If there is a typedef specifier present then set 
20638      conflicting_specifiers_p which will trigger an error later
20639      on in grokdeclarator. */
20640   if (decl_specs->specs[(int)ds_typedef])
20641     decl_specs->conflicting_specifiers_p = true;
20642 }
20643
20644 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20645    is true, the type is a user-defined type; otherwise it is a
20646    built-in type specified by a keyword.  */
20647
20648 static void
20649 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20650                               tree type_spec,
20651                               location_t location,
20652                               bool user_defined_p)
20653 {
20654   decl_specs->any_specifiers_p = true;
20655
20656   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20657      (with, for example, in "typedef int wchar_t;") we remember that
20658      this is what happened.  In system headers, we ignore these
20659      declarations so that G++ can work with system headers that are not
20660      C++-safe.  */
20661   if (decl_specs->specs[(int) ds_typedef]
20662       && !user_defined_p
20663       && (type_spec == boolean_type_node
20664           || type_spec == char16_type_node
20665           || type_spec == char32_type_node
20666           || type_spec == wchar_type_node)
20667       && (decl_specs->type
20668           || decl_specs->specs[(int) ds_long]
20669           || decl_specs->specs[(int) ds_short]
20670           || decl_specs->specs[(int) ds_unsigned]
20671           || decl_specs->specs[(int) ds_signed]))
20672     {
20673       decl_specs->redefined_builtin_type = type_spec;
20674       if (!decl_specs->type)
20675         {
20676           decl_specs->type = type_spec;
20677           decl_specs->user_defined_type_p = false;
20678           decl_specs->type_location = location;
20679         }
20680     }
20681   else if (decl_specs->type)
20682     decl_specs->multiple_types_p = true;
20683   else
20684     {
20685       decl_specs->type = type_spec;
20686       decl_specs->user_defined_type_p = user_defined_p;
20687       decl_specs->redefined_builtin_type = NULL_TREE;
20688       decl_specs->type_location = location;
20689     }
20690 }
20691
20692 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20693    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20694
20695 static bool
20696 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20697 {
20698   return decl_specifiers->specs[(int) ds_friend] != 0;
20699 }
20700
20701 /* Issue an error message indicating that TOKEN_DESC was expected.
20702    If KEYWORD is true, it indicated this function is called by
20703    cp_parser_require_keword and the required token can only be
20704    a indicated keyword. */
20705
20706 static void
20707 cp_parser_required_error (cp_parser *parser,
20708                           required_token token_desc,
20709                           bool keyword)
20710 {
20711   switch (token_desc)
20712     {
20713       case RT_NEW:
20714         cp_parser_error (parser, "expected %<new%>");
20715         return;
20716       case RT_DELETE:
20717         cp_parser_error (parser, "expected %<delete%>");
20718         return;
20719       case RT_RETURN:
20720         cp_parser_error (parser, "expected %<return%>");
20721         return;
20722       case RT_WHILE:
20723         cp_parser_error (parser, "expected %<while%>");
20724         return;
20725       case RT_EXTERN:
20726         cp_parser_error (parser, "expected %<extern%>");
20727         return;
20728       case RT_STATIC_ASSERT:
20729         cp_parser_error (parser, "expected %<static_assert%>");
20730         return;
20731       case RT_DECLTYPE:
20732         cp_parser_error (parser, "expected %<decltype%>");
20733         return;
20734       case RT_OPERATOR:
20735         cp_parser_error (parser, "expected %<operator%>");
20736         return;
20737       case RT_CLASS:
20738         cp_parser_error (parser, "expected %<class%>");
20739         return;
20740       case RT_TEMPLATE:
20741         cp_parser_error (parser, "expected %<template%>");
20742         return;
20743       case RT_NAMESPACE:
20744         cp_parser_error (parser, "expected %<namespace%>");
20745         return;
20746       case RT_USING:
20747         cp_parser_error (parser, "expected %<using%>");
20748         return;
20749       case RT_ASM:
20750         cp_parser_error (parser, "expected %<asm%>");
20751         return;
20752       case RT_TRY:
20753         cp_parser_error (parser, "expected %<try%>");
20754         return;
20755       case RT_CATCH:
20756         cp_parser_error (parser, "expected %<catch%>");
20757         return;
20758       case RT_THROW:
20759         cp_parser_error (parser, "expected %<throw%>");
20760         return;
20761       case RT_LABEL:
20762         cp_parser_error (parser, "expected %<__label__%>");
20763         return;
20764       case RT_AT_TRY:
20765         cp_parser_error (parser, "expected %<@try%>");
20766         return;
20767       case RT_AT_SYNCHRONIZED:
20768         cp_parser_error (parser, "expected %<@synchronized%>");
20769         return;
20770       case RT_AT_THROW:
20771         cp_parser_error (parser, "expected %<@throw%>");
20772         return;
20773       default:
20774         break;
20775     }
20776   if (!keyword)
20777     {
20778       switch (token_desc)
20779         {
20780           case RT_SEMICOLON:
20781             cp_parser_error (parser, "expected %<;%>");
20782             return;
20783           case RT_OPEN_PAREN:
20784             cp_parser_error (parser, "expected %<(%>");
20785             return;
20786           case RT_CLOSE_BRACE:
20787             cp_parser_error (parser, "expected %<}%>");
20788             return;
20789           case RT_OPEN_BRACE:
20790             cp_parser_error (parser, "expected %<{%>");
20791             return;
20792           case RT_CLOSE_SQUARE:
20793             cp_parser_error (parser, "expected %<]%>");
20794             return;
20795           case RT_OPEN_SQUARE:
20796             cp_parser_error (parser, "expected %<[%>");
20797             return;
20798           case RT_COMMA:
20799             cp_parser_error (parser, "expected %<,%>");
20800             return;
20801           case RT_SCOPE:
20802             cp_parser_error (parser, "expected %<::%>");
20803             return;
20804           case RT_LESS:
20805             cp_parser_error (parser, "expected %<<%>");
20806             return;
20807           case RT_GREATER:
20808             cp_parser_error (parser, "expected %<>%>");
20809             return;
20810           case RT_EQ:
20811             cp_parser_error (parser, "expected %<=%>");
20812             return;
20813           case RT_ELLIPSIS:
20814             cp_parser_error (parser, "expected %<...%>");
20815             return;
20816           case RT_MULT:
20817             cp_parser_error (parser, "expected %<*%>");
20818             return;
20819           case RT_COMPL:
20820             cp_parser_error (parser, "expected %<~%>");
20821             return;
20822           case RT_COLON:
20823             cp_parser_error (parser, "expected %<:%>");
20824             return;
20825           case RT_COLON_SCOPE:
20826             cp_parser_error (parser, "expected %<:%> or %<::%>");
20827             return;
20828           case RT_CLOSE_PAREN:
20829             cp_parser_error (parser, "expected %<)%>");
20830             return;
20831           case RT_COMMA_CLOSE_PAREN:
20832             cp_parser_error (parser, "expected %<,%> or %<)%>");
20833             return;
20834           case RT_PRAGMA_EOL:
20835             cp_parser_error (parser, "expected end of line");
20836             return;
20837           case RT_NAME:
20838             cp_parser_error (parser, "expected identifier");
20839             return;
20840           case RT_SELECT:
20841             cp_parser_error (parser, "expected selection-statement");
20842             return;
20843           case RT_INTERATION:
20844             cp_parser_error (parser, "expected iteration-statement");
20845             return;
20846           case RT_JUMP:
20847             cp_parser_error (parser, "expected jump-statement");
20848             return;
20849           case RT_CLASS_KEY:
20850             cp_parser_error (parser, "expected class-key");
20851             return;
20852           case RT_CLASS_TYPENAME_TEMPLATE:
20853             cp_parser_error (parser,
20854                  "expected %<class%>, %<typename%>, or %<template%>");
20855             return;
20856           default:
20857             gcc_unreachable ();
20858         }
20859     }
20860   else
20861     gcc_unreachable ();
20862 }
20863
20864
20865
20866 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20867    issue an error message indicating that TOKEN_DESC was expected.
20868
20869    Returns the token consumed, if the token had the appropriate type.
20870    Otherwise, returns NULL.  */
20871
20872 static cp_token *
20873 cp_parser_require (cp_parser* parser,
20874                    enum cpp_ttype type,
20875                    required_token token_desc)
20876 {
20877   if (cp_lexer_next_token_is (parser->lexer, type))
20878     return cp_lexer_consume_token (parser->lexer);
20879   else
20880     {
20881       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20882       if (!cp_parser_simulate_error (parser))
20883         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20884       return NULL;
20885     }
20886 }
20887
20888 /* An error message is produced if the next token is not '>'.
20889    All further tokens are skipped until the desired token is
20890    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20891
20892 static void
20893 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20894 {
20895   /* Current level of '< ... >'.  */
20896   unsigned level = 0;
20897   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20898   unsigned nesting_depth = 0;
20899
20900   /* Are we ready, yet?  If not, issue error message.  */
20901   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20902     return;
20903
20904   /* Skip tokens until the desired token is found.  */
20905   while (true)
20906     {
20907       /* Peek at the next token.  */
20908       switch (cp_lexer_peek_token (parser->lexer)->type)
20909         {
20910         case CPP_LESS:
20911           if (!nesting_depth)
20912             ++level;
20913           break;
20914
20915         case CPP_RSHIFT:
20916           if (cxx_dialect == cxx98)
20917             /* C++0x views the `>>' operator as two `>' tokens, but
20918                C++98 does not. */
20919             break;
20920           else if (!nesting_depth && level-- == 0)
20921             {
20922               /* We've hit a `>>' where the first `>' closes the
20923                  template argument list, and the second `>' is
20924                  spurious.  Just consume the `>>' and stop; we've
20925                  already produced at least one error.  */
20926               cp_lexer_consume_token (parser->lexer);
20927               return;
20928             }
20929           /* Fall through for C++0x, so we handle the second `>' in
20930              the `>>'.  */
20931
20932         case CPP_GREATER:
20933           if (!nesting_depth && level-- == 0)
20934             {
20935               /* We've reached the token we want, consume it and stop.  */
20936               cp_lexer_consume_token (parser->lexer);
20937               return;
20938             }
20939           break;
20940
20941         case CPP_OPEN_PAREN:
20942         case CPP_OPEN_SQUARE:
20943           ++nesting_depth;
20944           break;
20945
20946         case CPP_CLOSE_PAREN:
20947         case CPP_CLOSE_SQUARE:
20948           if (nesting_depth-- == 0)
20949             return;
20950           break;
20951
20952         case CPP_EOF:
20953         case CPP_PRAGMA_EOL:
20954         case CPP_SEMICOLON:
20955         case CPP_OPEN_BRACE:
20956         case CPP_CLOSE_BRACE:
20957           /* The '>' was probably forgotten, don't look further.  */
20958           return;
20959
20960         default:
20961           break;
20962         }
20963
20964       /* Consume this token.  */
20965       cp_lexer_consume_token (parser->lexer);
20966     }
20967 }
20968
20969 /* If the next token is the indicated keyword, consume it.  Otherwise,
20970    issue an error message indicating that TOKEN_DESC was expected.
20971
20972    Returns the token consumed, if the token had the appropriate type.
20973    Otherwise, returns NULL.  */
20974
20975 static cp_token *
20976 cp_parser_require_keyword (cp_parser* parser,
20977                            enum rid keyword,
20978                            required_token token_desc)
20979 {
20980   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20981
20982   if (token && token->keyword != keyword)
20983     {
20984       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20985       return NULL;
20986     }
20987
20988   return token;
20989 }
20990
20991 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20992    function-definition.  */
20993
20994 static bool
20995 cp_parser_token_starts_function_definition_p (cp_token* token)
20996 {
20997   return (/* An ordinary function-body begins with an `{'.  */
20998           token->type == CPP_OPEN_BRACE
20999           /* A ctor-initializer begins with a `:'.  */
21000           || token->type == CPP_COLON
21001           /* A function-try-block begins with `try'.  */
21002           || token->keyword == RID_TRY
21003           /* The named return value extension begins with `return'.  */
21004           || token->keyword == RID_RETURN);
21005 }
21006
21007 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21008    definition.  */
21009
21010 static bool
21011 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21012 {
21013   cp_token *token;
21014
21015   token = cp_lexer_peek_token (parser->lexer);
21016   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21017 }
21018
21019 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21020    C++0x) ending a template-argument.  */
21021
21022 static bool
21023 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21024 {
21025   cp_token *token;
21026
21027   token = cp_lexer_peek_token (parser->lexer);
21028   return (token->type == CPP_COMMA 
21029           || token->type == CPP_GREATER
21030           || token->type == CPP_ELLIPSIS
21031           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21032 }
21033
21034 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21035    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21036
21037 static bool
21038 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21039                                                      size_t n)
21040 {
21041   cp_token *token;
21042
21043   token = cp_lexer_peek_nth_token (parser->lexer, n);
21044   if (token->type == CPP_LESS)
21045     return true;
21046   /* Check for the sequence `<::' in the original code. It would be lexed as
21047      `[:', where `[' is a digraph, and there is no whitespace before
21048      `:'.  */
21049   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21050     {
21051       cp_token *token2;
21052       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21053       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21054         return true;
21055     }
21056   return false;
21057 }
21058
21059 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21060    or none_type otherwise.  */
21061
21062 static enum tag_types
21063 cp_parser_token_is_class_key (cp_token* token)
21064 {
21065   switch (token->keyword)
21066     {
21067     case RID_CLASS:
21068       return class_type;
21069     case RID_STRUCT:
21070       return record_type;
21071     case RID_UNION:
21072       return union_type;
21073
21074     default:
21075       return none_type;
21076     }
21077 }
21078
21079 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21080
21081 static void
21082 cp_parser_check_class_key (enum tag_types class_key, tree type)
21083 {
21084   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21085     permerror (input_location, "%qs tag used in naming %q#T",
21086             class_key == union_type ? "union"
21087              : class_key == record_type ? "struct" : "class",
21088              type);
21089 }
21090
21091 /* Issue an error message if DECL is redeclared with different
21092    access than its original declaration [class.access.spec/3].
21093    This applies to nested classes and nested class templates.
21094    [class.mem/1].  */
21095
21096 static void
21097 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21098 {
21099   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21100     return;
21101
21102   if ((TREE_PRIVATE (decl)
21103        != (current_access_specifier == access_private_node))
21104       || (TREE_PROTECTED (decl)
21105           != (current_access_specifier == access_protected_node)))
21106     error_at (location, "%qD redeclared with different access", decl);
21107 }
21108
21109 /* Look for the `template' keyword, as a syntactic disambiguator.
21110    Return TRUE iff it is present, in which case it will be
21111    consumed.  */
21112
21113 static bool
21114 cp_parser_optional_template_keyword (cp_parser *parser)
21115 {
21116   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21117     {
21118       /* The `template' keyword can only be used within templates;
21119          outside templates the parser can always figure out what is a
21120          template and what is not.  */
21121       if (!processing_template_decl)
21122         {
21123           cp_token *token = cp_lexer_peek_token (parser->lexer);
21124           error_at (token->location,
21125                     "%<template%> (as a disambiguator) is only allowed "
21126                     "within templates");
21127           /* If this part of the token stream is rescanned, the same
21128              error message would be generated.  So, we purge the token
21129              from the stream.  */
21130           cp_lexer_purge_token (parser->lexer);
21131           return false;
21132         }
21133       else
21134         {
21135           /* Consume the `template' keyword.  */
21136           cp_lexer_consume_token (parser->lexer);
21137           return true;
21138         }
21139     }
21140
21141   return false;
21142 }
21143
21144 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21145    set PARSER->SCOPE, and perform other related actions.  */
21146
21147 static void
21148 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21149 {
21150   int i;
21151   struct tree_check *check_value;
21152   deferred_access_check *chk;
21153   VEC (deferred_access_check,gc) *checks;
21154
21155   /* Get the stored value.  */
21156   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21157   /* Perform any access checks that were deferred.  */
21158   checks = check_value->checks;
21159   if (checks)
21160     {
21161       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21162         perform_or_defer_access_check (chk->binfo,
21163                                        chk->decl,
21164                                        chk->diag_decl);
21165     }
21166   /* Set the scope from the stored value.  */
21167   parser->scope = check_value->value;
21168   parser->qualifying_scope = check_value->qualifying_scope;
21169   parser->object_scope = NULL_TREE;
21170 }
21171
21172 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21173    encounter the end of a block before what we were looking for.  */
21174
21175 static bool
21176 cp_parser_cache_group (cp_parser *parser,
21177                        enum cpp_ttype end,
21178                        unsigned depth)
21179 {
21180   while (true)
21181     {
21182       cp_token *token = cp_lexer_peek_token (parser->lexer);
21183
21184       /* Abort a parenthesized expression if we encounter a semicolon.  */
21185       if ((end == CPP_CLOSE_PAREN || depth == 0)
21186           && token->type == CPP_SEMICOLON)
21187         return true;
21188       /* If we've reached the end of the file, stop.  */
21189       if (token->type == CPP_EOF
21190           || (end != CPP_PRAGMA_EOL
21191               && token->type == CPP_PRAGMA_EOL))
21192         return true;
21193       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21194         /* We've hit the end of an enclosing block, so there's been some
21195            kind of syntax error.  */
21196         return true;
21197
21198       /* Consume the token.  */
21199       cp_lexer_consume_token (parser->lexer);
21200       /* See if it starts a new group.  */
21201       if (token->type == CPP_OPEN_BRACE)
21202         {
21203           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21204           /* In theory this should probably check end == '}', but
21205              cp_parser_save_member_function_body needs it to exit
21206              after either '}' or ')' when called with ')'.  */
21207           if (depth == 0)
21208             return false;
21209         }
21210       else if (token->type == CPP_OPEN_PAREN)
21211         {
21212           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21213           if (depth == 0 && end == CPP_CLOSE_PAREN)
21214             return false;
21215         }
21216       else if (token->type == CPP_PRAGMA)
21217         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21218       else if (token->type == end)
21219         return false;
21220     }
21221 }
21222
21223 /* Begin parsing tentatively.  We always save tokens while parsing
21224    tentatively so that if the tentative parsing fails we can restore the
21225    tokens.  */
21226
21227 static void
21228 cp_parser_parse_tentatively (cp_parser* parser)
21229 {
21230   /* Enter a new parsing context.  */
21231   parser->context = cp_parser_context_new (parser->context);
21232   /* Begin saving tokens.  */
21233   cp_lexer_save_tokens (parser->lexer);
21234   /* In order to avoid repetitive access control error messages,
21235      access checks are queued up until we are no longer parsing
21236      tentatively.  */
21237   push_deferring_access_checks (dk_deferred);
21238 }
21239
21240 /* Commit to the currently active tentative parse.  */
21241
21242 static void
21243 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21244 {
21245   cp_parser_context *context;
21246   cp_lexer *lexer;
21247
21248   /* Mark all of the levels as committed.  */
21249   lexer = parser->lexer;
21250   for (context = parser->context; context->next; context = context->next)
21251     {
21252       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21253         break;
21254       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21255       while (!cp_lexer_saving_tokens (lexer))
21256         lexer = lexer->next;
21257       cp_lexer_commit_tokens (lexer);
21258     }
21259 }
21260
21261 /* Abort the currently active tentative parse.  All consumed tokens
21262    will be rolled back, and no diagnostics will be issued.  */
21263
21264 static void
21265 cp_parser_abort_tentative_parse (cp_parser* parser)
21266 {
21267   cp_parser_simulate_error (parser);
21268   /* Now, pretend that we want to see if the construct was
21269      successfully parsed.  */
21270   cp_parser_parse_definitely (parser);
21271 }
21272
21273 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21274    token stream.  Otherwise, commit to the tokens we have consumed.
21275    Returns true if no error occurred; false otherwise.  */
21276
21277 static bool
21278 cp_parser_parse_definitely (cp_parser* parser)
21279 {
21280   bool error_occurred;
21281   cp_parser_context *context;
21282
21283   /* Remember whether or not an error occurred, since we are about to
21284      destroy that information.  */
21285   error_occurred = cp_parser_error_occurred (parser);
21286   /* Remove the topmost context from the stack.  */
21287   context = parser->context;
21288   parser->context = context->next;
21289   /* If no parse errors occurred, commit to the tentative parse.  */
21290   if (!error_occurred)
21291     {
21292       /* Commit to the tokens read tentatively, unless that was
21293          already done.  */
21294       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21295         cp_lexer_commit_tokens (parser->lexer);
21296
21297       pop_to_parent_deferring_access_checks ();
21298     }
21299   /* Otherwise, if errors occurred, roll back our state so that things
21300      are just as they were before we began the tentative parse.  */
21301   else
21302     {
21303       cp_lexer_rollback_tokens (parser->lexer);
21304       pop_deferring_access_checks ();
21305     }
21306   /* Add the context to the front of the free list.  */
21307   context->next = cp_parser_context_free_list;
21308   cp_parser_context_free_list = context;
21309
21310   return !error_occurred;
21311 }
21312
21313 /* Returns true if we are parsing tentatively and are not committed to
21314    this tentative parse.  */
21315
21316 static bool
21317 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21318 {
21319   return (cp_parser_parsing_tentatively (parser)
21320           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21321 }
21322
21323 /* Returns nonzero iff an error has occurred during the most recent
21324    tentative parse.  */
21325
21326 static bool
21327 cp_parser_error_occurred (cp_parser* parser)
21328 {
21329   return (cp_parser_parsing_tentatively (parser)
21330           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21331 }
21332
21333 /* Returns nonzero if GNU extensions are allowed.  */
21334
21335 static bool
21336 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21337 {
21338   return parser->allow_gnu_extensions_p;
21339 }
21340 \f
21341 /* Objective-C++ Productions */
21342
21343
21344 /* Parse an Objective-C expression, which feeds into a primary-expression
21345    above.
21346
21347    objc-expression:
21348      objc-message-expression
21349      objc-string-literal
21350      objc-encode-expression
21351      objc-protocol-expression
21352      objc-selector-expression
21353
21354   Returns a tree representation of the expression.  */
21355
21356 static tree
21357 cp_parser_objc_expression (cp_parser* parser)
21358 {
21359   /* Try to figure out what kind of declaration is present.  */
21360   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21361
21362   switch (kwd->type)
21363     {
21364     case CPP_OPEN_SQUARE:
21365       return cp_parser_objc_message_expression (parser);
21366
21367     case CPP_OBJC_STRING:
21368       kwd = cp_lexer_consume_token (parser->lexer);
21369       return objc_build_string_object (kwd->u.value);
21370
21371     case CPP_KEYWORD:
21372       switch (kwd->keyword)
21373         {
21374         case RID_AT_ENCODE:
21375           return cp_parser_objc_encode_expression (parser);
21376
21377         case RID_AT_PROTOCOL:
21378           return cp_parser_objc_protocol_expression (parser);
21379
21380         case RID_AT_SELECTOR:
21381           return cp_parser_objc_selector_expression (parser);
21382
21383         default:
21384           break;
21385         }
21386     default:
21387       error_at (kwd->location,
21388                 "misplaced %<@%D%> Objective-C++ construct",
21389                 kwd->u.value);
21390       cp_parser_skip_to_end_of_block_or_statement (parser);
21391     }
21392
21393   return error_mark_node;
21394 }
21395
21396 /* Parse an Objective-C message expression.
21397
21398    objc-message-expression:
21399      [ objc-message-receiver objc-message-args ]
21400
21401    Returns a representation of an Objective-C message.  */
21402
21403 static tree
21404 cp_parser_objc_message_expression (cp_parser* parser)
21405 {
21406   tree receiver, messageargs;
21407
21408   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21409   receiver = cp_parser_objc_message_receiver (parser);
21410   messageargs = cp_parser_objc_message_args (parser);
21411   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21412
21413   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21414 }
21415
21416 /* Parse an objc-message-receiver.
21417
21418    objc-message-receiver:
21419      expression
21420      simple-type-specifier
21421
21422   Returns a representation of the type or expression.  */
21423
21424 static tree
21425 cp_parser_objc_message_receiver (cp_parser* parser)
21426 {
21427   tree rcv;
21428
21429   /* An Objective-C message receiver may be either (1) a type
21430      or (2) an expression.  */
21431   cp_parser_parse_tentatively (parser);
21432   rcv = cp_parser_expression (parser, false, NULL);
21433
21434   if (cp_parser_parse_definitely (parser))
21435     return rcv;
21436
21437   rcv = cp_parser_simple_type_specifier (parser,
21438                                          /*decl_specs=*/NULL,
21439                                          CP_PARSER_FLAGS_NONE);
21440
21441   return objc_get_class_reference (rcv);
21442 }
21443
21444 /* Parse the arguments and selectors comprising an Objective-C message.
21445
21446    objc-message-args:
21447      objc-selector
21448      objc-selector-args
21449      objc-selector-args , objc-comma-args
21450
21451    objc-selector-args:
21452      objc-selector [opt] : assignment-expression
21453      objc-selector-args objc-selector [opt] : assignment-expression
21454
21455    objc-comma-args:
21456      assignment-expression
21457      objc-comma-args , assignment-expression
21458
21459    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21460    selector arguments and TREE_VALUE containing a list of comma
21461    arguments.  */
21462
21463 static tree
21464 cp_parser_objc_message_args (cp_parser* parser)
21465 {
21466   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21467   bool maybe_unary_selector_p = true;
21468   cp_token *token = cp_lexer_peek_token (parser->lexer);
21469
21470   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21471     {
21472       tree selector = NULL_TREE, arg;
21473
21474       if (token->type != CPP_COLON)
21475         selector = cp_parser_objc_selector (parser);
21476
21477       /* Detect if we have a unary selector.  */
21478       if (maybe_unary_selector_p
21479           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21480         return build_tree_list (selector, NULL_TREE);
21481
21482       maybe_unary_selector_p = false;
21483       cp_parser_require (parser, CPP_COLON, RT_COLON);
21484       arg = cp_parser_assignment_expression (parser, false, NULL);
21485
21486       sel_args
21487         = chainon (sel_args,
21488                    build_tree_list (selector, arg));
21489
21490       token = cp_lexer_peek_token (parser->lexer);
21491     }
21492
21493   /* Handle non-selector arguments, if any. */
21494   while (token->type == CPP_COMMA)
21495     {
21496       tree arg;
21497
21498       cp_lexer_consume_token (parser->lexer);
21499       arg = cp_parser_assignment_expression (parser, false, NULL);
21500
21501       addl_args
21502         = chainon (addl_args,
21503                    build_tree_list (NULL_TREE, arg));
21504
21505       token = cp_lexer_peek_token (parser->lexer);
21506     }
21507
21508   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21509     {
21510       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21511       return build_tree_list (error_mark_node, error_mark_node);
21512     }
21513
21514   return build_tree_list (sel_args, addl_args);
21515 }
21516
21517 /* Parse an Objective-C encode expression.
21518
21519    objc-encode-expression:
21520      @encode objc-typename
21521
21522    Returns an encoded representation of the type argument.  */
21523
21524 static tree
21525 cp_parser_objc_encode_expression (cp_parser* parser)
21526 {
21527   tree type;
21528   cp_token *token;
21529
21530   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21531   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21532   token = cp_lexer_peek_token (parser->lexer);
21533   type = complete_type (cp_parser_type_id (parser));
21534   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21535
21536   if (!type)
21537     {
21538       error_at (token->location, 
21539                 "%<@encode%> must specify a type as an argument");
21540       return error_mark_node;
21541     }
21542
21543   /* This happens if we find @encode(T) (where T is a template
21544      typename or something dependent on a template typename) when
21545      parsing a template.  In that case, we can't compile it
21546      immediately, but we rather create an AT_ENCODE_EXPR which will
21547      need to be instantiated when the template is used.
21548   */
21549   if (dependent_type_p (type))
21550     {
21551       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21552       TREE_READONLY (value) = 1;
21553       return value;
21554     }
21555
21556   return objc_build_encode_expr (type);
21557 }
21558
21559 /* Parse an Objective-C @defs expression.  */
21560
21561 static tree
21562 cp_parser_objc_defs_expression (cp_parser *parser)
21563 {
21564   tree name;
21565
21566   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21567   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21568   name = cp_parser_identifier (parser);
21569   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21570
21571   return objc_get_class_ivars (name);
21572 }
21573
21574 /* Parse an Objective-C protocol expression.
21575
21576   objc-protocol-expression:
21577     @protocol ( identifier )
21578
21579   Returns a representation of the protocol expression.  */
21580
21581 static tree
21582 cp_parser_objc_protocol_expression (cp_parser* parser)
21583 {
21584   tree proto;
21585
21586   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21587   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21588   proto = cp_parser_identifier (parser);
21589   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21590
21591   return objc_build_protocol_expr (proto);
21592 }
21593
21594 /* Parse an Objective-C selector expression.
21595
21596    objc-selector-expression:
21597      @selector ( objc-method-signature )
21598
21599    objc-method-signature:
21600      objc-selector
21601      objc-selector-seq
21602
21603    objc-selector-seq:
21604      objc-selector :
21605      objc-selector-seq objc-selector :
21606
21607   Returns a representation of the method selector.  */
21608
21609 static tree
21610 cp_parser_objc_selector_expression (cp_parser* parser)
21611 {
21612   tree sel_seq = NULL_TREE;
21613   bool maybe_unary_selector_p = true;
21614   cp_token *token;
21615   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21616
21617   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21618   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21619   token = cp_lexer_peek_token (parser->lexer);
21620
21621   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21622          || token->type == CPP_SCOPE)
21623     {
21624       tree selector = NULL_TREE;
21625
21626       if (token->type != CPP_COLON
21627           || token->type == CPP_SCOPE)
21628         selector = cp_parser_objc_selector (parser);
21629
21630       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21631           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21632         {
21633           /* Detect if we have a unary selector.  */
21634           if (maybe_unary_selector_p)
21635             {
21636               sel_seq = selector;
21637               goto finish_selector;
21638             }
21639           else
21640             {
21641               cp_parser_error (parser, "expected %<:%>");
21642             }
21643         }
21644       maybe_unary_selector_p = false;
21645       token = cp_lexer_consume_token (parser->lexer);
21646
21647       if (token->type == CPP_SCOPE)
21648         {
21649           sel_seq
21650             = chainon (sel_seq,
21651                        build_tree_list (selector, NULL_TREE));
21652           sel_seq
21653             = chainon (sel_seq,
21654                        build_tree_list (NULL_TREE, NULL_TREE));
21655         }
21656       else
21657         sel_seq
21658           = chainon (sel_seq,
21659                      build_tree_list (selector, NULL_TREE));
21660
21661       token = cp_lexer_peek_token (parser->lexer);
21662     }
21663
21664  finish_selector:
21665   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21666
21667   return objc_build_selector_expr (loc, sel_seq);
21668 }
21669
21670 /* Parse a list of identifiers.
21671
21672    objc-identifier-list:
21673      identifier
21674      objc-identifier-list , identifier
21675
21676    Returns a TREE_LIST of identifier nodes.  */
21677
21678 static tree
21679 cp_parser_objc_identifier_list (cp_parser* parser)
21680 {
21681   tree identifier;
21682   tree list;
21683   cp_token *sep;
21684
21685   identifier = cp_parser_identifier (parser);
21686   if (identifier == error_mark_node)
21687     return error_mark_node;      
21688
21689   list = build_tree_list (NULL_TREE, identifier);
21690   sep = cp_lexer_peek_token (parser->lexer);
21691
21692   while (sep->type == CPP_COMMA)
21693     {
21694       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21695       identifier = cp_parser_identifier (parser);
21696       if (identifier == error_mark_node)
21697         return list;
21698
21699       list = chainon (list, build_tree_list (NULL_TREE,
21700                                              identifier));
21701       sep = cp_lexer_peek_token (parser->lexer);
21702     }
21703   
21704   return list;
21705 }
21706
21707 /* Parse an Objective-C alias declaration.
21708
21709    objc-alias-declaration:
21710      @compatibility_alias identifier identifier ;
21711
21712    This function registers the alias mapping with the Objective-C front end.
21713    It returns nothing.  */
21714
21715 static void
21716 cp_parser_objc_alias_declaration (cp_parser* parser)
21717 {
21718   tree alias, orig;
21719
21720   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21721   alias = cp_parser_identifier (parser);
21722   orig = cp_parser_identifier (parser);
21723   objc_declare_alias (alias, orig);
21724   cp_parser_consume_semicolon_at_end_of_statement (parser);
21725 }
21726
21727 /* Parse an Objective-C class forward-declaration.
21728
21729    objc-class-declaration:
21730      @class objc-identifier-list ;
21731
21732    The function registers the forward declarations with the Objective-C
21733    front end.  It returns nothing.  */
21734
21735 static void
21736 cp_parser_objc_class_declaration (cp_parser* parser)
21737 {
21738   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21739   objc_declare_class (cp_parser_objc_identifier_list (parser));
21740   cp_parser_consume_semicolon_at_end_of_statement (parser);
21741 }
21742
21743 /* Parse a list of Objective-C protocol references.
21744
21745    objc-protocol-refs-opt:
21746      objc-protocol-refs [opt]
21747
21748    objc-protocol-refs:
21749      < objc-identifier-list >
21750
21751    Returns a TREE_LIST of identifiers, if any.  */
21752
21753 static tree
21754 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21755 {
21756   tree protorefs = NULL_TREE;
21757
21758   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21759     {
21760       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21761       protorefs = cp_parser_objc_identifier_list (parser);
21762       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21763     }
21764
21765   return protorefs;
21766 }
21767
21768 /* Parse a Objective-C visibility specification.  */
21769
21770 static void
21771 cp_parser_objc_visibility_spec (cp_parser* parser)
21772 {
21773   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21774
21775   switch (vis->keyword)
21776     {
21777     case RID_AT_PRIVATE:
21778       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21779       break;
21780     case RID_AT_PROTECTED:
21781       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21782       break;
21783     case RID_AT_PUBLIC:
21784       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21785       break;
21786     case RID_AT_PACKAGE:
21787       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21788       break;
21789     default:
21790       return;
21791     }
21792
21793   /* Eat '@private'/'@protected'/'@public'.  */
21794   cp_lexer_consume_token (parser->lexer);
21795 }
21796
21797 /* Parse an Objective-C method type.  Return 'true' if it is a class
21798    (+) method, and 'false' if it is an instance (-) method.  */
21799
21800 static inline bool
21801 cp_parser_objc_method_type (cp_parser* parser)
21802 {
21803   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21804     return true;
21805   else
21806     return false;
21807 }
21808
21809 /* Parse an Objective-C protocol qualifier.  */
21810
21811 static tree
21812 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21813 {
21814   tree quals = NULL_TREE, node;
21815   cp_token *token = cp_lexer_peek_token (parser->lexer);
21816
21817   node = token->u.value;
21818
21819   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21820          && (node == ridpointers [(int) RID_IN]
21821              || node == ridpointers [(int) RID_OUT]
21822              || node == ridpointers [(int) RID_INOUT]
21823              || node == ridpointers [(int) RID_BYCOPY]
21824              || node == ridpointers [(int) RID_BYREF]
21825              || node == ridpointers [(int) RID_ONEWAY]))
21826     {
21827       quals = tree_cons (NULL_TREE, node, quals);
21828       cp_lexer_consume_token (parser->lexer);
21829       token = cp_lexer_peek_token (parser->lexer);
21830       node = token->u.value;
21831     }
21832
21833   return quals;
21834 }
21835
21836 /* Parse an Objective-C typename.  */
21837
21838 static tree
21839 cp_parser_objc_typename (cp_parser* parser)
21840 {
21841   tree type_name = NULL_TREE;
21842
21843   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21844     {
21845       tree proto_quals, cp_type = NULL_TREE;
21846
21847       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21848       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21849
21850       /* An ObjC type name may consist of just protocol qualifiers, in which
21851          case the type shall default to 'id'.  */
21852       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21853         cp_type = cp_parser_type_id (parser);
21854
21855       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21856       type_name = build_tree_list (proto_quals, cp_type);
21857     }
21858
21859   return type_name;
21860 }
21861
21862 /* Check to see if TYPE refers to an Objective-C selector name.  */
21863
21864 static bool
21865 cp_parser_objc_selector_p (enum cpp_ttype type)
21866 {
21867   return (type == CPP_NAME || type == CPP_KEYWORD
21868           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21869           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21870           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21871           || type == CPP_XOR || type == CPP_XOR_EQ);
21872 }
21873
21874 /* Parse an Objective-C selector.  */
21875
21876 static tree
21877 cp_parser_objc_selector (cp_parser* parser)
21878 {
21879   cp_token *token = cp_lexer_consume_token (parser->lexer);
21880
21881   if (!cp_parser_objc_selector_p (token->type))
21882     {
21883       error_at (token->location, "invalid Objective-C++ selector name");
21884       return error_mark_node;
21885     }
21886
21887   /* C++ operator names are allowed to appear in ObjC selectors.  */
21888   switch (token->type)
21889     {
21890     case CPP_AND_AND: return get_identifier ("and");
21891     case CPP_AND_EQ: return get_identifier ("and_eq");
21892     case CPP_AND: return get_identifier ("bitand");
21893     case CPP_OR: return get_identifier ("bitor");
21894     case CPP_COMPL: return get_identifier ("compl");
21895     case CPP_NOT: return get_identifier ("not");
21896     case CPP_NOT_EQ: return get_identifier ("not_eq");
21897     case CPP_OR_OR: return get_identifier ("or");
21898     case CPP_OR_EQ: return get_identifier ("or_eq");
21899     case CPP_XOR: return get_identifier ("xor");
21900     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21901     default: return token->u.value;
21902     }
21903 }
21904
21905 /* Parse an Objective-C params list.  */
21906
21907 static tree
21908 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21909 {
21910   tree params = NULL_TREE;
21911   bool maybe_unary_selector_p = true;
21912   cp_token *token = cp_lexer_peek_token (parser->lexer);
21913
21914   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21915     {
21916       tree selector = NULL_TREE, type_name, identifier;
21917       tree parm_attr = NULL_TREE;
21918
21919       if (token->keyword == RID_ATTRIBUTE)
21920         break;
21921
21922       if (token->type != CPP_COLON)
21923         selector = cp_parser_objc_selector (parser);
21924
21925       /* Detect if we have a unary selector.  */
21926       if (maybe_unary_selector_p
21927           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21928         {
21929           params = selector; /* Might be followed by attributes.  */
21930           break;
21931         }
21932
21933       maybe_unary_selector_p = false;
21934       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21935         {
21936           /* Something went quite wrong.  There should be a colon
21937              here, but there is not.  Stop parsing parameters.  */
21938           break;
21939         }
21940       type_name = cp_parser_objc_typename (parser);
21941       /* New ObjC allows attributes on parameters too.  */
21942       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21943         parm_attr = cp_parser_attributes_opt (parser);
21944       identifier = cp_parser_identifier (parser);
21945
21946       params
21947         = chainon (params,
21948                    objc_build_keyword_decl (selector,
21949                                             type_name,
21950                                             identifier,
21951                                             parm_attr));
21952
21953       token = cp_lexer_peek_token (parser->lexer);
21954     }
21955
21956   if (params == NULL_TREE)
21957     {
21958       cp_parser_error (parser, "objective-c++ method declaration is expected");
21959       return error_mark_node;
21960     }
21961
21962   /* We allow tail attributes for the method.  */
21963   if (token->keyword == RID_ATTRIBUTE)
21964     {
21965       *attributes = cp_parser_attributes_opt (parser);
21966       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21967           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21968         return params;
21969       cp_parser_error (parser, 
21970                        "method attributes must be specified at the end");
21971       return error_mark_node;
21972     }
21973
21974   if (params == NULL_TREE)
21975     {
21976       cp_parser_error (parser, "objective-c++ method declaration is expected");
21977       return error_mark_node;
21978     }
21979   return params;
21980 }
21981
21982 /* Parse the non-keyword Objective-C params.  */
21983
21984 static tree
21985 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
21986                                        tree* attributes)
21987 {
21988   tree params = make_node (TREE_LIST);
21989   cp_token *token = cp_lexer_peek_token (parser->lexer);
21990   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21991
21992   while (token->type == CPP_COMMA)
21993     {
21994       cp_parameter_declarator *parmdecl;
21995       tree parm;
21996
21997       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21998       token = cp_lexer_peek_token (parser->lexer);
21999
22000       if (token->type == CPP_ELLIPSIS)
22001         {
22002           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22003           *ellipsisp = true;
22004           token = cp_lexer_peek_token (parser->lexer);
22005           break;
22006         }
22007
22008       /* TODO: parse attributes for tail parameters.  */
22009       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22010       parm = grokdeclarator (parmdecl->declarator,
22011                              &parmdecl->decl_specifiers,
22012                              PARM, /*initialized=*/0,
22013                              /*attrlist=*/NULL);
22014
22015       chainon (params, build_tree_list (NULL_TREE, parm));
22016       token = cp_lexer_peek_token (parser->lexer);
22017     }
22018
22019   /* We allow tail attributes for the method.  */
22020   if (token->keyword == RID_ATTRIBUTE)
22021     {
22022       if (*attributes == NULL_TREE)
22023         {
22024           *attributes = cp_parser_attributes_opt (parser);
22025           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22026               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22027             return params;
22028         }
22029       else        
22030         /* We have an error, but parse the attributes, so that we can 
22031            carry on.  */
22032         *attributes = cp_parser_attributes_opt (parser);
22033
22034       cp_parser_error (parser, 
22035                        "method attributes must be specified at the end");
22036       return error_mark_node;
22037     }
22038
22039   return params;
22040 }
22041
22042 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22043
22044 static void
22045 cp_parser_objc_interstitial_code (cp_parser* parser)
22046 {
22047   cp_token *token = cp_lexer_peek_token (parser->lexer);
22048
22049   /* If the next token is `extern' and the following token is a string
22050      literal, then we have a linkage specification.  */
22051   if (token->keyword == RID_EXTERN
22052       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22053     cp_parser_linkage_specification (parser);
22054   /* Handle #pragma, if any.  */
22055   else if (token->type == CPP_PRAGMA)
22056     cp_parser_pragma (parser, pragma_external);
22057   /* Allow stray semicolons.  */
22058   else if (token->type == CPP_SEMICOLON)
22059     cp_lexer_consume_token (parser->lexer);
22060   /* Mark methods as optional or required, when building protocols.  */
22061   else if (token->keyword == RID_AT_OPTIONAL)
22062     {
22063       cp_lexer_consume_token (parser->lexer);
22064       objc_set_method_opt (true);
22065     }
22066   else if (token->keyword == RID_AT_REQUIRED)
22067     {
22068       cp_lexer_consume_token (parser->lexer);
22069       objc_set_method_opt (false);
22070     }
22071   else if (token->keyword == RID_NAMESPACE)
22072     cp_parser_namespace_definition (parser);
22073   /* Other stray characters must generate errors.  */
22074   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22075     {
22076       cp_lexer_consume_token (parser->lexer);
22077       error ("stray %qs between Objective-C++ methods",
22078              token->type == CPP_OPEN_BRACE ? "{" : "}");
22079     }
22080   /* Finally, try to parse a block-declaration, or a function-definition.  */
22081   else
22082     cp_parser_block_declaration (parser, /*statement_p=*/false);
22083 }
22084
22085 /* Parse a method signature.  */
22086
22087 static tree
22088 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22089 {
22090   tree rettype, kwdparms, optparms;
22091   bool ellipsis = false;
22092   bool is_class_method;
22093
22094   is_class_method = cp_parser_objc_method_type (parser);
22095   rettype = cp_parser_objc_typename (parser);
22096   *attributes = NULL_TREE;
22097   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22098   if (kwdparms == error_mark_node)
22099     return error_mark_node;
22100   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22101   if (optparms == error_mark_node)
22102     return error_mark_node;
22103
22104   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22105 }
22106
22107 static bool
22108 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22109 {
22110   tree tattr;  
22111   cp_lexer_save_tokens (parser->lexer);
22112   tattr = cp_parser_attributes_opt (parser);
22113   gcc_assert (tattr) ;
22114   
22115   /* If the attributes are followed by a method introducer, this is not allowed.
22116      Dump the attributes and flag the situation.  */
22117   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22118       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22119     return true;
22120
22121   /* Otherwise, the attributes introduce some interstitial code, possibly so
22122      rewind to allow that check.  */
22123   cp_lexer_rollback_tokens (parser->lexer);
22124   return false;  
22125 }
22126
22127 /* Parse an Objective-C method prototype list.  */
22128
22129 static void
22130 cp_parser_objc_method_prototype_list (cp_parser* parser)
22131 {
22132   cp_token *token = cp_lexer_peek_token (parser->lexer);
22133
22134   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22135     {
22136       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22137         {
22138           tree attributes, sig;
22139           bool is_class_method;
22140           if (token->type == CPP_PLUS)
22141             is_class_method = true;
22142           else
22143             is_class_method = false;
22144           sig = cp_parser_objc_method_signature (parser, &attributes);
22145           if (sig == error_mark_node)
22146             {
22147               cp_parser_skip_to_end_of_block_or_statement (parser);
22148               token = cp_lexer_peek_token (parser->lexer);
22149               continue;
22150             }
22151           objc_add_method_declaration (is_class_method, sig, attributes);
22152           cp_parser_consume_semicolon_at_end_of_statement (parser);
22153         }
22154       else if (token->keyword == RID_AT_PROPERTY)
22155         cp_parser_objc_at_property_declaration (parser);
22156       else if (token->keyword == RID_ATTRIBUTE 
22157                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22158         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22159                     OPT_Wattributes, 
22160                     "prefix attributes are ignored for methods");
22161       else
22162         /* Allow for interspersed non-ObjC++ code.  */
22163         cp_parser_objc_interstitial_code (parser);
22164
22165       token = cp_lexer_peek_token (parser->lexer);
22166     }
22167
22168   if (token->type != CPP_EOF)
22169     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22170   else
22171     cp_parser_error (parser, "expected %<@end%>");
22172
22173   objc_finish_interface ();
22174 }
22175
22176 /* Parse an Objective-C method definition list.  */
22177
22178 static void
22179 cp_parser_objc_method_definition_list (cp_parser* parser)
22180 {
22181   cp_token *token = cp_lexer_peek_token (parser->lexer);
22182
22183   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22184     {
22185       tree meth;
22186
22187       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22188         {
22189           cp_token *ptk;
22190           tree sig, attribute;
22191           bool is_class_method;
22192           if (token->type == CPP_PLUS)
22193             is_class_method = true;
22194           else
22195             is_class_method = false;
22196           push_deferring_access_checks (dk_deferred);
22197           sig = cp_parser_objc_method_signature (parser, &attribute);
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_start_method_definition (is_class_method, sig, attribute);
22205
22206           /* For historical reasons, we accept an optional semicolon.  */
22207           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22208             cp_lexer_consume_token (parser->lexer);
22209
22210           ptk = cp_lexer_peek_token (parser->lexer);
22211           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22212                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22213             {
22214               perform_deferred_access_checks ();
22215               stop_deferring_access_checks ();
22216               meth = cp_parser_function_definition_after_declarator (parser,
22217                                                                      false);
22218               pop_deferring_access_checks ();
22219               objc_finish_method_definition (meth);
22220             }
22221         }
22222       /* The following case will be removed once @synthesize is
22223          completely implemented.  */
22224       else if (token->keyword == RID_AT_PROPERTY)
22225         cp_parser_objc_at_property_declaration (parser);
22226       else if (token->keyword == RID_AT_SYNTHESIZE)
22227         cp_parser_objc_at_synthesize_declaration (parser);
22228       else if (token->keyword == RID_AT_DYNAMIC)
22229         cp_parser_objc_at_dynamic_declaration (parser);
22230       else if (token->keyword == RID_ATTRIBUTE 
22231                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22232         warning_at (token->location, OPT_Wattributes,
22233                     "prefix attributes are ignored for methods");
22234       else
22235         /* Allow for interspersed non-ObjC++ code.  */
22236         cp_parser_objc_interstitial_code (parser);
22237
22238       token = cp_lexer_peek_token (parser->lexer);
22239     }
22240
22241   if (token->type != CPP_EOF)
22242     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22243   else
22244     cp_parser_error (parser, "expected %<@end%>");
22245
22246   objc_finish_implementation ();
22247 }
22248
22249 /* Parse Objective-C ivars.  */
22250
22251 static void
22252 cp_parser_objc_class_ivars (cp_parser* parser)
22253 {
22254   cp_token *token = cp_lexer_peek_token (parser->lexer);
22255
22256   if (token->type != CPP_OPEN_BRACE)
22257     return;     /* No ivars specified.  */
22258
22259   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22260   token = cp_lexer_peek_token (parser->lexer);
22261
22262   while (token->type != CPP_CLOSE_BRACE 
22263         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22264     {
22265       cp_decl_specifier_seq declspecs;
22266       int decl_class_or_enum_p;
22267       tree prefix_attributes;
22268
22269       cp_parser_objc_visibility_spec (parser);
22270
22271       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22272         break;
22273
22274       cp_parser_decl_specifier_seq (parser,
22275                                     CP_PARSER_FLAGS_OPTIONAL,
22276                                     &declspecs,
22277                                     &decl_class_or_enum_p);
22278
22279       /* auto, register, static, extern, mutable.  */
22280       if (declspecs.storage_class != sc_none)
22281         {
22282           cp_parser_error (parser, "invalid type for instance variable");         
22283           declspecs.storage_class = sc_none;
22284         }
22285
22286       /* __thread.  */
22287       if (declspecs.specs[(int) ds_thread])
22288         {
22289           cp_parser_error (parser, "invalid type for instance variable");
22290           declspecs.specs[(int) ds_thread] = 0;
22291         }
22292       
22293       /* typedef.  */
22294       if (declspecs.specs[(int) ds_typedef])
22295         {
22296           cp_parser_error (parser, "invalid type for instance variable");
22297           declspecs.specs[(int) ds_typedef] = 0;
22298         }
22299
22300       prefix_attributes = declspecs.attributes;
22301       declspecs.attributes = NULL_TREE;
22302
22303       /* Keep going until we hit the `;' at the end of the
22304          declaration.  */
22305       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22306         {
22307           tree width = NULL_TREE, attributes, first_attribute, decl;
22308           cp_declarator *declarator = NULL;
22309           int ctor_dtor_or_conv_p;
22310
22311           /* Check for a (possibly unnamed) bitfield declaration.  */
22312           token = cp_lexer_peek_token (parser->lexer);
22313           if (token->type == CPP_COLON)
22314             goto eat_colon;
22315
22316           if (token->type == CPP_NAME
22317               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22318                   == CPP_COLON))
22319             {
22320               /* Get the name of the bitfield.  */
22321               declarator = make_id_declarator (NULL_TREE,
22322                                                cp_parser_identifier (parser),
22323                                                sfk_none);
22324
22325              eat_colon:
22326               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22327               /* Get the width of the bitfield.  */
22328               width
22329                 = cp_parser_constant_expression (parser,
22330                                                  /*allow_non_constant=*/false,
22331                                                  NULL);
22332             }
22333           else
22334             {
22335               /* Parse the declarator.  */
22336               declarator
22337                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22338                                         &ctor_dtor_or_conv_p,
22339                                         /*parenthesized_p=*/NULL,
22340                                         /*member_p=*/false);
22341             }
22342
22343           /* Look for attributes that apply to the ivar.  */
22344           attributes = cp_parser_attributes_opt (parser);
22345           /* Remember which attributes are prefix attributes and
22346              which are not.  */
22347           first_attribute = attributes;
22348           /* Combine the attributes.  */
22349           attributes = chainon (prefix_attributes, attributes);
22350
22351           if (width)
22352               /* Create the bitfield declaration.  */
22353               decl = grokbitfield (declarator, &declspecs,
22354                                    width,
22355                                    attributes);
22356           else
22357             decl = grokfield (declarator, &declspecs,
22358                               NULL_TREE, /*init_const_expr_p=*/false,
22359                               NULL_TREE, attributes);
22360
22361           /* Add the instance variable.  */
22362           objc_add_instance_variable (decl);
22363
22364           /* Reset PREFIX_ATTRIBUTES.  */
22365           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22366             attributes = TREE_CHAIN (attributes);
22367           if (attributes)
22368             TREE_CHAIN (attributes) = NULL_TREE;
22369
22370           token = cp_lexer_peek_token (parser->lexer);
22371
22372           if (token->type == CPP_COMMA)
22373             {
22374               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22375               continue;
22376             }
22377           break;
22378         }
22379
22380       cp_parser_consume_semicolon_at_end_of_statement (parser);
22381       token = cp_lexer_peek_token (parser->lexer);
22382     }
22383
22384   if (token->keyword == RID_AT_END)
22385     cp_parser_error (parser, "expected %<}%>");
22386
22387   /* Do not consume the RID_AT_END, so it will be read again as terminating
22388      the @interface of @implementation.  */ 
22389   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22390     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22391     
22392   /* For historical reasons, we accept an optional semicolon.  */
22393   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22394     cp_lexer_consume_token (parser->lexer);
22395 }
22396
22397 /* Parse an Objective-C protocol declaration.  */
22398
22399 static void
22400 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22401 {
22402   tree proto, protorefs;
22403   cp_token *tok;
22404
22405   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22406   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22407     {
22408       tok = cp_lexer_peek_token (parser->lexer);
22409       error_at (tok->location, "identifier expected after %<@protocol%>");
22410       goto finish;
22411     }
22412
22413   /* See if we have a forward declaration or a definition.  */
22414   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22415
22416   /* Try a forward declaration first.  */
22417   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22418     {
22419       objc_declare_protocols (cp_parser_objc_identifier_list (parser), 
22420                               attributes);
22421      finish:
22422       cp_parser_consume_semicolon_at_end_of_statement (parser);
22423     }
22424
22425   /* Ok, we got a full-fledged definition (or at least should).  */
22426   else
22427     {
22428       proto = cp_parser_identifier (parser);
22429       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22430       objc_start_protocol (proto, protorefs, attributes);
22431       cp_parser_objc_method_prototype_list (parser);
22432     }
22433 }
22434
22435 /* Parse an Objective-C superclass or category.  */
22436
22437 static void
22438 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22439                                                           tree *categ)
22440 {
22441   cp_token *next = cp_lexer_peek_token (parser->lexer);
22442
22443   *super = *categ = NULL_TREE;
22444   if (next->type == CPP_COLON)
22445     {
22446       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22447       *super = cp_parser_identifier (parser);
22448     }
22449   else if (next->type == CPP_OPEN_PAREN)
22450     {
22451       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22452       *categ = cp_parser_identifier (parser);
22453       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22454     }
22455 }
22456
22457 /* Parse an Objective-C class interface.  */
22458
22459 static void
22460 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22461 {
22462   tree name, super, categ, protos;
22463
22464   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22465   name = cp_parser_identifier (parser);
22466   if (name == error_mark_node)
22467     {
22468       /* It's hard to recover because even if valid @interface stuff
22469          is to follow, we can't compile it (or validate it) if we
22470          don't even know which class it refers to.  Let's assume this
22471          was a stray '@interface' token in the stream and skip it.
22472       */
22473       return;
22474     }
22475   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22476   protos = cp_parser_objc_protocol_refs_opt (parser);
22477
22478   /* We have either a class or a category on our hands.  */
22479   if (categ)
22480     objc_start_category_interface (name, categ, protos, attributes);
22481   else
22482     {
22483       objc_start_class_interface (name, super, protos, attributes);
22484       /* Handle instance variable declarations, if any.  */
22485       cp_parser_objc_class_ivars (parser);
22486       objc_continue_interface ();
22487     }
22488
22489   cp_parser_objc_method_prototype_list (parser);
22490 }
22491
22492 /* Parse an Objective-C class implementation.  */
22493
22494 static void
22495 cp_parser_objc_class_implementation (cp_parser* parser)
22496 {
22497   tree name, super, categ;
22498
22499   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22500   name = cp_parser_identifier (parser);
22501   if (name == error_mark_node)
22502     {
22503       /* It's hard to recover because even if valid @implementation
22504          stuff is to follow, we can't compile it (or validate it) if
22505          we don't even know which class it refers to.  Let's assume
22506          this was a stray '@implementation' token in the stream and
22507          skip it.
22508       */
22509       return;
22510     }
22511   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22512
22513   /* We have either a class or a category on our hands.  */
22514   if (categ)
22515     objc_start_category_implementation (name, categ);
22516   else
22517     {
22518       objc_start_class_implementation (name, super);
22519       /* Handle instance variable declarations, if any.  */
22520       cp_parser_objc_class_ivars (parser);
22521       objc_continue_implementation ();
22522     }
22523
22524   cp_parser_objc_method_definition_list (parser);
22525 }
22526
22527 /* Consume the @end token and finish off the implementation.  */
22528
22529 static void
22530 cp_parser_objc_end_implementation (cp_parser* parser)
22531 {
22532   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22533   objc_finish_implementation ();
22534 }
22535
22536 /* Parse an Objective-C declaration.  */
22537
22538 static void
22539 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22540 {
22541   /* Try to figure out what kind of declaration is present.  */
22542   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22543
22544   if (attributes)
22545     switch (kwd->keyword)
22546       {
22547         case RID_AT_ALIAS:
22548         case RID_AT_CLASS:
22549         case RID_AT_END:
22550           error_at (kwd->location, "attributes may not be specified before"
22551                     " the %<@%D%> Objective-C++ keyword",
22552                     kwd->u.value);
22553           attributes = NULL;
22554           break;
22555         case RID_AT_IMPLEMENTATION:
22556           warning_at (kwd->location, OPT_Wattributes,
22557                       "prefix attributes are ignored before %<@%D%>",
22558                       kwd->u.value);
22559           attributes = NULL;
22560         default:
22561           break;
22562       }
22563
22564   switch (kwd->keyword)
22565     {
22566     case RID_AT_ALIAS:
22567       cp_parser_objc_alias_declaration (parser);
22568       break;
22569     case RID_AT_CLASS:
22570       cp_parser_objc_class_declaration (parser);
22571       break;
22572     case RID_AT_PROTOCOL:
22573       cp_parser_objc_protocol_declaration (parser, attributes);
22574       break;
22575     case RID_AT_INTERFACE:
22576       cp_parser_objc_class_interface (parser, attributes);
22577       break;
22578     case RID_AT_IMPLEMENTATION:
22579       cp_parser_objc_class_implementation (parser);
22580       break;
22581     case RID_AT_END:
22582       cp_parser_objc_end_implementation (parser);
22583       break;
22584     default:
22585       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22586                 kwd->u.value);
22587       cp_parser_skip_to_end_of_block_or_statement (parser);
22588     }
22589 }
22590
22591 /* Parse an Objective-C try-catch-finally statement.
22592
22593    objc-try-catch-finally-stmt:
22594      @try compound-statement objc-catch-clause-seq [opt]
22595        objc-finally-clause [opt]
22596
22597    objc-catch-clause-seq:
22598      objc-catch-clause objc-catch-clause-seq [opt]
22599
22600    objc-catch-clause:
22601      @catch ( exception-declaration ) compound-statement
22602
22603    objc-finally-clause
22604      @finally compound-statement
22605
22606    Returns NULL_TREE.  */
22607
22608 static tree
22609 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22610   location_t location;
22611   tree stmt;
22612
22613   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22614   location = cp_lexer_peek_token (parser->lexer)->location;
22615   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22616      node, lest it get absorbed into the surrounding block.  */
22617   stmt = push_stmt_list ();
22618   cp_parser_compound_statement (parser, NULL, false);
22619   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22620
22621   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22622     {
22623       cp_parameter_declarator *parmdecl;
22624       tree parm;
22625
22626       cp_lexer_consume_token (parser->lexer);
22627       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22628       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22629       parm = grokdeclarator (parmdecl->declarator,
22630                              &parmdecl->decl_specifiers,
22631                              PARM, /*initialized=*/0,
22632                              /*attrlist=*/NULL);
22633       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22634       objc_begin_catch_clause (parm);
22635       cp_parser_compound_statement (parser, NULL, false);
22636       objc_finish_catch_clause ();
22637     }
22638
22639   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22640     {
22641       cp_lexer_consume_token (parser->lexer);
22642       location = cp_lexer_peek_token (parser->lexer)->location;
22643       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22644          node, lest it get absorbed into the surrounding block.  */
22645       stmt = push_stmt_list ();
22646       cp_parser_compound_statement (parser, NULL, false);
22647       objc_build_finally_clause (location, pop_stmt_list (stmt));
22648     }
22649
22650   return objc_finish_try_stmt ();
22651 }
22652
22653 /* Parse an Objective-C synchronized statement.
22654
22655    objc-synchronized-stmt:
22656      @synchronized ( expression ) compound-statement
22657
22658    Returns NULL_TREE.  */
22659
22660 static tree
22661 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22662   location_t location;
22663   tree lock, stmt;
22664
22665   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22666
22667   location = cp_lexer_peek_token (parser->lexer)->location;
22668   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22669   lock = cp_parser_expression (parser, false, NULL);
22670   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22671
22672   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22673      node, lest it get absorbed into the surrounding block.  */
22674   stmt = push_stmt_list ();
22675   cp_parser_compound_statement (parser, NULL, false);
22676
22677   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22678 }
22679
22680 /* Parse an Objective-C throw statement.
22681
22682    objc-throw-stmt:
22683      @throw assignment-expression [opt] ;
22684
22685    Returns a constructed '@throw' statement.  */
22686
22687 static tree
22688 cp_parser_objc_throw_statement (cp_parser *parser) {
22689   tree expr = NULL_TREE;
22690   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22691
22692   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22693
22694   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22695     expr = cp_parser_assignment_expression (parser, false, NULL);
22696
22697   cp_parser_consume_semicolon_at_end_of_statement (parser);
22698
22699   return objc_build_throw_stmt (loc, expr);
22700 }
22701
22702 /* Parse an Objective-C statement.  */
22703
22704 static tree
22705 cp_parser_objc_statement (cp_parser * parser) {
22706   /* Try to figure out what kind of declaration is present.  */
22707   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22708
22709   switch (kwd->keyword)
22710     {
22711     case RID_AT_TRY:
22712       return cp_parser_objc_try_catch_finally_statement (parser);
22713     case RID_AT_SYNCHRONIZED:
22714       return cp_parser_objc_synchronized_statement (parser);
22715     case RID_AT_THROW:
22716       return cp_parser_objc_throw_statement (parser);
22717     default:
22718       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22719                kwd->u.value);
22720       cp_parser_skip_to_end_of_block_or_statement (parser);
22721     }
22722
22723   return error_mark_node;
22724 }
22725
22726 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22727    look ahead to see if an objc keyword follows the attributes.  This
22728    is to detect the use of prefix attributes on ObjC @interface and 
22729    @protocol.  */
22730
22731 static bool
22732 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22733 {
22734   cp_lexer_save_tokens (parser->lexer);
22735   *attrib = cp_parser_attributes_opt (parser);
22736   gcc_assert (*attrib);
22737   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22738     {
22739       cp_lexer_commit_tokens (parser->lexer);
22740       return true;
22741     }
22742   cp_lexer_rollback_tokens (parser->lexer);
22743   return false;  
22744 }
22745
22746 /* This routine is a minimal replacement for
22747    c_parser_struct_declaration () used when parsing the list of
22748    types/names or ObjC++ properties.  For example, when parsing the
22749    code
22750
22751    @property (readonly) int a, b, c;
22752
22753    this function is responsible for parsing "int a, int b, int c" and
22754    returning the declarations as CHAIN of DECLs.
22755
22756    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22757    similar parsing.  */
22758 static tree
22759 cp_parser_objc_struct_declaration (cp_parser *parser)
22760 {
22761   tree decls = NULL_TREE;
22762   cp_decl_specifier_seq declspecs;
22763   int decl_class_or_enum_p;
22764   tree prefix_attributes;
22765
22766   cp_parser_decl_specifier_seq (parser,
22767                                 CP_PARSER_FLAGS_NONE,
22768                                 &declspecs,
22769                                 &decl_class_or_enum_p);
22770
22771   if (declspecs.type == error_mark_node)
22772     return error_mark_node;
22773
22774   /* auto, register, static, extern, mutable.  */
22775   if (declspecs.storage_class != sc_none)
22776     {
22777       cp_parser_error (parser, "invalid type for property");
22778       declspecs.storage_class = sc_none;
22779     }
22780   
22781   /* __thread.  */
22782   if (declspecs.specs[(int) ds_thread])
22783     {
22784       cp_parser_error (parser, "invalid type for property");
22785       declspecs.specs[(int) ds_thread] = 0;
22786     }
22787   
22788   /* typedef.  */
22789   if (declspecs.specs[(int) ds_typedef])
22790     {
22791       cp_parser_error (parser, "invalid type for property");
22792       declspecs.specs[(int) ds_typedef] = 0;
22793     }
22794
22795   prefix_attributes = declspecs.attributes;
22796   declspecs.attributes = NULL_TREE;
22797
22798   /* Keep going until we hit the `;' at the end of the declaration. */
22799   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22800     {
22801       tree attributes, first_attribute, decl;
22802       cp_declarator *declarator;
22803       cp_token *token;
22804
22805       /* Parse the declarator.  */
22806       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22807                                          NULL, NULL, false);
22808
22809       /* Look for attributes that apply to the ivar.  */
22810       attributes = cp_parser_attributes_opt (parser);
22811       /* Remember which attributes are prefix attributes and
22812          which are not.  */
22813       first_attribute = attributes;
22814       /* Combine the attributes.  */
22815       attributes = chainon (prefix_attributes, attributes);
22816       
22817       decl = grokfield (declarator, &declspecs,
22818                         NULL_TREE, /*init_const_expr_p=*/false,
22819                         NULL_TREE, attributes);
22820
22821       if (decl == error_mark_node || decl == NULL_TREE)
22822         return error_mark_node;
22823       
22824       /* Reset PREFIX_ATTRIBUTES.  */
22825       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22826         attributes = TREE_CHAIN (attributes);
22827       if (attributes)
22828         TREE_CHAIN (attributes) = NULL_TREE;
22829
22830       DECL_CHAIN (decl) = decls;
22831       decls = decl;
22832
22833       token = cp_lexer_peek_token (parser->lexer);
22834       if (token->type == CPP_COMMA)
22835         {
22836           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22837           continue;
22838         }
22839       else
22840         break;
22841     }
22842   return decls;
22843 }
22844
22845 /* Parse an Objective-C @property declaration.  The syntax is:
22846
22847    objc-property-declaration:
22848      '@property' objc-property-attributes[opt] struct-declaration ;
22849
22850    objc-property-attributes:
22851     '(' objc-property-attribute-list ')'
22852
22853    objc-property-attribute-list:
22854      objc-property-attribute
22855      objc-property-attribute-list, objc-property-attribute
22856
22857    objc-property-attribute
22858      'getter' = identifier
22859      'setter' = identifier
22860      'readonly'
22861      'readwrite'
22862      'assign'
22863      'retain'
22864      'copy'
22865      'nonatomic'
22866
22867   For example:
22868     @property NSString *name;
22869     @property (readonly) id object;
22870     @property (retain, nonatomic, getter=getTheName) id name;
22871     @property int a, b, c;
22872
22873    PS: This function is identical to
22874    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
22875 static void 
22876 cp_parser_objc_at_property_declaration (cp_parser *parser)
22877 {
22878   /* The following variables hold the attributes of the properties as
22879      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
22880      seen.  When we see an attribute, we set them to 'true' (if they
22881      are boolean properties) or to the identifier (if they have an
22882      argument, ie, for getter and setter).  Note that here we only
22883      parse the list of attributes, check the syntax and accumulate the
22884      attributes that we find.  objc_add_property_declaration() will
22885      then process the information.  */
22886   bool property_assign = false;
22887   bool property_copy = false;
22888   tree property_getter_ident = NULL_TREE;
22889   bool property_nonatomic = false;
22890   bool property_readonly = false;
22891   bool property_readwrite = false;
22892   bool property_retain = false;
22893   tree property_setter_ident = NULL_TREE;
22894
22895   /* 'properties' is the list of properties that we read.  Usually a
22896      single one, but maybe more (eg, in "@property int a, b, c;" there
22897      are three).  */
22898   tree properties;
22899   location_t loc;
22900
22901   loc = cp_lexer_peek_token (parser->lexer)->location;
22902
22903   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
22904
22905   /* Parse the optional attribute list...  */
22906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22907     {
22908       /* Eat the '('.  */
22909       cp_lexer_consume_token (parser->lexer);
22910
22911       while (true)
22912         {
22913           bool syntax_error = false;
22914           cp_token *token = cp_lexer_peek_token (parser->lexer);
22915           enum rid keyword;
22916
22917           if (token->type != CPP_NAME)
22918             {
22919               cp_parser_error (parser, "expected identifier");
22920               break;
22921             }
22922           keyword = C_RID_CODE (token->u.value);
22923           cp_lexer_consume_token (parser->lexer);
22924           switch (keyword)
22925             {
22926             case RID_ASSIGN:    property_assign = true;    break;
22927             case RID_COPY:      property_copy = true;      break;
22928             case RID_NONATOMIC: property_nonatomic = true; break;
22929             case RID_READONLY:  property_readonly = true;  break;
22930             case RID_READWRITE: property_readwrite = true; break;
22931             case RID_RETAIN:    property_retain = true;    break;
22932
22933             case RID_GETTER:
22934             case RID_SETTER:
22935               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22936                 {
22937                   cp_parser_error (parser,
22938                                    "getter/setter/ivar attribute must be followed by %<=%>");
22939                   syntax_error = true;
22940                   break;
22941                 }
22942               cp_lexer_consume_token (parser->lexer); /* eat the = */
22943               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22944                 {
22945                   cp_parser_error (parser, "expected identifier");
22946                   syntax_error = true;
22947                   break;
22948                 }
22949               if (keyword == RID_SETTER)
22950                 {
22951                   if (property_setter_ident != NULL_TREE)
22952                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22953                   else
22954                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22955                   cp_lexer_consume_token (parser->lexer);
22956                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22957                     cp_parser_error (parser, "setter name must terminate with %<:%>");
22958                   else
22959                     cp_lexer_consume_token (parser->lexer);
22960                 }
22961               else
22962                 {
22963                   if (property_getter_ident != NULL_TREE)
22964                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22965                   else
22966                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22967                   cp_lexer_consume_token (parser->lexer);
22968                 }
22969               break;
22970             default:
22971               cp_parser_error (parser, "unknown property attribute");
22972               syntax_error = true;
22973               break;
22974             }
22975
22976           if (syntax_error)
22977             break;
22978           
22979           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22980             cp_lexer_consume_token (parser->lexer);
22981           else
22982             break;
22983         }
22984
22985       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22986         {
22987           cp_parser_skip_to_closing_parenthesis (parser,
22988                                                  /*recovering=*/true,
22989                                                  /*or_comma=*/false,
22990                                                  /*consume_paren=*/true);
22991         }
22992     }
22993
22994   /* ... and the property declaration(s).  */
22995   properties = cp_parser_objc_struct_declaration (parser);
22996
22997   if (properties == error_mark_node)
22998     {
22999       cp_parser_skip_to_end_of_statement (parser);
23000       /* If the next token is now a `;', consume it.  */
23001       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23002         cp_lexer_consume_token (parser->lexer);
23003       return;
23004     }
23005
23006   if (properties == NULL_TREE)
23007     cp_parser_error (parser, "expected identifier");
23008   else
23009     {
23010       /* Comma-separated properties are chained together in
23011          reverse order; add them one by one.  */
23012       properties = nreverse (properties);
23013       
23014       for (; properties; properties = TREE_CHAIN (properties))
23015         objc_add_property_declaration (loc, copy_node (properties),
23016                                        property_readonly, property_readwrite,
23017                                        property_assign, property_retain,
23018                                        property_copy, property_nonatomic,
23019                                        property_getter_ident, property_setter_ident);
23020     }
23021   
23022   cp_parser_consume_semicolon_at_end_of_statement (parser);
23023 }
23024
23025 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23026
23027    objc-synthesize-declaration:
23028      @synthesize objc-synthesize-identifier-list ;
23029
23030    objc-synthesize-identifier-list:
23031      objc-synthesize-identifier
23032      objc-synthesize-identifier-list, objc-synthesize-identifier
23033
23034    objc-synthesize-identifier
23035      identifier
23036      identifier = identifier
23037
23038   For example:
23039     @synthesize MyProperty;
23040     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23041
23042   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23043   for C.  Keep them in sync.
23044 */
23045 static void 
23046 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23047 {
23048   tree list = NULL_TREE;
23049   location_t loc;
23050   loc = cp_lexer_peek_token (parser->lexer)->location;
23051
23052   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23053   while (true)
23054     {
23055       tree property, ivar;
23056       property = cp_parser_identifier (parser);
23057       if (property == error_mark_node)
23058         {
23059           cp_parser_consume_semicolon_at_end_of_statement (parser);
23060           return;
23061         }
23062       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23063         {
23064           cp_lexer_consume_token (parser->lexer);
23065           ivar = cp_parser_identifier (parser);
23066           if (ivar == error_mark_node)
23067             {
23068               cp_parser_consume_semicolon_at_end_of_statement (parser);
23069               return;
23070             }
23071         }
23072       else
23073         ivar = NULL_TREE;
23074       list = chainon (list, build_tree_list (ivar, property));
23075       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23076         cp_lexer_consume_token (parser->lexer);
23077       else
23078         break;
23079     }
23080   cp_parser_consume_semicolon_at_end_of_statement (parser);
23081   objc_add_synthesize_declaration (loc, list);
23082 }
23083
23084 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23085
23086    objc-dynamic-declaration:
23087      @dynamic identifier-list ;
23088
23089    For example:
23090      @dynamic MyProperty;
23091      @dynamic MyProperty, AnotherProperty;
23092
23093   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23094   for C.  Keep them in sync.
23095 */
23096 static void 
23097 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23098 {
23099   tree list = NULL_TREE;
23100   location_t loc;
23101   loc = cp_lexer_peek_token (parser->lexer)->location;
23102
23103   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23104   while (true)
23105     {
23106       tree property;
23107       property = cp_parser_identifier (parser);
23108       if (property == error_mark_node)
23109         {
23110           cp_parser_consume_semicolon_at_end_of_statement (parser);
23111           return;
23112         }
23113       list = chainon (list, build_tree_list (NULL, property));
23114       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23115         cp_lexer_consume_token (parser->lexer);
23116       else
23117         break;
23118     }
23119   cp_parser_consume_semicolon_at_end_of_statement (parser);
23120   objc_add_dynamic_declaration (loc, list);
23121 }
23122
23123 \f
23124 /* OpenMP 2.5 parsing routines.  */
23125
23126 /* Returns name of the next clause.
23127    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23128    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23129    returned and the token is consumed.  */
23130
23131 static pragma_omp_clause
23132 cp_parser_omp_clause_name (cp_parser *parser)
23133 {
23134   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23135
23136   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23137     result = PRAGMA_OMP_CLAUSE_IF;
23138   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23139     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23140   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23141     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23142   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23143     {
23144       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23145       const char *p = IDENTIFIER_POINTER (id);
23146
23147       switch (p[0])
23148         {
23149         case 'c':
23150           if (!strcmp ("collapse", p))
23151             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23152           else if (!strcmp ("copyin", p))
23153             result = PRAGMA_OMP_CLAUSE_COPYIN;
23154           else if (!strcmp ("copyprivate", p))
23155             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23156           break;
23157         case 'f':
23158           if (!strcmp ("firstprivate", p))
23159             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23160           break;
23161         case 'l':
23162           if (!strcmp ("lastprivate", p))
23163             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23164           break;
23165         case 'n':
23166           if (!strcmp ("nowait", p))
23167             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23168           else if (!strcmp ("num_threads", p))
23169             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23170           break;
23171         case 'o':
23172           if (!strcmp ("ordered", p))
23173             result = PRAGMA_OMP_CLAUSE_ORDERED;
23174           break;
23175         case 'r':
23176           if (!strcmp ("reduction", p))
23177             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23178           break;
23179         case 's':
23180           if (!strcmp ("schedule", p))
23181             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23182           else if (!strcmp ("shared", p))
23183             result = PRAGMA_OMP_CLAUSE_SHARED;
23184           break;
23185         case 'u':
23186           if (!strcmp ("untied", p))
23187             result = PRAGMA_OMP_CLAUSE_UNTIED;
23188           break;
23189         }
23190     }
23191
23192   if (result != PRAGMA_OMP_CLAUSE_NONE)
23193     cp_lexer_consume_token (parser->lexer);
23194
23195   return result;
23196 }
23197
23198 /* Validate that a clause of the given type does not already exist.  */
23199
23200 static void
23201 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23202                            const char *name, location_t location)
23203 {
23204   tree c;
23205
23206   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23207     if (OMP_CLAUSE_CODE (c) == code)
23208       {
23209         error_at (location, "too many %qs clauses", name);
23210         break;
23211       }
23212 }
23213
23214 /* OpenMP 2.5:
23215    variable-list:
23216      identifier
23217      variable-list , identifier
23218
23219    In addition, we match a closing parenthesis.  An opening parenthesis
23220    will have been consumed by the caller.
23221
23222    If KIND is nonzero, create the appropriate node and install the decl
23223    in OMP_CLAUSE_DECL and add the node to the head of the list.
23224
23225    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23226    return the list created.  */
23227
23228 static tree
23229 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23230                                 tree list)
23231 {
23232   cp_token *token;
23233   while (1)
23234     {
23235       tree name, decl;
23236
23237       token = cp_lexer_peek_token (parser->lexer);
23238       name = cp_parser_id_expression (parser, /*template_p=*/false,
23239                                       /*check_dependency_p=*/true,
23240                                       /*template_p=*/NULL,
23241                                       /*declarator_p=*/false,
23242                                       /*optional_p=*/false);
23243       if (name == error_mark_node)
23244         goto skip_comma;
23245
23246       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23247       if (decl == error_mark_node)
23248         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23249                                      token->location);
23250       else if (kind != 0)
23251         {
23252           tree u = build_omp_clause (token->location, kind);
23253           OMP_CLAUSE_DECL (u) = decl;
23254           OMP_CLAUSE_CHAIN (u) = list;
23255           list = u;
23256         }
23257       else
23258         list = tree_cons (decl, NULL_TREE, list);
23259
23260     get_comma:
23261       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23262         break;
23263       cp_lexer_consume_token (parser->lexer);
23264     }
23265
23266   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23267     {
23268       int ending;
23269
23270       /* Try to resync to an unnested comma.  Copied from
23271          cp_parser_parenthesized_expression_list.  */
23272     skip_comma:
23273       ending = cp_parser_skip_to_closing_parenthesis (parser,
23274                                                       /*recovering=*/true,
23275                                                       /*or_comma=*/true,
23276                                                       /*consume_paren=*/true);
23277       if (ending < 0)
23278         goto get_comma;
23279     }
23280
23281   return list;
23282 }
23283
23284 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23285    common case for omp clauses.  */
23286
23287 static tree
23288 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23289 {
23290   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23291     return cp_parser_omp_var_list_no_open (parser, kind, list);
23292   return list;
23293 }
23294
23295 /* OpenMP 3.0:
23296    collapse ( constant-expression ) */
23297
23298 static tree
23299 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23300 {
23301   tree c, num;
23302   location_t loc;
23303   HOST_WIDE_INT n;
23304
23305   loc = cp_lexer_peek_token (parser->lexer)->location;
23306   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23307     return list;
23308
23309   num = cp_parser_constant_expression (parser, false, NULL);
23310
23311   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23312     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23313                                            /*or_comma=*/false,
23314                                            /*consume_paren=*/true);
23315
23316   if (num == error_mark_node)
23317     return list;
23318   num = fold_non_dependent_expr (num);
23319   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23320       || !host_integerp (num, 0)
23321       || (n = tree_low_cst (num, 0)) <= 0
23322       || (int) n != n)
23323     {
23324       error_at (loc, "collapse argument needs positive constant integer expression");
23325       return list;
23326     }
23327
23328   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23329   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23330   OMP_CLAUSE_CHAIN (c) = list;
23331   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23332
23333   return c;
23334 }
23335
23336 /* OpenMP 2.5:
23337    default ( shared | none ) */
23338
23339 static tree
23340 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23341 {
23342   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23343   tree c;
23344
23345   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23346     return list;
23347   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23348     {
23349       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23350       const char *p = IDENTIFIER_POINTER (id);
23351
23352       switch (p[0])
23353         {
23354         case 'n':
23355           if (strcmp ("none", p) != 0)
23356             goto invalid_kind;
23357           kind = OMP_CLAUSE_DEFAULT_NONE;
23358           break;
23359
23360         case 's':
23361           if (strcmp ("shared", p) != 0)
23362             goto invalid_kind;
23363           kind = OMP_CLAUSE_DEFAULT_SHARED;
23364           break;
23365
23366         default:
23367           goto invalid_kind;
23368         }
23369
23370       cp_lexer_consume_token (parser->lexer);
23371     }
23372   else
23373     {
23374     invalid_kind:
23375       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23376     }
23377
23378   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23379     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23380                                            /*or_comma=*/false,
23381                                            /*consume_paren=*/true);
23382
23383   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23384     return list;
23385
23386   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23387   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23388   OMP_CLAUSE_CHAIN (c) = list;
23389   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23390
23391   return c;
23392 }
23393
23394 /* OpenMP 2.5:
23395    if ( expression ) */
23396
23397 static tree
23398 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23399 {
23400   tree t, c;
23401
23402   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23403     return list;
23404
23405   t = cp_parser_condition (parser);
23406
23407   if (t == error_mark_node
23408       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23409     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23410                                            /*or_comma=*/false,
23411                                            /*consume_paren=*/true);
23412
23413   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23414
23415   c = build_omp_clause (location, OMP_CLAUSE_IF);
23416   OMP_CLAUSE_IF_EXPR (c) = t;
23417   OMP_CLAUSE_CHAIN (c) = list;
23418
23419   return c;
23420 }
23421
23422 /* OpenMP 2.5:
23423    nowait */
23424
23425 static tree
23426 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23427                              tree list, location_t location)
23428 {
23429   tree c;
23430
23431   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23432
23433   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23434   OMP_CLAUSE_CHAIN (c) = list;
23435   return c;
23436 }
23437
23438 /* OpenMP 2.5:
23439    num_threads ( expression ) */
23440
23441 static tree
23442 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23443                                   location_t location)
23444 {
23445   tree t, c;
23446
23447   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23448     return list;
23449
23450   t = cp_parser_expression (parser, false, NULL);
23451
23452   if (t == error_mark_node
23453       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23454     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23455                                            /*or_comma=*/false,
23456                                            /*consume_paren=*/true);
23457
23458   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23459                              "num_threads", location);
23460
23461   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23462   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23463   OMP_CLAUSE_CHAIN (c) = list;
23464
23465   return c;
23466 }
23467
23468 /* OpenMP 2.5:
23469    ordered */
23470
23471 static tree
23472 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23473                               tree list, location_t location)
23474 {
23475   tree c;
23476
23477   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23478                              "ordered", location);
23479
23480   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23481   OMP_CLAUSE_CHAIN (c) = list;
23482   return c;
23483 }
23484
23485 /* OpenMP 2.5:
23486    reduction ( reduction-operator : variable-list )
23487
23488    reduction-operator:
23489      One of: + * - & ^ | && || */
23490
23491 static tree
23492 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23493 {
23494   enum tree_code code;
23495   tree nlist, c;
23496
23497   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23498     return list;
23499
23500   switch (cp_lexer_peek_token (parser->lexer)->type)
23501     {
23502     case CPP_PLUS:
23503       code = PLUS_EXPR;
23504       break;
23505     case CPP_MULT:
23506       code = MULT_EXPR;
23507       break;
23508     case CPP_MINUS:
23509       code = MINUS_EXPR;
23510       break;
23511     case CPP_AND:
23512       code = BIT_AND_EXPR;
23513       break;
23514     case CPP_XOR:
23515       code = BIT_XOR_EXPR;
23516       break;
23517     case CPP_OR:
23518       code = BIT_IOR_EXPR;
23519       break;
23520     case CPP_AND_AND:
23521       code = TRUTH_ANDIF_EXPR;
23522       break;
23523     case CPP_OR_OR:
23524       code = TRUTH_ORIF_EXPR;
23525       break;
23526     default:
23527       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23528                                "%<|%>, %<&&%>, or %<||%>");
23529     resync_fail:
23530       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23531                                              /*or_comma=*/false,
23532                                              /*consume_paren=*/true);
23533       return list;
23534     }
23535   cp_lexer_consume_token (parser->lexer);
23536
23537   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23538     goto resync_fail;
23539
23540   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23541   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23542     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23543
23544   return nlist;
23545 }
23546
23547 /* OpenMP 2.5:
23548    schedule ( schedule-kind )
23549    schedule ( schedule-kind , expression )
23550
23551    schedule-kind:
23552      static | dynamic | guided | runtime | auto  */
23553
23554 static tree
23555 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23556 {
23557   tree c, t;
23558
23559   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23560     return list;
23561
23562   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23563
23564   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23565     {
23566       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23567       const char *p = IDENTIFIER_POINTER (id);
23568
23569       switch (p[0])
23570         {
23571         case 'd':
23572           if (strcmp ("dynamic", p) != 0)
23573             goto invalid_kind;
23574           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23575           break;
23576
23577         case 'g':
23578           if (strcmp ("guided", p) != 0)
23579             goto invalid_kind;
23580           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23581           break;
23582
23583         case 'r':
23584           if (strcmp ("runtime", p) != 0)
23585             goto invalid_kind;
23586           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23587           break;
23588
23589         default:
23590           goto invalid_kind;
23591         }
23592     }
23593   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23594     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23595   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23596     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23597   else
23598     goto invalid_kind;
23599   cp_lexer_consume_token (parser->lexer);
23600
23601   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23602     {
23603       cp_token *token;
23604       cp_lexer_consume_token (parser->lexer);
23605
23606       token = cp_lexer_peek_token (parser->lexer);
23607       t = cp_parser_assignment_expression (parser, false, NULL);
23608
23609       if (t == error_mark_node)
23610         goto resync_fail;
23611       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23612         error_at (token->location, "schedule %<runtime%> does not take "
23613                   "a %<chunk_size%> parameter");
23614       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23615         error_at (token->location, "schedule %<auto%> does not take "
23616                   "a %<chunk_size%> parameter");
23617       else
23618         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23619
23620       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23621         goto resync_fail;
23622     }
23623   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23624     goto resync_fail;
23625
23626   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23627   OMP_CLAUSE_CHAIN (c) = list;
23628   return c;
23629
23630  invalid_kind:
23631   cp_parser_error (parser, "invalid schedule kind");
23632  resync_fail:
23633   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23634                                          /*or_comma=*/false,
23635                                          /*consume_paren=*/true);
23636   return list;
23637 }
23638
23639 /* OpenMP 3.0:
23640    untied */
23641
23642 static tree
23643 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23644                              tree list, location_t location)
23645 {
23646   tree c;
23647
23648   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23649
23650   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23651   OMP_CLAUSE_CHAIN (c) = list;
23652   return c;
23653 }
23654
23655 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23656    is a bitmask in MASK.  Return the list of clauses found; the result
23657    of clause default goes in *pdefault.  */
23658
23659 static tree
23660 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23661                            const char *where, cp_token *pragma_tok)
23662 {
23663   tree clauses = NULL;
23664   bool first = true;
23665   cp_token *token = NULL;
23666
23667   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23668     {
23669       pragma_omp_clause c_kind;
23670       const char *c_name;
23671       tree prev = clauses;
23672
23673       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23674         cp_lexer_consume_token (parser->lexer);
23675
23676       token = cp_lexer_peek_token (parser->lexer);
23677       c_kind = cp_parser_omp_clause_name (parser);
23678       first = false;
23679
23680       switch (c_kind)
23681         {
23682         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23683           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23684                                                    token->location);
23685           c_name = "collapse";
23686           break;
23687         case PRAGMA_OMP_CLAUSE_COPYIN:
23688           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23689           c_name = "copyin";
23690           break;
23691         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23692           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23693                                             clauses);
23694           c_name = "copyprivate";
23695           break;
23696         case PRAGMA_OMP_CLAUSE_DEFAULT:
23697           clauses = cp_parser_omp_clause_default (parser, clauses,
23698                                                   token->location);
23699           c_name = "default";
23700           break;
23701         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23702           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23703                                             clauses);
23704           c_name = "firstprivate";
23705           break;
23706         case PRAGMA_OMP_CLAUSE_IF:
23707           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23708           c_name = "if";
23709           break;
23710         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23711           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23712                                             clauses);
23713           c_name = "lastprivate";
23714           break;
23715         case PRAGMA_OMP_CLAUSE_NOWAIT:
23716           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23717           c_name = "nowait";
23718           break;
23719         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23720           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23721                                                       token->location);
23722           c_name = "num_threads";
23723           break;
23724         case PRAGMA_OMP_CLAUSE_ORDERED:
23725           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23726                                                   token->location);
23727           c_name = "ordered";
23728           break;
23729         case PRAGMA_OMP_CLAUSE_PRIVATE:
23730           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23731                                             clauses);
23732           c_name = "private";
23733           break;
23734         case PRAGMA_OMP_CLAUSE_REDUCTION:
23735           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23736           c_name = "reduction";
23737           break;
23738         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23739           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23740                                                    token->location);
23741           c_name = "schedule";
23742           break;
23743         case PRAGMA_OMP_CLAUSE_SHARED:
23744           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23745                                             clauses);
23746           c_name = "shared";
23747           break;
23748         case PRAGMA_OMP_CLAUSE_UNTIED:
23749           clauses = cp_parser_omp_clause_untied (parser, clauses,
23750                                                  token->location);
23751           c_name = "nowait";
23752           break;
23753         default:
23754           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23755           goto saw_error;
23756         }
23757
23758       if (((mask >> c_kind) & 1) == 0)
23759         {
23760           /* Remove the invalid clause(s) from the list to avoid
23761              confusing the rest of the compiler.  */
23762           clauses = prev;
23763           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23764         }
23765     }
23766  saw_error:
23767   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23768   return finish_omp_clauses (clauses);
23769 }
23770
23771 /* OpenMP 2.5:
23772    structured-block:
23773      statement
23774
23775    In practice, we're also interested in adding the statement to an
23776    outer node.  So it is convenient if we work around the fact that
23777    cp_parser_statement calls add_stmt.  */
23778
23779 static unsigned
23780 cp_parser_begin_omp_structured_block (cp_parser *parser)
23781 {
23782   unsigned save = parser->in_statement;
23783
23784   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23785      This preserves the "not within loop or switch" style error messages
23786      for nonsense cases like
23787         void foo() {
23788         #pragma omp single
23789           break;
23790         }
23791   */
23792   if (parser->in_statement)
23793     parser->in_statement = IN_OMP_BLOCK;
23794
23795   return save;
23796 }
23797
23798 static void
23799 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23800 {
23801   parser->in_statement = save;
23802 }
23803
23804 static tree
23805 cp_parser_omp_structured_block (cp_parser *parser)
23806 {
23807   tree stmt = begin_omp_structured_block ();
23808   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23809
23810   cp_parser_statement (parser, NULL_TREE, false, NULL);
23811
23812   cp_parser_end_omp_structured_block (parser, save);
23813   return finish_omp_structured_block (stmt);
23814 }
23815
23816 /* OpenMP 2.5:
23817    # pragma omp atomic new-line
23818      expression-stmt
23819
23820    expression-stmt:
23821      x binop= expr | x++ | ++x | x-- | --x
23822    binop:
23823      +, *, -, /, &, ^, |, <<, >>
23824
23825   where x is an lvalue expression with scalar type.  */
23826
23827 static void
23828 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23829 {
23830   tree lhs, rhs;
23831   enum tree_code code;
23832
23833   cp_parser_require_pragma_eol (parser, pragma_tok);
23834
23835   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23836                                     /*cast_p=*/false, NULL);
23837   switch (TREE_CODE (lhs))
23838     {
23839     case ERROR_MARK:
23840       goto saw_error;
23841
23842     case PREINCREMENT_EXPR:
23843     case POSTINCREMENT_EXPR:
23844       lhs = TREE_OPERAND (lhs, 0);
23845       code = PLUS_EXPR;
23846       rhs = integer_one_node;
23847       break;
23848
23849     case PREDECREMENT_EXPR:
23850     case POSTDECREMENT_EXPR:
23851       lhs = TREE_OPERAND (lhs, 0);
23852       code = MINUS_EXPR;
23853       rhs = integer_one_node;
23854       break;
23855
23856     case COMPOUND_EXPR:
23857       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23858          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23859          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23860          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23861          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23862                                              (TREE_OPERAND (lhs, 1), 0), 0)))
23863             == BOOLEAN_TYPE)
23864        /* Undo effects of boolean_increment for post {in,de}crement.  */
23865        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23866       /* FALLTHRU */
23867     case MODIFY_EXPR:
23868       if (TREE_CODE (lhs) == MODIFY_EXPR
23869          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23870        {
23871          /* Undo effects of boolean_increment.  */
23872          if (integer_onep (TREE_OPERAND (lhs, 1)))
23873            {
23874              /* This is pre or post increment.  */
23875              rhs = TREE_OPERAND (lhs, 1);
23876              lhs = TREE_OPERAND (lhs, 0);
23877              code = NOP_EXPR;
23878              break;
23879            }
23880        }
23881       /* FALLTHRU */
23882     default:
23883       switch (cp_lexer_peek_token (parser->lexer)->type)
23884         {
23885         case CPP_MULT_EQ:
23886           code = MULT_EXPR;
23887           break;
23888         case CPP_DIV_EQ:
23889           code = TRUNC_DIV_EXPR;
23890           break;
23891         case CPP_PLUS_EQ:
23892           code = PLUS_EXPR;
23893           break;
23894         case CPP_MINUS_EQ:
23895           code = MINUS_EXPR;
23896           break;
23897         case CPP_LSHIFT_EQ:
23898           code = LSHIFT_EXPR;
23899           break;
23900         case CPP_RSHIFT_EQ:
23901           code = RSHIFT_EXPR;
23902           break;
23903         case CPP_AND_EQ:
23904           code = BIT_AND_EXPR;
23905           break;
23906         case CPP_OR_EQ:
23907           code = BIT_IOR_EXPR;
23908           break;
23909         case CPP_XOR_EQ:
23910           code = BIT_XOR_EXPR;
23911           break;
23912         default:
23913           cp_parser_error (parser,
23914                            "invalid operator for %<#pragma omp atomic%>");
23915           goto saw_error;
23916         }
23917       cp_lexer_consume_token (parser->lexer);
23918
23919       rhs = cp_parser_expression (parser, false, NULL);
23920       if (rhs == error_mark_node)
23921         goto saw_error;
23922       break;
23923     }
23924   finish_omp_atomic (code, lhs, rhs);
23925   cp_parser_consume_semicolon_at_end_of_statement (parser);
23926   return;
23927
23928  saw_error:
23929   cp_parser_skip_to_end_of_block_or_statement (parser);
23930 }
23931
23932
23933 /* OpenMP 2.5:
23934    # pragma omp barrier new-line  */
23935
23936 static void
23937 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23938 {
23939   cp_parser_require_pragma_eol (parser, pragma_tok);
23940   finish_omp_barrier ();
23941 }
23942
23943 /* OpenMP 2.5:
23944    # pragma omp critical [(name)] new-line
23945      structured-block  */
23946
23947 static tree
23948 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23949 {
23950   tree stmt, name = NULL;
23951
23952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23953     {
23954       cp_lexer_consume_token (parser->lexer);
23955
23956       name = cp_parser_identifier (parser);
23957
23958       if (name == error_mark_node
23959           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23960         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23961                                                /*or_comma=*/false,
23962                                                /*consume_paren=*/true);
23963       if (name == error_mark_node)
23964         name = NULL;
23965     }
23966   cp_parser_require_pragma_eol (parser, pragma_tok);
23967
23968   stmt = cp_parser_omp_structured_block (parser);
23969   return c_finish_omp_critical (input_location, stmt, name);
23970 }
23971
23972 /* OpenMP 2.5:
23973    # pragma omp flush flush-vars[opt] new-line
23974
23975    flush-vars:
23976      ( variable-list ) */
23977
23978 static void
23979 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23980 {
23981   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23982     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23983   cp_parser_require_pragma_eol (parser, pragma_tok);
23984
23985   finish_omp_flush ();
23986 }
23987
23988 /* Helper function, to parse omp for increment expression.  */
23989
23990 static tree
23991 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23992 {
23993   tree cond = cp_parser_binary_expression (parser, false, true,
23994                                            PREC_NOT_OPERATOR, NULL);
23995   bool overloaded_p;
23996
23997   if (cond == error_mark_node
23998       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23999     {
24000       cp_parser_skip_to_end_of_statement (parser);
24001       return error_mark_node;
24002     }
24003
24004   switch (TREE_CODE (cond))
24005     {
24006     case GT_EXPR:
24007     case GE_EXPR:
24008     case LT_EXPR:
24009     case LE_EXPR:
24010       break;
24011     default:
24012       return error_mark_node;
24013     }
24014
24015   /* If decl is an iterator, preserve LHS and RHS of the relational
24016      expr until finish_omp_for.  */
24017   if (decl
24018       && (type_dependent_expression_p (decl)
24019           || CLASS_TYPE_P (TREE_TYPE (decl))))
24020     return cond;
24021
24022   return build_x_binary_op (TREE_CODE (cond),
24023                             TREE_OPERAND (cond, 0), ERROR_MARK,
24024                             TREE_OPERAND (cond, 1), ERROR_MARK,
24025                             &overloaded_p, tf_warning_or_error);
24026 }
24027
24028 /* Helper function, to parse omp for increment expression.  */
24029
24030 static tree
24031 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24032 {
24033   cp_token *token = cp_lexer_peek_token (parser->lexer);
24034   enum tree_code op;
24035   tree lhs, rhs;
24036   cp_id_kind idk;
24037   bool decl_first;
24038
24039   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24040     {
24041       op = (token->type == CPP_PLUS_PLUS
24042             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24043       cp_lexer_consume_token (parser->lexer);
24044       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24045       if (lhs != decl)
24046         return error_mark_node;
24047       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24048     }
24049
24050   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24051   if (lhs != decl)
24052     return error_mark_node;
24053
24054   token = cp_lexer_peek_token (parser->lexer);
24055   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24056     {
24057       op = (token->type == CPP_PLUS_PLUS
24058             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24059       cp_lexer_consume_token (parser->lexer);
24060       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24061     }
24062
24063   op = cp_parser_assignment_operator_opt (parser);
24064   if (op == ERROR_MARK)
24065     return error_mark_node;
24066
24067   if (op != NOP_EXPR)
24068     {
24069       rhs = cp_parser_assignment_expression (parser, false, NULL);
24070       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24071       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24072     }
24073
24074   lhs = cp_parser_binary_expression (parser, false, false,
24075                                      PREC_ADDITIVE_EXPRESSION, NULL);
24076   token = cp_lexer_peek_token (parser->lexer);
24077   decl_first = lhs == decl;
24078   if (decl_first)
24079     lhs = NULL_TREE;
24080   if (token->type != CPP_PLUS
24081       && token->type != CPP_MINUS)
24082     return error_mark_node;
24083
24084   do
24085     {
24086       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24087       cp_lexer_consume_token (parser->lexer);
24088       rhs = cp_parser_binary_expression (parser, false, false,
24089                                          PREC_ADDITIVE_EXPRESSION, NULL);
24090       token = cp_lexer_peek_token (parser->lexer);
24091       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24092         {
24093           if (lhs == NULL_TREE)
24094             {
24095               if (op == PLUS_EXPR)
24096                 lhs = rhs;
24097               else
24098                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24099             }
24100           else
24101             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24102                                      NULL, tf_warning_or_error);
24103         }
24104     }
24105   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24106
24107   if (!decl_first)
24108     {
24109       if (rhs != decl || op == MINUS_EXPR)
24110         return error_mark_node;
24111       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24112     }
24113   else
24114     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24115
24116   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24117 }
24118
24119 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24120
24121 static tree
24122 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24123 {
24124   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24125   tree real_decl, initv, condv, incrv, declv;
24126   tree this_pre_body, cl;
24127   location_t loc_first;
24128   bool collapse_err = false;
24129   int i, collapse = 1, nbraces = 0;
24130   VEC(tree,gc) *for_block = make_tree_vector ();
24131
24132   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24133     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24134       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24135
24136   gcc_assert (collapse >= 1);
24137
24138   declv = make_tree_vec (collapse);
24139   initv = make_tree_vec (collapse);
24140   condv = make_tree_vec (collapse);
24141   incrv = make_tree_vec (collapse);
24142
24143   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24144
24145   for (i = 0; i < collapse; i++)
24146     {
24147       int bracecount = 0;
24148       bool add_private_clause = false;
24149       location_t loc;
24150
24151       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24152         {
24153           cp_parser_error (parser, "for statement expected");
24154           return NULL;
24155         }
24156       loc = cp_lexer_consume_token (parser->lexer)->location;
24157
24158       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24159         return NULL;
24160
24161       init = decl = real_decl = NULL;
24162       this_pre_body = push_stmt_list ();
24163       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24164         {
24165           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24166
24167              init-expr:
24168                        var = lb
24169                        integer-type var = lb
24170                        random-access-iterator-type var = lb
24171                        pointer-type var = lb
24172           */
24173           cp_decl_specifier_seq type_specifiers;
24174
24175           /* First, try to parse as an initialized declaration.  See
24176              cp_parser_condition, from whence the bulk of this is copied.  */
24177
24178           cp_parser_parse_tentatively (parser);
24179           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24180                                         /*is_trailing_return=*/false,
24181                                         &type_specifiers);
24182           if (cp_parser_parse_definitely (parser))
24183             {
24184               /* If parsing a type specifier seq succeeded, then this
24185                  MUST be a initialized declaration.  */
24186               tree asm_specification, attributes;
24187               cp_declarator *declarator;
24188
24189               declarator = cp_parser_declarator (parser,
24190                                                  CP_PARSER_DECLARATOR_NAMED,
24191                                                  /*ctor_dtor_or_conv_p=*/NULL,
24192                                                  /*parenthesized_p=*/NULL,
24193                                                  /*member_p=*/false);
24194               attributes = cp_parser_attributes_opt (parser);
24195               asm_specification = cp_parser_asm_specification_opt (parser);
24196
24197               if (declarator == cp_error_declarator) 
24198                 cp_parser_skip_to_end_of_statement (parser);
24199
24200               else 
24201                 {
24202                   tree pushed_scope, auto_node;
24203
24204                   decl = start_decl (declarator, &type_specifiers,
24205                                      SD_INITIALIZED, attributes,
24206                                      /*prefix_attributes=*/NULL_TREE,
24207                                      &pushed_scope);
24208
24209                   auto_node = type_uses_auto (TREE_TYPE (decl));
24210                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24211                     {
24212                       if (cp_lexer_next_token_is (parser->lexer, 
24213                                                   CPP_OPEN_PAREN))
24214                         error ("parenthesized initialization is not allowed in "
24215                                "OpenMP %<for%> loop");
24216                       else
24217                         /* Trigger an error.  */
24218                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24219
24220                       init = error_mark_node;
24221                       cp_parser_skip_to_end_of_statement (parser);
24222                     }
24223                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24224                            || type_dependent_expression_p (decl)
24225                            || auto_node)
24226                     {
24227                       bool is_direct_init, is_non_constant_init;
24228
24229                       init = cp_parser_initializer (parser,
24230                                                     &is_direct_init,
24231                                                     &is_non_constant_init);
24232
24233                       if (auto_node && describable_type (init))
24234                         {
24235                           TREE_TYPE (decl)
24236                             = do_auto_deduction (TREE_TYPE (decl), init,
24237                                                  auto_node);
24238
24239                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24240                               && !type_dependent_expression_p (decl))
24241                             goto non_class;
24242                         }
24243                       
24244                       cp_finish_decl (decl, init, !is_non_constant_init,
24245                                       asm_specification,
24246                                       LOOKUP_ONLYCONVERTING);
24247                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24248                         {
24249                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24250                           init = NULL_TREE;
24251                         }
24252                       else
24253                         init = pop_stmt_list (this_pre_body);
24254                       this_pre_body = NULL_TREE;
24255                     }
24256                   else
24257                     {
24258                       /* Consume '='.  */
24259                       cp_lexer_consume_token (parser->lexer);
24260                       init = cp_parser_assignment_expression (parser, false, NULL);
24261
24262                     non_class:
24263                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24264                         init = error_mark_node;
24265                       else
24266                         cp_finish_decl (decl, NULL_TREE,
24267                                         /*init_const_expr_p=*/false,
24268                                         asm_specification,
24269                                         LOOKUP_ONLYCONVERTING);
24270                     }
24271
24272                   if (pushed_scope)
24273                     pop_scope (pushed_scope);
24274                 }
24275             }
24276           else 
24277             {
24278               cp_id_kind idk;
24279               /* If parsing a type specifier sequence failed, then
24280                  this MUST be a simple expression.  */
24281               cp_parser_parse_tentatively (parser);
24282               decl = cp_parser_primary_expression (parser, false, false,
24283                                                    false, &idk);
24284               if (!cp_parser_error_occurred (parser)
24285                   && decl
24286                   && DECL_P (decl)
24287                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24288                 {
24289                   tree rhs;
24290
24291                   cp_parser_parse_definitely (parser);
24292                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24293                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24294                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24295                                                          rhs,
24296                                                          tf_warning_or_error));
24297                   add_private_clause = true;
24298                 }
24299               else
24300                 {
24301                   decl = NULL;
24302                   cp_parser_abort_tentative_parse (parser);
24303                   init = cp_parser_expression (parser, false, NULL);
24304                   if (init)
24305                     {
24306                       if (TREE_CODE (init) == MODIFY_EXPR
24307                           || TREE_CODE (init) == MODOP_EXPR)
24308                         real_decl = TREE_OPERAND (init, 0);
24309                     }
24310                 }
24311             }
24312         }
24313       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24314       if (this_pre_body)
24315         {
24316           this_pre_body = pop_stmt_list (this_pre_body);
24317           if (pre_body)
24318             {
24319               tree t = pre_body;
24320               pre_body = push_stmt_list ();
24321               add_stmt (t);
24322               add_stmt (this_pre_body);
24323               pre_body = pop_stmt_list (pre_body);
24324             }
24325           else
24326             pre_body = this_pre_body;
24327         }
24328
24329       if (decl)
24330         real_decl = decl;
24331       if (par_clauses != NULL && real_decl != NULL_TREE)
24332         {
24333           tree *c;
24334           for (c = par_clauses; *c ; )
24335             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24336                 && OMP_CLAUSE_DECL (*c) == real_decl)
24337               {
24338                 error_at (loc, "iteration variable %qD"
24339                           " should not be firstprivate", real_decl);
24340                 *c = OMP_CLAUSE_CHAIN (*c);
24341               }
24342             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24343                      && OMP_CLAUSE_DECL (*c) == real_decl)
24344               {
24345                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24346                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24347                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24348                 OMP_CLAUSE_DECL (l) = real_decl;
24349                 OMP_CLAUSE_CHAIN (l) = clauses;
24350                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24351                 clauses = l;
24352                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24353                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24354                 add_private_clause = false;
24355               }
24356             else
24357               {
24358                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24359                     && OMP_CLAUSE_DECL (*c) == real_decl)
24360                   add_private_clause = false;
24361                 c = &OMP_CLAUSE_CHAIN (*c);
24362               }
24363         }
24364
24365       if (add_private_clause)
24366         {
24367           tree c;
24368           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24369             {
24370               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24371                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24372                   && OMP_CLAUSE_DECL (c) == decl)
24373                 break;
24374               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24375                        && OMP_CLAUSE_DECL (c) == decl)
24376                 error_at (loc, "iteration variable %qD "
24377                           "should not be firstprivate",
24378                           decl);
24379               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24380                        && OMP_CLAUSE_DECL (c) == decl)
24381                 error_at (loc, "iteration variable %qD should not be reduction",
24382                           decl);
24383             }
24384           if (c == NULL)
24385             {
24386               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24387               OMP_CLAUSE_DECL (c) = decl;
24388               c = finish_omp_clauses (c);
24389               if (c)
24390                 {
24391                   OMP_CLAUSE_CHAIN (c) = clauses;
24392                   clauses = c;
24393                 }
24394             }
24395         }
24396
24397       cond = NULL;
24398       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24399         cond = cp_parser_omp_for_cond (parser, decl);
24400       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24401
24402       incr = NULL;
24403       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24404         {
24405           /* If decl is an iterator, preserve the operator on decl
24406              until finish_omp_for.  */
24407           if (decl
24408               && (type_dependent_expression_p (decl)
24409                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24410             incr = cp_parser_omp_for_incr (parser, decl);
24411           else
24412             incr = cp_parser_expression (parser, false, NULL);
24413         }
24414
24415       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24416         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24417                                                /*or_comma=*/false,
24418                                                /*consume_paren=*/true);
24419
24420       TREE_VEC_ELT (declv, i) = decl;
24421       TREE_VEC_ELT (initv, i) = init;
24422       TREE_VEC_ELT (condv, i) = cond;
24423       TREE_VEC_ELT (incrv, i) = incr;
24424
24425       if (i == collapse - 1)
24426         break;
24427
24428       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24429          in between the collapsed for loops to be still considered perfectly
24430          nested.  Hopefully the final version clarifies this.
24431          For now handle (multiple) {'s and empty statements.  */
24432       cp_parser_parse_tentatively (parser);
24433       do
24434         {
24435           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24436             break;
24437           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24438             {
24439               cp_lexer_consume_token (parser->lexer);
24440               bracecount++;
24441             }
24442           else if (bracecount
24443                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24444             cp_lexer_consume_token (parser->lexer);
24445           else
24446             {
24447               loc = cp_lexer_peek_token (parser->lexer)->location;
24448               error_at (loc, "not enough collapsed for loops");
24449               collapse_err = true;
24450               cp_parser_abort_tentative_parse (parser);
24451               declv = NULL_TREE;
24452               break;
24453             }
24454         }
24455       while (1);
24456
24457       if (declv)
24458         {
24459           cp_parser_parse_definitely (parser);
24460           nbraces += bracecount;
24461         }
24462     }
24463
24464   /* Note that we saved the original contents of this flag when we entered
24465      the structured block, and so we don't need to re-save it here.  */
24466   parser->in_statement = IN_OMP_FOR;
24467
24468   /* Note that the grammar doesn't call for a structured block here,
24469      though the loop as a whole is a structured block.  */
24470   body = push_stmt_list ();
24471   cp_parser_statement (parser, NULL_TREE, false, NULL);
24472   body = pop_stmt_list (body);
24473
24474   if (declv == NULL_TREE)
24475     ret = NULL_TREE;
24476   else
24477     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24478                           pre_body, clauses);
24479
24480   while (nbraces)
24481     {
24482       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24483         {
24484           cp_lexer_consume_token (parser->lexer);
24485           nbraces--;
24486         }
24487       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24488         cp_lexer_consume_token (parser->lexer);
24489       else
24490         {
24491           if (!collapse_err)
24492             {
24493               error_at (cp_lexer_peek_token (parser->lexer)->location,
24494                         "collapsed loops not perfectly nested");
24495             }
24496           collapse_err = true;
24497           cp_parser_statement_seq_opt (parser, NULL);
24498           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24499             break;
24500         }
24501     }
24502
24503   while (!VEC_empty (tree, for_block))
24504     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24505   release_tree_vector (for_block);
24506
24507   return ret;
24508 }
24509
24510 /* OpenMP 2.5:
24511    #pragma omp for for-clause[optseq] new-line
24512      for-loop  */
24513
24514 #define OMP_FOR_CLAUSE_MASK                             \
24515         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24516         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24517         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24518         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24519         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24520         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24521         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24522         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24523
24524 static tree
24525 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24526 {
24527   tree clauses, sb, ret;
24528   unsigned int save;
24529
24530   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24531                                        "#pragma omp for", pragma_tok);
24532
24533   sb = begin_omp_structured_block ();
24534   save = cp_parser_begin_omp_structured_block (parser);
24535
24536   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24537
24538   cp_parser_end_omp_structured_block (parser, save);
24539   add_stmt (finish_omp_structured_block (sb));
24540
24541   return ret;
24542 }
24543
24544 /* OpenMP 2.5:
24545    # pragma omp master new-line
24546      structured-block  */
24547
24548 static tree
24549 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24550 {
24551   cp_parser_require_pragma_eol (parser, pragma_tok);
24552   return c_finish_omp_master (input_location,
24553                               cp_parser_omp_structured_block (parser));
24554 }
24555
24556 /* OpenMP 2.5:
24557    # pragma omp ordered new-line
24558      structured-block  */
24559
24560 static tree
24561 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24562 {
24563   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24564   cp_parser_require_pragma_eol (parser, pragma_tok);
24565   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24566 }
24567
24568 /* OpenMP 2.5:
24569
24570    section-scope:
24571      { section-sequence }
24572
24573    section-sequence:
24574      section-directive[opt] structured-block
24575      section-sequence section-directive structured-block  */
24576
24577 static tree
24578 cp_parser_omp_sections_scope (cp_parser *parser)
24579 {
24580   tree stmt, substmt;
24581   bool error_suppress = false;
24582   cp_token *tok;
24583
24584   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24585     return NULL_TREE;
24586
24587   stmt = push_stmt_list ();
24588
24589   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24590     {
24591       unsigned save;
24592
24593       substmt = begin_omp_structured_block ();
24594       save = cp_parser_begin_omp_structured_block (parser);
24595
24596       while (1)
24597         {
24598           cp_parser_statement (parser, NULL_TREE, false, NULL);
24599
24600           tok = cp_lexer_peek_token (parser->lexer);
24601           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24602             break;
24603           if (tok->type == CPP_CLOSE_BRACE)
24604             break;
24605           if (tok->type == CPP_EOF)
24606             break;
24607         }
24608
24609       cp_parser_end_omp_structured_block (parser, save);
24610       substmt = finish_omp_structured_block (substmt);
24611       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24612       add_stmt (substmt);
24613     }
24614
24615   while (1)
24616     {
24617       tok = cp_lexer_peek_token (parser->lexer);
24618       if (tok->type == CPP_CLOSE_BRACE)
24619         break;
24620       if (tok->type == CPP_EOF)
24621         break;
24622
24623       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24624         {
24625           cp_lexer_consume_token (parser->lexer);
24626           cp_parser_require_pragma_eol (parser, tok);
24627           error_suppress = false;
24628         }
24629       else if (!error_suppress)
24630         {
24631           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24632           error_suppress = true;
24633         }
24634
24635       substmt = cp_parser_omp_structured_block (parser);
24636       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24637       add_stmt (substmt);
24638     }
24639   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24640
24641   substmt = pop_stmt_list (stmt);
24642
24643   stmt = make_node (OMP_SECTIONS);
24644   TREE_TYPE (stmt) = void_type_node;
24645   OMP_SECTIONS_BODY (stmt) = substmt;
24646
24647   add_stmt (stmt);
24648   return stmt;
24649 }
24650
24651 /* OpenMP 2.5:
24652    # pragma omp sections sections-clause[optseq] newline
24653      sections-scope  */
24654
24655 #define OMP_SECTIONS_CLAUSE_MASK                        \
24656         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24657         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24658         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24659         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24660         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24661
24662 static tree
24663 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24664 {
24665   tree clauses, ret;
24666
24667   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24668                                        "#pragma omp sections", pragma_tok);
24669
24670   ret = cp_parser_omp_sections_scope (parser);
24671   if (ret)
24672     OMP_SECTIONS_CLAUSES (ret) = clauses;
24673
24674   return ret;
24675 }
24676
24677 /* OpenMP 2.5:
24678    # pragma parallel parallel-clause new-line
24679    # pragma parallel for parallel-for-clause new-line
24680    # pragma parallel sections parallel-sections-clause new-line  */
24681
24682 #define OMP_PARALLEL_CLAUSE_MASK                        \
24683         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24684         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24685         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24686         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24687         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24688         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24689         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24690         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24691
24692 static tree
24693 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24694 {
24695   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24696   const char *p_name = "#pragma omp parallel";
24697   tree stmt, clauses, par_clause, ws_clause, block;
24698   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24699   unsigned int save;
24700   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24701
24702   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24703     {
24704       cp_lexer_consume_token (parser->lexer);
24705       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24706       p_name = "#pragma omp parallel for";
24707       mask |= OMP_FOR_CLAUSE_MASK;
24708       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24709     }
24710   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24711     {
24712       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24713       const char *p = IDENTIFIER_POINTER (id);
24714       if (strcmp (p, "sections") == 0)
24715         {
24716           cp_lexer_consume_token (parser->lexer);
24717           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24718           p_name = "#pragma omp parallel sections";
24719           mask |= OMP_SECTIONS_CLAUSE_MASK;
24720           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24721         }
24722     }
24723
24724   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24725   block = begin_omp_parallel ();
24726   save = cp_parser_begin_omp_structured_block (parser);
24727
24728   switch (p_kind)
24729     {
24730     case PRAGMA_OMP_PARALLEL:
24731       cp_parser_statement (parser, NULL_TREE, false, NULL);
24732       par_clause = clauses;
24733       break;
24734
24735     case PRAGMA_OMP_PARALLEL_FOR:
24736       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24737       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24738       break;
24739
24740     case PRAGMA_OMP_PARALLEL_SECTIONS:
24741       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24742       stmt = cp_parser_omp_sections_scope (parser);
24743       if (stmt)
24744         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24745       break;
24746
24747     default:
24748       gcc_unreachable ();
24749     }
24750
24751   cp_parser_end_omp_structured_block (parser, save);
24752   stmt = finish_omp_parallel (par_clause, block);
24753   if (p_kind != PRAGMA_OMP_PARALLEL)
24754     OMP_PARALLEL_COMBINED (stmt) = 1;
24755   return stmt;
24756 }
24757
24758 /* OpenMP 2.5:
24759    # pragma omp single single-clause[optseq] new-line
24760      structured-block  */
24761
24762 #define OMP_SINGLE_CLAUSE_MASK                          \
24763         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24764         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24765         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24766         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24767
24768 static tree
24769 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24770 {
24771   tree stmt = make_node (OMP_SINGLE);
24772   TREE_TYPE (stmt) = void_type_node;
24773
24774   OMP_SINGLE_CLAUSES (stmt)
24775     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24776                                  "#pragma omp single", pragma_tok);
24777   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24778
24779   return add_stmt (stmt);
24780 }
24781
24782 /* OpenMP 3.0:
24783    # pragma omp task task-clause[optseq] new-line
24784      structured-block  */
24785
24786 #define OMP_TASK_CLAUSE_MASK                            \
24787         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24788         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24789         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24790         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24791         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24792         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24793
24794 static tree
24795 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24796 {
24797   tree clauses, block;
24798   unsigned int save;
24799
24800   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24801                                        "#pragma omp task", pragma_tok);
24802   block = begin_omp_task ();
24803   save = cp_parser_begin_omp_structured_block (parser);
24804   cp_parser_statement (parser, NULL_TREE, false, NULL);
24805   cp_parser_end_omp_structured_block (parser, save);
24806   return finish_omp_task (clauses, block);
24807 }
24808
24809 /* OpenMP 3.0:
24810    # pragma omp taskwait new-line  */
24811
24812 static void
24813 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24814 {
24815   cp_parser_require_pragma_eol (parser, pragma_tok);
24816   finish_omp_taskwait ();
24817 }
24818
24819 /* OpenMP 2.5:
24820    # pragma omp threadprivate (variable-list) */
24821
24822 static void
24823 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24824 {
24825   tree vars;
24826
24827   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24828   cp_parser_require_pragma_eol (parser, pragma_tok);
24829
24830   finish_omp_threadprivate (vars);
24831 }
24832
24833 /* Main entry point to OpenMP statement pragmas.  */
24834
24835 static void
24836 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24837 {
24838   tree stmt;
24839
24840   switch (pragma_tok->pragma_kind)
24841     {
24842     case PRAGMA_OMP_ATOMIC:
24843       cp_parser_omp_atomic (parser, pragma_tok);
24844       return;
24845     case PRAGMA_OMP_CRITICAL:
24846       stmt = cp_parser_omp_critical (parser, pragma_tok);
24847       break;
24848     case PRAGMA_OMP_FOR:
24849       stmt = cp_parser_omp_for (parser, pragma_tok);
24850       break;
24851     case PRAGMA_OMP_MASTER:
24852       stmt = cp_parser_omp_master (parser, pragma_tok);
24853       break;
24854     case PRAGMA_OMP_ORDERED:
24855       stmt = cp_parser_omp_ordered (parser, pragma_tok);
24856       break;
24857     case PRAGMA_OMP_PARALLEL:
24858       stmt = cp_parser_omp_parallel (parser, pragma_tok);
24859       break;
24860     case PRAGMA_OMP_SECTIONS:
24861       stmt = cp_parser_omp_sections (parser, pragma_tok);
24862       break;
24863     case PRAGMA_OMP_SINGLE:
24864       stmt = cp_parser_omp_single (parser, pragma_tok);
24865       break;
24866     case PRAGMA_OMP_TASK:
24867       stmt = cp_parser_omp_task (parser, pragma_tok);
24868       break;
24869     default:
24870       gcc_unreachable ();
24871     }
24872
24873   if (stmt)
24874     SET_EXPR_LOCATION (stmt, pragma_tok->location);
24875 }
24876 \f
24877 /* The parser.  */
24878
24879 static GTY (()) cp_parser *the_parser;
24880
24881 \f
24882 /* Special handling for the first token or line in the file.  The first
24883    thing in the file might be #pragma GCC pch_preprocess, which loads a
24884    PCH file, which is a GC collection point.  So we need to handle this
24885    first pragma without benefit of an existing lexer structure.
24886
24887    Always returns one token to the caller in *FIRST_TOKEN.  This is
24888    either the true first token of the file, or the first token after
24889    the initial pragma.  */
24890
24891 static void
24892 cp_parser_initial_pragma (cp_token *first_token)
24893 {
24894   tree name = NULL;
24895
24896   cp_lexer_get_preprocessor_token (NULL, first_token);
24897   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24898     return;
24899
24900   cp_lexer_get_preprocessor_token (NULL, first_token);
24901   if (first_token->type == CPP_STRING)
24902     {
24903       name = first_token->u.value;
24904
24905       cp_lexer_get_preprocessor_token (NULL, first_token);
24906       if (first_token->type != CPP_PRAGMA_EOL)
24907         error_at (first_token->location,
24908                   "junk at end of %<#pragma GCC pch_preprocess%>");
24909     }
24910   else
24911     error_at (first_token->location, "expected string literal");
24912
24913   /* Skip to the end of the pragma.  */
24914   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24915     cp_lexer_get_preprocessor_token (NULL, first_token);
24916
24917   /* Now actually load the PCH file.  */
24918   if (name)
24919     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24920
24921   /* Read one more token to return to our caller.  We have to do this
24922      after reading the PCH file in, since its pointers have to be
24923      live.  */
24924   cp_lexer_get_preprocessor_token (NULL, first_token);
24925 }
24926
24927 /* Normal parsing of a pragma token.  Here we can (and must) use the
24928    regular lexer.  */
24929
24930 static bool
24931 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24932 {
24933   cp_token *pragma_tok;
24934   unsigned int id;
24935
24936   pragma_tok = cp_lexer_consume_token (parser->lexer);
24937   gcc_assert (pragma_tok->type == CPP_PRAGMA);
24938   parser->lexer->in_pragma = true;
24939
24940   id = pragma_tok->pragma_kind;
24941   switch (id)
24942     {
24943     case PRAGMA_GCC_PCH_PREPROCESS:
24944       error_at (pragma_tok->location,
24945                 "%<#pragma GCC pch_preprocess%> must be first");
24946       break;
24947
24948     case PRAGMA_OMP_BARRIER:
24949       switch (context)
24950         {
24951         case pragma_compound:
24952           cp_parser_omp_barrier (parser, pragma_tok);
24953           return false;
24954         case pragma_stmt:
24955           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24956                     "used in compound statements");
24957           break;
24958         default:
24959           goto bad_stmt;
24960         }
24961       break;
24962
24963     case PRAGMA_OMP_FLUSH:
24964       switch (context)
24965         {
24966         case pragma_compound:
24967           cp_parser_omp_flush (parser, pragma_tok);
24968           return false;
24969         case pragma_stmt:
24970           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24971                     "used in compound statements");
24972           break;
24973         default:
24974           goto bad_stmt;
24975         }
24976       break;
24977
24978     case PRAGMA_OMP_TASKWAIT:
24979       switch (context)
24980         {
24981         case pragma_compound:
24982           cp_parser_omp_taskwait (parser, pragma_tok);
24983           return false;
24984         case pragma_stmt:
24985           error_at (pragma_tok->location,
24986                     "%<#pragma omp taskwait%> may only be "
24987                     "used in compound statements");
24988           break;
24989         default:
24990           goto bad_stmt;
24991         }
24992       break;
24993
24994     case PRAGMA_OMP_THREADPRIVATE:
24995       cp_parser_omp_threadprivate (parser, pragma_tok);
24996       return false;
24997
24998     case PRAGMA_OMP_ATOMIC:
24999     case PRAGMA_OMP_CRITICAL:
25000     case PRAGMA_OMP_FOR:
25001     case PRAGMA_OMP_MASTER:
25002     case PRAGMA_OMP_ORDERED:
25003     case PRAGMA_OMP_PARALLEL:
25004     case PRAGMA_OMP_SECTIONS:
25005     case PRAGMA_OMP_SINGLE:
25006     case PRAGMA_OMP_TASK:
25007       if (context == pragma_external)
25008         goto bad_stmt;
25009       cp_parser_omp_construct (parser, pragma_tok);
25010       return true;
25011
25012     case PRAGMA_OMP_SECTION:
25013       error_at (pragma_tok->location, 
25014                 "%<#pragma omp section%> may only be used in "
25015                 "%<#pragma omp sections%> construct");
25016       break;
25017
25018     default:
25019       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25020       c_invoke_pragma_handler (id);
25021       break;
25022
25023     bad_stmt:
25024       cp_parser_error (parser, "expected declaration specifiers");
25025       break;
25026     }
25027
25028   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25029   return false;
25030 }
25031
25032 /* The interface the pragma parsers have to the lexer.  */
25033
25034 enum cpp_ttype
25035 pragma_lex (tree *value)
25036 {
25037   cp_token *tok;
25038   enum cpp_ttype ret;
25039
25040   tok = cp_lexer_peek_token (the_parser->lexer);
25041
25042   ret = tok->type;
25043   *value = tok->u.value;
25044
25045   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25046     ret = CPP_EOF;
25047   else if (ret == CPP_STRING)
25048     *value = cp_parser_string_literal (the_parser, false, false);
25049   else
25050     {
25051       cp_lexer_consume_token (the_parser->lexer);
25052       if (ret == CPP_KEYWORD)
25053         ret = CPP_NAME;
25054     }
25055
25056   return ret;
25057 }
25058
25059 \f
25060 /* External interface.  */
25061
25062 /* Parse one entire translation unit.  */
25063
25064 void
25065 c_parse_file (void)
25066 {
25067   static bool already_called = false;
25068
25069   if (already_called)
25070     {
25071       sorry ("inter-module optimizations not implemented for C++");
25072       return;
25073     }
25074   already_called = true;
25075
25076   the_parser = cp_parser_new ();
25077   push_deferring_access_checks (flag_access_control
25078                                 ? dk_no_deferred : dk_no_check);
25079   cp_parser_translation_unit (the_parser);
25080   the_parser = NULL;
25081 }
25082
25083 #include "gt-cp-parser.h"