OSDN Git Service

6a9e4d7b981fe1864f3e800edef57e45ed0f05a1
[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 cp_token *
506 cp_lexer_previous_token (cp_lexer *lexer)
507 {
508   cp_token_position tp;
509
510   if (lexer->next_token == &eof_token)
511     tp = lexer->last_token - 1;
512   else
513     tp = cp_lexer_token_position (lexer, true);
514
515   return cp_lexer_token_at (lexer, tp);
516 }
517
518 /* nonzero if we are presently saving tokens.  */
519
520 static inline int
521 cp_lexer_saving_tokens (const cp_lexer* lexer)
522 {
523   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
524 }
525
526 /* Store the next token from the preprocessor in *TOKEN.  Return true
527    if we reach EOF.  If LEXER is NULL, assume we are handling an
528    initial #pragma pch_preprocess, and thus want the lexer to return
529    processed strings.  */
530
531 static void
532 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
533 {
534   static int is_extern_c = 0;
535
536    /* Get a new token from the preprocessor.  */
537   token->type
538     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
539                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
540   token->keyword = RID_MAX;
541   token->pragma_kind = PRAGMA_NONE;
542
543   /* On some systems, some header files are surrounded by an
544      implicit extern "C" block.  Set a flag in the token if it
545      comes from such a header.  */
546   is_extern_c += pending_lang_change;
547   pending_lang_change = 0;
548   token->implicit_extern_c = is_extern_c > 0;
549
550   /* Check to see if this token is a keyword.  */
551   if (token->type == CPP_NAME)
552     {
553       if (C_IS_RESERVED_WORD (token->u.value))
554         {
555           /* Mark this token as a keyword.  */
556           token->type = CPP_KEYWORD;
557           /* Record which keyword.  */
558           token->keyword = C_RID_CODE (token->u.value);
559         }
560       else
561         {
562           if (warn_cxx0x_compat
563               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
564               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
565             {
566               /* Warn about the C++0x keyword (but still treat it as
567                  an identifier).  */
568               warning (OPT_Wc__0x_compat, 
569                        "identifier %qE will become a keyword in C++0x",
570                        token->u.value);
571
572               /* Clear out the C_RID_CODE so we don't warn about this
573                  particular identifier-turned-keyword again.  */
574               C_SET_RID_CODE (token->u.value, RID_MAX);
575             }
576
577           token->ambiguous_p = false;
578           token->keyword = RID_MAX;
579         }
580     }
581   else if (token->type == CPP_AT_NAME)
582     {
583       /* This only happens in Objective-C++; it must be a keyword.  */
584       token->type = CPP_KEYWORD;
585       switch (C_RID_CODE (token->u.value))
586         {
587           /* Replace 'class' with '@class', 'private' with '@private',
588              etc.  This prevents confusion with the C++ keyword
589              'class', and makes the tokens consistent with other
590              Objective-C 'AT' keywords.  For example '@class' is
591              reported as RID_AT_CLASS which is consistent with
592              '@synchronized', which is reported as
593              RID_AT_SYNCHRONIZED.
594           */
595         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
596         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
597         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
598         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
599         case RID_THROW:     token->keyword = RID_AT_THROW; break;
600         case RID_TRY:       token->keyword = RID_AT_TRY; break;
601         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
602         default:            token->keyword = C_RID_CODE (token->u.value);
603         }
604     }
605   else if (token->type == CPP_PRAGMA)
606     {
607       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
608       token->pragma_kind = ((enum pragma_kind)
609                             TREE_INT_CST_LOW (token->u.value));
610       token->u.value = NULL_TREE;
611     }
612 }
613
614 /* Update the globals input_location and the input file stack from TOKEN.  */
615 static inline void
616 cp_lexer_set_source_position_from_token (cp_token *token)
617 {
618   if (token->type != CPP_EOF)
619     {
620       input_location = token->location;
621     }
622 }
623
624 /* Return a pointer to the next token in the token stream, but do not
625    consume it.  */
626
627 static inline cp_token *
628 cp_lexer_peek_token (cp_lexer *lexer)
629 {
630   if (cp_lexer_debugging_p (lexer))
631     {
632       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
633       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
634       putc ('\n', cp_lexer_debug_stream);
635     }
636   return lexer->next_token;
637 }
638
639 /* Return true if the next token has the indicated TYPE.  */
640
641 static inline bool
642 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
643 {
644   return cp_lexer_peek_token (lexer)->type == type;
645 }
646
647 /* Return true if the next token does not have the indicated TYPE.  */
648
649 static inline bool
650 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
651 {
652   return !cp_lexer_next_token_is (lexer, type);
653 }
654
655 /* Return true if the next token is the indicated KEYWORD.  */
656
657 static inline bool
658 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
659 {
660   return cp_lexer_peek_token (lexer)->keyword == keyword;
661 }
662
663 /* Return true if the next token is not the indicated KEYWORD.  */
664
665 static inline bool
666 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
667 {
668   return cp_lexer_peek_token (lexer)->keyword != keyword;
669 }
670
671 /* Return true if the next token is a keyword for a decl-specifier.  */
672
673 static bool
674 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
675 {
676   cp_token *token;
677
678   token = cp_lexer_peek_token (lexer);
679   switch (token->keyword) 
680     {
681       /* auto specifier: storage-class-specifier in C++,
682          simple-type-specifier in C++0x.  */
683     case RID_AUTO:
684       /* Storage classes.  */
685     case RID_REGISTER:
686     case RID_STATIC:
687     case RID_EXTERN:
688     case RID_MUTABLE:
689     case RID_THREAD:
690       /* Elaborated type specifiers.  */
691     case RID_ENUM:
692     case RID_CLASS:
693     case RID_STRUCT:
694     case RID_UNION:
695     case RID_TYPENAME:
696       /* Simple type specifiers.  */
697     case RID_CHAR:
698     case RID_CHAR16:
699     case RID_CHAR32:
700     case RID_WCHAR:
701     case RID_BOOL:
702     case RID_SHORT:
703     case RID_INT:
704     case RID_LONG:
705     case RID_INT128:
706     case RID_SIGNED:
707     case RID_UNSIGNED:
708     case RID_FLOAT:
709     case RID_DOUBLE:
710     case RID_VOID:
711       /* GNU extensions.  */ 
712     case RID_ATTRIBUTE:
713     case RID_TYPEOF:
714       /* C++0x extensions.  */
715     case RID_DECLTYPE:
716       return true;
717
718     default:
719       return false;
720     }
721 }
722
723 /* Return a pointer to the Nth token in the token stream.  If N is 1,
724    then this is precisely equivalent to cp_lexer_peek_token (except
725    that it is not inline).  One would like to disallow that case, but
726    there is one case (cp_parser_nth_token_starts_template_id) where
727    the caller passes a variable for N and it might be 1.  */
728
729 static cp_token *
730 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
731 {
732   cp_token *token;
733
734   /* N is 1-based, not zero-based.  */
735   gcc_assert (n > 0);
736
737   if (cp_lexer_debugging_p (lexer))
738     fprintf (cp_lexer_debug_stream,
739              "cp_lexer: peeking ahead %ld at token: ", (long)n);
740
741   --n;
742   token = lexer->next_token;
743   gcc_assert (!n || token != &eof_token);
744   while (n != 0)
745     {
746       ++token;
747       if (token == lexer->last_token)
748         {
749           token = &eof_token;
750           break;
751         }
752
753       if (token->type != CPP_PURGED)
754         --n;
755     }
756
757   if (cp_lexer_debugging_p (lexer))
758     {
759       cp_lexer_print_token (cp_lexer_debug_stream, token);
760       putc ('\n', cp_lexer_debug_stream);
761     }
762
763   return token;
764 }
765
766 /* Return the next token, and advance the lexer's next_token pointer
767    to point to the next non-purged token.  */
768
769 static cp_token *
770 cp_lexer_consume_token (cp_lexer* lexer)
771 {
772   cp_token *token = lexer->next_token;
773
774   gcc_assert (token != &eof_token);
775   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
776
777   do
778     {
779       lexer->next_token++;
780       if (lexer->next_token == lexer->last_token)
781         {
782           lexer->next_token = &eof_token;
783           break;
784         }
785
786     }
787   while (lexer->next_token->type == CPP_PURGED);
788
789   cp_lexer_set_source_position_from_token (token);
790
791   /* Provide debugging output.  */
792   if (cp_lexer_debugging_p (lexer))
793     {
794       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
795       cp_lexer_print_token (cp_lexer_debug_stream, token);
796       putc ('\n', cp_lexer_debug_stream);
797     }
798
799   return token;
800 }
801
802 /* Permanently remove the next token from the token stream, and
803    advance the next_token pointer to refer to the next non-purged
804    token.  */
805
806 static void
807 cp_lexer_purge_token (cp_lexer *lexer)
808 {
809   cp_token *tok = lexer->next_token;
810
811   gcc_assert (tok != &eof_token);
812   tok->type = CPP_PURGED;
813   tok->location = UNKNOWN_LOCATION;
814   tok->u.value = NULL_TREE;
815   tok->keyword = RID_MAX;
816
817   do
818     {
819       tok++;
820       if (tok == lexer->last_token)
821         {
822           tok = &eof_token;
823           break;
824         }
825     }
826   while (tok->type == CPP_PURGED);
827   lexer->next_token = tok;
828 }
829
830 /* Permanently remove all tokens after TOK, up to, but not
831    including, the token that will be returned next by
832    cp_lexer_peek_token.  */
833
834 static void
835 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
836 {
837   cp_token *peek = lexer->next_token;
838
839   if (peek == &eof_token)
840     peek = lexer->last_token;
841
842   gcc_assert (tok < peek);
843
844   for ( tok += 1; tok != peek; tok += 1)
845     {
846       tok->type = CPP_PURGED;
847       tok->location = UNKNOWN_LOCATION;
848       tok->u.value = NULL_TREE;
849       tok->keyword = RID_MAX;
850     }
851 }
852
853 /* Begin saving tokens.  All tokens consumed after this point will be
854    preserved.  */
855
856 static void
857 cp_lexer_save_tokens (cp_lexer* lexer)
858 {
859   /* Provide debugging output.  */
860   if (cp_lexer_debugging_p (lexer))
861     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
862
863   VEC_safe_push (cp_token_position, heap,
864                  lexer->saved_tokens, lexer->next_token);
865 }
866
867 /* Commit to the portion of the token stream most recently saved.  */
868
869 static void
870 cp_lexer_commit_tokens (cp_lexer* lexer)
871 {
872   /* Provide debugging output.  */
873   if (cp_lexer_debugging_p (lexer))
874     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
875
876   VEC_pop (cp_token_position, lexer->saved_tokens);
877 }
878
879 /* Return all tokens saved since the last call to cp_lexer_save_tokens
880    to the token stream.  Stop saving tokens.  */
881
882 static void
883 cp_lexer_rollback_tokens (cp_lexer* lexer)
884 {
885   /* Provide debugging output.  */
886   if (cp_lexer_debugging_p (lexer))
887     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
888
889   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
890 }
891
892 /* Print a representation of the TOKEN on the STREAM.  */
893
894 #ifdef ENABLE_CHECKING
895
896 static void
897 cp_lexer_print_token (FILE * stream, cp_token *token)
898 {
899   /* We don't use cpp_type2name here because the parser defines
900      a few tokens of its own.  */
901   static const char *const token_names[] = {
902     /* cpplib-defined token types */
903 #define OP(e, s) #e,
904 #define TK(e, s) #e,
905     TTYPE_TABLE
906 #undef OP
907 #undef TK
908     /* C++ parser token types - see "Manifest constants", above.  */
909     "KEYWORD",
910     "TEMPLATE_ID",
911     "NESTED_NAME_SPECIFIER",
912     "PURGED"
913   };
914
915   /* If we have a name for the token, print it out.  Otherwise, we
916      simply give the numeric code.  */
917   gcc_assert (token->type < ARRAY_SIZE(token_names));
918   fputs (token_names[token->type], stream);
919
920   /* For some tokens, print the associated data.  */
921   switch (token->type)
922     {
923     case CPP_KEYWORD:
924       /* Some keywords have a value that is not an IDENTIFIER_NODE.
925          For example, `struct' is mapped to an INTEGER_CST.  */
926       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
927         break;
928       /* else fall through */
929     case CPP_NAME:
930       fputs (IDENTIFIER_POINTER (token->u.value), stream);
931       break;
932
933     case CPP_STRING:
934     case CPP_STRING16:
935     case CPP_STRING32:
936     case CPP_WSTRING:
937     case CPP_UTF8STRING:
938       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
939       break;
940
941     default:
942       break;
943     }
944 }
945
946 /* Start emitting debugging information.  */
947
948 static void
949 cp_lexer_start_debugging (cp_lexer* lexer)
950 {
951   lexer->debugging_p = true;
952 }
953
954 /* Stop emitting debugging information.  */
955
956 static void
957 cp_lexer_stop_debugging (cp_lexer* lexer)
958 {
959   lexer->debugging_p = false;
960 }
961
962 #endif /* ENABLE_CHECKING */
963
964 /* Create a new cp_token_cache, representing a range of tokens.  */
965
966 static cp_token_cache *
967 cp_token_cache_new (cp_token *first, cp_token *last)
968 {
969   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
970   cache->first = first;
971   cache->last = last;
972   return cache;
973 }
974
975 \f
976 /* Decl-specifiers.  */
977
978 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
979
980 static void
981 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
982 {
983   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
984 }
985
986 /* Declarators.  */
987
988 /* Nothing other than the parser should be creating declarators;
989    declarators are a semi-syntactic representation of C++ entities.
990    Other parts of the front end that need to create entities (like
991    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
992
993 static cp_declarator *make_call_declarator
994   (cp_declarator *, tree, cp_cv_quals, tree, tree);
995 static cp_declarator *make_array_declarator
996   (cp_declarator *, tree);
997 static cp_declarator *make_pointer_declarator
998   (cp_cv_quals, cp_declarator *);
999 static cp_declarator *make_reference_declarator
1000   (cp_cv_quals, cp_declarator *, bool);
1001 static cp_parameter_declarator *make_parameter_declarator
1002   (cp_decl_specifier_seq *, cp_declarator *, tree);
1003 static cp_declarator *make_ptrmem_declarator
1004   (cp_cv_quals, tree, cp_declarator *);
1005
1006 /* An erroneous declarator.  */
1007 static cp_declarator *cp_error_declarator;
1008
1009 /* The obstack on which declarators and related data structures are
1010    allocated.  */
1011 static struct obstack declarator_obstack;
1012
1013 /* Alloc BYTES from the declarator memory pool.  */
1014
1015 static inline void *
1016 alloc_declarator (size_t bytes)
1017 {
1018   return obstack_alloc (&declarator_obstack, bytes);
1019 }
1020
1021 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1022    common to all declarators.  */
1023
1024 static cp_declarator *
1025 make_declarator (cp_declarator_kind kind)
1026 {
1027   cp_declarator *declarator;
1028
1029   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1030   declarator->kind = kind;
1031   declarator->attributes = NULL_TREE;
1032   declarator->declarator = NULL;
1033   declarator->parameter_pack_p = false;
1034   declarator->id_loc = UNKNOWN_LOCATION;
1035
1036   return declarator;
1037 }
1038
1039 /* Make a declarator for a generalized identifier.  If
1040    QUALIFYING_SCOPE is non-NULL, the identifier is
1041    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1042    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1043    is, if any.   */
1044
1045 static cp_declarator *
1046 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1047                     special_function_kind sfk)
1048 {
1049   cp_declarator *declarator;
1050
1051   /* It is valid to write:
1052
1053        class C { void f(); };
1054        typedef C D;
1055        void D::f();
1056
1057      The standard is not clear about whether `typedef const C D' is
1058      legal; as of 2002-09-15 the committee is considering that
1059      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1060      well.  */
1061   if (qualifying_scope && TYPE_P (qualifying_scope))
1062     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1063
1064   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1065               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1066               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1067
1068   declarator = make_declarator (cdk_id);
1069   declarator->u.id.qualifying_scope = qualifying_scope;
1070   declarator->u.id.unqualified_name = unqualified_name;
1071   declarator->u.id.sfk = sfk;
1072   
1073   return declarator;
1074 }
1075
1076 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1077    of modifiers such as const or volatile to apply to the pointer
1078    type, represented as identifiers.  */
1079
1080 cp_declarator *
1081 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1082 {
1083   cp_declarator *declarator;
1084
1085   declarator = make_declarator (cdk_pointer);
1086   declarator->declarator = target;
1087   declarator->u.pointer.qualifiers = cv_qualifiers;
1088   declarator->u.pointer.class_type = NULL_TREE;
1089   if (target)
1090     {
1091       declarator->id_loc = target->id_loc;
1092       declarator->parameter_pack_p = target->parameter_pack_p;
1093       target->parameter_pack_p = false;
1094     }
1095   else
1096     declarator->parameter_pack_p = false;
1097
1098   return declarator;
1099 }
1100
1101 /* Like make_pointer_declarator -- but for references.  */
1102
1103 cp_declarator *
1104 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1105                            bool rvalue_ref)
1106 {
1107   cp_declarator *declarator;
1108
1109   declarator = make_declarator (cdk_reference);
1110   declarator->declarator = target;
1111   declarator->u.reference.qualifiers = cv_qualifiers;
1112   declarator->u.reference.rvalue_ref = rvalue_ref;
1113   if (target)
1114     {
1115       declarator->id_loc = target->id_loc;
1116       declarator->parameter_pack_p = target->parameter_pack_p;
1117       target->parameter_pack_p = false;
1118     }
1119   else
1120     declarator->parameter_pack_p = false;
1121
1122   return declarator;
1123 }
1124
1125 /* Like make_pointer_declarator -- but for a pointer to a non-static
1126    member of CLASS_TYPE.  */
1127
1128 cp_declarator *
1129 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1130                         cp_declarator *pointee)
1131 {
1132   cp_declarator *declarator;
1133
1134   declarator = make_declarator (cdk_ptrmem);
1135   declarator->declarator = pointee;
1136   declarator->u.pointer.qualifiers = cv_qualifiers;
1137   declarator->u.pointer.class_type = class_type;
1138
1139   if (pointee)
1140     {
1141       declarator->parameter_pack_p = pointee->parameter_pack_p;
1142       pointee->parameter_pack_p = false;
1143     }
1144   else
1145     declarator->parameter_pack_p = false;
1146
1147   return declarator;
1148 }
1149
1150 /* Make a declarator for the function given by TARGET, with the
1151    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1152    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1153    indicates what exceptions can be thrown.  */
1154
1155 cp_declarator *
1156 make_call_declarator (cp_declarator *target,
1157                       tree parms,
1158                       cp_cv_quals cv_qualifiers,
1159                       tree exception_specification,
1160                       tree late_return_type)
1161 {
1162   cp_declarator *declarator;
1163
1164   declarator = make_declarator (cdk_function);
1165   declarator->declarator = target;
1166   declarator->u.function.parameters = parms;
1167   declarator->u.function.qualifiers = cv_qualifiers;
1168   declarator->u.function.exception_specification = exception_specification;
1169   declarator->u.function.late_return_type = late_return_type;
1170   if (target)
1171     {
1172       declarator->id_loc = target->id_loc;
1173       declarator->parameter_pack_p = target->parameter_pack_p;
1174       target->parameter_pack_p = false;
1175     }
1176   else
1177     declarator->parameter_pack_p = false;
1178
1179   return declarator;
1180 }
1181
1182 /* Make a declarator for an array of BOUNDS elements, each of which is
1183    defined by ELEMENT.  */
1184
1185 cp_declarator *
1186 make_array_declarator (cp_declarator *element, tree bounds)
1187 {
1188   cp_declarator *declarator;
1189
1190   declarator = make_declarator (cdk_array);
1191   declarator->declarator = element;
1192   declarator->u.array.bounds = bounds;
1193   if (element)
1194     {
1195       declarator->id_loc = element->id_loc;
1196       declarator->parameter_pack_p = element->parameter_pack_p;
1197       element->parameter_pack_p = false;
1198     }
1199   else
1200     declarator->parameter_pack_p = false;
1201
1202   return declarator;
1203 }
1204
1205 /* Determine whether the declarator we've seen so far can be a
1206    parameter pack, when followed by an ellipsis.  */
1207 static bool 
1208 declarator_can_be_parameter_pack (cp_declarator *declarator)
1209 {
1210   /* Search for a declarator name, or any other declarator that goes
1211      after the point where the ellipsis could appear in a parameter
1212      pack. If we find any of these, then this declarator can not be
1213      made into a parameter pack.  */
1214   bool found = false;
1215   while (declarator && !found)
1216     {
1217       switch ((int)declarator->kind)
1218         {
1219         case cdk_id:
1220         case cdk_array:
1221           found = true;
1222           break;
1223
1224         case cdk_error:
1225           return true;
1226
1227         default:
1228           declarator = declarator->declarator;
1229           break;
1230         }
1231     }
1232
1233   return !found;
1234 }
1235
1236 cp_parameter_declarator *no_parameters;
1237
1238 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1239    DECLARATOR and DEFAULT_ARGUMENT.  */
1240
1241 cp_parameter_declarator *
1242 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1243                            cp_declarator *declarator,
1244                            tree default_argument)
1245 {
1246   cp_parameter_declarator *parameter;
1247
1248   parameter = ((cp_parameter_declarator *)
1249                alloc_declarator (sizeof (cp_parameter_declarator)));
1250   parameter->next = NULL;
1251   if (decl_specifiers)
1252     parameter->decl_specifiers = *decl_specifiers;
1253   else
1254     clear_decl_specs (&parameter->decl_specifiers);
1255   parameter->declarator = declarator;
1256   parameter->default_argument = default_argument;
1257   parameter->ellipsis_p = false;
1258
1259   return parameter;
1260 }
1261
1262 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1263
1264 static bool
1265 function_declarator_p (const cp_declarator *declarator)
1266 {
1267   while (declarator)
1268     {
1269       if (declarator->kind == cdk_function
1270           && declarator->declarator->kind == cdk_id)
1271         return true;
1272       if (declarator->kind == cdk_id
1273           || declarator->kind == cdk_error)
1274         return false;
1275       declarator = declarator->declarator;
1276     }
1277   return false;
1278 }
1279  
1280 /* The parser.  */
1281
1282 /* Overview
1283    --------
1284
1285    A cp_parser parses the token stream as specified by the C++
1286    grammar.  Its job is purely parsing, not semantic analysis.  For
1287    example, the parser breaks the token stream into declarators,
1288    expressions, statements, and other similar syntactic constructs.
1289    It does not check that the types of the expressions on either side
1290    of an assignment-statement are compatible, or that a function is
1291    not declared with a parameter of type `void'.
1292
1293    The parser invokes routines elsewhere in the compiler to perform
1294    semantic analysis and to build up the abstract syntax tree for the
1295    code processed.
1296
1297    The parser (and the template instantiation code, which is, in a
1298    way, a close relative of parsing) are the only parts of the
1299    compiler that should be calling push_scope and pop_scope, or
1300    related functions.  The parser (and template instantiation code)
1301    keeps track of what scope is presently active; everything else
1302    should simply honor that.  (The code that generates static
1303    initializers may also need to set the scope, in order to check
1304    access control correctly when emitting the initializers.)
1305
1306    Methodology
1307    -----------
1308
1309    The parser is of the standard recursive-descent variety.  Upcoming
1310    tokens in the token stream are examined in order to determine which
1311    production to use when parsing a non-terminal.  Some C++ constructs
1312    require arbitrary look ahead to disambiguate.  For example, it is
1313    impossible, in the general case, to tell whether a statement is an
1314    expression or declaration without scanning the entire statement.
1315    Therefore, the parser is capable of "parsing tentatively."  When the
1316    parser is not sure what construct comes next, it enters this mode.
1317    Then, while we attempt to parse the construct, the parser queues up
1318    error messages, rather than issuing them immediately, and saves the
1319    tokens it consumes.  If the construct is parsed successfully, the
1320    parser "commits", i.e., it issues any queued error messages and
1321    the tokens that were being preserved are permanently discarded.
1322    If, however, the construct is not parsed successfully, the parser
1323    rolls back its state completely so that it can resume parsing using
1324    a different alternative.
1325
1326    Future Improvements
1327    -------------------
1328
1329    The performance of the parser could probably be improved substantially.
1330    We could often eliminate the need to parse tentatively by looking ahead
1331    a little bit.  In some places, this approach might not entirely eliminate
1332    the need to parse tentatively, but it might still speed up the average
1333    case.  */
1334
1335 /* Flags that are passed to some parsing functions.  These values can
1336    be bitwise-ored together.  */
1337
1338 enum
1339 {
1340   /* No flags.  */
1341   CP_PARSER_FLAGS_NONE = 0x0,
1342   /* The construct is optional.  If it is not present, then no error
1343      should be issued.  */
1344   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1345   /* When parsing a type-specifier, treat user-defined type-names
1346      as non-type identifiers.  */
1347   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1348   /* When parsing a type-specifier, do not try to parse a class-specifier
1349      or enum-specifier.  */
1350   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1351   /* When parsing a decl-specifier-seq, only allow type-specifier or
1352      constexpr.  */
1353   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1354 };
1355
1356 /* This type is used for parameters and variables which hold
1357    combinations of the above flags.  */
1358 typedef int cp_parser_flags;
1359
1360 /* The different kinds of declarators we want to parse.  */
1361
1362 typedef enum cp_parser_declarator_kind
1363 {
1364   /* We want an abstract declarator.  */
1365   CP_PARSER_DECLARATOR_ABSTRACT,
1366   /* We want a named declarator.  */
1367   CP_PARSER_DECLARATOR_NAMED,
1368   /* We don't mind, but the name must be an unqualified-id.  */
1369   CP_PARSER_DECLARATOR_EITHER
1370 } cp_parser_declarator_kind;
1371
1372 /* The precedence values used to parse binary expressions.  The minimum value
1373    of PREC must be 1, because zero is reserved to quickly discriminate
1374    binary operators from other tokens.  */
1375
1376 enum cp_parser_prec
1377 {
1378   PREC_NOT_OPERATOR,
1379   PREC_LOGICAL_OR_EXPRESSION,
1380   PREC_LOGICAL_AND_EXPRESSION,
1381   PREC_INCLUSIVE_OR_EXPRESSION,
1382   PREC_EXCLUSIVE_OR_EXPRESSION,
1383   PREC_AND_EXPRESSION,
1384   PREC_EQUALITY_EXPRESSION,
1385   PREC_RELATIONAL_EXPRESSION,
1386   PREC_SHIFT_EXPRESSION,
1387   PREC_ADDITIVE_EXPRESSION,
1388   PREC_MULTIPLICATIVE_EXPRESSION,
1389   PREC_PM_EXPRESSION,
1390   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1391 };
1392
1393 /* A mapping from a token type to a corresponding tree node type, with a
1394    precedence value.  */
1395
1396 typedef struct cp_parser_binary_operations_map_node
1397 {
1398   /* The token type.  */
1399   enum cpp_ttype token_type;
1400   /* The corresponding tree code.  */
1401   enum tree_code tree_type;
1402   /* The precedence of this operator.  */
1403   enum cp_parser_prec prec;
1404 } cp_parser_binary_operations_map_node;
1405
1406 /* The status of a tentative parse.  */
1407
1408 typedef enum cp_parser_status_kind
1409 {
1410   /* No errors have occurred.  */
1411   CP_PARSER_STATUS_KIND_NO_ERROR,
1412   /* An error has occurred.  */
1413   CP_PARSER_STATUS_KIND_ERROR,
1414   /* We are committed to this tentative parse, whether or not an error
1415      has occurred.  */
1416   CP_PARSER_STATUS_KIND_COMMITTED
1417 } cp_parser_status_kind;
1418
1419 typedef struct cp_parser_expression_stack_entry
1420 {
1421   /* Left hand side of the binary operation we are currently
1422      parsing.  */
1423   tree lhs;
1424   /* Original tree code for left hand side, if it was a binary
1425      expression itself (used for -Wparentheses).  */
1426   enum tree_code lhs_type;
1427   /* Tree code for the binary operation we are parsing.  */
1428   enum tree_code tree_type;
1429   /* Precedence of the binary operation we are parsing.  */
1430   enum cp_parser_prec prec;
1431 } cp_parser_expression_stack_entry;
1432
1433 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1434    entries because precedence levels on the stack are monotonically
1435    increasing.  */
1436 typedef struct cp_parser_expression_stack_entry
1437   cp_parser_expression_stack[NUM_PREC_VALUES];
1438
1439 /* Context that is saved and restored when parsing tentatively.  */
1440 typedef struct GTY (()) cp_parser_context {
1441   /* If this is a tentative parsing context, the status of the
1442      tentative parse.  */
1443   enum cp_parser_status_kind status;
1444   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1445      that are looked up in this context must be looked up both in the
1446      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1447      the context of the containing expression.  */
1448   tree object_type;
1449
1450   /* The next parsing context in the stack.  */
1451   struct cp_parser_context *next;
1452 } cp_parser_context;
1453
1454 /* Prototypes.  */
1455
1456 /* Constructors and destructors.  */
1457
1458 static cp_parser_context *cp_parser_context_new
1459   (cp_parser_context *);
1460
1461 /* Class variables.  */
1462
1463 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1464
1465 /* The operator-precedence table used by cp_parser_binary_expression.
1466    Transformed into an associative array (binops_by_token) by
1467    cp_parser_new.  */
1468
1469 static const cp_parser_binary_operations_map_node binops[] = {
1470   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1471   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1472
1473   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1474   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1475   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1476
1477   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1478   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1479
1480   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1481   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1482
1483   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1484   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1485   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1486   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1487
1488   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1489   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1490
1491   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1492
1493   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1494
1495   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1496
1497   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1498
1499   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1500 };
1501
1502 /* The same as binops, but initialized by cp_parser_new so that
1503    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1504    for speed.  */
1505 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1506
1507 /* Constructors and destructors.  */
1508
1509 /* Construct a new context.  The context below this one on the stack
1510    is given by NEXT.  */
1511
1512 static cp_parser_context *
1513 cp_parser_context_new (cp_parser_context* next)
1514 {
1515   cp_parser_context *context;
1516
1517   /* Allocate the storage.  */
1518   if (cp_parser_context_free_list != NULL)
1519     {
1520       /* Pull the first entry from the free list.  */
1521       context = cp_parser_context_free_list;
1522       cp_parser_context_free_list = context->next;
1523       memset (context, 0, sizeof (*context));
1524     }
1525   else
1526     context = ggc_alloc_cleared_cp_parser_context ();
1527
1528   /* No errors have occurred yet in this context.  */
1529   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1530   /* If this is not the bottommost context, copy information that we
1531      need from the previous context.  */
1532   if (next)
1533     {
1534       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1535          expression, then we are parsing one in this context, too.  */
1536       context->object_type = next->object_type;
1537       /* Thread the stack.  */
1538       context->next = next;
1539     }
1540
1541   return context;
1542 }
1543
1544 /* An entry in a queue of function arguments that require post-processing.  */
1545
1546 typedef struct GTY(()) cp_default_arg_entry_d {
1547   /* The current_class_type when we parsed this arg.  */
1548   tree class_type;
1549
1550   /* The function decl itself.  */
1551   tree decl;
1552 } cp_default_arg_entry;
1553
1554 DEF_VEC_O(cp_default_arg_entry);
1555 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1556
1557 /* An entry in a stack for member functions of local classes.  */
1558
1559 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1560   /* Functions with default arguments that require post-processing.
1561      Functions appear in this list in declaration order.  */
1562   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1563
1564   /* Functions with defintions that require post-processing.  Functions
1565      appear in this list in declaration order.  */
1566   VEC(tree,gc) *funs_with_definitions;
1567 } cp_unparsed_functions_entry;
1568
1569 DEF_VEC_O(cp_unparsed_functions_entry);
1570 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1571
1572 /* The cp_parser structure represents the C++ parser.  */
1573
1574 typedef struct GTY(()) cp_parser {
1575   /* The lexer from which we are obtaining tokens.  */
1576   cp_lexer *lexer;
1577
1578   /* The scope in which names should be looked up.  If NULL_TREE, then
1579      we look up names in the scope that is currently open in the
1580      source program.  If non-NULL, this is either a TYPE or
1581      NAMESPACE_DECL for the scope in which we should look.  It can
1582      also be ERROR_MARK, when we've parsed a bogus scope.
1583
1584      This value is not cleared automatically after a name is looked
1585      up, so we must be careful to clear it before starting a new look
1586      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1587      will look up `Z' in the scope of `X', rather than the current
1588      scope.)  Unfortunately, it is difficult to tell when name lookup
1589      is complete, because we sometimes peek at a token, look it up,
1590      and then decide not to consume it.   */
1591   tree scope;
1592
1593   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1594      last lookup took place.  OBJECT_SCOPE is used if an expression
1595      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1596      respectively.  QUALIFYING_SCOPE is used for an expression of the
1597      form "X::Y"; it refers to X.  */
1598   tree object_scope;
1599   tree qualifying_scope;
1600
1601   /* A stack of parsing contexts.  All but the bottom entry on the
1602      stack will be tentative contexts.
1603
1604      We parse tentatively in order to determine which construct is in
1605      use in some situations.  For example, in order to determine
1606      whether a statement is an expression-statement or a
1607      declaration-statement we parse it tentatively as a
1608      declaration-statement.  If that fails, we then reparse the same
1609      token stream as an expression-statement.  */
1610   cp_parser_context *context;
1611
1612   /* True if we are parsing GNU C++.  If this flag is not set, then
1613      GNU extensions are not recognized.  */
1614   bool allow_gnu_extensions_p;
1615
1616   /* TRUE if the `>' token should be interpreted as the greater-than
1617      operator.  FALSE if it is the end of a template-id or
1618      template-parameter-list. In C++0x mode, this flag also applies to
1619      `>>' tokens, which are viewed as two consecutive `>' tokens when
1620      this flag is FALSE.  */
1621   bool greater_than_is_operator_p;
1622
1623   /* TRUE if default arguments are allowed within a parameter list
1624      that starts at this point. FALSE if only a gnu extension makes
1625      them permissible.  */
1626   bool default_arg_ok_p;
1627
1628   /* TRUE if we are parsing an integral constant-expression.  See
1629      [expr.const] for a precise definition.  */
1630   bool integral_constant_expression_p;
1631
1632   /* TRUE if we are parsing an integral constant-expression -- but a
1633      non-constant expression should be permitted as well.  This flag
1634      is used when parsing an array bound so that GNU variable-length
1635      arrays are tolerated.  */
1636   bool allow_non_integral_constant_expression_p;
1637
1638   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1639      been seen that makes the expression non-constant.  */
1640   bool non_integral_constant_expression_p;
1641
1642   /* TRUE if local variable names and `this' are forbidden in the
1643      current context.  */
1644   bool local_variables_forbidden_p;
1645
1646   /* TRUE if the declaration we are parsing is part of a
1647      linkage-specification of the form `extern string-literal
1648      declaration'.  */
1649   bool in_unbraced_linkage_specification_p;
1650
1651   /* TRUE if we are presently parsing a declarator, after the
1652      direct-declarator.  */
1653   bool in_declarator_p;
1654
1655   /* TRUE if we are presently parsing a template-argument-list.  */
1656   bool in_template_argument_list_p;
1657
1658   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1659      to IN_OMP_BLOCK if parsing OpenMP structured block and
1660      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1661      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1662      iteration-statement, OpenMP block or loop within that switch.  */
1663 #define IN_SWITCH_STMT          1
1664 #define IN_ITERATION_STMT       2
1665 #define IN_OMP_BLOCK            4
1666 #define IN_OMP_FOR              8
1667 #define IN_IF_STMT             16
1668   unsigned char in_statement;
1669
1670   /* TRUE if we are presently parsing the body of a switch statement.
1671      Note that this doesn't quite overlap with in_statement above.
1672      The difference relates to giving the right sets of error messages:
1673      "case not in switch" vs "break statement used with OpenMP...".  */
1674   bool in_switch_statement_p;
1675
1676   /* TRUE if we are parsing a type-id in an expression context.  In
1677      such a situation, both "type (expr)" and "type (type)" are valid
1678      alternatives.  */
1679   bool in_type_id_in_expr_p;
1680
1681   /* TRUE if we are currently in a header file where declarations are
1682      implicitly extern "C".  */
1683   bool implicit_extern_c;
1684
1685   /* TRUE if strings in expressions should be translated to the execution
1686      character set.  */
1687   bool translate_strings_p;
1688
1689   /* TRUE if we are presently parsing the body of a function, but not
1690      a local class.  */
1691   bool in_function_body;
1692
1693   /* If non-NULL, then we are parsing a construct where new type
1694      definitions are not permitted.  The string stored here will be
1695      issued as an error message if a type is defined.  */
1696   const char *type_definition_forbidden_message;
1697
1698   /* A stack used for member functions of local classes.  The lists
1699      contained in an individual entry can only be processed once the
1700      outermost class being defined is complete.  */
1701   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1702
1703   /* The number of classes whose definitions are currently in
1704      progress.  */
1705   unsigned num_classes_being_defined;
1706
1707   /* The number of template parameter lists that apply directly to the
1708      current declaration.  */
1709   unsigned num_template_parameter_lists;
1710 } cp_parser;
1711
1712 /* Managing the unparsed function queues.  */
1713
1714 #define unparsed_funs_with_default_args \
1715   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1716 #define unparsed_funs_with_definitions \
1717   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1718
1719 static void
1720 push_unparsed_function_queues (cp_parser *parser)
1721 {
1722   VEC_safe_push (cp_unparsed_functions_entry, gc,
1723                  parser->unparsed_queues, NULL);
1724   unparsed_funs_with_default_args = NULL;
1725   unparsed_funs_with_definitions = make_tree_vector ();
1726 }
1727
1728 static void
1729 pop_unparsed_function_queues (cp_parser *parser)
1730 {
1731   release_tree_vector (unparsed_funs_with_definitions);
1732   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1733 }
1734
1735 /* Prototypes.  */
1736
1737 /* Constructors and destructors.  */
1738
1739 static cp_parser *cp_parser_new
1740   (void);
1741
1742 /* Routines to parse various constructs.
1743
1744    Those that return `tree' will return the error_mark_node (rather
1745    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1746    Sometimes, they will return an ordinary node if error-recovery was
1747    attempted, even though a parse error occurred.  So, to check
1748    whether or not a parse error occurred, you should always use
1749    cp_parser_error_occurred.  If the construct is optional (indicated
1750    either by an `_opt' in the name of the function that does the
1751    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1752    the construct is not present.  */
1753
1754 /* Lexical conventions [gram.lex]  */
1755
1756 static tree cp_parser_identifier
1757   (cp_parser *);
1758 static tree cp_parser_string_literal
1759   (cp_parser *, bool, bool);
1760
1761 /* Basic concepts [gram.basic]  */
1762
1763 static bool cp_parser_translation_unit
1764   (cp_parser *);
1765
1766 /* Expressions [gram.expr]  */
1767
1768 static tree cp_parser_primary_expression
1769   (cp_parser *, bool, bool, bool, cp_id_kind *);
1770 static tree cp_parser_id_expression
1771   (cp_parser *, bool, bool, bool *, bool, bool);
1772 static tree cp_parser_unqualified_id
1773   (cp_parser *, bool, bool, bool, bool);
1774 static tree cp_parser_nested_name_specifier_opt
1775   (cp_parser *, bool, bool, bool, bool);
1776 static tree cp_parser_nested_name_specifier
1777   (cp_parser *, bool, bool, bool, bool);
1778 static tree cp_parser_qualifying_entity
1779   (cp_parser *, bool, bool, bool, bool, bool);
1780 static tree cp_parser_postfix_expression
1781   (cp_parser *, bool, bool, bool, cp_id_kind *);
1782 static tree cp_parser_postfix_open_square_expression
1783   (cp_parser *, tree, bool);
1784 static tree cp_parser_postfix_dot_deref_expression
1785   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1786 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1787   (cp_parser *, int, bool, bool, bool *);
1788 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1789 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1790 static void cp_parser_pseudo_destructor_name
1791   (cp_parser *, tree *, tree *);
1792 static tree cp_parser_unary_expression
1793   (cp_parser *, bool, bool, cp_id_kind *);
1794 static enum tree_code cp_parser_unary_operator
1795   (cp_token *);
1796 static tree cp_parser_new_expression
1797   (cp_parser *);
1798 static VEC(tree,gc) *cp_parser_new_placement
1799   (cp_parser *);
1800 static tree cp_parser_new_type_id
1801   (cp_parser *, tree *);
1802 static cp_declarator *cp_parser_new_declarator_opt
1803   (cp_parser *);
1804 static cp_declarator *cp_parser_direct_new_declarator
1805   (cp_parser *);
1806 static VEC(tree,gc) *cp_parser_new_initializer
1807   (cp_parser *);
1808 static tree cp_parser_delete_expression
1809   (cp_parser *);
1810 static tree cp_parser_cast_expression
1811   (cp_parser *, bool, bool, cp_id_kind *);
1812 static tree cp_parser_binary_expression
1813   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1814 static tree cp_parser_question_colon_clause
1815   (cp_parser *, tree);
1816 static tree cp_parser_assignment_expression
1817   (cp_parser *, bool, cp_id_kind *);
1818 static enum tree_code cp_parser_assignment_operator_opt
1819   (cp_parser *);
1820 static tree cp_parser_expression
1821   (cp_parser *, bool, cp_id_kind *);
1822 static tree cp_parser_constant_expression
1823   (cp_parser *, bool, bool *);
1824 static tree cp_parser_builtin_offsetof
1825   (cp_parser *);
1826 static tree cp_parser_lambda_expression
1827   (cp_parser *);
1828 static void cp_parser_lambda_introducer
1829   (cp_parser *, tree);
1830 static void cp_parser_lambda_declarator_opt
1831   (cp_parser *, tree);
1832 static void cp_parser_lambda_body
1833   (cp_parser *, tree);
1834
1835 /* Statements [gram.stmt.stmt]  */
1836
1837 static void cp_parser_statement
1838   (cp_parser *, tree, bool, bool *);
1839 static void cp_parser_label_for_labeled_statement
1840   (cp_parser *);
1841 static tree cp_parser_expression_statement
1842   (cp_parser *, tree);
1843 static tree cp_parser_compound_statement
1844   (cp_parser *, tree, bool);
1845 static void cp_parser_statement_seq_opt
1846   (cp_parser *, tree);
1847 static tree cp_parser_selection_statement
1848   (cp_parser *, bool *);
1849 static tree cp_parser_condition
1850   (cp_parser *);
1851 static tree cp_parser_iteration_statement
1852   (cp_parser *);
1853 static void cp_parser_for_init_statement
1854   (cp_parser *);
1855 static tree  cp_parser_c_for
1856   (cp_parser *);
1857 static tree  cp_parser_range_for
1858   (cp_parser *);
1859 static tree cp_parser_jump_statement
1860   (cp_parser *);
1861 static void cp_parser_declaration_statement
1862   (cp_parser *);
1863
1864 static tree cp_parser_implicitly_scoped_statement
1865   (cp_parser *, bool *);
1866 static void cp_parser_already_scoped_statement
1867   (cp_parser *);
1868
1869 /* Declarations [gram.dcl.dcl] */
1870
1871 static void cp_parser_declaration_seq_opt
1872   (cp_parser *);
1873 static void cp_parser_declaration
1874   (cp_parser *);
1875 static void cp_parser_block_declaration
1876   (cp_parser *, bool);
1877 static void cp_parser_simple_declaration
1878   (cp_parser *, bool);
1879 static void cp_parser_decl_specifier_seq
1880   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1881 static tree cp_parser_storage_class_specifier_opt
1882   (cp_parser *);
1883 static tree cp_parser_function_specifier_opt
1884   (cp_parser *, cp_decl_specifier_seq *);
1885 static tree cp_parser_type_specifier
1886   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1887    int *, bool *);
1888 static tree cp_parser_simple_type_specifier
1889   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1890 static tree cp_parser_type_name
1891   (cp_parser *);
1892 static tree cp_parser_nonclass_name 
1893   (cp_parser* parser);
1894 static tree cp_parser_elaborated_type_specifier
1895   (cp_parser *, bool, bool);
1896 static tree cp_parser_enum_specifier
1897   (cp_parser *);
1898 static void cp_parser_enumerator_list
1899   (cp_parser *, tree);
1900 static void cp_parser_enumerator_definition
1901   (cp_parser *, tree);
1902 static tree cp_parser_namespace_name
1903   (cp_parser *);
1904 static void cp_parser_namespace_definition
1905   (cp_parser *);
1906 static void cp_parser_namespace_body
1907   (cp_parser *);
1908 static tree cp_parser_qualified_namespace_specifier
1909   (cp_parser *);
1910 static void cp_parser_namespace_alias_definition
1911   (cp_parser *);
1912 static bool cp_parser_using_declaration
1913   (cp_parser *, bool);
1914 static void cp_parser_using_directive
1915   (cp_parser *);
1916 static void cp_parser_asm_definition
1917   (cp_parser *);
1918 static void cp_parser_linkage_specification
1919   (cp_parser *);
1920 static void cp_parser_static_assert
1921   (cp_parser *, bool);
1922 static tree cp_parser_decltype
1923   (cp_parser *);
1924
1925 /* Declarators [gram.dcl.decl] */
1926
1927 static tree cp_parser_init_declarator
1928   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1929 static cp_declarator *cp_parser_declarator
1930   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1931 static cp_declarator *cp_parser_direct_declarator
1932   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1933 static enum tree_code cp_parser_ptr_operator
1934   (cp_parser *, tree *, cp_cv_quals *);
1935 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1936   (cp_parser *);
1937 static tree cp_parser_late_return_type_opt
1938   (cp_parser *);
1939 static tree cp_parser_declarator_id
1940   (cp_parser *, bool);
1941 static tree cp_parser_type_id
1942   (cp_parser *);
1943 static tree cp_parser_template_type_arg
1944   (cp_parser *);
1945 static tree cp_parser_trailing_type_id (cp_parser *);
1946 static tree cp_parser_type_id_1
1947   (cp_parser *, bool, bool);
1948 static void cp_parser_type_specifier_seq
1949   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1950 static tree cp_parser_parameter_declaration_clause
1951   (cp_parser *);
1952 static tree cp_parser_parameter_declaration_list
1953   (cp_parser *, bool *);
1954 static cp_parameter_declarator *cp_parser_parameter_declaration
1955   (cp_parser *, bool, bool *);
1956 static tree cp_parser_default_argument 
1957   (cp_parser *, bool);
1958 static void cp_parser_function_body
1959   (cp_parser *);
1960 static tree cp_parser_initializer
1961   (cp_parser *, bool *, bool *);
1962 static tree cp_parser_initializer_clause
1963   (cp_parser *, bool *);
1964 static tree cp_parser_braced_list
1965   (cp_parser*, bool*);
1966 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1967   (cp_parser *, bool *);
1968
1969 static bool cp_parser_ctor_initializer_opt_and_function_body
1970   (cp_parser *);
1971
1972 /* Classes [gram.class] */
1973
1974 static tree cp_parser_class_name
1975   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1976 static tree cp_parser_class_specifier
1977   (cp_parser *);
1978 static tree cp_parser_class_head
1979   (cp_parser *, bool *, tree *, tree *);
1980 static enum tag_types cp_parser_class_key
1981   (cp_parser *);
1982 static void cp_parser_member_specification_opt
1983   (cp_parser *);
1984 static void cp_parser_member_declaration
1985   (cp_parser *);
1986 static tree cp_parser_pure_specifier
1987   (cp_parser *);
1988 static tree cp_parser_constant_initializer
1989   (cp_parser *);
1990
1991 /* Derived classes [gram.class.derived] */
1992
1993 static tree cp_parser_base_clause
1994   (cp_parser *);
1995 static tree cp_parser_base_specifier
1996   (cp_parser *);
1997
1998 /* Special member functions [gram.special] */
1999
2000 static tree cp_parser_conversion_function_id
2001   (cp_parser *);
2002 static tree cp_parser_conversion_type_id
2003   (cp_parser *);
2004 static cp_declarator *cp_parser_conversion_declarator_opt
2005   (cp_parser *);
2006 static bool cp_parser_ctor_initializer_opt
2007   (cp_parser *);
2008 static void cp_parser_mem_initializer_list
2009   (cp_parser *);
2010 static tree cp_parser_mem_initializer
2011   (cp_parser *);
2012 static tree cp_parser_mem_initializer_id
2013   (cp_parser *);
2014
2015 /* Overloading [gram.over] */
2016
2017 static tree cp_parser_operator_function_id
2018   (cp_parser *);
2019 static tree cp_parser_operator
2020   (cp_parser *);
2021
2022 /* Templates [gram.temp] */
2023
2024 static void cp_parser_template_declaration
2025   (cp_parser *, bool);
2026 static tree cp_parser_template_parameter_list
2027   (cp_parser *);
2028 static tree cp_parser_template_parameter
2029   (cp_parser *, bool *, bool *);
2030 static tree cp_parser_type_parameter
2031   (cp_parser *, bool *);
2032 static tree cp_parser_template_id
2033   (cp_parser *, bool, bool, bool);
2034 static tree cp_parser_template_name
2035   (cp_parser *, bool, bool, bool, bool *);
2036 static tree cp_parser_template_argument_list
2037   (cp_parser *);
2038 static tree cp_parser_template_argument
2039   (cp_parser *);
2040 static void cp_parser_explicit_instantiation
2041   (cp_parser *);
2042 static void cp_parser_explicit_specialization
2043   (cp_parser *);
2044
2045 /* Exception handling [gram.exception] */
2046
2047 static tree cp_parser_try_block
2048   (cp_parser *);
2049 static bool cp_parser_function_try_block
2050   (cp_parser *);
2051 static void cp_parser_handler_seq
2052   (cp_parser *);
2053 static void cp_parser_handler
2054   (cp_parser *);
2055 static tree cp_parser_exception_declaration
2056   (cp_parser *);
2057 static tree cp_parser_throw_expression
2058   (cp_parser *);
2059 static tree cp_parser_exception_specification_opt
2060   (cp_parser *);
2061 static tree cp_parser_type_id_list
2062   (cp_parser *);
2063
2064 /* GNU Extensions */
2065
2066 static tree cp_parser_asm_specification_opt
2067   (cp_parser *);
2068 static tree cp_parser_asm_operand_list
2069   (cp_parser *);
2070 static tree cp_parser_asm_clobber_list
2071   (cp_parser *);
2072 static tree cp_parser_asm_label_list
2073   (cp_parser *);
2074 static tree cp_parser_attributes_opt
2075   (cp_parser *);
2076 static tree cp_parser_attribute_list
2077   (cp_parser *);
2078 static bool cp_parser_extension_opt
2079   (cp_parser *, int *);
2080 static void cp_parser_label_declaration
2081   (cp_parser *);
2082
2083 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2084 static bool cp_parser_pragma
2085   (cp_parser *, enum pragma_context);
2086
2087 /* Objective-C++ Productions */
2088
2089 static tree cp_parser_objc_message_receiver
2090   (cp_parser *);
2091 static tree cp_parser_objc_message_args
2092   (cp_parser *);
2093 static tree cp_parser_objc_message_expression
2094   (cp_parser *);
2095 static tree cp_parser_objc_encode_expression
2096   (cp_parser *);
2097 static tree cp_parser_objc_defs_expression
2098   (cp_parser *);
2099 static tree cp_parser_objc_protocol_expression
2100   (cp_parser *);
2101 static tree cp_parser_objc_selector_expression
2102   (cp_parser *);
2103 static tree cp_parser_objc_expression
2104   (cp_parser *);
2105 static bool cp_parser_objc_selector_p
2106   (enum cpp_ttype);
2107 static tree cp_parser_objc_selector
2108   (cp_parser *);
2109 static tree cp_parser_objc_protocol_refs_opt
2110   (cp_parser *);
2111 static void cp_parser_objc_declaration
2112   (cp_parser *, tree);
2113 static tree cp_parser_objc_statement
2114   (cp_parser *);
2115 static bool cp_parser_objc_valid_prefix_attributes
2116   (cp_parser *, tree *);
2117 static void cp_parser_objc_at_property_declaration 
2118   (cp_parser *) ;
2119 static void cp_parser_objc_at_synthesize_declaration 
2120   (cp_parser *) ;
2121 static void cp_parser_objc_at_dynamic_declaration
2122   (cp_parser *) ;
2123 static tree cp_parser_objc_struct_declaration
2124   (cp_parser *) ;
2125
2126 /* Utility Routines */
2127
2128 static tree cp_parser_lookup_name
2129   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2130 static tree cp_parser_lookup_name_simple
2131   (cp_parser *, tree, location_t);
2132 static tree cp_parser_maybe_treat_template_as_class
2133   (tree, bool);
2134 static bool cp_parser_check_declarator_template_parameters
2135   (cp_parser *, cp_declarator *, location_t);
2136 static bool cp_parser_check_template_parameters
2137   (cp_parser *, unsigned, location_t, cp_declarator *);
2138 static tree cp_parser_simple_cast_expression
2139   (cp_parser *);
2140 static tree cp_parser_global_scope_opt
2141   (cp_parser *, bool);
2142 static bool cp_parser_constructor_declarator_p
2143   (cp_parser *, bool);
2144 static tree cp_parser_function_definition_from_specifiers_and_declarator
2145   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2146 static tree cp_parser_function_definition_after_declarator
2147   (cp_parser *, bool);
2148 static void cp_parser_template_declaration_after_export
2149   (cp_parser *, bool);
2150 static void cp_parser_perform_template_parameter_access_checks
2151   (VEC (deferred_access_check,gc)*);
2152 static tree cp_parser_single_declaration
2153   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2154 static tree cp_parser_functional_cast
2155   (cp_parser *, tree);
2156 static tree cp_parser_save_member_function_body
2157   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2158 static tree cp_parser_enclosed_template_argument_list
2159   (cp_parser *);
2160 static void cp_parser_save_default_args
2161   (cp_parser *, tree);
2162 static void cp_parser_late_parsing_for_member
2163   (cp_parser *, tree);
2164 static void cp_parser_late_parsing_default_args
2165   (cp_parser *, tree);
2166 static tree cp_parser_sizeof_operand
2167   (cp_parser *, enum rid);
2168 static tree cp_parser_trait_expr
2169   (cp_parser *, enum rid);
2170 static bool cp_parser_declares_only_class_p
2171   (cp_parser *);
2172 static void cp_parser_set_storage_class
2173   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2174 static void cp_parser_set_decl_spec_type
2175   (cp_decl_specifier_seq *, tree, location_t, bool);
2176 static bool cp_parser_friend_p
2177   (const cp_decl_specifier_seq *);
2178 static void cp_parser_required_error
2179   (cp_parser *, required_token, bool);
2180 static cp_token *cp_parser_require
2181   (cp_parser *, enum cpp_ttype, required_token);
2182 static cp_token *cp_parser_require_keyword
2183   (cp_parser *, enum rid, required_token);
2184 static bool cp_parser_token_starts_function_definition_p
2185   (cp_token *);
2186 static bool cp_parser_next_token_starts_class_definition_p
2187   (cp_parser *);
2188 static bool cp_parser_next_token_ends_template_argument_p
2189   (cp_parser *);
2190 static bool cp_parser_nth_token_starts_template_argument_list_p
2191   (cp_parser *, size_t);
2192 static enum tag_types cp_parser_token_is_class_key
2193   (cp_token *);
2194 static void cp_parser_check_class_key
2195   (enum tag_types, tree type);
2196 static void cp_parser_check_access_in_redeclaration
2197   (tree type, location_t location);
2198 static bool cp_parser_optional_template_keyword
2199   (cp_parser *);
2200 static void cp_parser_pre_parsed_nested_name_specifier
2201   (cp_parser *);
2202 static bool cp_parser_cache_group
2203   (cp_parser *, enum cpp_ttype, unsigned);
2204 static void cp_parser_parse_tentatively
2205   (cp_parser *);
2206 static void cp_parser_commit_to_tentative_parse
2207   (cp_parser *);
2208 static void cp_parser_abort_tentative_parse
2209   (cp_parser *);
2210 static bool cp_parser_parse_definitely
2211   (cp_parser *);
2212 static inline bool cp_parser_parsing_tentatively
2213   (cp_parser *);
2214 static bool cp_parser_uncommitted_to_tentative_parse_p
2215   (cp_parser *);
2216 static void cp_parser_error
2217   (cp_parser *, const char *);
2218 static void cp_parser_name_lookup_error
2219   (cp_parser *, tree, tree, name_lookup_error, location_t);
2220 static bool cp_parser_simulate_error
2221   (cp_parser *);
2222 static bool cp_parser_check_type_definition
2223   (cp_parser *);
2224 static void cp_parser_check_for_definition_in_return_type
2225   (cp_declarator *, tree, location_t type_location);
2226 static void cp_parser_check_for_invalid_template_id
2227   (cp_parser *, tree, location_t location);
2228 static bool cp_parser_non_integral_constant_expression
2229   (cp_parser *, non_integral_constant);
2230 static void cp_parser_diagnose_invalid_type_name
2231   (cp_parser *, tree, tree, location_t);
2232 static bool cp_parser_parse_and_diagnose_invalid_type_name
2233   (cp_parser *);
2234 static int cp_parser_skip_to_closing_parenthesis
2235   (cp_parser *, bool, bool, bool);
2236 static void cp_parser_skip_to_end_of_statement
2237   (cp_parser *);
2238 static void cp_parser_consume_semicolon_at_end_of_statement
2239   (cp_parser *);
2240 static void cp_parser_skip_to_end_of_block_or_statement
2241   (cp_parser *);
2242 static bool cp_parser_skip_to_closing_brace
2243   (cp_parser *);
2244 static void cp_parser_skip_to_end_of_template_parameter_list
2245   (cp_parser *);
2246 static void cp_parser_skip_to_pragma_eol
2247   (cp_parser*, cp_token *);
2248 static bool cp_parser_error_occurred
2249   (cp_parser *);
2250 static bool cp_parser_allow_gnu_extensions_p
2251   (cp_parser *);
2252 static bool cp_parser_is_string_literal
2253   (cp_token *);
2254 static bool cp_parser_is_keyword
2255   (cp_token *, enum rid);
2256 static tree cp_parser_make_typename_type
2257   (cp_parser *, tree, tree, location_t location);
2258 static cp_declarator * cp_parser_make_indirect_declarator
2259   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2260
2261 /* Returns nonzero if we are parsing tentatively.  */
2262
2263 static inline bool
2264 cp_parser_parsing_tentatively (cp_parser* parser)
2265 {
2266   return parser->context->next != NULL;
2267 }
2268
2269 /* Returns nonzero if TOKEN is a string literal.  */
2270
2271 static bool
2272 cp_parser_is_string_literal (cp_token* token)
2273 {
2274   return (token->type == CPP_STRING ||
2275           token->type == CPP_STRING16 ||
2276           token->type == CPP_STRING32 ||
2277           token->type == CPP_WSTRING ||
2278           token->type == CPP_UTF8STRING);
2279 }
2280
2281 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2282
2283 static bool
2284 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2285 {
2286   return token->keyword == keyword;
2287 }
2288
2289 /* If not parsing tentatively, issue a diagnostic of the form
2290       FILE:LINE: MESSAGE before TOKEN
2291    where TOKEN is the next token in the input stream.  MESSAGE
2292    (specified by the caller) is usually of the form "expected
2293    OTHER-TOKEN".  */
2294
2295 static void
2296 cp_parser_error (cp_parser* parser, const char* gmsgid)
2297 {
2298   if (!cp_parser_simulate_error (parser))
2299     {
2300       cp_token *token = cp_lexer_peek_token (parser->lexer);
2301       /* This diagnostic makes more sense if it is tagged to the line
2302          of the token we just peeked at.  */
2303       cp_lexer_set_source_position_from_token (token);
2304
2305       if (token->type == CPP_PRAGMA)
2306         {
2307           error_at (token->location,
2308                     "%<#pragma%> is not allowed here");
2309           cp_parser_skip_to_pragma_eol (parser, token);
2310           return;
2311         }
2312
2313       c_parse_error (gmsgid,
2314                      /* Because c_parser_error does not understand
2315                         CPP_KEYWORD, keywords are treated like
2316                         identifiers.  */
2317                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2318                      token->u.value, token->flags);
2319     }
2320 }
2321
2322 /* Issue an error about name-lookup failing.  NAME is the
2323    IDENTIFIER_NODE DECL is the result of
2324    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2325    the thing that we hoped to find.  */
2326
2327 static void
2328 cp_parser_name_lookup_error (cp_parser* parser,
2329                              tree name,
2330                              tree decl,
2331                              name_lookup_error desired,
2332                              location_t location)
2333 {
2334   /* If name lookup completely failed, tell the user that NAME was not
2335      declared.  */
2336   if (decl == error_mark_node)
2337     {
2338       if (parser->scope && parser->scope != global_namespace)
2339         error_at (location, "%<%E::%E%> has not been declared",
2340                   parser->scope, name);
2341       else if (parser->scope == global_namespace)
2342         error_at (location, "%<::%E%> has not been declared", name);
2343       else if (parser->object_scope
2344                && !CLASS_TYPE_P (parser->object_scope))
2345         error_at (location, "request for member %qE in non-class type %qT",
2346                   name, parser->object_scope);
2347       else if (parser->object_scope)
2348         error_at (location, "%<%T::%E%> has not been declared",
2349                   parser->object_scope, name);
2350       else
2351         error_at (location, "%qE has not been declared", name);
2352     }
2353   else if (parser->scope && parser->scope != global_namespace)
2354     {
2355       switch (desired)
2356         {
2357           case NLE_TYPE:
2358             error_at (location, "%<%E::%E%> is not a type",
2359                                 parser->scope, name);
2360             break;
2361           case NLE_CXX98:
2362             error_at (location, "%<%E::%E%> is not a class or namespace",
2363                                 parser->scope, name);
2364             break;
2365           case NLE_NOT_CXX98:
2366             error_at (location,
2367                       "%<%E::%E%> is not a class, namespace, or enumeration",
2368                       parser->scope, name);
2369             break;
2370           default:
2371             gcc_unreachable ();
2372             
2373         }
2374     }
2375   else if (parser->scope == global_namespace)
2376     {
2377       switch (desired)
2378         {
2379           case NLE_TYPE:
2380             error_at (location, "%<::%E%> is not a type", name);
2381             break;
2382           case NLE_CXX98:
2383             error_at (location, "%<::%E%> is not a class or namespace", name);
2384             break;
2385           case NLE_NOT_CXX98:
2386             error_at (location,
2387                       "%<::%E%> is not a class, namespace, or enumeration",
2388                       name);
2389             break;
2390           default:
2391             gcc_unreachable ();
2392         }
2393     }
2394   else
2395     {
2396       switch (desired)
2397         {
2398           case NLE_TYPE:
2399             error_at (location, "%qE is not a type", name);
2400             break;
2401           case NLE_CXX98:
2402             error_at (location, "%qE is not a class or namespace", name);
2403             break;
2404           case NLE_NOT_CXX98:
2405             error_at (location,
2406                       "%qE is not a class, namespace, or enumeration", name);
2407             break;
2408           default:
2409             gcc_unreachable ();
2410         }
2411     }
2412 }
2413
2414 /* If we are parsing tentatively, remember that an error has occurred
2415    during this tentative parse.  Returns true if the error was
2416    simulated; false if a message should be issued by the caller.  */
2417
2418 static bool
2419 cp_parser_simulate_error (cp_parser* parser)
2420 {
2421   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2422     {
2423       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2424       return true;
2425     }
2426   return false;
2427 }
2428
2429 /* Check for repeated decl-specifiers.  */
2430
2431 static void
2432 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2433                            location_t location)
2434 {
2435   int ds;
2436
2437   for (ds = ds_first; ds != ds_last; ++ds)
2438     {
2439       unsigned count = decl_specs->specs[ds];
2440       if (count < 2)
2441         continue;
2442       /* The "long" specifier is a special case because of "long long".  */
2443       if (ds == ds_long)
2444         {
2445           if (count > 2)
2446             error_at (location, "%<long long long%> is too long for GCC");
2447           else 
2448             pedwarn_cxx98 (location, OPT_Wlong_long, 
2449                            "ISO C++ 1998 does not support %<long long%>");
2450         }
2451       else if (count > 1)
2452         {
2453           static const char *const decl_spec_names[] = {
2454             "signed",
2455             "unsigned",
2456             "short",
2457             "long",
2458             "const",
2459             "volatile",
2460             "restrict",
2461             "inline",
2462             "virtual",
2463             "explicit",
2464             "friend",
2465             "typedef",
2466             "constexpr",
2467             "__complex",
2468             "__thread"
2469           };
2470           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2471         }
2472     }
2473 }
2474
2475 /* This function is called when a type is defined.  If type
2476    definitions are forbidden at this point, an error message is
2477    issued.  */
2478
2479 static bool
2480 cp_parser_check_type_definition (cp_parser* parser)
2481 {
2482   /* If types are forbidden here, issue a message.  */
2483   if (parser->type_definition_forbidden_message)
2484     {
2485       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2486          in the message need to be interpreted.  */
2487       error (parser->type_definition_forbidden_message);
2488       return false;
2489     }
2490   return true;
2491 }
2492
2493 /* This function is called when the DECLARATOR is processed.  The TYPE
2494    was a type defined in the decl-specifiers.  If it is invalid to
2495    define a type in the decl-specifiers for DECLARATOR, an error is
2496    issued. TYPE_LOCATION is the location of TYPE and is used
2497    for error reporting.  */
2498
2499 static void
2500 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2501                                                tree type, location_t type_location)
2502 {
2503   /* [dcl.fct] forbids type definitions in return types.
2504      Unfortunately, it's not easy to know whether or not we are
2505      processing a return type until after the fact.  */
2506   while (declarator
2507          && (declarator->kind == cdk_pointer
2508              || declarator->kind == cdk_reference
2509              || declarator->kind == cdk_ptrmem))
2510     declarator = declarator->declarator;
2511   if (declarator
2512       && declarator->kind == cdk_function)
2513     {
2514       error_at (type_location,
2515                 "new types may not be defined in a return type");
2516       inform (type_location, 
2517               "(perhaps a semicolon is missing after the definition of %qT)",
2518               type);
2519     }
2520 }
2521
2522 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2523    "<" in any valid C++ program.  If the next token is indeed "<",
2524    issue a message warning the user about what appears to be an
2525    invalid attempt to form a template-id. LOCATION is the location
2526    of the type-specifier (TYPE) */
2527
2528 static void
2529 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2530                                          tree type, location_t location)
2531 {
2532   cp_token_position start = 0;
2533
2534   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2535     {
2536       if (TYPE_P (type))
2537         error_at (location, "%qT is not a template", type);
2538       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2539         error_at (location, "%qE is not a template", type);
2540       else
2541         error_at (location, "invalid template-id");
2542       /* Remember the location of the invalid "<".  */
2543       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2544         start = cp_lexer_token_position (parser->lexer, true);
2545       /* Consume the "<".  */
2546       cp_lexer_consume_token (parser->lexer);
2547       /* Parse the template arguments.  */
2548       cp_parser_enclosed_template_argument_list (parser);
2549       /* Permanently remove the invalid template arguments so that
2550          this error message is not issued again.  */
2551       if (start)
2552         cp_lexer_purge_tokens_after (parser->lexer, start);
2553     }
2554 }
2555
2556 /* If parsing an integral constant-expression, issue an error message
2557    about the fact that THING appeared and return true.  Otherwise,
2558    return false.  In either case, set
2559    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2560
2561 static bool
2562 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2563                                             non_integral_constant thing)
2564 {
2565   parser->non_integral_constant_expression_p = true;
2566   if (parser->integral_constant_expression_p)
2567     {
2568       if (!parser->allow_non_integral_constant_expression_p)
2569         {
2570           const char *msg = NULL;
2571           switch (thing)
2572             {
2573               case NIC_FLOAT:
2574                 error ("floating-point literal "
2575                        "cannot appear in a constant-expression");
2576                 return true;
2577               case NIC_CAST:
2578                 error ("a cast to a type other than an integral or "
2579                        "enumeration type cannot appear in a "
2580                        "constant-expression");
2581                 return true;
2582               case NIC_TYPEID:
2583                 error ("%<typeid%> operator "
2584                        "cannot appear in a constant-expression");
2585                 return true;
2586               case NIC_NCC:
2587                 error ("non-constant compound literals "
2588                        "cannot appear in a constant-expression");
2589                 return true;
2590               case NIC_FUNC_CALL:
2591                 error ("a function call "
2592                        "cannot appear in a constant-expression");
2593                 return true;
2594               case NIC_INC:
2595                 error ("an increment "
2596                        "cannot appear in a constant-expression");
2597                 return true;
2598               case NIC_DEC:
2599                 error ("an decrement "
2600                        "cannot appear in a constant-expression");
2601                 return true;
2602               case NIC_ARRAY_REF:
2603                 error ("an array reference "
2604                        "cannot appear in a constant-expression");
2605                 return true;
2606               case NIC_ADDR_LABEL:
2607                 error ("the address of a label "
2608                        "cannot appear in a constant-expression");
2609                 return true;
2610               case NIC_OVERLOADED:
2611                 error ("calls to overloaded operators "
2612                        "cannot appear in a constant-expression");
2613                 return true;
2614               case NIC_ASSIGNMENT:
2615                 error ("an assignment cannot appear in a constant-expression");
2616                 return true;
2617               case NIC_COMMA:
2618                 error ("a comma operator "
2619                        "cannot appear in a constant-expression");
2620                 return true;
2621               case NIC_CONSTRUCTOR:
2622                 error ("a call to a constructor "
2623                        "cannot appear in a constant-expression");
2624                 return true;
2625               case NIC_THIS:
2626                 msg = "this";
2627                 break;
2628               case NIC_FUNC_NAME:
2629                 msg = "__FUNCTION__";
2630                 break;
2631               case NIC_PRETTY_FUNC:
2632                 msg = "__PRETTY_FUNCTION__";
2633                 break;
2634               case NIC_C99_FUNC:
2635                 msg = "__func__";
2636                 break;
2637               case NIC_VA_ARG:
2638                 msg = "va_arg";
2639                 break;
2640               case NIC_ARROW:
2641                 msg = "->";
2642                 break;
2643               case NIC_POINT:
2644                 msg = ".";
2645                 break;
2646               case NIC_STAR:
2647                 msg = "*";
2648                 break;
2649               case NIC_ADDR:
2650                 msg = "&";
2651                 break;
2652               case NIC_PREINCREMENT:
2653                 msg = "++";
2654                 break;
2655               case NIC_PREDECREMENT:
2656                 msg = "--";
2657                 break;
2658               case NIC_NEW:
2659                 msg = "new";
2660                 break;
2661               case NIC_DEL:
2662                 msg = "delete";
2663                 break;
2664               default:
2665                 gcc_unreachable ();
2666             }
2667           if (msg)
2668             error ("%qs cannot appear in a constant-expression", msg);
2669           return true;
2670         }
2671     }
2672   return false;
2673 }
2674
2675 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2676    qualifying scope (or NULL, if none) for ID.  This function commits
2677    to the current active tentative parse, if any.  (Otherwise, the
2678    problematic construct might be encountered again later, resulting
2679    in duplicate error messages.) LOCATION is the location of ID.  */
2680
2681 static void
2682 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2683                                       tree scope, tree id,
2684                                       location_t location)
2685 {
2686   tree decl, old_scope;
2687   /* Try to lookup the identifier.  */
2688   old_scope = parser->scope;
2689   parser->scope = scope;
2690   decl = cp_parser_lookup_name_simple (parser, id, location);
2691   parser->scope = old_scope;
2692   /* If the lookup found a template-name, it means that the user forgot
2693   to specify an argument list. Emit a useful error message.  */
2694   if (TREE_CODE (decl) == TEMPLATE_DECL)
2695     error_at (location,
2696               "invalid use of template-name %qE without an argument list",
2697               decl);
2698   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2699     error_at (location, "invalid use of destructor %qD as a type", id);
2700   else if (TREE_CODE (decl) == TYPE_DECL)
2701     /* Something like 'unsigned A a;'  */
2702     error_at (location, "invalid combination of multiple type-specifiers");
2703   else if (!parser->scope)
2704     {
2705       /* Issue an error message.  */
2706       error_at (location, "%qE does not name a type", id);
2707       /* If we're in a template class, it's possible that the user was
2708          referring to a type from a base class.  For example:
2709
2710            template <typename T> struct A { typedef T X; };
2711            template <typename T> struct B : public A<T> { X x; };
2712
2713          The user should have said "typename A<T>::X".  */
2714       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2715         inform (location, "C++0x %<constexpr%> only available with "
2716                 "-std=c++0x or -std=gnu++0x");
2717       else if (processing_template_decl && current_class_type
2718                && TYPE_BINFO (current_class_type))
2719         {
2720           tree b;
2721
2722           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2723                b;
2724                b = TREE_CHAIN (b))
2725             {
2726               tree base_type = BINFO_TYPE (b);
2727               if (CLASS_TYPE_P (base_type)
2728                   && dependent_type_p (base_type))
2729                 {
2730                   tree field;
2731                   /* Go from a particular instantiation of the
2732                      template (which will have an empty TYPE_FIELDs),
2733                      to the main version.  */
2734                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2735                   for (field = TYPE_FIELDS (base_type);
2736                        field;
2737                        field = DECL_CHAIN (field))
2738                     if (TREE_CODE (field) == TYPE_DECL
2739                         && DECL_NAME (field) == id)
2740                       {
2741                         inform (location, 
2742                                 "(perhaps %<typename %T::%E%> was intended)",
2743                                 BINFO_TYPE (b), id);
2744                         break;
2745                       }
2746                   if (field)
2747                     break;
2748                 }
2749             }
2750         }
2751     }
2752   /* Here we diagnose qualified-ids where the scope is actually correct,
2753      but the identifier does not resolve to a valid type name.  */
2754   else if (parser->scope != error_mark_node)
2755     {
2756       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2757         error_at (location, "%qE in namespace %qE does not name a type",
2758                   id, parser->scope);
2759       else if (CLASS_TYPE_P (parser->scope)
2760                && constructor_name_p (id, parser->scope))
2761         {
2762           /* A<T>::A<T>() */
2763           error_at (location, "%<%T::%E%> names the constructor, not"
2764                     " the type", parser->scope, id);
2765           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2766             error_at (location, "and %qT has no template constructors",
2767                       parser->scope);
2768         }
2769       else if (TYPE_P (parser->scope)
2770                && dependent_scope_p (parser->scope))
2771         error_at (location, "need %<typename%> before %<%T::%E%> because "
2772                   "%qT is a dependent scope",
2773                   parser->scope, id, parser->scope);
2774       else if (TYPE_P (parser->scope))
2775         error_at (location, "%qE in class %qT does not name a type",
2776                   id, parser->scope);
2777       else
2778         gcc_unreachable ();
2779     }
2780   cp_parser_commit_to_tentative_parse (parser);
2781 }
2782
2783 /* Check for a common situation where a type-name should be present,
2784    but is not, and issue a sensible error message.  Returns true if an
2785    invalid type-name was detected.
2786
2787    The situation handled by this function are variable declarations of the
2788    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2789    Usually, `ID' should name a type, but if we got here it means that it
2790    does not. We try to emit the best possible error message depending on
2791    how exactly the id-expression looks like.  */
2792
2793 static bool
2794 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2795 {
2796   tree id;
2797   cp_token *token = cp_lexer_peek_token (parser->lexer);
2798
2799   /* Avoid duplicate error about ambiguous lookup.  */
2800   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2801     {
2802       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2803       if (next->type == CPP_NAME && next->ambiguous_p)
2804         goto out;
2805     }
2806
2807   cp_parser_parse_tentatively (parser);
2808   id = cp_parser_id_expression (parser,
2809                                 /*template_keyword_p=*/false,
2810                                 /*check_dependency_p=*/true,
2811                                 /*template_p=*/NULL,
2812                                 /*declarator_p=*/true,
2813                                 /*optional_p=*/false);
2814   /* If the next token is a (, this is a function with no explicit return
2815      type, i.e. constructor, destructor or conversion op.  */
2816   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2817       || TREE_CODE (id) == TYPE_DECL)
2818     {
2819       cp_parser_abort_tentative_parse (parser);
2820       return false;
2821     }
2822   if (!cp_parser_parse_definitely (parser))
2823     return false;
2824
2825   /* Emit a diagnostic for the invalid type.  */
2826   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2827                                         id, token->location);
2828  out:
2829   /* If we aren't in the middle of a declarator (i.e. in a
2830      parameter-declaration-clause), skip to the end of the declaration;
2831      there's no point in trying to process it.  */
2832   if (!parser->in_declarator_p)
2833     cp_parser_skip_to_end_of_block_or_statement (parser);
2834   return true;
2835 }
2836
2837 /* Consume tokens up to, and including, the next non-nested closing `)'.
2838    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2839    are doing error recovery. Returns -1 if OR_COMMA is true and we
2840    found an unnested comma.  */
2841
2842 static int
2843 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2844                                        bool recovering,
2845                                        bool or_comma,
2846                                        bool consume_paren)
2847 {
2848   unsigned paren_depth = 0;
2849   unsigned brace_depth = 0;
2850   unsigned square_depth = 0;
2851
2852   if (recovering && !or_comma
2853       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2854     return 0;
2855
2856   while (true)
2857     {
2858       cp_token * token = cp_lexer_peek_token (parser->lexer);
2859
2860       switch (token->type)
2861         {
2862         case CPP_EOF:
2863         case CPP_PRAGMA_EOL:
2864           /* If we've run out of tokens, then there is no closing `)'.  */
2865           return 0;
2866
2867         /* This is good for lambda expression capture-lists.  */
2868         case CPP_OPEN_SQUARE:
2869           ++square_depth;
2870           break;
2871         case CPP_CLOSE_SQUARE:
2872           if (!square_depth--)
2873             return 0;
2874           break;
2875
2876         case CPP_SEMICOLON:
2877           /* This matches the processing in skip_to_end_of_statement.  */
2878           if (!brace_depth)
2879             return 0;
2880           break;
2881
2882         case CPP_OPEN_BRACE:
2883           ++brace_depth;
2884           break;
2885         case CPP_CLOSE_BRACE:
2886           if (!brace_depth--)
2887             return 0;
2888           break;
2889
2890         case CPP_COMMA:
2891           if (recovering && or_comma && !brace_depth && !paren_depth
2892               && !square_depth)
2893             return -1;
2894           break;
2895
2896         case CPP_OPEN_PAREN:
2897           if (!brace_depth)
2898             ++paren_depth;
2899           break;
2900
2901         case CPP_CLOSE_PAREN:
2902           if (!brace_depth && !paren_depth--)
2903             {
2904               if (consume_paren)
2905                 cp_lexer_consume_token (parser->lexer);
2906               return 1;
2907             }
2908           break;
2909
2910         default:
2911           break;
2912         }
2913
2914       /* Consume the token.  */
2915       cp_lexer_consume_token (parser->lexer);
2916     }
2917 }
2918
2919 /* Consume tokens until we reach the end of the current statement.
2920    Normally, that will be just before consuming a `;'.  However, if a
2921    non-nested `}' comes first, then we stop before consuming that.  */
2922
2923 static void
2924 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2925 {
2926   unsigned nesting_depth = 0;
2927
2928   while (true)
2929     {
2930       cp_token *token = cp_lexer_peek_token (parser->lexer);
2931
2932       switch (token->type)
2933         {
2934         case CPP_EOF:
2935         case CPP_PRAGMA_EOL:
2936           /* If we've run out of tokens, stop.  */
2937           return;
2938
2939         case CPP_SEMICOLON:
2940           /* If the next token is a `;', we have reached the end of the
2941              statement.  */
2942           if (!nesting_depth)
2943             return;
2944           break;
2945
2946         case CPP_CLOSE_BRACE:
2947           /* If this is a non-nested '}', stop before consuming it.
2948              That way, when confronted with something like:
2949
2950                { 3 + }
2951
2952              we stop before consuming the closing '}', even though we
2953              have not yet reached a `;'.  */
2954           if (nesting_depth == 0)
2955             return;
2956
2957           /* If it is the closing '}' for a block that we have
2958              scanned, stop -- but only after consuming the token.
2959              That way given:
2960
2961                 void f g () { ... }
2962                 typedef int I;
2963
2964              we will stop after the body of the erroneously declared
2965              function, but before consuming the following `typedef'
2966              declaration.  */
2967           if (--nesting_depth == 0)
2968             {
2969               cp_lexer_consume_token (parser->lexer);
2970               return;
2971             }
2972
2973         case CPP_OPEN_BRACE:
2974           ++nesting_depth;
2975           break;
2976
2977         default:
2978           break;
2979         }
2980
2981       /* Consume the token.  */
2982       cp_lexer_consume_token (parser->lexer);
2983     }
2984 }
2985
2986 /* This function is called at the end of a statement or declaration.
2987    If the next token is a semicolon, it is consumed; otherwise, error
2988    recovery is attempted.  */
2989
2990 static void
2991 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2992 {
2993   /* Look for the trailing `;'.  */
2994   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2995     {
2996       /* If there is additional (erroneous) input, skip to the end of
2997          the statement.  */
2998       cp_parser_skip_to_end_of_statement (parser);
2999       /* If the next token is now a `;', consume it.  */
3000       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3001         cp_lexer_consume_token (parser->lexer);
3002     }
3003 }
3004
3005 /* Skip tokens until we have consumed an entire block, or until we
3006    have consumed a non-nested `;'.  */
3007
3008 static void
3009 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3010 {
3011   int nesting_depth = 0;
3012
3013   while (nesting_depth >= 0)
3014     {
3015       cp_token *token = cp_lexer_peek_token (parser->lexer);
3016
3017       switch (token->type)
3018         {
3019         case CPP_EOF:
3020         case CPP_PRAGMA_EOL:
3021           /* If we've run out of tokens, stop.  */
3022           return;
3023
3024         case CPP_SEMICOLON:
3025           /* Stop if this is an unnested ';'. */
3026           if (!nesting_depth)
3027             nesting_depth = -1;
3028           break;
3029
3030         case CPP_CLOSE_BRACE:
3031           /* Stop if this is an unnested '}', or closes the outermost
3032              nesting level.  */
3033           nesting_depth--;
3034           if (nesting_depth < 0)
3035             return;
3036           if (!nesting_depth)
3037             nesting_depth = -1;
3038           break;
3039
3040         case CPP_OPEN_BRACE:
3041           /* Nest. */
3042           nesting_depth++;
3043           break;
3044
3045         default:
3046           break;
3047         }
3048
3049       /* Consume the token.  */
3050       cp_lexer_consume_token (parser->lexer);
3051     }
3052 }
3053
3054 /* Skip tokens until a non-nested closing curly brace is the next
3055    token, or there are no more tokens. Return true in the first case,
3056    false otherwise.  */
3057
3058 static bool
3059 cp_parser_skip_to_closing_brace (cp_parser *parser)
3060 {
3061   unsigned nesting_depth = 0;
3062
3063   while (true)
3064     {
3065       cp_token *token = cp_lexer_peek_token (parser->lexer);
3066
3067       switch (token->type)
3068         {
3069         case CPP_EOF:
3070         case CPP_PRAGMA_EOL:
3071           /* If we've run out of tokens, stop.  */
3072           return false;
3073
3074         case CPP_CLOSE_BRACE:
3075           /* If the next token is a non-nested `}', then we have reached
3076              the end of the current block.  */
3077           if (nesting_depth-- == 0)
3078             return true;
3079           break;
3080
3081         case CPP_OPEN_BRACE:
3082           /* If it the next token is a `{', then we are entering a new
3083              block.  Consume the entire block.  */
3084           ++nesting_depth;
3085           break;
3086
3087         default:
3088           break;
3089         }
3090
3091       /* Consume the token.  */
3092       cp_lexer_consume_token (parser->lexer);
3093     }
3094 }
3095
3096 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3097    parameter is the PRAGMA token, allowing us to purge the entire pragma
3098    sequence.  */
3099
3100 static void
3101 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3102 {
3103   cp_token *token;
3104
3105   parser->lexer->in_pragma = false;
3106
3107   do
3108     token = cp_lexer_consume_token (parser->lexer);
3109   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3110
3111   /* Ensure that the pragma is not parsed again.  */
3112   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3113 }
3114
3115 /* Require pragma end of line, resyncing with it as necessary.  The
3116    arguments are as for cp_parser_skip_to_pragma_eol.  */
3117
3118 static void
3119 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3120 {
3121   parser->lexer->in_pragma = false;
3122   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3123     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3124 }
3125
3126 /* This is a simple wrapper around make_typename_type. When the id is
3127    an unresolved identifier node, we can provide a superior diagnostic
3128    using cp_parser_diagnose_invalid_type_name.  */
3129
3130 static tree
3131 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3132                               tree id, location_t id_location)
3133 {
3134   tree result;
3135   if (TREE_CODE (id) == IDENTIFIER_NODE)
3136     {
3137       result = make_typename_type (scope, id, typename_type,
3138                                    /*complain=*/tf_none);
3139       if (result == error_mark_node)
3140         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3141       return result;
3142     }
3143   return make_typename_type (scope, id, typename_type, tf_error);
3144 }
3145
3146 /* This is a wrapper around the
3147    make_{pointer,ptrmem,reference}_declarator functions that decides
3148    which one to call based on the CODE and CLASS_TYPE arguments. The
3149    CODE argument should be one of the values returned by
3150    cp_parser_ptr_operator. */
3151 static cp_declarator *
3152 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3153                                     cp_cv_quals cv_qualifiers,
3154                                     cp_declarator *target)
3155 {
3156   if (code == ERROR_MARK)
3157     return cp_error_declarator;
3158
3159   if (code == INDIRECT_REF)
3160     if (class_type == NULL_TREE)
3161       return make_pointer_declarator (cv_qualifiers, target);
3162     else
3163       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3164   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3165     return make_reference_declarator (cv_qualifiers, target, false);
3166   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3167     return make_reference_declarator (cv_qualifiers, target, true);
3168   gcc_unreachable ();
3169 }
3170
3171 /* Create a new C++ parser.  */
3172
3173 static cp_parser *
3174 cp_parser_new (void)
3175 {
3176   cp_parser *parser;
3177   cp_lexer *lexer;
3178   unsigned i;
3179
3180   /* cp_lexer_new_main is called before doing GC allocation because
3181      cp_lexer_new_main might load a PCH file.  */
3182   lexer = cp_lexer_new_main ();
3183
3184   /* Initialize the binops_by_token so that we can get the tree
3185      directly from the token.  */
3186   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3187     binops_by_token[binops[i].token_type] = binops[i];
3188
3189   parser = ggc_alloc_cleared_cp_parser ();
3190   parser->lexer = lexer;
3191   parser->context = cp_parser_context_new (NULL);
3192
3193   /* For now, we always accept GNU extensions.  */
3194   parser->allow_gnu_extensions_p = 1;
3195
3196   /* The `>' token is a greater-than operator, not the end of a
3197      template-id.  */
3198   parser->greater_than_is_operator_p = true;
3199
3200   parser->default_arg_ok_p = true;
3201
3202   /* We are not parsing a constant-expression.  */
3203   parser->integral_constant_expression_p = false;
3204   parser->allow_non_integral_constant_expression_p = false;
3205   parser->non_integral_constant_expression_p = false;
3206
3207   /* Local variable names are not forbidden.  */
3208   parser->local_variables_forbidden_p = false;
3209
3210   /* We are not processing an `extern "C"' declaration.  */
3211   parser->in_unbraced_linkage_specification_p = false;
3212
3213   /* We are not processing a declarator.  */
3214   parser->in_declarator_p = false;
3215
3216   /* We are not processing a template-argument-list.  */
3217   parser->in_template_argument_list_p = false;
3218
3219   /* We are not in an iteration statement.  */
3220   parser->in_statement = 0;
3221
3222   /* We are not in a switch statement.  */
3223   parser->in_switch_statement_p = false;
3224
3225   /* We are not parsing a type-id inside an expression.  */
3226   parser->in_type_id_in_expr_p = false;
3227
3228   /* Declarations aren't implicitly extern "C".  */
3229   parser->implicit_extern_c = false;
3230
3231   /* String literals should be translated to the execution character set.  */
3232   parser->translate_strings_p = true;
3233
3234   /* We are not parsing a function body.  */
3235   parser->in_function_body = false;
3236
3237   /* The unparsed function queue is empty.  */
3238   push_unparsed_function_queues (parser);
3239
3240   /* There are no classes being defined.  */
3241   parser->num_classes_being_defined = 0;
3242
3243   /* No template parameters apply.  */
3244   parser->num_template_parameter_lists = 0;
3245
3246   return parser;
3247 }
3248
3249 /* Create a cp_lexer structure which will emit the tokens in CACHE
3250    and push it onto the parser's lexer stack.  This is used for delayed
3251    parsing of in-class method bodies and default arguments, and should
3252    not be confused with tentative parsing.  */
3253 static void
3254 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3255 {
3256   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3257   lexer->next = parser->lexer;
3258   parser->lexer = lexer;
3259
3260   /* Move the current source position to that of the first token in the
3261      new lexer.  */
3262   cp_lexer_set_source_position_from_token (lexer->next_token);
3263 }
3264
3265 /* Pop the top lexer off the parser stack.  This is never used for the
3266    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3267 static void
3268 cp_parser_pop_lexer (cp_parser *parser)
3269 {
3270   cp_lexer *lexer = parser->lexer;
3271   parser->lexer = lexer->next;
3272   cp_lexer_destroy (lexer);
3273
3274   /* Put the current source position back where it was before this
3275      lexer was pushed.  */
3276   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3277 }
3278
3279 /* Lexical conventions [gram.lex]  */
3280
3281 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3282    identifier.  */
3283
3284 static tree
3285 cp_parser_identifier (cp_parser* parser)
3286 {
3287   cp_token *token;
3288
3289   /* Look for the identifier.  */
3290   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3291   /* Return the value.  */
3292   return token ? token->u.value : error_mark_node;
3293 }
3294
3295 /* Parse a sequence of adjacent string constants.  Returns a
3296    TREE_STRING representing the combined, nul-terminated string
3297    constant.  If TRANSLATE is true, translate the string to the
3298    execution character set.  If WIDE_OK is true, a wide string is
3299    invalid here.
3300
3301    C++98 [lex.string] says that if a narrow string literal token is
3302    adjacent to a wide string literal token, the behavior is undefined.
3303    However, C99 6.4.5p4 says that this results in a wide string literal.
3304    We follow C99 here, for consistency with the C front end.
3305
3306    This code is largely lifted from lex_string() in c-lex.c.
3307
3308    FUTURE: ObjC++ will need to handle @-strings here.  */
3309 static tree
3310 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3311 {
3312   tree value;
3313   size_t count;
3314   struct obstack str_ob;
3315   cpp_string str, istr, *strs;
3316   cp_token *tok;
3317   enum cpp_ttype type;
3318
3319   tok = cp_lexer_peek_token (parser->lexer);
3320   if (!cp_parser_is_string_literal (tok))
3321     {
3322       cp_parser_error (parser, "expected string-literal");
3323       return error_mark_node;
3324     }
3325
3326   type = tok->type;
3327
3328   /* Try to avoid the overhead of creating and destroying an obstack
3329      for the common case of just one string.  */
3330   if (!cp_parser_is_string_literal
3331       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3332     {
3333       cp_lexer_consume_token (parser->lexer);
3334
3335       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3336       str.len = TREE_STRING_LENGTH (tok->u.value);
3337       count = 1;
3338
3339       strs = &str;
3340     }
3341   else
3342     {
3343       gcc_obstack_init (&str_ob);
3344       count = 0;
3345
3346       do
3347         {
3348           cp_lexer_consume_token (parser->lexer);
3349           count++;
3350           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3351           str.len = TREE_STRING_LENGTH (tok->u.value);
3352
3353           if (type != tok->type)
3354             {
3355               if (type == CPP_STRING)
3356                 type = tok->type;
3357               else if (tok->type != CPP_STRING)
3358                 error_at (tok->location,
3359                           "unsupported non-standard concatenation "
3360                           "of string literals");
3361             }
3362
3363           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3364
3365           tok = cp_lexer_peek_token (parser->lexer);
3366         }
3367       while (cp_parser_is_string_literal (tok));
3368
3369       strs = (cpp_string *) obstack_finish (&str_ob);
3370     }
3371
3372   if (type != CPP_STRING && !wide_ok)
3373     {
3374       cp_parser_error (parser, "a wide string is invalid in this context");
3375       type = CPP_STRING;
3376     }
3377
3378   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3379       (parse_in, strs, count, &istr, type))
3380     {
3381       value = build_string (istr.len, (const char *)istr.text);
3382       free (CONST_CAST (unsigned char *, istr.text));
3383
3384       switch (type)
3385         {
3386         default:
3387         case CPP_STRING:
3388         case CPP_UTF8STRING:
3389           TREE_TYPE (value) = char_array_type_node;
3390           break;
3391         case CPP_STRING16:
3392           TREE_TYPE (value) = char16_array_type_node;
3393           break;
3394         case CPP_STRING32:
3395           TREE_TYPE (value) = char32_array_type_node;
3396           break;
3397         case CPP_WSTRING:
3398           TREE_TYPE (value) = wchar_array_type_node;
3399           break;
3400         }
3401
3402       value = fix_string_type (value);
3403     }
3404   else
3405     /* cpp_interpret_string has issued an error.  */
3406     value = error_mark_node;
3407
3408   if (count > 1)
3409     obstack_free (&str_ob, 0);
3410
3411   return value;
3412 }
3413
3414
3415 /* Basic concepts [gram.basic]  */
3416
3417 /* Parse a translation-unit.
3418
3419    translation-unit:
3420      declaration-seq [opt]
3421
3422    Returns TRUE if all went well.  */
3423
3424 static bool
3425 cp_parser_translation_unit (cp_parser* parser)
3426 {
3427   /* The address of the first non-permanent object on the declarator
3428      obstack.  */
3429   static void *declarator_obstack_base;
3430
3431   bool success;
3432
3433   /* Create the declarator obstack, if necessary.  */
3434   if (!cp_error_declarator)
3435     {
3436       gcc_obstack_init (&declarator_obstack);
3437       /* Create the error declarator.  */
3438       cp_error_declarator = make_declarator (cdk_error);
3439       /* Create the empty parameter list.  */
3440       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3441       /* Remember where the base of the declarator obstack lies.  */
3442       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3443     }
3444
3445   cp_parser_declaration_seq_opt (parser);
3446
3447   /* If there are no tokens left then all went well.  */
3448   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3449     {
3450       /* Get rid of the token array; we don't need it any more.  */
3451       cp_lexer_destroy (parser->lexer);
3452       parser->lexer = NULL;
3453
3454       /* This file might have been a context that's implicitly extern
3455          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3456       if (parser->implicit_extern_c)
3457         {
3458           pop_lang_context ();
3459           parser->implicit_extern_c = false;
3460         }
3461
3462       /* Finish up.  */
3463       finish_translation_unit ();
3464
3465       success = true;
3466     }
3467   else
3468     {
3469       cp_parser_error (parser, "expected declaration");
3470       success = false;
3471     }
3472
3473   /* Make sure the declarator obstack was fully cleaned up.  */
3474   gcc_assert (obstack_next_free (&declarator_obstack)
3475               == declarator_obstack_base);
3476
3477   /* All went well.  */
3478   return success;
3479 }
3480
3481 /* Expressions [gram.expr] */
3482
3483 /* Parse a primary-expression.
3484
3485    primary-expression:
3486      literal
3487      this
3488      ( expression )
3489      id-expression
3490
3491    GNU Extensions:
3492
3493    primary-expression:
3494      ( compound-statement )
3495      __builtin_va_arg ( assignment-expression , type-id )
3496      __builtin_offsetof ( type-id , offsetof-expression )
3497
3498    C++ Extensions:
3499      __has_nothrow_assign ( type-id )   
3500      __has_nothrow_constructor ( type-id )
3501      __has_nothrow_copy ( type-id )
3502      __has_trivial_assign ( type-id )   
3503      __has_trivial_constructor ( type-id )
3504      __has_trivial_copy ( type-id )
3505      __has_trivial_destructor ( type-id )
3506      __has_virtual_destructor ( type-id )     
3507      __is_abstract ( type-id )
3508      __is_base_of ( type-id , type-id )
3509      __is_class ( type-id )
3510      __is_convertible_to ( type-id , type-id )     
3511      __is_empty ( type-id )
3512      __is_enum ( type-id )
3513      __is_pod ( type-id )
3514      __is_polymorphic ( type-id )
3515      __is_union ( type-id )
3516
3517    Objective-C++ Extension:
3518
3519    primary-expression:
3520      objc-expression
3521
3522    literal:
3523      __null
3524
3525    ADDRESS_P is true iff this expression was immediately preceded by
3526    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3527    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3528    true iff this expression is a template argument.
3529
3530    Returns a representation of the expression.  Upon return, *IDK
3531    indicates what kind of id-expression (if any) was present.  */
3532
3533 static tree
3534 cp_parser_primary_expression (cp_parser *parser,
3535                               bool address_p,
3536                               bool cast_p,
3537                               bool template_arg_p,
3538                               cp_id_kind *idk)
3539 {
3540   cp_token *token = NULL;
3541
3542   /* Assume the primary expression is not an id-expression.  */
3543   *idk = CP_ID_KIND_NONE;
3544
3545   /* Peek at the next token.  */
3546   token = cp_lexer_peek_token (parser->lexer);
3547   switch (token->type)
3548     {
3549       /* literal:
3550            integer-literal
3551            character-literal
3552            floating-literal
3553            string-literal
3554            boolean-literal  */
3555     case CPP_CHAR:
3556     case CPP_CHAR16:
3557     case CPP_CHAR32:
3558     case CPP_WCHAR:
3559     case CPP_NUMBER:
3560       token = cp_lexer_consume_token (parser->lexer);
3561       if (TREE_CODE (token->u.value) == FIXED_CST)
3562         {
3563           error_at (token->location,
3564                     "fixed-point types not supported in C++");
3565           return error_mark_node;
3566         }
3567       /* Floating-point literals are only allowed in an integral
3568          constant expression if they are cast to an integral or
3569          enumeration type.  */
3570       if (TREE_CODE (token->u.value) == REAL_CST
3571           && parser->integral_constant_expression_p
3572           && pedantic)
3573         {
3574           /* CAST_P will be set even in invalid code like "int(2.7 +
3575              ...)".   Therefore, we have to check that the next token
3576              is sure to end the cast.  */
3577           if (cast_p)
3578             {
3579               cp_token *next_token;
3580
3581               next_token = cp_lexer_peek_token (parser->lexer);
3582               if (/* The comma at the end of an
3583                      enumerator-definition.  */
3584                   next_token->type != CPP_COMMA
3585                   /* The curly brace at the end of an enum-specifier.  */
3586                   && next_token->type != CPP_CLOSE_BRACE
3587                   /* The end of a statement.  */
3588                   && next_token->type != CPP_SEMICOLON
3589                   /* The end of the cast-expression.  */
3590                   && next_token->type != CPP_CLOSE_PAREN
3591                   /* The end of an array bound.  */
3592                   && next_token->type != CPP_CLOSE_SQUARE
3593                   /* The closing ">" in a template-argument-list.  */
3594                   && (next_token->type != CPP_GREATER
3595                       || parser->greater_than_is_operator_p)
3596                   /* C++0x only: A ">>" treated like two ">" tokens,
3597                      in a template-argument-list.  */
3598                   && (next_token->type != CPP_RSHIFT
3599                       || (cxx_dialect == cxx98)
3600                       || parser->greater_than_is_operator_p))
3601                 cast_p = false;
3602             }
3603
3604           /* If we are within a cast, then the constraint that the
3605              cast is to an integral or enumeration type will be
3606              checked at that point.  If we are not within a cast, then
3607              this code is invalid.  */
3608           if (!cast_p)
3609             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3610         }
3611       return token->u.value;
3612
3613     case CPP_STRING:
3614     case CPP_STRING16:
3615     case CPP_STRING32:
3616     case CPP_WSTRING:
3617     case CPP_UTF8STRING:
3618       /* ??? Should wide strings be allowed when parser->translate_strings_p
3619          is false (i.e. in attributes)?  If not, we can kill the third
3620          argument to cp_parser_string_literal.  */
3621       return cp_parser_string_literal (parser,
3622                                        parser->translate_strings_p,
3623                                        true);
3624
3625     case CPP_OPEN_PAREN:
3626       {
3627         tree expr;
3628         bool saved_greater_than_is_operator_p;
3629
3630         /* Consume the `('.  */
3631         cp_lexer_consume_token (parser->lexer);
3632         /* Within a parenthesized expression, a `>' token is always
3633            the greater-than operator.  */
3634         saved_greater_than_is_operator_p
3635           = parser->greater_than_is_operator_p;
3636         parser->greater_than_is_operator_p = true;
3637         /* If we see `( { ' then we are looking at the beginning of
3638            a GNU statement-expression.  */
3639         if (cp_parser_allow_gnu_extensions_p (parser)
3640             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3641           {
3642             /* Statement-expressions are not allowed by the standard.  */
3643             pedwarn (token->location, OPT_pedantic, 
3644                      "ISO C++ forbids braced-groups within expressions");
3645
3646             /* And they're not allowed outside of a function-body; you
3647                cannot, for example, write:
3648
3649                  int i = ({ int j = 3; j + 1; });
3650
3651                at class or namespace scope.  */
3652             if (!parser->in_function_body
3653                 || parser->in_template_argument_list_p)
3654               {
3655                 error_at (token->location,
3656                           "statement-expressions are not allowed outside "
3657                           "functions nor in template-argument lists");
3658                 cp_parser_skip_to_end_of_block_or_statement (parser);
3659                 expr = error_mark_node;
3660               }
3661             else
3662               {
3663                 /* Start the statement-expression.  */
3664                 expr = begin_stmt_expr ();
3665                 /* Parse the compound-statement.  */
3666                 cp_parser_compound_statement (parser, expr, false);
3667                 /* Finish up.  */
3668                 expr = finish_stmt_expr (expr, false);
3669               }
3670           }
3671         else
3672           {
3673             /* Parse the parenthesized expression.  */
3674             expr = cp_parser_expression (parser, cast_p, idk);
3675             /* Let the front end know that this expression was
3676                enclosed in parentheses. This matters in case, for
3677                example, the expression is of the form `A::B', since
3678                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3679                not.  */
3680             finish_parenthesized_expr (expr);
3681           }
3682         /* The `>' token might be the end of a template-id or
3683            template-parameter-list now.  */
3684         parser->greater_than_is_operator_p
3685           = saved_greater_than_is_operator_p;
3686         /* Consume the `)'.  */
3687         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3688           cp_parser_skip_to_end_of_statement (parser);
3689
3690         return expr;
3691       }
3692
3693     case CPP_OPEN_SQUARE:
3694       if (c_dialect_objc ())
3695         /* We have an Objective-C++ message. */
3696         return cp_parser_objc_expression (parser);
3697       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3698       return cp_parser_lambda_expression (parser);
3699
3700     case CPP_OBJC_STRING:
3701       if (c_dialect_objc ())
3702         /* We have an Objective-C++ string literal. */
3703         return cp_parser_objc_expression (parser);
3704       cp_parser_error (parser, "expected primary-expression");
3705       return error_mark_node;
3706
3707     case CPP_KEYWORD:
3708       switch (token->keyword)
3709         {
3710           /* These two are the boolean literals.  */
3711         case RID_TRUE:
3712           cp_lexer_consume_token (parser->lexer);
3713           return boolean_true_node;
3714         case RID_FALSE:
3715           cp_lexer_consume_token (parser->lexer);
3716           return boolean_false_node;
3717
3718           /* The `__null' literal.  */
3719         case RID_NULL:
3720           cp_lexer_consume_token (parser->lexer);
3721           return null_node;
3722
3723           /* The `nullptr' literal.  */
3724         case RID_NULLPTR:
3725           cp_lexer_consume_token (parser->lexer);
3726           return nullptr_node;
3727
3728           /* Recognize the `this' keyword.  */
3729         case RID_THIS:
3730           cp_lexer_consume_token (parser->lexer);
3731           if (parser->local_variables_forbidden_p)
3732             {
3733               error_at (token->location,
3734                         "%<this%> may not be used in this context");
3735               return error_mark_node;
3736             }
3737           /* Pointers cannot appear in constant-expressions.  */
3738           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3739             return error_mark_node;
3740           return finish_this_expr ();
3741
3742           /* The `operator' keyword can be the beginning of an
3743              id-expression.  */
3744         case RID_OPERATOR:
3745           goto id_expression;
3746
3747         case RID_FUNCTION_NAME:
3748         case RID_PRETTY_FUNCTION_NAME:
3749         case RID_C99_FUNCTION_NAME:
3750           {
3751             non_integral_constant name;
3752
3753             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3754                __func__ are the names of variables -- but they are
3755                treated specially.  Therefore, they are handled here,
3756                rather than relying on the generic id-expression logic
3757                below.  Grammatically, these names are id-expressions.
3758
3759                Consume the token.  */
3760             token = cp_lexer_consume_token (parser->lexer);
3761
3762             switch (token->keyword)
3763               {
3764               case RID_FUNCTION_NAME:
3765                 name = NIC_FUNC_NAME;
3766                 break;
3767               case RID_PRETTY_FUNCTION_NAME:
3768                 name = NIC_PRETTY_FUNC;
3769                 break;
3770               case RID_C99_FUNCTION_NAME:
3771                 name = NIC_C99_FUNC;
3772                 break;
3773               default:
3774                 gcc_unreachable ();
3775               }
3776
3777             if (cp_parser_non_integral_constant_expression (parser, name))
3778               return error_mark_node;
3779
3780             /* Look up the name.  */
3781             return finish_fname (token->u.value);
3782           }
3783
3784         case RID_VA_ARG:
3785           {
3786             tree expression;
3787             tree type;
3788
3789             /* The `__builtin_va_arg' construct is used to handle
3790                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3791             cp_lexer_consume_token (parser->lexer);
3792             /* Look for the opening `('.  */
3793             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3794             /* Now, parse the assignment-expression.  */
3795             expression = cp_parser_assignment_expression (parser,
3796                                                           /*cast_p=*/false, NULL);
3797             /* Look for the `,'.  */
3798             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3799             /* Parse the type-id.  */
3800             type = cp_parser_type_id (parser);
3801             /* Look for the closing `)'.  */
3802             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3803             /* Using `va_arg' in a constant-expression is not
3804                allowed.  */
3805             if (cp_parser_non_integral_constant_expression (parser,
3806                                                             NIC_VA_ARG))
3807               return error_mark_node;
3808             return build_x_va_arg (expression, type);
3809           }
3810
3811         case RID_OFFSETOF:
3812           return cp_parser_builtin_offsetof (parser);
3813
3814         case RID_HAS_NOTHROW_ASSIGN:
3815         case RID_HAS_NOTHROW_CONSTRUCTOR:
3816         case RID_HAS_NOTHROW_COPY:        
3817         case RID_HAS_TRIVIAL_ASSIGN:
3818         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3819         case RID_HAS_TRIVIAL_COPY:        
3820         case RID_HAS_TRIVIAL_DESTRUCTOR:
3821         case RID_HAS_VIRTUAL_DESTRUCTOR:
3822         case RID_IS_ABSTRACT:
3823         case RID_IS_BASE_OF:
3824         case RID_IS_CLASS:
3825         case RID_IS_CONVERTIBLE_TO:
3826         case RID_IS_EMPTY:
3827         case RID_IS_ENUM:
3828         case RID_IS_POD:
3829         case RID_IS_POLYMORPHIC:
3830         case RID_IS_STD_LAYOUT:
3831         case RID_IS_TRIVIAL:
3832         case RID_IS_UNION:
3833         case RID_IS_LITERAL_TYPE:
3834           return cp_parser_trait_expr (parser, token->keyword);
3835
3836         /* Objective-C++ expressions.  */
3837         case RID_AT_ENCODE:
3838         case RID_AT_PROTOCOL:
3839         case RID_AT_SELECTOR:
3840           return cp_parser_objc_expression (parser);
3841
3842         case RID_TEMPLATE:
3843           if (parser->in_function_body
3844               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3845                   == CPP_LESS))
3846             {
3847               error_at (token->location,
3848                         "a template declaration cannot appear at block scope");
3849               cp_parser_skip_to_end_of_block_or_statement (parser);
3850               return error_mark_node;
3851             }
3852         default:
3853           cp_parser_error (parser, "expected primary-expression");
3854           return error_mark_node;
3855         }
3856
3857       /* An id-expression can start with either an identifier, a
3858          `::' as the beginning of a qualified-id, or the "operator"
3859          keyword.  */
3860     case CPP_NAME:
3861     case CPP_SCOPE:
3862     case CPP_TEMPLATE_ID:
3863     case CPP_NESTED_NAME_SPECIFIER:
3864       {
3865         tree id_expression;
3866         tree decl;
3867         const char *error_msg;
3868         bool template_p;
3869         bool done;
3870         cp_token *id_expr_token;
3871
3872       id_expression:
3873         /* Parse the id-expression.  */
3874         id_expression
3875           = cp_parser_id_expression (parser,
3876                                      /*template_keyword_p=*/false,
3877                                      /*check_dependency_p=*/true,
3878                                      &template_p,
3879                                      /*declarator_p=*/false,
3880                                      /*optional_p=*/false);
3881         if (id_expression == error_mark_node)
3882           return error_mark_node;
3883         id_expr_token = token;
3884         token = cp_lexer_peek_token (parser->lexer);
3885         done = (token->type != CPP_OPEN_SQUARE
3886                 && token->type != CPP_OPEN_PAREN
3887                 && token->type != CPP_DOT
3888                 && token->type != CPP_DEREF
3889                 && token->type != CPP_PLUS_PLUS
3890                 && token->type != CPP_MINUS_MINUS);
3891         /* If we have a template-id, then no further lookup is
3892            required.  If the template-id was for a template-class, we
3893            will sometimes have a TYPE_DECL at this point.  */
3894         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3895                  || TREE_CODE (id_expression) == TYPE_DECL)
3896           decl = id_expression;
3897         /* Look up the name.  */
3898         else
3899           {
3900             tree ambiguous_decls;
3901
3902             /* If we already know that this lookup is ambiguous, then
3903                we've already issued an error message; there's no reason
3904                to check again.  */
3905             if (id_expr_token->type == CPP_NAME
3906                 && id_expr_token->ambiguous_p)
3907               {
3908                 cp_parser_simulate_error (parser);
3909                 return error_mark_node;
3910               }
3911
3912             decl = cp_parser_lookup_name (parser, id_expression,
3913                                           none_type,
3914                                           template_p,
3915                                           /*is_namespace=*/false,
3916                                           /*check_dependency=*/true,
3917                                           &ambiguous_decls,
3918                                           id_expr_token->location);
3919             /* If the lookup was ambiguous, an error will already have
3920                been issued.  */
3921             if (ambiguous_decls)
3922               return error_mark_node;
3923
3924             /* In Objective-C++, we may have an Objective-C 2.0
3925                dot-syntax for classes here.  */
3926             if (c_dialect_objc ()
3927                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3928                 && TREE_CODE (decl) == TYPE_DECL
3929                 && objc_is_class_name (decl))
3930               {
3931                 tree component;
3932                 cp_lexer_consume_token (parser->lexer);
3933                 component = cp_parser_identifier (parser);
3934                 if (component == error_mark_node)
3935                   return error_mark_node;
3936
3937                 return objc_build_class_component_ref (id_expression, component);
3938               }
3939
3940             /* In Objective-C++, an instance variable (ivar) may be preferred
3941                to whatever cp_parser_lookup_name() found.  */
3942             decl = objc_lookup_ivar (decl, id_expression);
3943
3944             /* If name lookup gives us a SCOPE_REF, then the
3945                qualifying scope was dependent.  */
3946             if (TREE_CODE (decl) == SCOPE_REF)
3947               {
3948                 /* At this point, we do not know if DECL is a valid
3949                    integral constant expression.  We assume that it is
3950                    in fact such an expression, so that code like:
3951
3952                       template <int N> struct A {
3953                         int a[B<N>::i];
3954                       };
3955                      
3956                    is accepted.  At template-instantiation time, we
3957                    will check that B<N>::i is actually a constant.  */
3958                 return decl;
3959               }
3960             /* Check to see if DECL is a local variable in a context
3961                where that is forbidden.  */
3962             if (parser->local_variables_forbidden_p
3963                 && local_variable_p (decl))
3964               {
3965                 /* It might be that we only found DECL because we are
3966                    trying to be generous with pre-ISO scoping rules.
3967                    For example, consider:
3968
3969                      int i;
3970                      void g() {
3971                        for (int i = 0; i < 10; ++i) {}
3972                        extern void f(int j = i);
3973                      }
3974
3975                    Here, name look up will originally find the out
3976                    of scope `i'.  We need to issue a warning message,
3977                    but then use the global `i'.  */
3978                 decl = check_for_out_of_scope_variable (decl);
3979                 if (local_variable_p (decl))
3980                   {
3981                     error_at (id_expr_token->location,
3982                               "local variable %qD may not appear in this context",
3983                               decl);
3984                     return error_mark_node;
3985                   }
3986               }
3987           }
3988
3989         decl = (finish_id_expression
3990                 (id_expression, decl, parser->scope,
3991                  idk,
3992                  parser->integral_constant_expression_p,
3993                  parser->allow_non_integral_constant_expression_p,
3994                  &parser->non_integral_constant_expression_p,
3995                  template_p, done, address_p,
3996                  template_arg_p,
3997                  &error_msg,
3998                  id_expr_token->location));
3999         if (error_msg)
4000           cp_parser_error (parser, error_msg);
4001         return decl;
4002       }
4003
4004       /* Anything else is an error.  */
4005     default:
4006       cp_parser_error (parser, "expected primary-expression");
4007       return error_mark_node;
4008     }
4009 }
4010
4011 /* Parse an id-expression.
4012
4013    id-expression:
4014      unqualified-id
4015      qualified-id
4016
4017    qualified-id:
4018      :: [opt] nested-name-specifier template [opt] unqualified-id
4019      :: identifier
4020      :: operator-function-id
4021      :: template-id
4022
4023    Return a representation of the unqualified portion of the
4024    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4025    a `::' or nested-name-specifier.
4026
4027    Often, if the id-expression was a qualified-id, the caller will
4028    want to make a SCOPE_REF to represent the qualified-id.  This
4029    function does not do this in order to avoid wastefully creating
4030    SCOPE_REFs when they are not required.
4031
4032    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4033    `template' keyword.
4034
4035    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4036    uninstantiated templates.
4037
4038    If *TEMPLATE_P is non-NULL, it is set to true iff the
4039    `template' keyword is used to explicitly indicate that the entity
4040    named is a template.
4041
4042    If DECLARATOR_P is true, the id-expression is appearing as part of
4043    a declarator, rather than as part of an expression.  */
4044
4045 static tree
4046 cp_parser_id_expression (cp_parser *parser,
4047                          bool template_keyword_p,
4048                          bool check_dependency_p,
4049                          bool *template_p,
4050                          bool declarator_p,
4051                          bool optional_p)
4052 {
4053   bool global_scope_p;
4054   bool nested_name_specifier_p;
4055
4056   /* Assume the `template' keyword was not used.  */
4057   if (template_p)
4058     *template_p = template_keyword_p;
4059
4060   /* Look for the optional `::' operator.  */
4061   global_scope_p
4062     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4063        != NULL_TREE);
4064   /* Look for the optional nested-name-specifier.  */
4065   nested_name_specifier_p
4066     = (cp_parser_nested_name_specifier_opt (parser,
4067                                             /*typename_keyword_p=*/false,
4068                                             check_dependency_p,
4069                                             /*type_p=*/false,
4070                                             declarator_p)
4071        != NULL_TREE);
4072   /* If there is a nested-name-specifier, then we are looking at
4073      the first qualified-id production.  */
4074   if (nested_name_specifier_p)
4075     {
4076       tree saved_scope;
4077       tree saved_object_scope;
4078       tree saved_qualifying_scope;
4079       tree unqualified_id;
4080       bool is_template;
4081
4082       /* See if the next token is the `template' keyword.  */
4083       if (!template_p)
4084         template_p = &is_template;
4085       *template_p = cp_parser_optional_template_keyword (parser);
4086       /* Name lookup we do during the processing of the
4087          unqualified-id might obliterate SCOPE.  */
4088       saved_scope = parser->scope;
4089       saved_object_scope = parser->object_scope;
4090       saved_qualifying_scope = parser->qualifying_scope;
4091       /* Process the final unqualified-id.  */
4092       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4093                                                  check_dependency_p,
4094                                                  declarator_p,
4095                                                  /*optional_p=*/false);
4096       /* Restore the SAVED_SCOPE for our caller.  */
4097       parser->scope = saved_scope;
4098       parser->object_scope = saved_object_scope;
4099       parser->qualifying_scope = saved_qualifying_scope;
4100
4101       return unqualified_id;
4102     }
4103   /* Otherwise, if we are in global scope, then we are looking at one
4104      of the other qualified-id productions.  */
4105   else if (global_scope_p)
4106     {
4107       cp_token *token;
4108       tree id;
4109
4110       /* Peek at the next token.  */
4111       token = cp_lexer_peek_token (parser->lexer);
4112
4113       /* If it's an identifier, and the next token is not a "<", then
4114          we can avoid the template-id case.  This is an optimization
4115          for this common case.  */
4116       if (token->type == CPP_NAME
4117           && !cp_parser_nth_token_starts_template_argument_list_p
4118                (parser, 2))
4119         return cp_parser_identifier (parser);
4120
4121       cp_parser_parse_tentatively (parser);
4122       /* Try a template-id.  */
4123       id = cp_parser_template_id (parser,
4124                                   /*template_keyword_p=*/false,
4125                                   /*check_dependency_p=*/true,
4126                                   declarator_p);
4127       /* If that worked, we're done.  */
4128       if (cp_parser_parse_definitely (parser))
4129         return id;
4130
4131       /* Peek at the next token.  (Changes in the token buffer may
4132          have invalidated the pointer obtained above.)  */
4133       token = cp_lexer_peek_token (parser->lexer);
4134
4135       switch (token->type)
4136         {
4137         case CPP_NAME:
4138           return cp_parser_identifier (parser);
4139
4140         case CPP_KEYWORD:
4141           if (token->keyword == RID_OPERATOR)
4142             return cp_parser_operator_function_id (parser);
4143           /* Fall through.  */
4144
4145         default:
4146           cp_parser_error (parser, "expected id-expression");
4147           return error_mark_node;
4148         }
4149     }
4150   else
4151     return cp_parser_unqualified_id (parser, template_keyword_p,
4152                                      /*check_dependency_p=*/true,
4153                                      declarator_p,
4154                                      optional_p);
4155 }
4156
4157 /* Parse an unqualified-id.
4158
4159    unqualified-id:
4160      identifier
4161      operator-function-id
4162      conversion-function-id
4163      ~ class-name
4164      template-id
4165
4166    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4167    keyword, in a construct like `A::template ...'.
4168
4169    Returns a representation of unqualified-id.  For the `identifier'
4170    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4171    production a BIT_NOT_EXPR is returned; the operand of the
4172    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4173    other productions, see the documentation accompanying the
4174    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4175    names are looked up in uninstantiated templates.  If DECLARATOR_P
4176    is true, the unqualified-id is appearing as part of a declarator,
4177    rather than as part of an expression.  */
4178
4179 static tree
4180 cp_parser_unqualified_id (cp_parser* parser,
4181                           bool template_keyword_p,
4182                           bool check_dependency_p,
4183                           bool declarator_p,
4184                           bool optional_p)
4185 {
4186   cp_token *token;
4187
4188   /* Peek at the next token.  */
4189   token = cp_lexer_peek_token (parser->lexer);
4190
4191   switch (token->type)
4192     {
4193     case CPP_NAME:
4194       {
4195         tree id;
4196
4197         /* We don't know yet whether or not this will be a
4198            template-id.  */
4199         cp_parser_parse_tentatively (parser);
4200         /* Try a template-id.  */
4201         id = cp_parser_template_id (parser, template_keyword_p,
4202                                     check_dependency_p,
4203                                     declarator_p);
4204         /* If it worked, we're done.  */
4205         if (cp_parser_parse_definitely (parser))
4206           return id;
4207         /* Otherwise, it's an ordinary identifier.  */
4208         return cp_parser_identifier (parser);
4209       }
4210
4211     case CPP_TEMPLATE_ID:
4212       return cp_parser_template_id (parser, template_keyword_p,
4213                                     check_dependency_p,
4214                                     declarator_p);
4215
4216     case CPP_COMPL:
4217       {
4218         tree type_decl;
4219         tree qualifying_scope;
4220         tree object_scope;
4221         tree scope;
4222         bool done;
4223
4224         /* Consume the `~' token.  */
4225         cp_lexer_consume_token (parser->lexer);
4226         /* Parse the class-name.  The standard, as written, seems to
4227            say that:
4228
4229              template <typename T> struct S { ~S (); };
4230              template <typename T> S<T>::~S() {}
4231
4232            is invalid, since `~' must be followed by a class-name, but
4233            `S<T>' is dependent, and so not known to be a class.
4234            That's not right; we need to look in uninstantiated
4235            templates.  A further complication arises from:
4236
4237              template <typename T> void f(T t) {
4238                t.T::~T();
4239              }
4240
4241            Here, it is not possible to look up `T' in the scope of `T'
4242            itself.  We must look in both the current scope, and the
4243            scope of the containing complete expression.
4244
4245            Yet another issue is:
4246
4247              struct S {
4248                int S;
4249                ~S();
4250              };
4251
4252              S::~S() {}
4253
4254            The standard does not seem to say that the `S' in `~S'
4255            should refer to the type `S' and not the data member
4256            `S::S'.  */
4257
4258         /* DR 244 says that we look up the name after the "~" in the
4259            same scope as we looked up the qualifying name.  That idea
4260            isn't fully worked out; it's more complicated than that.  */
4261         scope = parser->scope;
4262         object_scope = parser->object_scope;
4263         qualifying_scope = parser->qualifying_scope;
4264
4265         /* Check for invalid scopes.  */
4266         if (scope == error_mark_node)
4267           {
4268             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4269               cp_lexer_consume_token (parser->lexer);
4270             return error_mark_node;
4271           }
4272         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4273           {
4274             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4275               error_at (token->location,
4276                         "scope %qT before %<~%> is not a class-name",
4277                         scope);
4278             cp_parser_simulate_error (parser);
4279             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4280               cp_lexer_consume_token (parser->lexer);
4281             return error_mark_node;
4282           }
4283         gcc_assert (!scope || TYPE_P (scope));
4284
4285         /* If the name is of the form "X::~X" it's OK even if X is a
4286            typedef.  */
4287         token = cp_lexer_peek_token (parser->lexer);
4288         if (scope
4289             && token->type == CPP_NAME
4290             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4291                 != CPP_LESS)
4292             && (token->u.value == TYPE_IDENTIFIER (scope)
4293                 || constructor_name_p (token->u.value, scope)))
4294           {
4295             cp_lexer_consume_token (parser->lexer);
4296             return build_nt (BIT_NOT_EXPR, scope);
4297           }
4298
4299         /* If there was an explicit qualification (S::~T), first look
4300            in the scope given by the qualification (i.e., S).
4301
4302            Note: in the calls to cp_parser_class_name below we pass
4303            typename_type so that lookup finds the injected-class-name
4304            rather than the constructor.  */
4305         done = false;
4306         type_decl = NULL_TREE;
4307         if (scope)
4308           {
4309             cp_parser_parse_tentatively (parser);
4310             type_decl = cp_parser_class_name (parser,
4311                                               /*typename_keyword_p=*/false,
4312                                               /*template_keyword_p=*/false,
4313                                               typename_type,
4314                                               /*check_dependency=*/false,
4315                                               /*class_head_p=*/false,
4316                                               declarator_p);
4317             if (cp_parser_parse_definitely (parser))
4318               done = true;
4319           }
4320         /* In "N::S::~S", look in "N" as well.  */
4321         if (!done && scope && qualifying_scope)
4322           {
4323             cp_parser_parse_tentatively (parser);
4324             parser->scope = qualifying_scope;
4325             parser->object_scope = NULL_TREE;
4326             parser->qualifying_scope = NULL_TREE;
4327             type_decl
4328               = cp_parser_class_name (parser,
4329                                       /*typename_keyword_p=*/false,
4330                                       /*template_keyword_p=*/false,
4331                                       typename_type,
4332                                       /*check_dependency=*/false,
4333                                       /*class_head_p=*/false,
4334                                       declarator_p);
4335             if (cp_parser_parse_definitely (parser))
4336               done = true;
4337           }
4338         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4339         else if (!done && object_scope)
4340           {
4341             cp_parser_parse_tentatively (parser);
4342             parser->scope = object_scope;
4343             parser->object_scope = NULL_TREE;
4344             parser->qualifying_scope = NULL_TREE;
4345             type_decl
4346               = cp_parser_class_name (parser,
4347                                       /*typename_keyword_p=*/false,
4348                                       /*template_keyword_p=*/false,
4349                                       typename_type,
4350                                       /*check_dependency=*/false,
4351                                       /*class_head_p=*/false,
4352                                       declarator_p);
4353             if (cp_parser_parse_definitely (parser))
4354               done = true;
4355           }
4356         /* Look in the surrounding context.  */
4357         if (!done)
4358           {
4359             parser->scope = NULL_TREE;
4360             parser->object_scope = NULL_TREE;
4361             parser->qualifying_scope = NULL_TREE;
4362             if (processing_template_decl)
4363               cp_parser_parse_tentatively (parser);
4364             type_decl
4365               = cp_parser_class_name (parser,
4366                                       /*typename_keyword_p=*/false,
4367                                       /*template_keyword_p=*/false,
4368                                       typename_type,
4369                                       /*check_dependency=*/false,
4370                                       /*class_head_p=*/false,
4371                                       declarator_p);
4372             if (processing_template_decl
4373                 && ! cp_parser_parse_definitely (parser))
4374               {
4375                 /* We couldn't find a type with this name, so just accept
4376                    it and check for a match at instantiation time.  */
4377                 type_decl = cp_parser_identifier (parser);
4378                 if (type_decl != error_mark_node)
4379                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4380                 return type_decl;
4381               }
4382           }
4383         /* If an error occurred, assume that the name of the
4384            destructor is the same as the name of the qualifying
4385            class.  That allows us to keep parsing after running
4386            into ill-formed destructor names.  */
4387         if (type_decl == error_mark_node && scope)
4388           return build_nt (BIT_NOT_EXPR, scope);
4389         else if (type_decl == error_mark_node)
4390           return error_mark_node;
4391
4392         /* Check that destructor name and scope match.  */
4393         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4394           {
4395             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4396               error_at (token->location,
4397                         "declaration of %<~%T%> as member of %qT",
4398                         type_decl, scope);
4399             cp_parser_simulate_error (parser);
4400             return error_mark_node;
4401           }
4402
4403         /* [class.dtor]
4404
4405            A typedef-name that names a class shall not be used as the
4406            identifier in the declarator for a destructor declaration.  */
4407         if (declarator_p
4408             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4409             && !DECL_SELF_REFERENCE_P (type_decl)
4410             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4411           error_at (token->location,
4412                     "typedef-name %qD used as destructor declarator",
4413                     type_decl);
4414
4415         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4416       }
4417
4418     case CPP_KEYWORD:
4419       if (token->keyword == RID_OPERATOR)
4420         {
4421           tree id;
4422
4423           /* This could be a template-id, so we try that first.  */
4424           cp_parser_parse_tentatively (parser);
4425           /* Try a template-id.  */
4426           id = cp_parser_template_id (parser, template_keyword_p,
4427                                       /*check_dependency_p=*/true,
4428                                       declarator_p);
4429           /* If that worked, we're done.  */
4430           if (cp_parser_parse_definitely (parser))
4431             return id;
4432           /* We still don't know whether we're looking at an
4433              operator-function-id or a conversion-function-id.  */
4434           cp_parser_parse_tentatively (parser);
4435           /* Try an operator-function-id.  */
4436           id = cp_parser_operator_function_id (parser);
4437           /* If that didn't work, try a conversion-function-id.  */
4438           if (!cp_parser_parse_definitely (parser))
4439             id = cp_parser_conversion_function_id (parser);
4440
4441           return id;
4442         }
4443       /* Fall through.  */
4444
4445     default:
4446       if (optional_p)
4447         return NULL_TREE;
4448       cp_parser_error (parser, "expected unqualified-id");
4449       return error_mark_node;
4450     }
4451 }
4452
4453 /* Parse an (optional) nested-name-specifier.
4454
4455    nested-name-specifier: [C++98]
4456      class-or-namespace-name :: nested-name-specifier [opt]
4457      class-or-namespace-name :: template nested-name-specifier [opt]
4458
4459    nested-name-specifier: [C++0x]
4460      type-name ::
4461      namespace-name ::
4462      nested-name-specifier identifier ::
4463      nested-name-specifier template [opt] simple-template-id ::
4464
4465    PARSER->SCOPE should be set appropriately before this function is
4466    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4467    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4468    in name lookups.
4469
4470    Sets PARSER->SCOPE to the class (TYPE) or namespace
4471    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4472    it unchanged if there is no nested-name-specifier.  Returns the new
4473    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4474
4475    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4476    part of a declaration and/or decl-specifier.  */
4477
4478 static tree
4479 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4480                                      bool typename_keyword_p,
4481                                      bool check_dependency_p,
4482                                      bool type_p,
4483                                      bool is_declaration)
4484 {
4485   bool success = false;
4486   cp_token_position start = 0;
4487   cp_token *token;
4488
4489   /* Remember where the nested-name-specifier starts.  */
4490   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4491     {
4492       start = cp_lexer_token_position (parser->lexer, false);
4493       push_deferring_access_checks (dk_deferred);
4494     }
4495
4496   while (true)
4497     {
4498       tree new_scope;
4499       tree old_scope;
4500       tree saved_qualifying_scope;
4501       bool template_keyword_p;
4502
4503       /* Spot cases that cannot be the beginning of a
4504          nested-name-specifier.  */
4505       token = cp_lexer_peek_token (parser->lexer);
4506
4507       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4508          the already parsed nested-name-specifier.  */
4509       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4510         {
4511           /* Grab the nested-name-specifier and continue the loop.  */
4512           cp_parser_pre_parsed_nested_name_specifier (parser);
4513           /* If we originally encountered this nested-name-specifier
4514              with IS_DECLARATION set to false, we will not have
4515              resolved TYPENAME_TYPEs, so we must do so here.  */
4516           if (is_declaration
4517               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4518             {
4519               new_scope = resolve_typename_type (parser->scope,
4520                                                  /*only_current_p=*/false);
4521               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4522                 parser->scope = new_scope;
4523             }
4524           success = true;
4525           continue;
4526         }
4527
4528       /* Spot cases that cannot be the beginning of a
4529          nested-name-specifier.  On the second and subsequent times
4530          through the loop, we look for the `template' keyword.  */
4531       if (success && token->keyword == RID_TEMPLATE)
4532         ;
4533       /* A template-id can start a nested-name-specifier.  */
4534       else if (token->type == CPP_TEMPLATE_ID)
4535         ;
4536       else
4537         {
4538           /* If the next token is not an identifier, then it is
4539              definitely not a type-name or namespace-name.  */
4540           if (token->type != CPP_NAME)
4541             break;
4542           /* If the following token is neither a `<' (to begin a
4543              template-id), nor a `::', then we are not looking at a
4544              nested-name-specifier.  */
4545           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4546           if (token->type != CPP_SCOPE
4547               && !cp_parser_nth_token_starts_template_argument_list_p
4548                   (parser, 2))
4549             break;
4550         }
4551
4552       /* The nested-name-specifier is optional, so we parse
4553          tentatively.  */
4554       cp_parser_parse_tentatively (parser);
4555
4556       /* Look for the optional `template' keyword, if this isn't the
4557          first time through the loop.  */
4558       if (success)
4559         template_keyword_p = cp_parser_optional_template_keyword (parser);
4560       else
4561         template_keyword_p = false;
4562
4563       /* Save the old scope since the name lookup we are about to do
4564          might destroy it.  */
4565       old_scope = parser->scope;
4566       saved_qualifying_scope = parser->qualifying_scope;
4567       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4568          look up names in "X<T>::I" in order to determine that "Y" is
4569          a template.  So, if we have a typename at this point, we make
4570          an effort to look through it.  */
4571       if (is_declaration
4572           && !typename_keyword_p
4573           && parser->scope
4574           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4575         parser->scope = resolve_typename_type (parser->scope,
4576                                                /*only_current_p=*/false);
4577       /* Parse the qualifying entity.  */
4578       new_scope
4579         = cp_parser_qualifying_entity (parser,
4580                                        typename_keyword_p,
4581                                        template_keyword_p,
4582                                        check_dependency_p,
4583                                        type_p,
4584                                        is_declaration);
4585       /* Look for the `::' token.  */
4586       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4587
4588       /* If we found what we wanted, we keep going; otherwise, we're
4589          done.  */
4590       if (!cp_parser_parse_definitely (parser))
4591         {
4592           bool error_p = false;
4593
4594           /* Restore the OLD_SCOPE since it was valid before the
4595              failed attempt at finding the last
4596              class-or-namespace-name.  */
4597           parser->scope = old_scope;
4598           parser->qualifying_scope = saved_qualifying_scope;
4599           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4600             break;
4601           /* If the next token is an identifier, and the one after
4602              that is a `::', then any valid interpretation would have
4603              found a class-or-namespace-name.  */
4604           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4605                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4606                      == CPP_SCOPE)
4607                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4608                      != CPP_COMPL))
4609             {
4610               token = cp_lexer_consume_token (parser->lexer);
4611               if (!error_p)
4612                 {
4613                   if (!token->ambiguous_p)
4614                     {
4615                       tree decl;
4616                       tree ambiguous_decls;
4617
4618                       decl = cp_parser_lookup_name (parser, token->u.value,
4619                                                     none_type,
4620                                                     /*is_template=*/false,
4621                                                     /*is_namespace=*/false,
4622                                                     /*check_dependency=*/true,
4623                                                     &ambiguous_decls,
4624                                                     token->location);
4625                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4626                         error_at (token->location,
4627                                   "%qD used without template parameters",
4628                                   decl);
4629                       else if (ambiguous_decls)
4630                         {
4631                           error_at (token->location,
4632                                     "reference to %qD is ambiguous",
4633                                     token->u.value);
4634                           print_candidates (ambiguous_decls);
4635                           decl = error_mark_node;
4636                         }
4637                       else
4638                         {
4639                           if (cxx_dialect != cxx98)
4640                             cp_parser_name_lookup_error
4641                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4642                              token->location);
4643                           else
4644                             cp_parser_name_lookup_error
4645                             (parser, token->u.value, decl, NLE_CXX98,
4646                              token->location);
4647                         }
4648                     }
4649                   parser->scope = error_mark_node;
4650                   error_p = true;
4651                   /* Treat this as a successful nested-name-specifier
4652                      due to:
4653
4654                      [basic.lookup.qual]
4655
4656                      If the name found is not a class-name (clause
4657                      _class_) or namespace-name (_namespace.def_), the
4658                      program is ill-formed.  */
4659                   success = true;
4660                 }
4661               cp_lexer_consume_token (parser->lexer);
4662             }
4663           break;
4664         }
4665       /* We've found one valid nested-name-specifier.  */
4666       success = true;
4667       /* Name lookup always gives us a DECL.  */
4668       if (TREE_CODE (new_scope) == TYPE_DECL)
4669         new_scope = TREE_TYPE (new_scope);
4670       /* Uses of "template" must be followed by actual templates.  */
4671       if (template_keyword_p
4672           && !(CLASS_TYPE_P (new_scope)
4673                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4674                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4675                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4676           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4677                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4678                    == TEMPLATE_ID_EXPR)))
4679         permerror (input_location, TYPE_P (new_scope)
4680                    ? "%qT is not a template"
4681                    : "%qD is not a template",
4682                    new_scope);
4683       /* If it is a class scope, try to complete it; we are about to
4684          be looking up names inside the class.  */
4685       if (TYPE_P (new_scope)
4686           /* Since checking types for dependency can be expensive,
4687              avoid doing it if the type is already complete.  */
4688           && !COMPLETE_TYPE_P (new_scope)
4689           /* Do not try to complete dependent types.  */
4690           && !dependent_type_p (new_scope))
4691         {
4692           new_scope = complete_type (new_scope);
4693           /* If it is a typedef to current class, use the current
4694              class instead, as the typedef won't have any names inside
4695              it yet.  */
4696           if (!COMPLETE_TYPE_P (new_scope)
4697               && currently_open_class (new_scope))
4698             new_scope = TYPE_MAIN_VARIANT (new_scope);
4699         }
4700       /* Make sure we look in the right scope the next time through
4701          the loop.  */
4702       parser->scope = new_scope;
4703     }
4704
4705   /* If parsing tentatively, replace the sequence of tokens that makes
4706      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4707      token.  That way, should we re-parse the token stream, we will
4708      not have to repeat the effort required to do the parse, nor will
4709      we issue duplicate error messages.  */
4710   if (success && start)
4711     {
4712       cp_token *token;
4713
4714       token = cp_lexer_token_at (parser->lexer, start);
4715       /* Reset the contents of the START token.  */
4716       token->type = CPP_NESTED_NAME_SPECIFIER;
4717       /* Retrieve any deferred checks.  Do not pop this access checks yet
4718          so the memory will not be reclaimed during token replacing below.  */
4719       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4720       token->u.tree_check_value->value = parser->scope;
4721       token->u.tree_check_value->checks = get_deferred_access_checks ();
4722       token->u.tree_check_value->qualifying_scope =
4723         parser->qualifying_scope;
4724       token->keyword = RID_MAX;
4725
4726       /* Purge all subsequent tokens.  */
4727       cp_lexer_purge_tokens_after (parser->lexer, start);
4728     }
4729
4730   if (start)
4731     pop_to_parent_deferring_access_checks ();
4732
4733   return success ? parser->scope : NULL_TREE;
4734 }
4735
4736 /* Parse a nested-name-specifier.  See
4737    cp_parser_nested_name_specifier_opt for details.  This function
4738    behaves identically, except that it will an issue an error if no
4739    nested-name-specifier is present.  */
4740
4741 static tree
4742 cp_parser_nested_name_specifier (cp_parser *parser,
4743                                  bool typename_keyword_p,
4744                                  bool check_dependency_p,
4745                                  bool type_p,
4746                                  bool is_declaration)
4747 {
4748   tree scope;
4749
4750   /* Look for the nested-name-specifier.  */
4751   scope = cp_parser_nested_name_specifier_opt (parser,
4752                                                typename_keyword_p,
4753                                                check_dependency_p,
4754                                                type_p,
4755                                                is_declaration);
4756   /* If it was not present, issue an error message.  */
4757   if (!scope)
4758     {
4759       cp_parser_error (parser, "expected nested-name-specifier");
4760       parser->scope = NULL_TREE;
4761     }
4762
4763   return scope;
4764 }
4765
4766 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4767    this is either a class-name or a namespace-name (which corresponds
4768    to the class-or-namespace-name production in the grammar). For
4769    C++0x, it can also be a type-name that refers to an enumeration
4770    type.
4771
4772    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4773    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4774    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4775    TYPE_P is TRUE iff the next name should be taken as a class-name,
4776    even the same name is declared to be another entity in the same
4777    scope.
4778
4779    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4780    specified by the class-or-namespace-name.  If neither is found the
4781    ERROR_MARK_NODE is returned.  */
4782
4783 static tree
4784 cp_parser_qualifying_entity (cp_parser *parser,
4785                              bool typename_keyword_p,
4786                              bool template_keyword_p,
4787                              bool check_dependency_p,
4788                              bool type_p,
4789                              bool is_declaration)
4790 {
4791   tree saved_scope;
4792   tree saved_qualifying_scope;
4793   tree saved_object_scope;
4794   tree scope;
4795   bool only_class_p;
4796   bool successful_parse_p;
4797
4798   /* Before we try to parse the class-name, we must save away the
4799      current PARSER->SCOPE since cp_parser_class_name will destroy
4800      it.  */
4801   saved_scope = parser->scope;
4802   saved_qualifying_scope = parser->qualifying_scope;
4803   saved_object_scope = parser->object_scope;
4804   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4805      there is no need to look for a namespace-name.  */
4806   only_class_p = template_keyword_p 
4807     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4808   if (!only_class_p)
4809     cp_parser_parse_tentatively (parser);
4810   scope = cp_parser_class_name (parser,
4811                                 typename_keyword_p,
4812                                 template_keyword_p,
4813                                 type_p ? class_type : none_type,
4814                                 check_dependency_p,
4815                                 /*class_head_p=*/false,
4816                                 is_declaration);
4817   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4818   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4819   if (!only_class_p 
4820       && cxx_dialect != cxx98
4821       && !successful_parse_p)
4822     {
4823       /* Restore the saved scope.  */
4824       parser->scope = saved_scope;
4825       parser->qualifying_scope = saved_qualifying_scope;
4826       parser->object_scope = saved_object_scope;
4827
4828       /* Parse tentatively.  */
4829       cp_parser_parse_tentatively (parser);
4830      
4831       /* Parse a typedef-name or enum-name.  */
4832       scope = cp_parser_nonclass_name (parser);
4833
4834       /* "If the name found does not designate a namespace or a class,
4835          enumeration, or dependent type, the program is ill-formed."
4836
4837          We cover classes and dependent types above and namespaces below,
4838          so this code is only looking for enums.  */
4839       if (!scope || TREE_CODE (scope) != TYPE_DECL
4840           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4841         cp_parser_simulate_error (parser);
4842
4843       successful_parse_p = cp_parser_parse_definitely (parser);
4844     }
4845   /* If that didn't work, try for a namespace-name.  */
4846   if (!only_class_p && !successful_parse_p)
4847     {
4848       /* Restore the saved scope.  */
4849       parser->scope = saved_scope;
4850       parser->qualifying_scope = saved_qualifying_scope;
4851       parser->object_scope = saved_object_scope;
4852       /* If we are not looking at an identifier followed by the scope
4853          resolution operator, then this is not part of a
4854          nested-name-specifier.  (Note that this function is only used
4855          to parse the components of a nested-name-specifier.)  */
4856       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4857           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4858         return error_mark_node;
4859       scope = cp_parser_namespace_name (parser);
4860     }
4861
4862   return scope;
4863 }
4864
4865 /* Parse a postfix-expression.
4866
4867    postfix-expression:
4868      primary-expression
4869      postfix-expression [ expression ]
4870      postfix-expression ( expression-list [opt] )
4871      simple-type-specifier ( expression-list [opt] )
4872      typename :: [opt] nested-name-specifier identifier
4873        ( expression-list [opt] )
4874      typename :: [opt] nested-name-specifier template [opt] template-id
4875        ( expression-list [opt] )
4876      postfix-expression . template [opt] id-expression
4877      postfix-expression -> template [opt] id-expression
4878      postfix-expression . pseudo-destructor-name
4879      postfix-expression -> pseudo-destructor-name
4880      postfix-expression ++
4881      postfix-expression --
4882      dynamic_cast < type-id > ( expression )
4883      static_cast < type-id > ( expression )
4884      reinterpret_cast < type-id > ( expression )
4885      const_cast < type-id > ( expression )
4886      typeid ( expression )
4887      typeid ( type-id )
4888
4889    GNU Extension:
4890
4891    postfix-expression:
4892      ( type-id ) { initializer-list , [opt] }
4893
4894    This extension is a GNU version of the C99 compound-literal
4895    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4896    but they are essentially the same concept.)
4897
4898    If ADDRESS_P is true, the postfix expression is the operand of the
4899    `&' operator.  CAST_P is true if this expression is the target of a
4900    cast.
4901
4902    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4903    class member access expressions [expr.ref].
4904
4905    Returns a representation of the expression.  */
4906
4907 static tree
4908 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4909                               bool member_access_only_p,
4910                               cp_id_kind * pidk_return)
4911 {
4912   cp_token *token;
4913   enum rid keyword;
4914   cp_id_kind idk = CP_ID_KIND_NONE;
4915   tree postfix_expression = NULL_TREE;
4916   bool is_member_access = false;
4917
4918   /* Peek at the next token.  */
4919   token = cp_lexer_peek_token (parser->lexer);
4920   /* Some of the productions are determined by keywords.  */
4921   keyword = token->keyword;
4922   switch (keyword)
4923     {
4924     case RID_DYNCAST:
4925     case RID_STATCAST:
4926     case RID_REINTCAST:
4927     case RID_CONSTCAST:
4928       {
4929         tree type;
4930         tree expression;
4931         const char *saved_message;
4932
4933         /* All of these can be handled in the same way from the point
4934            of view of parsing.  Begin by consuming the token
4935            identifying the cast.  */
4936         cp_lexer_consume_token (parser->lexer);
4937
4938         /* New types cannot be defined in the cast.  */
4939         saved_message = parser->type_definition_forbidden_message;
4940         parser->type_definition_forbidden_message
4941           = G_("types may not be defined in casts");
4942
4943         /* Look for the opening `<'.  */
4944         cp_parser_require (parser, CPP_LESS, RT_LESS);
4945         /* Parse the type to which we are casting.  */
4946         type = cp_parser_type_id (parser);
4947         /* Look for the closing `>'.  */
4948         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4949         /* Restore the old message.  */
4950         parser->type_definition_forbidden_message = saved_message;
4951
4952         /* And the expression which is being cast.  */
4953         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4955         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4956
4957         /* Only type conversions to integral or enumeration types
4958            can be used in constant-expressions.  */
4959         if (!cast_valid_in_integral_constant_expression_p (type)
4960             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4961           return error_mark_node;
4962
4963         switch (keyword)
4964           {
4965           case RID_DYNCAST:
4966             postfix_expression
4967               = build_dynamic_cast (type, expression, tf_warning_or_error);
4968             break;
4969           case RID_STATCAST:
4970             postfix_expression
4971               = build_static_cast (type, expression, tf_warning_or_error);
4972             break;
4973           case RID_REINTCAST:
4974             postfix_expression
4975               = build_reinterpret_cast (type, expression, 
4976                                         tf_warning_or_error);
4977             break;
4978           case RID_CONSTCAST:
4979             postfix_expression
4980               = build_const_cast (type, expression, tf_warning_or_error);
4981             break;
4982           default:
4983             gcc_unreachable ();
4984           }
4985       }
4986       break;
4987
4988     case RID_TYPEID:
4989       {
4990         tree type;
4991         const char *saved_message;
4992         bool saved_in_type_id_in_expr_p;
4993
4994         /* Consume the `typeid' token.  */
4995         cp_lexer_consume_token (parser->lexer);
4996         /* Look for the `(' token.  */
4997         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4998         /* Types cannot be defined in a `typeid' expression.  */
4999         saved_message = parser->type_definition_forbidden_message;
5000         parser->type_definition_forbidden_message
5001           = G_("types may not be defined in a %<typeid%> expression");
5002         /* We can't be sure yet whether we're looking at a type-id or an
5003            expression.  */
5004         cp_parser_parse_tentatively (parser);
5005         /* Try a type-id first.  */
5006         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5007         parser->in_type_id_in_expr_p = true;
5008         type = cp_parser_type_id (parser);
5009         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5010         /* Look for the `)' token.  Otherwise, we can't be sure that
5011            we're not looking at an expression: consider `typeid (int
5012            (3))', for example.  */
5013         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5014         /* If all went well, simply lookup the type-id.  */
5015         if (cp_parser_parse_definitely (parser))
5016           postfix_expression = get_typeid (type);
5017         /* Otherwise, fall back to the expression variant.  */
5018         else
5019           {
5020             tree expression;
5021
5022             /* Look for an expression.  */
5023             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5024             /* Compute its typeid.  */
5025             postfix_expression = build_typeid (expression);
5026             /* Look for the `)' token.  */
5027             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5028           }
5029         /* Restore the saved message.  */
5030         parser->type_definition_forbidden_message = saved_message;
5031         /* `typeid' may not appear in an integral constant expression.  */
5032         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5033           return error_mark_node;
5034       }
5035       break;
5036
5037     case RID_TYPENAME:
5038       {
5039         tree type;
5040         /* The syntax permitted here is the same permitted for an
5041            elaborated-type-specifier.  */
5042         type = cp_parser_elaborated_type_specifier (parser,
5043                                                     /*is_friend=*/false,
5044                                                     /*is_declaration=*/false);
5045         postfix_expression = cp_parser_functional_cast (parser, type);
5046       }
5047       break;
5048
5049     default:
5050       {
5051         tree type;
5052
5053         /* If the next thing is a simple-type-specifier, we may be
5054            looking at a functional cast.  We could also be looking at
5055            an id-expression.  So, we try the functional cast, and if
5056            that doesn't work we fall back to the primary-expression.  */
5057         cp_parser_parse_tentatively (parser);
5058         /* Look for the simple-type-specifier.  */
5059         type = cp_parser_simple_type_specifier (parser,
5060                                                 /*decl_specs=*/NULL,
5061                                                 CP_PARSER_FLAGS_NONE);
5062         /* Parse the cast itself.  */
5063         if (!cp_parser_error_occurred (parser))
5064           postfix_expression
5065             = cp_parser_functional_cast (parser, type);
5066         /* If that worked, we're done.  */
5067         if (cp_parser_parse_definitely (parser))
5068           break;
5069
5070         /* If the functional-cast didn't work out, try a
5071            compound-literal.  */
5072         if (cp_parser_allow_gnu_extensions_p (parser)
5073             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5074           {
5075             VEC(constructor_elt,gc) *initializer_list = NULL;
5076             bool saved_in_type_id_in_expr_p;
5077
5078             cp_parser_parse_tentatively (parser);
5079             /* Consume the `('.  */
5080             cp_lexer_consume_token (parser->lexer);
5081             /* Parse the type.  */
5082             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5083             parser->in_type_id_in_expr_p = true;
5084             type = cp_parser_type_id (parser);
5085             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5086             /* Look for the `)'.  */
5087             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5088             /* Look for the `{'.  */
5089             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5090             /* If things aren't going well, there's no need to
5091                keep going.  */
5092             if (!cp_parser_error_occurred (parser))
5093               {
5094                 bool non_constant_p;
5095                 /* Parse the initializer-list.  */
5096                 initializer_list
5097                   = cp_parser_initializer_list (parser, &non_constant_p);
5098                 /* Allow a trailing `,'.  */
5099                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5100                   cp_lexer_consume_token (parser->lexer);
5101                 /* Look for the final `}'.  */
5102                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5103               }
5104             /* If that worked, we're definitely looking at a
5105                compound-literal expression.  */
5106             if (cp_parser_parse_definitely (parser))
5107               {
5108                 /* Warn the user that a compound literal is not
5109                    allowed in standard C++.  */
5110                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5111                 /* For simplicity, we disallow compound literals in
5112                    constant-expressions.  We could
5113                    allow compound literals of integer type, whose
5114                    initializer was a constant, in constant
5115                    expressions.  Permitting that usage, as a further
5116                    extension, would not change the meaning of any
5117                    currently accepted programs.  (Of course, as
5118                    compound literals are not part of ISO C++, the
5119                    standard has nothing to say.)  */
5120                 if (cp_parser_non_integral_constant_expression (parser,
5121                                                                 NIC_NCC))
5122                   {
5123                     postfix_expression = error_mark_node;
5124                     break;
5125                   }
5126                 /* Form the representation of the compound-literal.  */
5127                 postfix_expression
5128                   = (finish_compound_literal
5129                      (type, build_constructor (init_list_type_node,
5130                                                initializer_list)));
5131                 break;
5132               }
5133           }
5134
5135         /* It must be a primary-expression.  */
5136         postfix_expression
5137           = cp_parser_primary_expression (parser, address_p, cast_p,
5138                                           /*template_arg_p=*/false,
5139                                           &idk);
5140       }
5141       break;
5142     }
5143
5144   /* Keep looping until the postfix-expression is complete.  */
5145   while (true)
5146     {
5147       if (idk == CP_ID_KIND_UNQUALIFIED
5148           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5149           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5150         /* It is not a Koenig lookup function call.  */
5151         postfix_expression
5152           = unqualified_name_lookup_error (postfix_expression);
5153
5154       /* Peek at the next token.  */
5155       token = cp_lexer_peek_token (parser->lexer);
5156
5157       switch (token->type)
5158         {
5159         case CPP_OPEN_SQUARE:
5160           postfix_expression
5161             = cp_parser_postfix_open_square_expression (parser,
5162                                                         postfix_expression,
5163                                                         false);
5164           idk = CP_ID_KIND_NONE;
5165           is_member_access = false;
5166           break;
5167
5168         case CPP_OPEN_PAREN:
5169           /* postfix-expression ( expression-list [opt] ) */
5170           {
5171             bool koenig_p;
5172             bool is_builtin_constant_p;
5173             bool saved_integral_constant_expression_p = false;
5174             bool saved_non_integral_constant_expression_p = false;
5175             VEC(tree,gc) *args;
5176
5177             is_member_access = false;
5178
5179             is_builtin_constant_p
5180               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5181             if (is_builtin_constant_p)
5182               {
5183                 /* The whole point of __builtin_constant_p is to allow
5184                    non-constant expressions to appear as arguments.  */
5185                 saved_integral_constant_expression_p
5186                   = parser->integral_constant_expression_p;
5187                 saved_non_integral_constant_expression_p
5188                   = parser->non_integral_constant_expression_p;
5189                 parser->integral_constant_expression_p = false;
5190               }
5191             args = (cp_parser_parenthesized_expression_list
5192                     (parser, non_attr,
5193                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5194                      /*non_constant_p=*/NULL));
5195             if (is_builtin_constant_p)
5196               {
5197                 parser->integral_constant_expression_p
5198                   = saved_integral_constant_expression_p;
5199                 parser->non_integral_constant_expression_p
5200                   = saved_non_integral_constant_expression_p;
5201               }
5202
5203             if (args == NULL)
5204               {
5205                 postfix_expression = error_mark_node;
5206                 break;
5207               }
5208
5209             /* Function calls are not permitted in
5210                constant-expressions.  */
5211             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5212                 && cp_parser_non_integral_constant_expression (parser,
5213                                                                NIC_FUNC_CALL))
5214               {
5215                 postfix_expression = error_mark_node;
5216                 release_tree_vector (args);
5217                 break;
5218               }
5219
5220             koenig_p = false;
5221             if (idk == CP_ID_KIND_UNQUALIFIED
5222                 || idk == CP_ID_KIND_TEMPLATE_ID)
5223               {
5224                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5225                   {
5226                     if (!VEC_empty (tree, args))
5227                       {
5228                         koenig_p = true;
5229                         if (!any_type_dependent_arguments_p (args))
5230                           postfix_expression
5231                             = perform_koenig_lookup (postfix_expression, args,
5232                                                      /*include_std=*/false);
5233                       }
5234                     else
5235                       postfix_expression
5236                         = unqualified_fn_lookup_error (postfix_expression);
5237                   }
5238                 /* We do not perform argument-dependent lookup if
5239                    normal lookup finds a non-function, in accordance
5240                    with the expected resolution of DR 218.  */
5241                 else if (!VEC_empty (tree, args)
5242                          && is_overloaded_fn (postfix_expression))
5243                   {
5244                     tree fn = get_first_fn (postfix_expression);
5245                     fn = STRIP_TEMPLATE (fn);
5246
5247                     /* Do not do argument dependent lookup if regular
5248                        lookup finds a member function or a block-scope
5249                        function declaration.  [basic.lookup.argdep]/3  */
5250                     if (!DECL_FUNCTION_MEMBER_P (fn)
5251                         && !DECL_LOCAL_FUNCTION_P (fn))
5252                       {
5253                         koenig_p = true;
5254                         if (!any_type_dependent_arguments_p (args))
5255                           postfix_expression
5256                             = perform_koenig_lookup (postfix_expression, args,
5257                                                      /*include_std=*/false);
5258                       }
5259                   }
5260               }
5261
5262             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5263               {
5264                 tree instance = TREE_OPERAND (postfix_expression, 0);
5265                 tree fn = TREE_OPERAND (postfix_expression, 1);
5266
5267                 if (processing_template_decl
5268                     && (type_dependent_expression_p (instance)
5269                         || (!BASELINK_P (fn)
5270                             && TREE_CODE (fn) != FIELD_DECL)
5271                         || type_dependent_expression_p (fn)
5272                         || any_type_dependent_arguments_p (args)))
5273                   {
5274                     postfix_expression
5275                       = build_nt_call_vec (postfix_expression, args);
5276                     release_tree_vector (args);
5277                     break;
5278                   }
5279
5280                 if (BASELINK_P (fn))
5281                   {
5282                   postfix_expression
5283                     = (build_new_method_call
5284                        (instance, fn, &args, NULL_TREE,
5285                         (idk == CP_ID_KIND_QUALIFIED
5286                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5287                         /*fn_p=*/NULL,
5288                         tf_warning_or_error));
5289                   }
5290                 else
5291                   postfix_expression
5292                     = finish_call_expr (postfix_expression, &args,
5293                                         /*disallow_virtual=*/false,
5294                                         /*koenig_p=*/false,
5295                                         tf_warning_or_error);
5296               }
5297             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5298                      || TREE_CODE (postfix_expression) == MEMBER_REF
5299                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5300               postfix_expression = (build_offset_ref_call_from_tree
5301                                     (postfix_expression, &args));
5302             else if (idk == CP_ID_KIND_QUALIFIED)
5303               /* A call to a static class member, or a namespace-scope
5304                  function.  */
5305               postfix_expression
5306                 = finish_call_expr (postfix_expression, &args,
5307                                     /*disallow_virtual=*/true,
5308                                     koenig_p,
5309                                     tf_warning_or_error);
5310             else
5311               /* All other function calls.  */
5312               postfix_expression
5313                 = finish_call_expr (postfix_expression, &args,
5314                                     /*disallow_virtual=*/false,
5315                                     koenig_p,
5316                                     tf_warning_or_error);
5317
5318             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5319             idk = CP_ID_KIND_NONE;
5320
5321             release_tree_vector (args);
5322           }
5323           break;
5324
5325         case CPP_DOT:
5326         case CPP_DEREF:
5327           /* postfix-expression . template [opt] id-expression
5328              postfix-expression . pseudo-destructor-name
5329              postfix-expression -> template [opt] id-expression
5330              postfix-expression -> pseudo-destructor-name */
5331
5332           /* Consume the `.' or `->' operator.  */
5333           cp_lexer_consume_token (parser->lexer);
5334
5335           postfix_expression
5336             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5337                                                       postfix_expression,
5338                                                       false, &idk,
5339                                                       token->location);
5340
5341           is_member_access = true;
5342           break;
5343
5344         case CPP_PLUS_PLUS:
5345           /* postfix-expression ++  */
5346           /* Consume the `++' token.  */
5347           cp_lexer_consume_token (parser->lexer);
5348           /* Generate a representation for the complete expression.  */
5349           postfix_expression
5350             = finish_increment_expr (postfix_expression,
5351                                      POSTINCREMENT_EXPR);
5352           /* Increments may not appear in constant-expressions.  */
5353           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5354             postfix_expression = error_mark_node;
5355           idk = CP_ID_KIND_NONE;
5356           is_member_access = false;
5357           break;
5358
5359         case CPP_MINUS_MINUS:
5360           /* postfix-expression -- */
5361           /* Consume the `--' token.  */
5362           cp_lexer_consume_token (parser->lexer);
5363           /* Generate a representation for the complete expression.  */
5364           postfix_expression
5365             = finish_increment_expr (postfix_expression,
5366                                      POSTDECREMENT_EXPR);
5367           /* Decrements may not appear in constant-expressions.  */
5368           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5369             postfix_expression = error_mark_node;
5370           idk = CP_ID_KIND_NONE;
5371           is_member_access = false;
5372           break;
5373
5374         default:
5375           if (pidk_return != NULL)
5376             * pidk_return = idk;
5377           if (member_access_only_p)
5378             return is_member_access? postfix_expression : error_mark_node;
5379           else
5380             return postfix_expression;
5381         }
5382     }
5383
5384   /* We should never get here.  */
5385   gcc_unreachable ();
5386   return error_mark_node;
5387 }
5388
5389 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5390    by cp_parser_builtin_offsetof.  We're looking for
5391
5392      postfix-expression [ expression ]
5393
5394    FOR_OFFSETOF is set if we're being called in that context, which
5395    changes how we deal with integer constant expressions.  */
5396
5397 static tree
5398 cp_parser_postfix_open_square_expression (cp_parser *parser,
5399                                           tree postfix_expression,
5400                                           bool for_offsetof)
5401 {
5402   tree index;
5403
5404   /* Consume the `[' token.  */
5405   cp_lexer_consume_token (parser->lexer);
5406
5407   /* Parse the index expression.  */
5408   /* ??? For offsetof, there is a question of what to allow here.  If
5409      offsetof is not being used in an integral constant expression context,
5410      then we *could* get the right answer by computing the value at runtime.
5411      If we are in an integral constant expression context, then we might
5412      could accept any constant expression; hard to say without analysis.
5413      Rather than open the barn door too wide right away, allow only integer
5414      constant expressions here.  */
5415   if (for_offsetof)
5416     index = cp_parser_constant_expression (parser, false, NULL);
5417   else
5418     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5419
5420   /* Look for the closing `]'.  */
5421   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5422
5423   /* Build the ARRAY_REF.  */
5424   postfix_expression = grok_array_decl (postfix_expression, index);
5425
5426   /* When not doing offsetof, array references are not permitted in
5427      constant-expressions.  */
5428   if (!for_offsetof
5429       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5430     postfix_expression = error_mark_node;
5431
5432   return postfix_expression;
5433 }
5434
5435 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5436    by cp_parser_builtin_offsetof.  We're looking for
5437
5438      postfix-expression . template [opt] id-expression
5439      postfix-expression . pseudo-destructor-name
5440      postfix-expression -> template [opt] id-expression
5441      postfix-expression -> pseudo-destructor-name
5442
5443    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5444    limits what of the above we'll actually accept, but nevermind.
5445    TOKEN_TYPE is the "." or "->" token, which will already have been
5446    removed from the stream.  */
5447
5448 static tree
5449 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5450                                         enum cpp_ttype token_type,
5451                                         tree postfix_expression,
5452                                         bool for_offsetof, cp_id_kind *idk,
5453                                         location_t location)
5454 {
5455   tree name;
5456   bool dependent_p;
5457   bool pseudo_destructor_p;
5458   tree scope = NULL_TREE;
5459
5460   /* If this is a `->' operator, dereference the pointer.  */
5461   if (token_type == CPP_DEREF)
5462     postfix_expression = build_x_arrow (postfix_expression);
5463   /* Check to see whether or not the expression is type-dependent.  */
5464   dependent_p = type_dependent_expression_p (postfix_expression);
5465   /* The identifier following the `->' or `.' is not qualified.  */
5466   parser->scope = NULL_TREE;
5467   parser->qualifying_scope = NULL_TREE;
5468   parser->object_scope = NULL_TREE;
5469   *idk = CP_ID_KIND_NONE;
5470
5471   /* Enter the scope corresponding to the type of the object
5472      given by the POSTFIX_EXPRESSION.  */
5473   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5474     {
5475       scope = TREE_TYPE (postfix_expression);
5476       /* According to the standard, no expression should ever have
5477          reference type.  Unfortunately, we do not currently match
5478          the standard in this respect in that our internal representation
5479          of an expression may have reference type even when the standard
5480          says it does not.  Therefore, we have to manually obtain the
5481          underlying type here.  */
5482       scope = non_reference (scope);
5483       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5484       if (scope == unknown_type_node)
5485         {
5486           error_at (location, "%qE does not have class type",
5487                     postfix_expression);
5488           scope = NULL_TREE;
5489         }
5490       else
5491         scope = complete_type_or_else (scope, NULL_TREE);
5492       /* Let the name lookup machinery know that we are processing a
5493          class member access expression.  */
5494       parser->context->object_type = scope;
5495       /* If something went wrong, we want to be able to discern that case,
5496          as opposed to the case where there was no SCOPE due to the type
5497          of expression being dependent.  */
5498       if (!scope)
5499         scope = error_mark_node;
5500       /* If the SCOPE was erroneous, make the various semantic analysis
5501          functions exit quickly -- and without issuing additional error
5502          messages.  */
5503       if (scope == error_mark_node)
5504         postfix_expression = error_mark_node;
5505     }
5506
5507   /* Assume this expression is not a pseudo-destructor access.  */
5508   pseudo_destructor_p = false;
5509
5510   /* If the SCOPE is a scalar type, then, if this is a valid program,
5511      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5512      is type dependent, it can be pseudo-destructor-name or something else.
5513      Try to parse it as pseudo-destructor-name first.  */
5514   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5515     {
5516       tree s;
5517       tree type;
5518
5519       cp_parser_parse_tentatively (parser);
5520       /* Parse the pseudo-destructor-name.  */
5521       s = NULL_TREE;
5522       cp_parser_pseudo_destructor_name (parser, &s, &type);
5523       if (dependent_p
5524           && (cp_parser_error_occurred (parser)
5525               || TREE_CODE (type) != TYPE_DECL
5526               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5527         cp_parser_abort_tentative_parse (parser);
5528       else if (cp_parser_parse_definitely (parser))
5529         {
5530           pseudo_destructor_p = true;
5531           postfix_expression
5532             = finish_pseudo_destructor_expr (postfix_expression,
5533                                              s, TREE_TYPE (type));
5534         }
5535     }
5536
5537   if (!pseudo_destructor_p)
5538     {
5539       /* If the SCOPE is not a scalar type, we are looking at an
5540          ordinary class member access expression, rather than a
5541          pseudo-destructor-name.  */
5542       bool template_p;
5543       cp_token *token = cp_lexer_peek_token (parser->lexer);
5544       /* Parse the id-expression.  */
5545       name = (cp_parser_id_expression
5546               (parser,
5547                cp_parser_optional_template_keyword (parser),
5548                /*check_dependency_p=*/true,
5549                &template_p,
5550                /*declarator_p=*/false,
5551                /*optional_p=*/false));
5552       /* In general, build a SCOPE_REF if the member name is qualified.
5553          However, if the name was not dependent and has already been
5554          resolved; there is no need to build the SCOPE_REF.  For example;
5555
5556              struct X { void f(); };
5557              template <typename T> void f(T* t) { t->X::f(); }
5558
5559          Even though "t" is dependent, "X::f" is not and has been resolved
5560          to a BASELINK; there is no need to include scope information.  */
5561
5562       /* But we do need to remember that there was an explicit scope for
5563          virtual function calls.  */
5564       if (parser->scope)
5565         *idk = CP_ID_KIND_QUALIFIED;
5566
5567       /* If the name is a template-id that names a type, we will get a
5568          TYPE_DECL here.  That is invalid code.  */
5569       if (TREE_CODE (name) == TYPE_DECL)
5570         {
5571           error_at (token->location, "invalid use of %qD", name);
5572           postfix_expression = error_mark_node;
5573         }
5574       else
5575         {
5576           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5577             {
5578               name = build_qualified_name (/*type=*/NULL_TREE,
5579                                            parser->scope,
5580                                            name,
5581                                            template_p);
5582               parser->scope = NULL_TREE;
5583               parser->qualifying_scope = NULL_TREE;
5584               parser->object_scope = NULL_TREE;
5585             }
5586           if (scope && name && BASELINK_P (name))
5587             adjust_result_of_qualified_name_lookup
5588               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5589           postfix_expression
5590             = finish_class_member_access_expr (postfix_expression, name,
5591                                                template_p, 
5592                                                tf_warning_or_error);
5593         }
5594     }
5595
5596   /* We no longer need to look up names in the scope of the object on
5597      the left-hand side of the `.' or `->' operator.  */
5598   parser->context->object_type = NULL_TREE;
5599
5600   /* Outside of offsetof, these operators may not appear in
5601      constant-expressions.  */
5602   if (!for_offsetof
5603       && (cp_parser_non_integral_constant_expression
5604           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5605     postfix_expression = error_mark_node;
5606
5607   return postfix_expression;
5608 }
5609
5610 /* Parse a parenthesized expression-list.
5611
5612    expression-list:
5613      assignment-expression
5614      expression-list, assignment-expression
5615
5616    attribute-list:
5617      expression-list
5618      identifier
5619      identifier, expression-list
5620
5621    CAST_P is true if this expression is the target of a cast.
5622
5623    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5624    argument pack.
5625
5626    Returns a vector of trees.  Each element is a representation of an
5627    assignment-expression.  NULL is returned if the ( and or ) are
5628    missing.  An empty, but allocated, vector is returned on no
5629    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5630    if we are parsing an attribute list for an attribute that wants a
5631    plain identifier argument, normal_attr for an attribute that wants
5632    an expression, or non_attr if we aren't parsing an attribute list.  If
5633    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5634    not all of the expressions in the list were constant.  */
5635
5636 static VEC(tree,gc) *
5637 cp_parser_parenthesized_expression_list (cp_parser* parser,
5638                                          int is_attribute_list,
5639                                          bool cast_p,
5640                                          bool allow_expansion_p,
5641                                          bool *non_constant_p)
5642 {
5643   VEC(tree,gc) *expression_list;
5644   bool fold_expr_p = is_attribute_list != non_attr;
5645   tree identifier = NULL_TREE;
5646   bool saved_greater_than_is_operator_p;
5647
5648   /* Assume all the expressions will be constant.  */
5649   if (non_constant_p)
5650     *non_constant_p = false;
5651
5652   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5653     return NULL;
5654
5655   expression_list = make_tree_vector ();
5656
5657   /* Within a parenthesized expression, a `>' token is always
5658      the greater-than operator.  */
5659   saved_greater_than_is_operator_p
5660     = parser->greater_than_is_operator_p;
5661   parser->greater_than_is_operator_p = true;
5662
5663   /* Consume expressions until there are no more.  */
5664   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5665     while (true)
5666       {
5667         tree expr;
5668
5669         /* At the beginning of attribute lists, check to see if the
5670            next token is an identifier.  */
5671         if (is_attribute_list == id_attr
5672             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5673           {
5674             cp_token *token;
5675
5676             /* Consume the identifier.  */
5677             token = cp_lexer_consume_token (parser->lexer);
5678             /* Save the identifier.  */
5679             identifier = token->u.value;
5680           }
5681         else
5682           {
5683             bool expr_non_constant_p;
5684
5685             /* Parse the next assignment-expression.  */
5686             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5687               {
5688                 /* A braced-init-list.  */
5689                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5690                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5691                 if (non_constant_p && expr_non_constant_p)
5692                   *non_constant_p = true;
5693               }
5694             else if (non_constant_p)
5695               {
5696                 expr = (cp_parser_constant_expression
5697                         (parser, /*allow_non_constant_p=*/true,
5698                          &expr_non_constant_p));
5699                 if (expr_non_constant_p)
5700                   *non_constant_p = true;
5701               }
5702             else
5703               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5704
5705             if (fold_expr_p)
5706               expr = fold_non_dependent_expr (expr);
5707
5708             /* If we have an ellipsis, then this is an expression
5709                expansion.  */
5710             if (allow_expansion_p
5711                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5712               {
5713                 /* Consume the `...'.  */
5714                 cp_lexer_consume_token (parser->lexer);
5715
5716                 /* Build the argument pack.  */
5717                 expr = make_pack_expansion (expr);
5718               }
5719
5720              /* Add it to the list.  We add error_mark_node
5721                 expressions to the list, so that we can still tell if
5722                 the correct form for a parenthesized expression-list
5723                 is found. That gives better errors.  */
5724             VEC_safe_push (tree, gc, expression_list, expr);
5725
5726             if (expr == error_mark_node)
5727               goto skip_comma;
5728           }
5729
5730         /* After the first item, attribute lists look the same as
5731            expression lists.  */
5732         is_attribute_list = non_attr;
5733
5734       get_comma:;
5735         /* If the next token isn't a `,', then we are done.  */
5736         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5737           break;
5738
5739         /* Otherwise, consume the `,' and keep going.  */
5740         cp_lexer_consume_token (parser->lexer);
5741       }
5742
5743   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5744     {
5745       int ending;
5746
5747     skip_comma:;
5748       /* We try and resync to an unnested comma, as that will give the
5749          user better diagnostics.  */
5750       ending = cp_parser_skip_to_closing_parenthesis (parser,
5751                                                       /*recovering=*/true,
5752                                                       /*or_comma=*/true,
5753                                                       /*consume_paren=*/true);
5754       if (ending < 0)
5755         goto get_comma;
5756       if (!ending)
5757         {
5758           parser->greater_than_is_operator_p
5759             = saved_greater_than_is_operator_p;
5760           return NULL;
5761         }
5762     }
5763
5764   parser->greater_than_is_operator_p
5765     = saved_greater_than_is_operator_p;
5766
5767   if (identifier)
5768     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5769
5770   return expression_list;
5771 }
5772
5773 /* Parse a pseudo-destructor-name.
5774
5775    pseudo-destructor-name:
5776      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5777      :: [opt] nested-name-specifier template template-id :: ~ type-name
5778      :: [opt] nested-name-specifier [opt] ~ type-name
5779
5780    If either of the first two productions is used, sets *SCOPE to the
5781    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5782    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5783    or ERROR_MARK_NODE if the parse fails.  */
5784
5785 static void
5786 cp_parser_pseudo_destructor_name (cp_parser* parser,
5787                                   tree* scope,
5788                                   tree* type)
5789 {
5790   bool nested_name_specifier_p;
5791
5792   /* Assume that things will not work out.  */
5793   *type = error_mark_node;
5794
5795   /* Look for the optional `::' operator.  */
5796   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5797   /* Look for the optional nested-name-specifier.  */
5798   nested_name_specifier_p
5799     = (cp_parser_nested_name_specifier_opt (parser,
5800                                             /*typename_keyword_p=*/false,
5801                                             /*check_dependency_p=*/true,
5802                                             /*type_p=*/false,
5803                                             /*is_declaration=*/false)
5804        != NULL_TREE);
5805   /* Now, if we saw a nested-name-specifier, we might be doing the
5806      second production.  */
5807   if (nested_name_specifier_p
5808       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5809     {
5810       /* Consume the `template' keyword.  */
5811       cp_lexer_consume_token (parser->lexer);
5812       /* Parse the template-id.  */
5813       cp_parser_template_id (parser,
5814                              /*template_keyword_p=*/true,
5815                              /*check_dependency_p=*/false,
5816                              /*is_declaration=*/true);
5817       /* Look for the `::' token.  */
5818       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5819     }
5820   /* If the next token is not a `~', then there might be some
5821      additional qualification.  */
5822   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5823     {
5824       /* At this point, we're looking for "type-name :: ~".  The type-name
5825          must not be a class-name, since this is a pseudo-destructor.  So,
5826          it must be either an enum-name, or a typedef-name -- both of which
5827          are just identifiers.  So, we peek ahead to check that the "::"
5828          and "~" tokens are present; if they are not, then we can avoid
5829          calling type_name.  */
5830       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5831           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5832           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5833         {
5834           cp_parser_error (parser, "non-scalar type");
5835           return;
5836         }
5837
5838       /* Look for the type-name.  */
5839       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5840       if (*scope == error_mark_node)
5841         return;
5842
5843       /* Look for the `::' token.  */
5844       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5845     }
5846   else
5847     *scope = NULL_TREE;
5848
5849   /* Look for the `~'.  */
5850   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5851   /* Look for the type-name again.  We are not responsible for
5852      checking that it matches the first type-name.  */
5853   *type = cp_parser_nonclass_name (parser);
5854 }
5855
5856 /* Parse a unary-expression.
5857
5858    unary-expression:
5859      postfix-expression
5860      ++ cast-expression
5861      -- cast-expression
5862      unary-operator cast-expression
5863      sizeof unary-expression
5864      sizeof ( type-id )
5865      new-expression
5866      delete-expression
5867
5868    GNU Extensions:
5869
5870    unary-expression:
5871      __extension__ cast-expression
5872      __alignof__ unary-expression
5873      __alignof__ ( type-id )
5874      __real__ cast-expression
5875      __imag__ cast-expression
5876      && identifier
5877
5878    ADDRESS_P is true iff the unary-expression is appearing as the
5879    operand of the `&' operator.   CAST_P is true if this expression is
5880    the target of a cast.
5881
5882    Returns a representation of the expression.  */
5883
5884 static tree
5885 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5886                             cp_id_kind * pidk)
5887 {
5888   cp_token *token;
5889   enum tree_code unary_operator;
5890
5891   /* Peek at the next token.  */
5892   token = cp_lexer_peek_token (parser->lexer);
5893   /* Some keywords give away the kind of expression.  */
5894   if (token->type == CPP_KEYWORD)
5895     {
5896       enum rid keyword = token->keyword;
5897
5898       switch (keyword)
5899         {
5900         case RID_ALIGNOF:
5901         case RID_SIZEOF:
5902           {
5903             tree operand;
5904             enum tree_code op;
5905
5906             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5907             /* Consume the token.  */
5908             cp_lexer_consume_token (parser->lexer);
5909             /* Parse the operand.  */
5910             operand = cp_parser_sizeof_operand (parser, keyword);
5911
5912             if (TYPE_P (operand))
5913               return cxx_sizeof_or_alignof_type (operand, op, true);
5914             else
5915               return cxx_sizeof_or_alignof_expr (operand, op, true);
5916           }
5917
5918         case RID_NEW:
5919           return cp_parser_new_expression (parser);
5920
5921         case RID_DELETE:
5922           return cp_parser_delete_expression (parser);
5923
5924         case RID_EXTENSION:
5925           {
5926             /* The saved value of the PEDANTIC flag.  */
5927             int saved_pedantic;
5928             tree expr;
5929
5930             /* Save away the PEDANTIC flag.  */
5931             cp_parser_extension_opt (parser, &saved_pedantic);
5932             /* Parse the cast-expression.  */
5933             expr = cp_parser_simple_cast_expression (parser);
5934             /* Restore the PEDANTIC flag.  */
5935             pedantic = saved_pedantic;
5936
5937             return expr;
5938           }
5939
5940         case RID_REALPART:
5941         case RID_IMAGPART:
5942           {
5943             tree expression;
5944
5945             /* Consume the `__real__' or `__imag__' token.  */
5946             cp_lexer_consume_token (parser->lexer);
5947             /* Parse the cast-expression.  */
5948             expression = cp_parser_simple_cast_expression (parser);
5949             /* Create the complete representation.  */
5950             return build_x_unary_op ((keyword == RID_REALPART
5951                                       ? REALPART_EXPR : IMAGPART_EXPR),
5952                                      expression,
5953                                      tf_warning_or_error);
5954           }
5955           break;
5956
5957         case RID_NOEXCEPT:
5958           {
5959             tree expr;
5960             const char *saved_message;
5961             bool saved_integral_constant_expression_p;
5962             bool saved_non_integral_constant_expression_p;
5963             bool saved_greater_than_is_operator_p;
5964
5965             cp_lexer_consume_token (parser->lexer);
5966             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5967
5968             saved_message = parser->type_definition_forbidden_message;
5969             parser->type_definition_forbidden_message
5970               = G_("types may not be defined in %<noexcept%> expressions");
5971
5972             saved_integral_constant_expression_p
5973               = parser->integral_constant_expression_p;
5974             saved_non_integral_constant_expression_p
5975               = parser->non_integral_constant_expression_p;
5976             parser->integral_constant_expression_p = false;
5977
5978             saved_greater_than_is_operator_p
5979               = parser->greater_than_is_operator_p;
5980             parser->greater_than_is_operator_p = true;
5981
5982             ++cp_unevaluated_operand;
5983             ++c_inhibit_evaluation_warnings;
5984             expr = cp_parser_expression (parser, false, NULL);
5985             --c_inhibit_evaluation_warnings;
5986             --cp_unevaluated_operand;
5987
5988             parser->greater_than_is_operator_p
5989               = saved_greater_than_is_operator_p;
5990
5991             parser->integral_constant_expression_p
5992               = saved_integral_constant_expression_p;
5993             parser->non_integral_constant_expression_p
5994               = saved_non_integral_constant_expression_p;
5995
5996             parser->type_definition_forbidden_message = saved_message;
5997
5998             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5999             return finish_noexcept_expr (expr, tf_warning_or_error);
6000           }
6001
6002         default:
6003           break;
6004         }
6005     }
6006
6007   /* Look for the `:: new' and `:: delete', which also signal the
6008      beginning of a new-expression, or delete-expression,
6009      respectively.  If the next token is `::', then it might be one of
6010      these.  */
6011   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6012     {
6013       enum rid keyword;
6014
6015       /* See if the token after the `::' is one of the keywords in
6016          which we're interested.  */
6017       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6018       /* If it's `new', we have a new-expression.  */
6019       if (keyword == RID_NEW)
6020         return cp_parser_new_expression (parser);
6021       /* Similarly, for `delete'.  */
6022       else if (keyword == RID_DELETE)
6023         return cp_parser_delete_expression (parser);
6024     }
6025
6026   /* Look for a unary operator.  */
6027   unary_operator = cp_parser_unary_operator (token);
6028   /* The `++' and `--' operators can be handled similarly, even though
6029      they are not technically unary-operators in the grammar.  */
6030   if (unary_operator == ERROR_MARK)
6031     {
6032       if (token->type == CPP_PLUS_PLUS)
6033         unary_operator = PREINCREMENT_EXPR;
6034       else if (token->type == CPP_MINUS_MINUS)
6035         unary_operator = PREDECREMENT_EXPR;
6036       /* Handle the GNU address-of-label extension.  */
6037       else if (cp_parser_allow_gnu_extensions_p (parser)
6038                && token->type == CPP_AND_AND)
6039         {
6040           tree identifier;
6041           tree expression;
6042           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6043
6044           /* Consume the '&&' token.  */
6045           cp_lexer_consume_token (parser->lexer);
6046           /* Look for the identifier.  */
6047           identifier = cp_parser_identifier (parser);
6048           /* Create an expression representing the address.  */
6049           expression = finish_label_address_expr (identifier, loc);
6050           if (cp_parser_non_integral_constant_expression (parser,
6051                                                           NIC_ADDR_LABEL))
6052             expression = error_mark_node;
6053           return expression;
6054         }
6055     }
6056   if (unary_operator != ERROR_MARK)
6057     {
6058       tree cast_expression;
6059       tree expression = error_mark_node;
6060       non_integral_constant non_constant_p = NIC_NONE;
6061
6062       /* Consume the operator token.  */
6063       token = cp_lexer_consume_token (parser->lexer);
6064       /* Parse the cast-expression.  */
6065       cast_expression
6066         = cp_parser_cast_expression (parser,
6067                                      unary_operator == ADDR_EXPR,
6068                                      /*cast_p=*/false, pidk);
6069       /* Now, build an appropriate representation.  */
6070       switch (unary_operator)
6071         {
6072         case INDIRECT_REF:
6073           non_constant_p = NIC_STAR;
6074           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6075                                              tf_warning_or_error);
6076           break;
6077
6078         case ADDR_EXPR:
6079            non_constant_p = NIC_ADDR;
6080           /* Fall through.  */
6081         case BIT_NOT_EXPR:
6082           expression = build_x_unary_op (unary_operator, cast_expression,
6083                                          tf_warning_or_error);
6084           break;
6085
6086         case PREINCREMENT_EXPR:
6087         case PREDECREMENT_EXPR:
6088           non_constant_p = unary_operator == PREINCREMENT_EXPR
6089                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6090           /* Fall through.  */
6091         case UNARY_PLUS_EXPR:
6092         case NEGATE_EXPR:
6093         case TRUTH_NOT_EXPR:
6094           expression = finish_unary_op_expr (unary_operator, cast_expression);
6095           break;
6096
6097         default:
6098           gcc_unreachable ();
6099         }
6100
6101       if (non_constant_p != NIC_NONE
6102           && cp_parser_non_integral_constant_expression (parser,
6103                                                          non_constant_p))
6104         expression = error_mark_node;
6105
6106       return expression;
6107     }
6108
6109   return cp_parser_postfix_expression (parser, address_p, cast_p,
6110                                        /*member_access_only_p=*/false,
6111                                        pidk);
6112 }
6113
6114 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6115    unary-operator, the corresponding tree code is returned.  */
6116
6117 static enum tree_code
6118 cp_parser_unary_operator (cp_token* token)
6119 {
6120   switch (token->type)
6121     {
6122     case CPP_MULT:
6123       return INDIRECT_REF;
6124
6125     case CPP_AND:
6126       return ADDR_EXPR;
6127
6128     case CPP_PLUS:
6129       return UNARY_PLUS_EXPR;
6130
6131     case CPP_MINUS:
6132       return NEGATE_EXPR;
6133
6134     case CPP_NOT:
6135       return TRUTH_NOT_EXPR;
6136
6137     case CPP_COMPL:
6138       return BIT_NOT_EXPR;
6139
6140     default:
6141       return ERROR_MARK;
6142     }
6143 }
6144
6145 /* Parse a new-expression.
6146
6147    new-expression:
6148      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6149      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6150
6151    Returns a representation of the expression.  */
6152
6153 static tree
6154 cp_parser_new_expression (cp_parser* parser)
6155 {
6156   bool global_scope_p;
6157   VEC(tree,gc) *placement;
6158   tree type;
6159   VEC(tree,gc) *initializer;
6160   tree nelts;
6161   tree ret;
6162
6163   /* Look for the optional `::' operator.  */
6164   global_scope_p
6165     = (cp_parser_global_scope_opt (parser,
6166                                    /*current_scope_valid_p=*/false)
6167        != NULL_TREE);
6168   /* Look for the `new' operator.  */
6169   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6170   /* There's no easy way to tell a new-placement from the
6171      `( type-id )' construct.  */
6172   cp_parser_parse_tentatively (parser);
6173   /* Look for a new-placement.  */
6174   placement = cp_parser_new_placement (parser);
6175   /* If that didn't work out, there's no new-placement.  */
6176   if (!cp_parser_parse_definitely (parser))
6177     {
6178       if (placement != NULL)
6179         release_tree_vector (placement);
6180       placement = NULL;
6181     }
6182
6183   /* If the next token is a `(', then we have a parenthesized
6184      type-id.  */
6185   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6186     {
6187       cp_token *token;
6188       /* Consume the `('.  */
6189       cp_lexer_consume_token (parser->lexer);
6190       /* Parse the type-id.  */
6191       type = cp_parser_type_id (parser);
6192       /* Look for the closing `)'.  */
6193       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6194       token = cp_lexer_peek_token (parser->lexer);
6195       /* There should not be a direct-new-declarator in this production,
6196          but GCC used to allowed this, so we check and emit a sensible error
6197          message for this case.  */
6198       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6199         {
6200           error_at (token->location,
6201                     "array bound forbidden after parenthesized type-id");
6202           inform (token->location, 
6203                   "try removing the parentheses around the type-id");
6204           cp_parser_direct_new_declarator (parser);
6205         }
6206       nelts = NULL_TREE;
6207     }
6208   /* Otherwise, there must be a new-type-id.  */
6209   else
6210     type = cp_parser_new_type_id (parser, &nelts);
6211
6212   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6213   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6214       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6215     initializer = cp_parser_new_initializer (parser);
6216   else
6217     initializer = NULL;
6218
6219   /* A new-expression may not appear in an integral constant
6220      expression.  */
6221   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6222     ret = error_mark_node;
6223   else
6224     {
6225       /* Create a representation of the new-expression.  */
6226       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6227                        tf_warning_or_error);
6228     }
6229
6230   if (placement != NULL)
6231     release_tree_vector (placement);
6232   if (initializer != NULL)
6233     release_tree_vector (initializer);
6234
6235   return ret;
6236 }
6237
6238 /* Parse a new-placement.
6239
6240    new-placement:
6241      ( expression-list )
6242
6243    Returns the same representation as for an expression-list.  */
6244
6245 static VEC(tree,gc) *
6246 cp_parser_new_placement (cp_parser* parser)
6247 {
6248   VEC(tree,gc) *expression_list;
6249
6250   /* Parse the expression-list.  */
6251   expression_list = (cp_parser_parenthesized_expression_list
6252                      (parser, non_attr, /*cast_p=*/false,
6253                       /*allow_expansion_p=*/true,
6254                       /*non_constant_p=*/NULL));
6255
6256   return expression_list;
6257 }
6258
6259 /* Parse a new-type-id.
6260
6261    new-type-id:
6262      type-specifier-seq new-declarator [opt]
6263
6264    Returns the TYPE allocated.  If the new-type-id indicates an array
6265    type, *NELTS is set to the number of elements in the last array
6266    bound; the TYPE will not include the last array bound.  */
6267
6268 static tree
6269 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6270 {
6271   cp_decl_specifier_seq type_specifier_seq;
6272   cp_declarator *new_declarator;
6273   cp_declarator *declarator;
6274   cp_declarator *outer_declarator;
6275   const char *saved_message;
6276   tree type;
6277
6278   /* The type-specifier sequence must not contain type definitions.
6279      (It cannot contain declarations of new types either, but if they
6280      are not definitions we will catch that because they are not
6281      complete.)  */
6282   saved_message = parser->type_definition_forbidden_message;
6283   parser->type_definition_forbidden_message
6284     = G_("types may not be defined in a new-type-id");
6285   /* Parse the type-specifier-seq.  */
6286   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6287                                 /*is_trailing_return=*/false,
6288                                 &type_specifier_seq);
6289   /* Restore the old message.  */
6290   parser->type_definition_forbidden_message = saved_message;
6291   /* Parse the new-declarator.  */
6292   new_declarator = cp_parser_new_declarator_opt (parser);
6293
6294   /* Determine the number of elements in the last array dimension, if
6295      any.  */
6296   *nelts = NULL_TREE;
6297   /* Skip down to the last array dimension.  */
6298   declarator = new_declarator;
6299   outer_declarator = NULL;
6300   while (declarator && (declarator->kind == cdk_pointer
6301                         || declarator->kind == cdk_ptrmem))
6302     {
6303       outer_declarator = declarator;
6304       declarator = declarator->declarator;
6305     }
6306   while (declarator
6307          && declarator->kind == cdk_array
6308          && declarator->declarator
6309          && declarator->declarator->kind == cdk_array)
6310     {
6311       outer_declarator = declarator;
6312       declarator = declarator->declarator;
6313     }
6314
6315   if (declarator && declarator->kind == cdk_array)
6316     {
6317       *nelts = declarator->u.array.bounds;
6318       if (*nelts == error_mark_node)
6319         *nelts = integer_one_node;
6320
6321       if (outer_declarator)
6322         outer_declarator->declarator = declarator->declarator;
6323       else
6324         new_declarator = NULL;
6325     }
6326
6327   type = groktypename (&type_specifier_seq, new_declarator, false);
6328   return type;
6329 }
6330
6331 /* Parse an (optional) new-declarator.
6332
6333    new-declarator:
6334      ptr-operator new-declarator [opt]
6335      direct-new-declarator
6336
6337    Returns the declarator.  */
6338
6339 static cp_declarator *
6340 cp_parser_new_declarator_opt (cp_parser* parser)
6341 {
6342   enum tree_code code;
6343   tree type;
6344   cp_cv_quals cv_quals;
6345
6346   /* We don't know if there's a ptr-operator next, or not.  */
6347   cp_parser_parse_tentatively (parser);
6348   /* Look for a ptr-operator.  */
6349   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6350   /* If that worked, look for more new-declarators.  */
6351   if (cp_parser_parse_definitely (parser))
6352     {
6353       cp_declarator *declarator;
6354
6355       /* Parse another optional declarator.  */
6356       declarator = cp_parser_new_declarator_opt (parser);
6357
6358       return cp_parser_make_indirect_declarator
6359         (code, type, cv_quals, declarator);
6360     }
6361
6362   /* If the next token is a `[', there is a direct-new-declarator.  */
6363   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6364     return cp_parser_direct_new_declarator (parser);
6365
6366   return NULL;
6367 }
6368
6369 /* Parse a direct-new-declarator.
6370
6371    direct-new-declarator:
6372      [ expression ]
6373      direct-new-declarator [constant-expression]
6374
6375    */
6376
6377 static cp_declarator *
6378 cp_parser_direct_new_declarator (cp_parser* parser)
6379 {
6380   cp_declarator *declarator = NULL;
6381
6382   while (true)
6383     {
6384       tree expression;
6385
6386       /* Look for the opening `['.  */
6387       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6388       /* The first expression is not required to be constant.  */
6389       if (!declarator)
6390         {
6391           cp_token *token = cp_lexer_peek_token (parser->lexer);
6392           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6393           /* The standard requires that the expression have integral
6394              type.  DR 74 adds enumeration types.  We believe that the
6395              real intent is that these expressions be handled like the
6396              expression in a `switch' condition, which also allows
6397              classes with a single conversion to integral or
6398              enumeration type.  */
6399           if (!processing_template_decl)
6400             {
6401               expression
6402                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6403                                               expression,
6404                                               /*complain=*/true);
6405               if (!expression)
6406                 {
6407                   error_at (token->location,
6408                             "expression in new-declarator must have integral "
6409                             "or enumeration type");
6410                   expression = error_mark_node;
6411                 }
6412             }
6413         }
6414       /* But all the other expressions must be.  */
6415       else
6416         expression
6417           = cp_parser_constant_expression (parser,
6418                                            /*allow_non_constant=*/false,
6419                                            NULL);
6420       /* Look for the closing `]'.  */
6421       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6422
6423       /* Add this bound to the declarator.  */
6424       declarator = make_array_declarator (declarator, expression);
6425
6426       /* If the next token is not a `[', then there are no more
6427          bounds.  */
6428       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6429         break;
6430     }
6431
6432   return declarator;
6433 }
6434
6435 /* Parse a new-initializer.
6436
6437    new-initializer:
6438      ( expression-list [opt] )
6439      braced-init-list
6440
6441    Returns a representation of the expression-list.  */
6442
6443 static VEC(tree,gc) *
6444 cp_parser_new_initializer (cp_parser* parser)
6445 {
6446   VEC(tree,gc) *expression_list;
6447
6448   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6449     {
6450       tree t;
6451       bool expr_non_constant_p;
6452       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6453       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6454       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6455       expression_list = make_tree_vector_single (t);
6456     }
6457   else
6458     expression_list = (cp_parser_parenthesized_expression_list
6459                        (parser, non_attr, /*cast_p=*/false,
6460                         /*allow_expansion_p=*/true,
6461                         /*non_constant_p=*/NULL));
6462
6463   return expression_list;
6464 }
6465
6466 /* Parse a delete-expression.
6467
6468    delete-expression:
6469      :: [opt] delete cast-expression
6470      :: [opt] delete [ ] cast-expression
6471
6472    Returns a representation of the expression.  */
6473
6474 static tree
6475 cp_parser_delete_expression (cp_parser* parser)
6476 {
6477   bool global_scope_p;
6478   bool array_p;
6479   tree expression;
6480
6481   /* Look for the optional `::' operator.  */
6482   global_scope_p
6483     = (cp_parser_global_scope_opt (parser,
6484                                    /*current_scope_valid_p=*/false)
6485        != NULL_TREE);
6486   /* Look for the `delete' keyword.  */
6487   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6488   /* See if the array syntax is in use.  */
6489   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6490     {
6491       /* Consume the `[' token.  */
6492       cp_lexer_consume_token (parser->lexer);
6493       /* Look for the `]' token.  */
6494       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6495       /* Remember that this is the `[]' construct.  */
6496       array_p = true;
6497     }
6498   else
6499     array_p = false;
6500
6501   /* Parse the cast-expression.  */
6502   expression = cp_parser_simple_cast_expression (parser);
6503
6504   /* A delete-expression may not appear in an integral constant
6505      expression.  */
6506   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6507     return error_mark_node;
6508
6509   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6510 }
6511
6512 /* Returns true if TOKEN may start a cast-expression and false
6513    otherwise.  */
6514
6515 static bool
6516 cp_parser_token_starts_cast_expression (cp_token *token)
6517 {
6518   switch (token->type)
6519     {
6520     case CPP_COMMA:
6521     case CPP_SEMICOLON:
6522     case CPP_QUERY:
6523     case CPP_COLON:
6524     case CPP_CLOSE_SQUARE:
6525     case CPP_CLOSE_PAREN:
6526     case CPP_CLOSE_BRACE:
6527     case CPP_DOT:
6528     case CPP_DOT_STAR:
6529     case CPP_DEREF:
6530     case CPP_DEREF_STAR:
6531     case CPP_DIV:
6532     case CPP_MOD:
6533     case CPP_LSHIFT:
6534     case CPP_RSHIFT:
6535     case CPP_LESS:
6536     case CPP_GREATER:
6537     case CPP_LESS_EQ:
6538     case CPP_GREATER_EQ:
6539     case CPP_EQ_EQ:
6540     case CPP_NOT_EQ:
6541     case CPP_EQ:
6542     case CPP_MULT_EQ:
6543     case CPP_DIV_EQ:
6544     case CPP_MOD_EQ:
6545     case CPP_PLUS_EQ:
6546     case CPP_MINUS_EQ:
6547     case CPP_RSHIFT_EQ:
6548     case CPP_LSHIFT_EQ:
6549     case CPP_AND_EQ:
6550     case CPP_XOR_EQ:
6551     case CPP_OR_EQ:
6552     case CPP_XOR:
6553     case CPP_OR:
6554     case CPP_OR_OR:
6555     case CPP_EOF:
6556       return false;
6557
6558       /* '[' may start a primary-expression in obj-c++.  */
6559     case CPP_OPEN_SQUARE:
6560       return c_dialect_objc ();
6561
6562     default:
6563       return true;
6564     }
6565 }
6566
6567 /* Parse a cast-expression.
6568
6569    cast-expression:
6570      unary-expression
6571      ( type-id ) cast-expression
6572
6573    ADDRESS_P is true iff the unary-expression is appearing as the
6574    operand of the `&' operator.   CAST_P is true if this expression is
6575    the target of a cast.
6576
6577    Returns a representation of the expression.  */
6578
6579 static tree
6580 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6581                            cp_id_kind * pidk)
6582 {
6583   /* If it's a `(', then we might be looking at a cast.  */
6584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6585     {
6586       tree type = NULL_TREE;
6587       tree expr = NULL_TREE;
6588       bool compound_literal_p;
6589       const char *saved_message;
6590
6591       /* There's no way to know yet whether or not this is a cast.
6592          For example, `(int (3))' is a unary-expression, while `(int)
6593          3' is a cast.  So, we resort to parsing tentatively.  */
6594       cp_parser_parse_tentatively (parser);
6595       /* Types may not be defined in a cast.  */
6596       saved_message = parser->type_definition_forbidden_message;
6597       parser->type_definition_forbidden_message
6598         = G_("types may not be defined in casts");
6599       /* Consume the `('.  */
6600       cp_lexer_consume_token (parser->lexer);
6601       /* A very tricky bit is that `(struct S) { 3 }' is a
6602          compound-literal (which we permit in C++ as an extension).
6603          But, that construct is not a cast-expression -- it is a
6604          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6605          is legal; if the compound-literal were a cast-expression,
6606          you'd need an extra set of parentheses.)  But, if we parse
6607          the type-id, and it happens to be a class-specifier, then we
6608          will commit to the parse at that point, because we cannot
6609          undo the action that is done when creating a new class.  So,
6610          then we cannot back up and do a postfix-expression.
6611
6612          Therefore, we scan ahead to the closing `)', and check to see
6613          if the token after the `)' is a `{'.  If so, we are not
6614          looking at a cast-expression.
6615
6616          Save tokens so that we can put them back.  */
6617       cp_lexer_save_tokens (parser->lexer);
6618       /* Skip tokens until the next token is a closing parenthesis.
6619          If we find the closing `)', and the next token is a `{', then
6620          we are looking at a compound-literal.  */
6621       compound_literal_p
6622         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6623                                                   /*consume_paren=*/true)
6624            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6625       /* Roll back the tokens we skipped.  */
6626       cp_lexer_rollback_tokens (parser->lexer);
6627       /* If we were looking at a compound-literal, simulate an error
6628          so that the call to cp_parser_parse_definitely below will
6629          fail.  */
6630       if (compound_literal_p)
6631         cp_parser_simulate_error (parser);
6632       else
6633         {
6634           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6635           parser->in_type_id_in_expr_p = true;
6636           /* Look for the type-id.  */
6637           type = cp_parser_type_id (parser);
6638           /* Look for the closing `)'.  */
6639           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6640           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6641         }
6642
6643       /* Restore the saved message.  */
6644       parser->type_definition_forbidden_message = saved_message;
6645
6646       /* At this point this can only be either a cast or a
6647          parenthesized ctor such as `(T ())' that looks like a cast to
6648          function returning T.  */
6649       if (!cp_parser_error_occurred (parser)
6650           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6651                                                      (parser->lexer)))
6652         {
6653           cp_parser_parse_definitely (parser);
6654           expr = cp_parser_cast_expression (parser,
6655                                             /*address_p=*/false,
6656                                             /*cast_p=*/true, pidk);
6657
6658           /* Warn about old-style casts, if so requested.  */
6659           if (warn_old_style_cast
6660               && !in_system_header
6661               && !VOID_TYPE_P (type)
6662               && current_lang_name != lang_name_c)
6663             warning (OPT_Wold_style_cast, "use of old-style cast");
6664
6665           /* Only type conversions to integral or enumeration types
6666              can be used in constant-expressions.  */
6667           if (!cast_valid_in_integral_constant_expression_p (type)
6668               && cp_parser_non_integral_constant_expression (parser,
6669                                                              NIC_CAST))
6670             return error_mark_node;
6671
6672           /* Perform the cast.  */
6673           expr = build_c_cast (input_location, type, expr);
6674           return expr;
6675         }
6676       else 
6677         cp_parser_abort_tentative_parse (parser);
6678     }
6679
6680   /* If we get here, then it's not a cast, so it must be a
6681      unary-expression.  */
6682   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6683 }
6684
6685 /* Parse a binary expression of the general form:
6686
6687    pm-expression:
6688      cast-expression
6689      pm-expression .* cast-expression
6690      pm-expression ->* cast-expression
6691
6692    multiplicative-expression:
6693      pm-expression
6694      multiplicative-expression * pm-expression
6695      multiplicative-expression / pm-expression
6696      multiplicative-expression % pm-expression
6697
6698    additive-expression:
6699      multiplicative-expression
6700      additive-expression + multiplicative-expression
6701      additive-expression - multiplicative-expression
6702
6703    shift-expression:
6704      additive-expression
6705      shift-expression << additive-expression
6706      shift-expression >> additive-expression
6707
6708    relational-expression:
6709      shift-expression
6710      relational-expression < shift-expression
6711      relational-expression > shift-expression
6712      relational-expression <= shift-expression
6713      relational-expression >= shift-expression
6714
6715   GNU Extension:
6716
6717    relational-expression:
6718      relational-expression <? shift-expression
6719      relational-expression >? shift-expression
6720
6721    equality-expression:
6722      relational-expression
6723      equality-expression == relational-expression
6724      equality-expression != relational-expression
6725
6726    and-expression:
6727      equality-expression
6728      and-expression & equality-expression
6729
6730    exclusive-or-expression:
6731      and-expression
6732      exclusive-or-expression ^ and-expression
6733
6734    inclusive-or-expression:
6735      exclusive-or-expression
6736      inclusive-or-expression | exclusive-or-expression
6737
6738    logical-and-expression:
6739      inclusive-or-expression
6740      logical-and-expression && inclusive-or-expression
6741
6742    logical-or-expression:
6743      logical-and-expression
6744      logical-or-expression || logical-and-expression
6745
6746    All these are implemented with a single function like:
6747
6748    binary-expression:
6749      simple-cast-expression
6750      binary-expression <token> binary-expression
6751
6752    CAST_P is true if this expression is the target of a cast.
6753
6754    The binops_by_token map is used to get the tree codes for each <token> type.
6755    binary-expressions are associated according to a precedence table.  */
6756
6757 #define TOKEN_PRECEDENCE(token)                              \
6758 (((token->type == CPP_GREATER                                \
6759    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6760   && !parser->greater_than_is_operator_p)                    \
6761  ? PREC_NOT_OPERATOR                                         \
6762  : binops_by_token[token->type].prec)
6763
6764 static tree
6765 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6766                              bool no_toplevel_fold_p,
6767                              enum cp_parser_prec prec,
6768                              cp_id_kind * pidk)
6769 {
6770   cp_parser_expression_stack stack;
6771   cp_parser_expression_stack_entry *sp = &stack[0];
6772   tree lhs, rhs;
6773   cp_token *token;
6774   enum tree_code tree_type, lhs_type, rhs_type;
6775   enum cp_parser_prec new_prec, lookahead_prec;
6776   bool overloaded_p;
6777
6778   /* Parse the first expression.  */
6779   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6780   lhs_type = ERROR_MARK;
6781
6782   for (;;)
6783     {
6784       /* Get an operator token.  */
6785       token = cp_lexer_peek_token (parser->lexer);
6786
6787       if (warn_cxx0x_compat
6788           && token->type == CPP_RSHIFT
6789           && !parser->greater_than_is_operator_p)
6790         {
6791           if (warning_at (token->location, OPT_Wc__0x_compat, 
6792                           "%<>>%> operator will be treated as"
6793                           " two right angle brackets in C++0x"))
6794             inform (token->location,
6795                     "suggest parentheses around %<>>%> expression");
6796         }
6797
6798       new_prec = TOKEN_PRECEDENCE (token);
6799
6800       /* Popping an entry off the stack means we completed a subexpression:
6801          - either we found a token which is not an operator (`>' where it is not
6802            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6803            will happen repeatedly;
6804          - or, we found an operator which has lower priority.  This is the case
6805            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6806            parsing `3 * 4'.  */
6807       if (new_prec <= prec)
6808         {
6809           if (sp == stack)
6810             break;
6811           else
6812             goto pop;
6813         }
6814
6815      get_rhs:
6816       tree_type = binops_by_token[token->type].tree_type;
6817
6818       /* We used the operator token.  */
6819       cp_lexer_consume_token (parser->lexer);
6820
6821       /* For "false && x" or "true || x", x will never be executed;
6822          disable warnings while evaluating it.  */
6823       if (tree_type == TRUTH_ANDIF_EXPR)
6824         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6825       else if (tree_type == TRUTH_ORIF_EXPR)
6826         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6827
6828       /* Extract another operand.  It may be the RHS of this expression
6829          or the LHS of a new, higher priority expression.  */
6830       rhs = cp_parser_simple_cast_expression (parser);
6831       rhs_type = ERROR_MARK;
6832
6833       /* Get another operator token.  Look up its precedence to avoid
6834          building a useless (immediately popped) stack entry for common
6835          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6836       token = cp_lexer_peek_token (parser->lexer);
6837       lookahead_prec = TOKEN_PRECEDENCE (token);
6838       if (lookahead_prec > new_prec)
6839         {
6840           /* ... and prepare to parse the RHS of the new, higher priority
6841              expression.  Since precedence levels on the stack are
6842              monotonically increasing, we do not have to care about
6843              stack overflows.  */
6844           sp->prec = prec;
6845           sp->tree_type = tree_type;
6846           sp->lhs = lhs;
6847           sp->lhs_type = lhs_type;
6848           sp++;
6849           lhs = rhs;
6850           lhs_type = rhs_type;
6851           prec = new_prec;
6852           new_prec = lookahead_prec;
6853           goto get_rhs;
6854
6855          pop:
6856           lookahead_prec = new_prec;
6857           /* If the stack is not empty, we have parsed into LHS the right side
6858              (`4' in the example above) of an expression we had suspended.
6859              We can use the information on the stack to recover the LHS (`3')
6860              from the stack together with the tree code (`MULT_EXPR'), and
6861              the precedence of the higher level subexpression
6862              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6863              which will be used to actually build the additive expression.  */
6864           --sp;
6865           prec = sp->prec;
6866           tree_type = sp->tree_type;
6867           rhs = lhs;
6868           rhs_type = lhs_type;
6869           lhs = sp->lhs;
6870           lhs_type = sp->lhs_type;
6871         }
6872
6873       /* Undo the disabling of warnings done above.  */
6874       if (tree_type == TRUTH_ANDIF_EXPR)
6875         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6876       else if (tree_type == TRUTH_ORIF_EXPR)
6877         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6878
6879       overloaded_p = false;
6880       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6881          ERROR_MARK for everything that is not a binary expression.
6882          This makes warn_about_parentheses miss some warnings that
6883          involve unary operators.  For unary expressions we should
6884          pass the correct tree_code unless the unary expression was
6885          surrounded by parentheses.
6886       */
6887       if (no_toplevel_fold_p
6888           && lookahead_prec <= prec
6889           && sp == stack
6890           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6891         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6892       else
6893         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6894                                  &overloaded_p, tf_warning_or_error);
6895       lhs_type = tree_type;
6896
6897       /* If the binary operator required the use of an overloaded operator,
6898          then this expression cannot be an integral constant-expression.
6899          An overloaded operator can be used even if both operands are
6900          otherwise permissible in an integral constant-expression if at
6901          least one of the operands is of enumeration type.  */
6902
6903       if (overloaded_p
6904           && cp_parser_non_integral_constant_expression (parser,
6905                                                          NIC_OVERLOADED))
6906         return error_mark_node;
6907     }
6908
6909   return lhs;
6910 }
6911
6912
6913 /* Parse the `? expression : assignment-expression' part of a
6914    conditional-expression.  The LOGICAL_OR_EXPR is the
6915    logical-or-expression that started the conditional-expression.
6916    Returns a representation of the entire conditional-expression.
6917
6918    This routine is used by cp_parser_assignment_expression.
6919
6920      ? expression : assignment-expression
6921
6922    GNU Extensions:
6923
6924      ? : assignment-expression */
6925
6926 static tree
6927 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6928 {
6929   tree expr;
6930   tree assignment_expr;
6931   struct cp_token *token;
6932
6933   /* Consume the `?' token.  */
6934   cp_lexer_consume_token (parser->lexer);
6935   token = cp_lexer_peek_token (parser->lexer);
6936   if (cp_parser_allow_gnu_extensions_p (parser)
6937       && token->type == CPP_COLON)
6938     {
6939       pedwarn (token->location, OPT_pedantic, 
6940                "ISO C++ does not allow ?: with omitted middle operand");
6941       /* Implicit true clause.  */
6942       expr = NULL_TREE;
6943       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6944       warn_for_omitted_condop (token->location, logical_or_expr);
6945     }
6946   else
6947     {
6948       /* Parse the expression.  */
6949       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6950       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6951       c_inhibit_evaluation_warnings +=
6952         ((logical_or_expr == truthvalue_true_node)
6953          - (logical_or_expr == truthvalue_false_node));
6954     }
6955
6956   /* The next token should be a `:'.  */
6957   cp_parser_require (parser, CPP_COLON, RT_COLON);
6958   /* Parse the assignment-expression.  */
6959   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6960   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6961
6962   /* Build the conditional-expression.  */
6963   return build_x_conditional_expr (logical_or_expr,
6964                                    expr,
6965                                    assignment_expr,
6966                                    tf_warning_or_error);
6967 }
6968
6969 /* Parse an assignment-expression.
6970
6971    assignment-expression:
6972      conditional-expression
6973      logical-or-expression assignment-operator assignment_expression
6974      throw-expression
6975
6976    CAST_P is true if this expression is the target of a cast.
6977
6978    Returns a representation for the expression.  */
6979
6980 static tree
6981 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6982                                  cp_id_kind * pidk)
6983 {
6984   tree expr;
6985
6986   /* If the next token is the `throw' keyword, then we're looking at
6987      a throw-expression.  */
6988   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6989     expr = cp_parser_throw_expression (parser);
6990   /* Otherwise, it must be that we are looking at a
6991      logical-or-expression.  */
6992   else
6993     {
6994       /* Parse the binary expressions (logical-or-expression).  */
6995       expr = cp_parser_binary_expression (parser, cast_p, false,
6996                                           PREC_NOT_OPERATOR, pidk);
6997       /* If the next token is a `?' then we're actually looking at a
6998          conditional-expression.  */
6999       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7000         return cp_parser_question_colon_clause (parser, expr);
7001       else
7002         {
7003           enum tree_code assignment_operator;
7004
7005           /* If it's an assignment-operator, we're using the second
7006              production.  */
7007           assignment_operator
7008             = cp_parser_assignment_operator_opt (parser);
7009           if (assignment_operator != ERROR_MARK)
7010             {
7011               bool non_constant_p;
7012
7013               /* Parse the right-hand side of the assignment.  */
7014               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7015
7016               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7017                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7018
7019               /* An assignment may not appear in a
7020                  constant-expression.  */
7021               if (cp_parser_non_integral_constant_expression (parser,
7022                                                               NIC_ASSIGNMENT))
7023                 return error_mark_node;
7024               /* Build the assignment expression.  */
7025               expr = build_x_modify_expr (expr,
7026                                           assignment_operator,
7027                                           rhs,
7028                                           tf_warning_or_error);
7029             }
7030         }
7031     }
7032
7033   return expr;
7034 }
7035
7036 /* Parse an (optional) assignment-operator.
7037
7038    assignment-operator: one of
7039      = *= /= %= += -= >>= <<= &= ^= |=
7040
7041    GNU Extension:
7042
7043    assignment-operator: one of
7044      <?= >?=
7045
7046    If the next token is an assignment operator, the corresponding tree
7047    code is returned, and the token is consumed.  For example, for
7048    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7049    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7050    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7051    operator, ERROR_MARK is returned.  */
7052
7053 static enum tree_code
7054 cp_parser_assignment_operator_opt (cp_parser* parser)
7055 {
7056   enum tree_code op;
7057   cp_token *token;
7058
7059   /* Peek at the next token.  */
7060   token = cp_lexer_peek_token (parser->lexer);
7061
7062   switch (token->type)
7063     {
7064     case CPP_EQ:
7065       op = NOP_EXPR;
7066       break;
7067
7068     case CPP_MULT_EQ:
7069       op = MULT_EXPR;
7070       break;
7071
7072     case CPP_DIV_EQ:
7073       op = TRUNC_DIV_EXPR;
7074       break;
7075
7076     case CPP_MOD_EQ:
7077       op = TRUNC_MOD_EXPR;
7078       break;
7079
7080     case CPP_PLUS_EQ:
7081       op = PLUS_EXPR;
7082       break;
7083
7084     case CPP_MINUS_EQ:
7085       op = MINUS_EXPR;
7086       break;
7087
7088     case CPP_RSHIFT_EQ:
7089       op = RSHIFT_EXPR;
7090       break;
7091
7092     case CPP_LSHIFT_EQ:
7093       op = LSHIFT_EXPR;
7094       break;
7095
7096     case CPP_AND_EQ:
7097       op = BIT_AND_EXPR;
7098       break;
7099
7100     case CPP_XOR_EQ:
7101       op = BIT_XOR_EXPR;
7102       break;
7103
7104     case CPP_OR_EQ:
7105       op = BIT_IOR_EXPR;
7106       break;
7107
7108     default:
7109       /* Nothing else is an assignment operator.  */
7110       op = ERROR_MARK;
7111     }
7112
7113   /* If it was an assignment operator, consume it.  */
7114   if (op != ERROR_MARK)
7115     cp_lexer_consume_token (parser->lexer);
7116
7117   return op;
7118 }
7119
7120 /* Parse an expression.
7121
7122    expression:
7123      assignment-expression
7124      expression , assignment-expression
7125
7126    CAST_P is true if this expression is the target of a cast.
7127
7128    Returns a representation of the expression.  */
7129
7130 static tree
7131 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7132 {
7133   tree expression = NULL_TREE;
7134
7135   while (true)
7136     {
7137       tree assignment_expression;
7138
7139       /* Parse the next assignment-expression.  */
7140       assignment_expression
7141         = cp_parser_assignment_expression (parser, cast_p, pidk);
7142       /* If this is the first assignment-expression, we can just
7143          save it away.  */
7144       if (!expression)
7145         expression = assignment_expression;
7146       else
7147         expression = build_x_compound_expr (expression,
7148                                             assignment_expression,
7149                                             tf_warning_or_error);
7150       /* If the next token is not a comma, then we are done with the
7151          expression.  */
7152       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7153         break;
7154       /* Consume the `,'.  */
7155       cp_lexer_consume_token (parser->lexer);
7156       /* A comma operator cannot appear in a constant-expression.  */
7157       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7158         expression = error_mark_node;
7159     }
7160
7161   return expression;
7162 }
7163
7164 /* Parse a constant-expression.
7165
7166    constant-expression:
7167      conditional-expression
7168
7169   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7170   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7171   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7172   is false, NON_CONSTANT_P should be NULL.  */
7173
7174 static tree
7175 cp_parser_constant_expression (cp_parser* parser,
7176                                bool allow_non_constant_p,
7177                                bool *non_constant_p)
7178 {
7179   bool saved_integral_constant_expression_p;
7180   bool saved_allow_non_integral_constant_expression_p;
7181   bool saved_non_integral_constant_expression_p;
7182   tree expression;
7183
7184   /* It might seem that we could simply parse the
7185      conditional-expression, and then check to see if it were
7186      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7187      one that the compiler can figure out is constant, possibly after
7188      doing some simplifications or optimizations.  The standard has a
7189      precise definition of constant-expression, and we must honor
7190      that, even though it is somewhat more restrictive.
7191
7192      For example:
7193
7194        int i[(2, 3)];
7195
7196      is not a legal declaration, because `(2, 3)' is not a
7197      constant-expression.  The `,' operator is forbidden in a
7198      constant-expression.  However, GCC's constant-folding machinery
7199      will fold this operation to an INTEGER_CST for `3'.  */
7200
7201   /* Save the old settings.  */
7202   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7203   saved_allow_non_integral_constant_expression_p
7204     = parser->allow_non_integral_constant_expression_p;
7205   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7206   /* We are now parsing a constant-expression.  */
7207   parser->integral_constant_expression_p = true;
7208   parser->allow_non_integral_constant_expression_p
7209     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7210   parser->non_integral_constant_expression_p = false;
7211   /* Although the grammar says "conditional-expression", we parse an
7212      "assignment-expression", which also permits "throw-expression"
7213      and the use of assignment operators.  In the case that
7214      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7215      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7216      actually essential that we look for an assignment-expression.
7217      For example, cp_parser_initializer_clauses uses this function to
7218      determine whether a particular assignment-expression is in fact
7219      constant.  */
7220   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7221   /* Restore the old settings.  */
7222   parser->integral_constant_expression_p
7223     = saved_integral_constant_expression_p;
7224   parser->allow_non_integral_constant_expression_p
7225     = saved_allow_non_integral_constant_expression_p;
7226   if (allow_non_constant_p)
7227     *non_constant_p = parser->non_integral_constant_expression_p;
7228   else if (parser->non_integral_constant_expression_p
7229            && cxx_dialect < cxx0x)
7230     expression = error_mark_node;
7231   parser->non_integral_constant_expression_p
7232     = saved_non_integral_constant_expression_p;
7233
7234   return expression;
7235 }
7236
7237 /* Parse __builtin_offsetof.
7238
7239    offsetof-expression:
7240      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7241
7242    offsetof-member-designator:
7243      id-expression
7244      | offsetof-member-designator "." id-expression
7245      | offsetof-member-designator "[" expression "]"
7246      | offsetof-member-designator "->" id-expression  */
7247
7248 static tree
7249 cp_parser_builtin_offsetof (cp_parser *parser)
7250 {
7251   int save_ice_p, save_non_ice_p;
7252   tree type, expr;
7253   cp_id_kind dummy;
7254   cp_token *token;
7255
7256   /* We're about to accept non-integral-constant things, but will
7257      definitely yield an integral constant expression.  Save and
7258      restore these values around our local parsing.  */
7259   save_ice_p = parser->integral_constant_expression_p;
7260   save_non_ice_p = parser->non_integral_constant_expression_p;
7261
7262   /* Consume the "__builtin_offsetof" token.  */
7263   cp_lexer_consume_token (parser->lexer);
7264   /* Consume the opening `('.  */
7265   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7266   /* Parse the type-id.  */
7267   type = cp_parser_type_id (parser);
7268   /* Look for the `,'.  */
7269   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7270   token = cp_lexer_peek_token (parser->lexer);
7271
7272   /* Build the (type *)null that begins the traditional offsetof macro.  */
7273   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7274                             tf_warning_or_error);
7275
7276   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7277   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7278                                                  true, &dummy, token->location);
7279   while (true)
7280     {
7281       token = cp_lexer_peek_token (parser->lexer);
7282       switch (token->type)
7283         {
7284         case CPP_OPEN_SQUARE:
7285           /* offsetof-member-designator "[" expression "]" */
7286           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7287           break;
7288
7289         case CPP_DEREF:
7290           /* offsetof-member-designator "->" identifier */
7291           expr = grok_array_decl (expr, integer_zero_node);
7292           /* FALLTHRU */
7293
7294         case CPP_DOT:
7295           /* offsetof-member-designator "." identifier */
7296           cp_lexer_consume_token (parser->lexer);
7297           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7298                                                          expr, true, &dummy,
7299                                                          token->location);
7300           break;
7301
7302         case CPP_CLOSE_PAREN:
7303           /* Consume the ")" token.  */
7304           cp_lexer_consume_token (parser->lexer);
7305           goto success;
7306
7307         default:
7308           /* Error.  We know the following require will fail, but
7309              that gives the proper error message.  */
7310           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7311           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7312           expr = error_mark_node;
7313           goto failure;
7314         }
7315     }
7316
7317  success:
7318   /* If we're processing a template, we can't finish the semantics yet.
7319      Otherwise we can fold the entire expression now.  */
7320   if (processing_template_decl)
7321     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7322   else
7323     expr = finish_offsetof (expr);
7324
7325  failure:
7326   parser->integral_constant_expression_p = save_ice_p;
7327   parser->non_integral_constant_expression_p = save_non_ice_p;
7328
7329   return expr;
7330 }
7331
7332 /* Parse a trait expression.  */
7333
7334 static tree
7335 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7336 {
7337   cp_trait_kind kind;
7338   tree type1, type2 = NULL_TREE;
7339   bool binary = false;
7340   cp_decl_specifier_seq decl_specs;
7341
7342   switch (keyword)
7343     {
7344     case RID_HAS_NOTHROW_ASSIGN:
7345       kind = CPTK_HAS_NOTHROW_ASSIGN;
7346       break;
7347     case RID_HAS_NOTHROW_CONSTRUCTOR:
7348       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7349       break;
7350     case RID_HAS_NOTHROW_COPY:
7351       kind = CPTK_HAS_NOTHROW_COPY;
7352       break;
7353     case RID_HAS_TRIVIAL_ASSIGN:
7354       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7355       break;
7356     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7357       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7358       break;
7359     case RID_HAS_TRIVIAL_COPY:
7360       kind = CPTK_HAS_TRIVIAL_COPY;
7361       break;
7362     case RID_HAS_TRIVIAL_DESTRUCTOR:
7363       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7364       break;
7365     case RID_HAS_VIRTUAL_DESTRUCTOR:
7366       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7367       break;
7368     case RID_IS_ABSTRACT:
7369       kind = CPTK_IS_ABSTRACT;
7370       break;
7371     case RID_IS_BASE_OF:
7372       kind = CPTK_IS_BASE_OF;
7373       binary = true;
7374       break;
7375     case RID_IS_CLASS:
7376       kind = CPTK_IS_CLASS;
7377       break;
7378     case RID_IS_CONVERTIBLE_TO:
7379       kind = CPTK_IS_CONVERTIBLE_TO;
7380       binary = true;
7381       break;
7382     case RID_IS_EMPTY:
7383       kind = CPTK_IS_EMPTY;
7384       break;
7385     case RID_IS_ENUM:
7386       kind = CPTK_IS_ENUM;
7387       break;
7388     case RID_IS_POD:
7389       kind = CPTK_IS_POD;
7390       break;
7391     case RID_IS_POLYMORPHIC:
7392       kind = CPTK_IS_POLYMORPHIC;
7393       break;
7394     case RID_IS_STD_LAYOUT:
7395       kind = CPTK_IS_STD_LAYOUT;
7396       break;
7397     case RID_IS_TRIVIAL:
7398       kind = CPTK_IS_TRIVIAL;
7399       break;
7400     case RID_IS_UNION:
7401       kind = CPTK_IS_UNION;
7402       break;
7403     case RID_IS_LITERAL_TYPE:
7404       kind = CPTK_IS_LITERAL_TYPE;
7405       break;
7406     default:
7407       gcc_unreachable ();
7408     }
7409
7410   /* Consume the token.  */
7411   cp_lexer_consume_token (parser->lexer);
7412
7413   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7414
7415   type1 = cp_parser_type_id (parser);
7416
7417   if (type1 == error_mark_node)
7418     return error_mark_node;
7419
7420   /* Build a trivial decl-specifier-seq.  */
7421   clear_decl_specs (&decl_specs);
7422   decl_specs.type = type1;
7423
7424   /* Call grokdeclarator to figure out what type this is.  */
7425   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7426                           /*initialized=*/0, /*attrlist=*/NULL);
7427
7428   if (binary)
7429     {
7430       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7431  
7432       type2 = cp_parser_type_id (parser);
7433
7434       if (type2 == error_mark_node)
7435         return error_mark_node;
7436
7437       /* Build a trivial decl-specifier-seq.  */
7438       clear_decl_specs (&decl_specs);
7439       decl_specs.type = type2;
7440
7441       /* Call grokdeclarator to figure out what type this is.  */
7442       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7443                               /*initialized=*/0, /*attrlist=*/NULL);
7444     }
7445
7446   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7447
7448   /* Complete the trait expression, which may mean either processing
7449      the trait expr now or saving it for template instantiation.  */
7450   return finish_trait_expr (kind, type1, type2);
7451 }
7452
7453 /* Lambdas that appear in variable initializer or default argument scope
7454    get that in their mangling, so we need to record it.  We might as well
7455    use the count for function and namespace scopes as well.  */
7456 static GTY(()) tree lambda_scope;
7457 static GTY(()) int lambda_count;
7458 typedef struct GTY(()) tree_int
7459 {
7460   tree t;
7461   int i;
7462 } tree_int;
7463 DEF_VEC_O(tree_int);
7464 DEF_VEC_ALLOC_O(tree_int,gc);
7465 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7466
7467 static void
7468 start_lambda_scope (tree decl)
7469 {
7470   tree_int ti;
7471   gcc_assert (decl);
7472   /* Once we're inside a function, we ignore other scopes and just push
7473      the function again so that popping works properly.  */
7474   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7475     decl = current_function_decl;
7476   ti.t = lambda_scope;
7477   ti.i = lambda_count;
7478   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7479   if (lambda_scope != decl)
7480     {
7481       /* Don't reset the count if we're still in the same function.  */
7482       lambda_scope = decl;
7483       lambda_count = 0;
7484     }
7485 }
7486
7487 static void
7488 record_lambda_scope (tree lambda)
7489 {
7490   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7491   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7492 }
7493
7494 static void
7495 finish_lambda_scope (void)
7496 {
7497   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7498   if (lambda_scope != p->t)
7499     {
7500       lambda_scope = p->t;
7501       lambda_count = p->i;
7502     }
7503   VEC_pop (tree_int, lambda_scope_stack);
7504 }
7505
7506 /* Parse a lambda expression.
7507
7508    lambda-expression:
7509      lambda-introducer lambda-declarator [opt] compound-statement
7510
7511    Returns a representation of the expression.  */
7512
7513 static tree
7514 cp_parser_lambda_expression (cp_parser* parser)
7515 {
7516   tree lambda_expr = build_lambda_expr ();
7517   tree type;
7518
7519   LAMBDA_EXPR_LOCATION (lambda_expr)
7520     = cp_lexer_peek_token (parser->lexer)->location;
7521
7522   if (cp_unevaluated_operand)
7523     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7524               "lambda-expression in unevaluated context");
7525
7526   /* We may be in the middle of deferred access check.  Disable
7527      it now.  */
7528   push_deferring_access_checks (dk_no_deferred);
7529
7530   cp_parser_lambda_introducer (parser, lambda_expr);
7531
7532   type = begin_lambda_type (lambda_expr);
7533
7534   record_lambda_scope (lambda_expr);
7535
7536   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7537   determine_visibility (TYPE_NAME (type));
7538
7539   /* Now that we've started the type, add the capture fields for any
7540      explicit captures.  */
7541   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7542
7543   {
7544     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7545     unsigned int saved_num_template_parameter_lists
7546         = parser->num_template_parameter_lists;
7547
7548     parser->num_template_parameter_lists = 0;
7549
7550     /* By virtue of defining a local class, a lambda expression has access to
7551        the private variables of enclosing classes.  */
7552
7553     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7554
7555     cp_parser_lambda_body (parser, lambda_expr);
7556
7557     /* The capture list was built up in reverse order; fix that now.  */
7558     {
7559       tree newlist = NULL_TREE;
7560       tree elt, next;
7561
7562       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7563            elt; elt = next)
7564         {
7565           tree field = TREE_PURPOSE (elt);
7566           char *buf;
7567
7568           next = TREE_CHAIN (elt);
7569           TREE_CHAIN (elt) = newlist;
7570           newlist = elt;
7571
7572           /* Also add __ to the beginning of the field name so that code
7573              outside the lambda body can't see the captured name.  We could
7574              just remove the name entirely, but this is more useful for
7575              debugging.  */
7576           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7577             /* The 'this' capture already starts with __.  */
7578             continue;
7579
7580           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7581           buf[1] = buf[0] = '_';
7582           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7583                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7584           DECL_NAME (field) = get_identifier (buf);
7585         }
7586       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7587     }
7588
7589     maybe_add_lambda_conv_op (type);
7590
7591     type = finish_struct (type, /*attributes=*/NULL_TREE);
7592
7593     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7594   }
7595
7596   pop_deferring_access_checks ();
7597
7598   return build_lambda_object (lambda_expr);
7599 }
7600
7601 /* Parse the beginning of a lambda expression.
7602
7603    lambda-introducer:
7604      [ lambda-capture [opt] ]
7605
7606    LAMBDA_EXPR is the current representation of the lambda expression.  */
7607
7608 static void
7609 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7610 {
7611   /* Need commas after the first capture.  */
7612   bool first = true;
7613
7614   /* Eat the leading `['.  */
7615   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7616
7617   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7618   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7619       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7620     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7621   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7622     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7623
7624   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7625     {
7626       cp_lexer_consume_token (parser->lexer);
7627       first = false;
7628     }
7629
7630   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7631     {
7632       cp_token* capture_token;
7633       tree capture_id;
7634       tree capture_init_expr;
7635       cp_id_kind idk = CP_ID_KIND_NONE;
7636       bool explicit_init_p = false;
7637
7638       enum capture_kind_type
7639       {
7640         BY_COPY,
7641         BY_REFERENCE
7642       };
7643       enum capture_kind_type capture_kind = BY_COPY;
7644
7645       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7646         {
7647           error ("expected end of capture-list");
7648           return;
7649         }
7650
7651       if (first)
7652         first = false;
7653       else
7654         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7655
7656       /* Possibly capture `this'.  */
7657       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7658         {
7659           cp_lexer_consume_token (parser->lexer);
7660           add_capture (lambda_expr,
7661                        /*id=*/get_identifier ("__this"),
7662                        /*initializer=*/finish_this_expr(),
7663                        /*by_reference_p=*/false,
7664                        explicit_init_p);
7665           continue;
7666         }
7667
7668       /* Remember whether we want to capture as a reference or not.  */
7669       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7670         {
7671           capture_kind = BY_REFERENCE;
7672           cp_lexer_consume_token (parser->lexer);
7673         }
7674
7675       /* Get the identifier.  */
7676       capture_token = cp_lexer_peek_token (parser->lexer);
7677       capture_id = cp_parser_identifier (parser);
7678
7679       if (capture_id == error_mark_node)
7680         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7681            delimiters, but I modified this to stop on unnested ']' as well.  It
7682            was already changed to stop on unnested '}', so the
7683            "closing_parenthesis" name is no more misleading with my change.  */
7684         {
7685           cp_parser_skip_to_closing_parenthesis (parser,
7686                                                  /*recovering=*/true,
7687                                                  /*or_comma=*/true,
7688                                                  /*consume_paren=*/true);
7689           break;
7690         }
7691
7692       /* Find the initializer for this capture.  */
7693       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7694         {
7695           /* An explicit expression exists.  */
7696           cp_lexer_consume_token (parser->lexer);
7697           pedwarn (input_location, OPT_pedantic,
7698                    "ISO C++ does not allow initializers "
7699                    "in lambda expression capture lists");
7700           capture_init_expr = cp_parser_assignment_expression (parser,
7701                                                                /*cast_p=*/true,
7702                                                                &idk);
7703           explicit_init_p = true;
7704         }
7705       else
7706         {
7707           const char* error_msg;
7708
7709           /* Turn the identifier into an id-expression.  */
7710           capture_init_expr
7711             = cp_parser_lookup_name
7712                 (parser,
7713                  capture_id,
7714                  none_type,
7715                  /*is_template=*/false,
7716                  /*is_namespace=*/false,
7717                  /*check_dependency=*/true,
7718                  /*ambiguous_decls=*/NULL,
7719                  capture_token->location);
7720
7721           capture_init_expr
7722             = finish_id_expression
7723                 (capture_id,
7724                  capture_init_expr,
7725                  parser->scope,
7726                  &idk,
7727                  /*integral_constant_expression_p=*/false,
7728                  /*allow_non_integral_constant_expression_p=*/false,
7729                  /*non_integral_constant_expression_p=*/NULL,
7730                  /*template_p=*/false,
7731                  /*done=*/true,
7732                  /*address_p=*/false,
7733                  /*template_arg_p=*/false,
7734                  &error_msg,
7735                  capture_token->location);
7736         }
7737
7738       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7739         capture_init_expr
7740           = unqualified_name_lookup_error (capture_init_expr);
7741
7742       add_capture (lambda_expr,
7743                    capture_id,
7744                    capture_init_expr,
7745                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7746                    explicit_init_p);
7747     }
7748
7749   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7750 }
7751
7752 /* Parse the (optional) middle of a lambda expression.
7753
7754    lambda-declarator:
7755      ( parameter-declaration-clause [opt] )
7756        attribute-specifier [opt]
7757        mutable [opt]
7758        exception-specification [opt]
7759        lambda-return-type-clause [opt]
7760
7761    LAMBDA_EXPR is the current representation of the lambda expression.  */
7762
7763 static void
7764 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7765 {
7766   /* 5.1.1.4 of the standard says:
7767        If a lambda-expression does not include a lambda-declarator, it is as if
7768        the lambda-declarator were ().
7769      This means an empty parameter list, no attributes, and no exception
7770      specification.  */
7771   tree param_list = void_list_node;
7772   tree attributes = NULL_TREE;
7773   tree exception_spec = NULL_TREE;
7774   tree t;
7775
7776   /* The lambda-declarator is optional, but must begin with an opening
7777      parenthesis if present.  */
7778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7779     {
7780       cp_lexer_consume_token (parser->lexer);
7781
7782       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7783
7784       /* Parse parameters.  */
7785       param_list = cp_parser_parameter_declaration_clause (parser);
7786
7787       /* Default arguments shall not be specified in the
7788          parameter-declaration-clause of a lambda-declarator.  */
7789       for (t = param_list; t; t = TREE_CHAIN (t))
7790         if (TREE_PURPOSE (t))
7791           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7792                    "default argument specified for lambda parameter");
7793
7794       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7795
7796       attributes = cp_parser_attributes_opt (parser);
7797
7798       /* Parse optional `mutable' keyword.  */
7799       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7800         {
7801           cp_lexer_consume_token (parser->lexer);
7802           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7803         }
7804
7805       /* Parse optional exception specification.  */
7806       exception_spec = cp_parser_exception_specification_opt (parser);
7807
7808       /* Parse optional trailing return type.  */
7809       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7810         {
7811           cp_lexer_consume_token (parser->lexer);
7812           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7813         }
7814
7815       /* The function parameters must be in scope all the way until after the
7816          trailing-return-type in case of decltype.  */
7817       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7818         pop_binding (DECL_NAME (t), t);
7819
7820       leave_scope ();
7821     }
7822
7823   /* Create the function call operator.
7824
7825      Messing with declarators like this is no uglier than building up the
7826      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7827      other code.  */
7828   {
7829     cp_decl_specifier_seq return_type_specs;
7830     cp_declarator* declarator;
7831     tree fco;
7832     int quals;
7833     void *p;
7834
7835     clear_decl_specs (&return_type_specs);
7836     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7837       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7838     else
7839       /* Maybe we will deduce the return type later, but we can use void
7840          as a placeholder return type anyways.  */
7841       return_type_specs.type = void_type_node;
7842
7843     p = obstack_alloc (&declarator_obstack, 0);
7844
7845     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7846                                      sfk_none);
7847
7848     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7849              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7850     declarator = make_call_declarator (declarator, param_list, quals,
7851                                        exception_spec,
7852                                        /*late_return_type=*/NULL_TREE);
7853     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7854
7855     fco = grokmethod (&return_type_specs,
7856                       declarator,
7857                       attributes);
7858     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7859     DECL_ARTIFICIAL (fco) = 1;
7860
7861     finish_member_declaration (fco);
7862
7863     obstack_free (&declarator_obstack, p);
7864   }
7865 }
7866
7867 /* Parse the body of a lambda expression, which is simply
7868
7869    compound-statement
7870
7871    but which requires special handling.
7872    LAMBDA_EXPR is the current representation of the lambda expression.  */
7873
7874 static void
7875 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7876 {
7877   bool nested = (current_function_decl != NULL_TREE);
7878   if (nested)
7879     push_function_context ();
7880
7881   /* Finish the function call operator
7882      - class_specifier
7883      + late_parsing_for_member
7884      + function_definition_after_declarator
7885      + ctor_initializer_opt_and_function_body  */
7886   {
7887     tree fco = lambda_function (lambda_expr);
7888     tree body;
7889     bool done = false;
7890
7891     /* Let the front end know that we are going to be defining this
7892        function.  */
7893     start_preparsed_function (fco,
7894                               NULL_TREE,
7895                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7896
7897     start_lambda_scope (fco);
7898     body = begin_function_body ();
7899
7900     /* 5.1.1.4 of the standard says:
7901          If a lambda-expression does not include a trailing-return-type, it
7902          is as if the trailing-return-type denotes the following type:
7903           * if the compound-statement is of the form
7904                { return attribute-specifier [opt] expression ; }
7905              the type of the returned expression after lvalue-to-rvalue
7906              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7907              (_conv.array_ 4.2), and function-to-pointer conversion
7908              (_conv.func_ 4.3);
7909           * otherwise, void.  */
7910
7911     /* In a lambda that has neither a lambda-return-type-clause
7912        nor a deducible form, errors should be reported for return statements
7913        in the body.  Since we used void as the placeholder return type, parsing
7914        the body as usual will give such desired behavior.  */
7915     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7916         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7917         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7918         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7919       {
7920         tree compound_stmt;
7921         tree expr = NULL_TREE;
7922         cp_id_kind idk = CP_ID_KIND_NONE;
7923
7924         /* Parse tentatively in case there's more after the initial return
7925            statement.  */
7926         cp_parser_parse_tentatively (parser);
7927
7928         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7929         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7930
7931         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7932
7933         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7934         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7935
7936         if (cp_parser_parse_definitely (parser))
7937           {
7938             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7939
7940             compound_stmt = begin_compound_stmt (0);
7941             /* Will get error here if type not deduced yet.  */
7942             finish_return_stmt (expr);
7943             finish_compound_stmt (compound_stmt);
7944
7945             done = true;
7946           }
7947       }
7948
7949     if (!done)
7950       {
7951         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7952           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7953         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7954            cp_parser_compound_stmt does not pass it.  */
7955         cp_parser_function_body (parser);
7956         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7957       }
7958
7959     finish_function_body (body);
7960     finish_lambda_scope ();
7961
7962     /* Finish the function and generate code for it if necessary.  */
7963     expand_or_defer_fn (finish_function (/*inline*/2));
7964   }
7965
7966   if (nested)
7967     pop_function_context();
7968 }
7969
7970 /* Statements [gram.stmt.stmt]  */
7971
7972 /* Parse a statement.
7973
7974    statement:
7975      labeled-statement
7976      expression-statement
7977      compound-statement
7978      selection-statement
7979      iteration-statement
7980      jump-statement
7981      declaration-statement
7982      try-block
7983
7984   IN_COMPOUND is true when the statement is nested inside a
7985   cp_parser_compound_statement; this matters for certain pragmas.
7986
7987   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7988   is a (possibly labeled) if statement which is not enclosed in braces
7989   and has an else clause.  This is used to implement -Wparentheses.  */
7990
7991 static void
7992 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7993                      bool in_compound, bool *if_p)
7994 {
7995   tree statement;
7996   cp_token *token;
7997   location_t statement_location;
7998
7999  restart:
8000   if (if_p != NULL)
8001     *if_p = false;
8002   /* There is no statement yet.  */
8003   statement = NULL_TREE;
8004   /* Peek at the next token.  */
8005   token = cp_lexer_peek_token (parser->lexer);
8006   /* Remember the location of the first token in the statement.  */
8007   statement_location = token->location;
8008   /* If this is a keyword, then that will often determine what kind of
8009      statement we have.  */
8010   if (token->type == CPP_KEYWORD)
8011     {
8012       enum rid keyword = token->keyword;
8013
8014       switch (keyword)
8015         {
8016         case RID_CASE:
8017         case RID_DEFAULT:
8018           /* Looks like a labeled-statement with a case label.
8019              Parse the label, and then use tail recursion to parse
8020              the statement.  */
8021           cp_parser_label_for_labeled_statement (parser);
8022           goto restart;
8023
8024         case RID_IF:
8025         case RID_SWITCH:
8026           statement = cp_parser_selection_statement (parser, if_p);
8027           break;
8028
8029         case RID_WHILE:
8030         case RID_DO:
8031         case RID_FOR:
8032           statement = cp_parser_iteration_statement (parser);
8033           break;
8034
8035         case RID_BREAK:
8036         case RID_CONTINUE:
8037         case RID_RETURN:
8038         case RID_GOTO:
8039           statement = cp_parser_jump_statement (parser);
8040           break;
8041
8042           /* Objective-C++ exception-handling constructs.  */
8043         case RID_AT_TRY:
8044         case RID_AT_CATCH:
8045         case RID_AT_FINALLY:
8046         case RID_AT_SYNCHRONIZED:
8047         case RID_AT_THROW:
8048           statement = cp_parser_objc_statement (parser);
8049           break;
8050
8051         case RID_TRY:
8052           statement = cp_parser_try_block (parser);
8053           break;
8054
8055         case RID_NAMESPACE:
8056           /* This must be a namespace alias definition.  */
8057           cp_parser_declaration_statement (parser);
8058           return;
8059           
8060         default:
8061           /* It might be a keyword like `int' that can start a
8062              declaration-statement.  */
8063           break;
8064         }
8065     }
8066   else if (token->type == CPP_NAME)
8067     {
8068       /* If the next token is a `:', then we are looking at a
8069          labeled-statement.  */
8070       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8071       if (token->type == CPP_COLON)
8072         {
8073           /* Looks like a labeled-statement with an ordinary label.
8074              Parse the label, and then use tail recursion to parse
8075              the statement.  */
8076           cp_parser_label_for_labeled_statement (parser);
8077           goto restart;
8078         }
8079     }
8080   /* Anything that starts with a `{' must be a compound-statement.  */
8081   else if (token->type == CPP_OPEN_BRACE)
8082     statement = cp_parser_compound_statement (parser, NULL, false);
8083   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8084      a statement all its own.  */
8085   else if (token->type == CPP_PRAGMA)
8086     {
8087       /* Only certain OpenMP pragmas are attached to statements, and thus
8088          are considered statements themselves.  All others are not.  In
8089          the context of a compound, accept the pragma as a "statement" and
8090          return so that we can check for a close brace.  Otherwise we
8091          require a real statement and must go back and read one.  */
8092       if (in_compound)
8093         cp_parser_pragma (parser, pragma_compound);
8094       else if (!cp_parser_pragma (parser, pragma_stmt))
8095         goto restart;
8096       return;
8097     }
8098   else if (token->type == CPP_EOF)
8099     {
8100       cp_parser_error (parser, "expected statement");
8101       return;
8102     }
8103
8104   /* Everything else must be a declaration-statement or an
8105      expression-statement.  Try for the declaration-statement
8106      first, unless we are looking at a `;', in which case we know that
8107      we have an expression-statement.  */
8108   if (!statement)
8109     {
8110       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8111         {
8112           cp_parser_parse_tentatively (parser);
8113           /* Try to parse the declaration-statement.  */
8114           cp_parser_declaration_statement (parser);
8115           /* If that worked, we're done.  */
8116           if (cp_parser_parse_definitely (parser))
8117             return;
8118         }
8119       /* Look for an expression-statement instead.  */
8120       statement = cp_parser_expression_statement (parser, in_statement_expr);
8121     }
8122
8123   /* Set the line number for the statement.  */
8124   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8125     SET_EXPR_LOCATION (statement, statement_location);
8126 }
8127
8128 /* Parse the label for a labeled-statement, i.e.
8129
8130    identifier :
8131    case constant-expression :
8132    default :
8133
8134    GNU Extension:
8135    case constant-expression ... constant-expression : statement
8136
8137    When a label is parsed without errors, the label is added to the
8138    parse tree by the finish_* functions, so this function doesn't
8139    have to return the label.  */
8140
8141 static void
8142 cp_parser_label_for_labeled_statement (cp_parser* parser)
8143 {
8144   cp_token *token;
8145   tree label = NULL_TREE;
8146
8147   /* The next token should be an identifier.  */
8148   token = cp_lexer_peek_token (parser->lexer);
8149   if (token->type != CPP_NAME
8150       && token->type != CPP_KEYWORD)
8151     {
8152       cp_parser_error (parser, "expected labeled-statement");
8153       return;
8154     }
8155
8156   switch (token->keyword)
8157     {
8158     case RID_CASE:
8159       {
8160         tree expr, expr_hi;
8161         cp_token *ellipsis;
8162
8163         /* Consume the `case' token.  */
8164         cp_lexer_consume_token (parser->lexer);
8165         /* Parse the constant-expression.  */
8166         expr = cp_parser_constant_expression (parser,
8167                                               /*allow_non_constant_p=*/false,
8168                                               NULL);
8169
8170         ellipsis = cp_lexer_peek_token (parser->lexer);
8171         if (ellipsis->type == CPP_ELLIPSIS)
8172           {
8173             /* Consume the `...' token.  */
8174             cp_lexer_consume_token (parser->lexer);
8175             expr_hi =
8176               cp_parser_constant_expression (parser,
8177                                              /*allow_non_constant_p=*/false,
8178                                              NULL);
8179             /* We don't need to emit warnings here, as the common code
8180                will do this for us.  */
8181           }
8182         else
8183           expr_hi = NULL_TREE;
8184
8185         if (parser->in_switch_statement_p)
8186           finish_case_label (token->location, expr, expr_hi);
8187         else
8188           error_at (token->location,
8189                     "case label %qE not within a switch statement",
8190                     expr);
8191       }
8192       break;
8193
8194     case RID_DEFAULT:
8195       /* Consume the `default' token.  */
8196       cp_lexer_consume_token (parser->lexer);
8197
8198       if (parser->in_switch_statement_p)
8199         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8200       else
8201         error_at (token->location, "case label not within a switch statement");
8202       break;
8203
8204     default:
8205       /* Anything else must be an ordinary label.  */
8206       label = finish_label_stmt (cp_parser_identifier (parser));
8207       break;
8208     }
8209
8210   /* Require the `:' token.  */
8211   cp_parser_require (parser, CPP_COLON, RT_COLON);
8212
8213   /* An ordinary label may optionally be followed by attributes.
8214      However, this is only permitted if the attributes are then
8215      followed by a semicolon.  This is because, for backward
8216      compatibility, when parsing
8217        lab: __attribute__ ((unused)) int i;
8218      we want the attribute to attach to "i", not "lab".  */
8219   if (label != NULL_TREE
8220       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8221     {
8222       tree attrs;
8223
8224       cp_parser_parse_tentatively (parser);
8225       attrs = cp_parser_attributes_opt (parser);
8226       if (attrs == NULL_TREE
8227           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8228         cp_parser_abort_tentative_parse (parser);
8229       else if (!cp_parser_parse_definitely (parser))
8230         ;
8231       else
8232         cplus_decl_attributes (&label, attrs, 0);
8233     }
8234 }
8235
8236 /* Parse an expression-statement.
8237
8238    expression-statement:
8239      expression [opt] ;
8240
8241    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8242    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8243    indicates whether this expression-statement is part of an
8244    expression statement.  */
8245
8246 static tree
8247 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8248 {
8249   tree statement = NULL_TREE;
8250   cp_token *token = cp_lexer_peek_token (parser->lexer);
8251
8252   /* If the next token is a ';', then there is no expression
8253      statement.  */
8254   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8255     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8256
8257   /* Give a helpful message for "A<T>::type t;" and the like.  */
8258   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8259       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8260     {
8261       if (TREE_CODE (statement) == SCOPE_REF)
8262         error_at (token->location, "need %<typename%> before %qE because "
8263                   "%qT is a dependent scope",
8264                   statement, TREE_OPERAND (statement, 0));
8265       else if (is_overloaded_fn (statement)
8266                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8267         {
8268           /* A::A a; */
8269           tree fn = get_first_fn (statement);
8270           error_at (token->location,
8271                     "%<%T::%D%> names the constructor, not the type",
8272                     DECL_CONTEXT (fn), DECL_NAME (fn));
8273         }
8274     }
8275
8276   /* Consume the final `;'.  */
8277   cp_parser_consume_semicolon_at_end_of_statement (parser);
8278
8279   if (in_statement_expr
8280       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8281     /* This is the final expression statement of a statement
8282        expression.  */
8283     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8284   else if (statement)
8285     statement = finish_expr_stmt (statement);
8286   else
8287     finish_stmt ();
8288
8289   return statement;
8290 }
8291
8292 /* Parse a compound-statement.
8293
8294    compound-statement:
8295      { statement-seq [opt] }
8296
8297    GNU extension:
8298
8299    compound-statement:
8300      { label-declaration-seq [opt] statement-seq [opt] }
8301
8302    label-declaration-seq:
8303      label-declaration
8304      label-declaration-seq label-declaration
8305
8306    Returns a tree representing the statement.  */
8307
8308 static tree
8309 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8310                               bool in_try)
8311 {
8312   tree compound_stmt;
8313
8314   /* Consume the `{'.  */
8315   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8316     return error_mark_node;
8317   /* Begin the compound-statement.  */
8318   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8319   /* If the next keyword is `__label__' we have a label declaration.  */
8320   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8321     cp_parser_label_declaration (parser);
8322   /* Parse an (optional) statement-seq.  */
8323   cp_parser_statement_seq_opt (parser, in_statement_expr);
8324   /* Finish the compound-statement.  */
8325   finish_compound_stmt (compound_stmt);
8326   /* Consume the `}'.  */
8327   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8328
8329   return compound_stmt;
8330 }
8331
8332 /* Parse an (optional) statement-seq.
8333
8334    statement-seq:
8335      statement
8336      statement-seq [opt] statement  */
8337
8338 static void
8339 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8340 {
8341   /* Scan statements until there aren't any more.  */
8342   while (true)
8343     {
8344       cp_token *token = cp_lexer_peek_token (parser->lexer);
8345
8346       /* If we are looking at a `}', then we have run out of
8347          statements; the same is true if we have reached the end
8348          of file, or have stumbled upon a stray '@end'.  */
8349       if (token->type == CPP_CLOSE_BRACE
8350           || token->type == CPP_EOF
8351           || token->type == CPP_PRAGMA_EOL
8352           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8353         break;
8354       
8355       /* If we are in a compound statement and find 'else' then
8356          something went wrong.  */
8357       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8358         {
8359           if (parser->in_statement & IN_IF_STMT) 
8360             break;
8361           else
8362             {
8363               token = cp_lexer_consume_token (parser->lexer);
8364               error_at (token->location, "%<else%> without a previous %<if%>");
8365             }
8366         }
8367
8368       /* Parse the statement.  */
8369       cp_parser_statement (parser, in_statement_expr, true, NULL);
8370     }
8371 }
8372
8373 /* Parse a selection-statement.
8374
8375    selection-statement:
8376      if ( condition ) statement
8377      if ( condition ) statement else statement
8378      switch ( condition ) statement
8379
8380    Returns the new IF_STMT or SWITCH_STMT.
8381
8382    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8383    is a (possibly labeled) if statement which is not enclosed in
8384    braces and has an else clause.  This is used to implement
8385    -Wparentheses.  */
8386
8387 static tree
8388 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8389 {
8390   cp_token *token;
8391   enum rid keyword;
8392
8393   if (if_p != NULL)
8394     *if_p = false;
8395
8396   /* Peek at the next token.  */
8397   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8398
8399   /* See what kind of keyword it is.  */
8400   keyword = token->keyword;
8401   switch (keyword)
8402     {
8403     case RID_IF:
8404     case RID_SWITCH:
8405       {
8406         tree statement;
8407         tree condition;
8408
8409         /* Look for the `('.  */
8410         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8411           {
8412             cp_parser_skip_to_end_of_statement (parser);
8413             return error_mark_node;
8414           }
8415
8416         /* Begin the selection-statement.  */
8417         if (keyword == RID_IF)
8418           statement = begin_if_stmt ();
8419         else
8420           statement = begin_switch_stmt ();
8421
8422         /* Parse the condition.  */
8423         condition = cp_parser_condition (parser);
8424         /* Look for the `)'.  */
8425         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8426           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8427                                                  /*consume_paren=*/true);
8428
8429         if (keyword == RID_IF)
8430           {
8431             bool nested_if;
8432             unsigned char in_statement;
8433
8434             /* Add the condition.  */
8435             finish_if_stmt_cond (condition, statement);
8436
8437             /* Parse the then-clause.  */
8438             in_statement = parser->in_statement;
8439             parser->in_statement |= IN_IF_STMT;
8440             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8441               {
8442                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8443                 add_stmt (build_empty_stmt (loc));
8444                 cp_lexer_consume_token (parser->lexer);
8445                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8446                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8447                               "empty body in an %<if%> statement");
8448                 nested_if = false;
8449               }
8450             else
8451               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8452             parser->in_statement = in_statement;
8453
8454             finish_then_clause (statement);
8455
8456             /* If the next token is `else', parse the else-clause.  */
8457             if (cp_lexer_next_token_is_keyword (parser->lexer,
8458                                                 RID_ELSE))
8459               {
8460                 /* Consume the `else' keyword.  */
8461                 cp_lexer_consume_token (parser->lexer);
8462                 begin_else_clause (statement);
8463                 /* Parse the else-clause.  */
8464                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8465                   {
8466                     location_t loc;
8467                     loc = cp_lexer_peek_token (parser->lexer)->location;
8468                     warning_at (loc,
8469                                 OPT_Wempty_body, "suggest braces around "
8470                                 "empty body in an %<else%> statement");
8471                     add_stmt (build_empty_stmt (loc));
8472                     cp_lexer_consume_token (parser->lexer);
8473                   }
8474                 else
8475                   cp_parser_implicitly_scoped_statement (parser, NULL);
8476
8477                 finish_else_clause (statement);
8478
8479                 /* If we are currently parsing a then-clause, then
8480                    IF_P will not be NULL.  We set it to true to
8481                    indicate that this if statement has an else clause.
8482                    This may trigger the Wparentheses warning below
8483                    when we get back up to the parent if statement.  */
8484                 if (if_p != NULL)
8485                   *if_p = true;
8486               }
8487             else
8488               {
8489                 /* This if statement does not have an else clause.  If
8490                    NESTED_IF is true, then the then-clause is an if
8491                    statement which does have an else clause.  We warn
8492                    about the potential ambiguity.  */
8493                 if (nested_if)
8494                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8495                               "suggest explicit braces to avoid ambiguous"
8496                               " %<else%>");
8497               }
8498
8499             /* Now we're all done with the if-statement.  */
8500             finish_if_stmt (statement);
8501           }
8502         else
8503           {
8504             bool in_switch_statement_p;
8505             unsigned char in_statement;
8506
8507             /* Add the condition.  */
8508             finish_switch_cond (condition, statement);
8509
8510             /* Parse the body of the switch-statement.  */
8511             in_switch_statement_p = parser->in_switch_statement_p;
8512             in_statement = parser->in_statement;
8513             parser->in_switch_statement_p = true;
8514             parser->in_statement |= IN_SWITCH_STMT;
8515             cp_parser_implicitly_scoped_statement (parser, NULL);
8516             parser->in_switch_statement_p = in_switch_statement_p;
8517             parser->in_statement = in_statement;
8518
8519             /* Now we're all done with the switch-statement.  */
8520             finish_switch_stmt (statement);
8521           }
8522
8523         return statement;
8524       }
8525       break;
8526
8527     default:
8528       cp_parser_error (parser, "expected selection-statement");
8529       return error_mark_node;
8530     }
8531 }
8532
8533 /* Parse a condition.
8534
8535    condition:
8536      expression
8537      type-specifier-seq declarator = initializer-clause
8538      type-specifier-seq declarator braced-init-list
8539
8540    GNU Extension:
8541
8542    condition:
8543      type-specifier-seq declarator asm-specification [opt]
8544        attributes [opt] = assignment-expression
8545
8546    Returns the expression that should be tested.  */
8547
8548 static tree
8549 cp_parser_condition (cp_parser* parser)
8550 {
8551   cp_decl_specifier_seq type_specifiers;
8552   const char *saved_message;
8553   int declares_class_or_enum;
8554
8555   /* Try the declaration first.  */
8556   cp_parser_parse_tentatively (parser);
8557   /* New types are not allowed in the type-specifier-seq for a
8558      condition.  */
8559   saved_message = parser->type_definition_forbidden_message;
8560   parser->type_definition_forbidden_message
8561     = G_("types may not be defined in conditions");
8562   /* Parse the type-specifier-seq.  */
8563   cp_parser_decl_specifier_seq (parser,
8564                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8565                                 &type_specifiers,
8566                                 &declares_class_or_enum);
8567   /* Restore the saved message.  */
8568   parser->type_definition_forbidden_message = saved_message;
8569   /* If all is well, we might be looking at a declaration.  */
8570   if (!cp_parser_error_occurred (parser))
8571     {
8572       tree decl;
8573       tree asm_specification;
8574       tree attributes;
8575       cp_declarator *declarator;
8576       tree initializer = NULL_TREE;
8577
8578       /* Parse the declarator.  */
8579       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8580                                          /*ctor_dtor_or_conv_p=*/NULL,
8581                                          /*parenthesized_p=*/NULL,
8582                                          /*member_p=*/false);
8583       /* Parse the attributes.  */
8584       attributes = cp_parser_attributes_opt (parser);
8585       /* Parse the asm-specification.  */
8586       asm_specification = cp_parser_asm_specification_opt (parser);
8587       /* If the next token is not an `=' or '{', then we might still be
8588          looking at an expression.  For example:
8589
8590            if (A(a).x)
8591
8592          looks like a decl-specifier-seq and a declarator -- but then
8593          there is no `=', so this is an expression.  */
8594       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8595           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8596         cp_parser_simulate_error (parser);
8597         
8598       /* If we did see an `=' or '{', then we are looking at a declaration
8599          for sure.  */
8600       if (cp_parser_parse_definitely (parser))
8601         {
8602           tree pushed_scope;
8603           bool non_constant_p;
8604           bool flags = LOOKUP_ONLYCONVERTING;
8605
8606           /* Create the declaration.  */
8607           decl = start_decl (declarator, &type_specifiers,
8608                              /*initialized_p=*/true,
8609                              attributes, /*prefix_attributes=*/NULL_TREE,
8610                              &pushed_scope);
8611
8612           /* Parse the initializer.  */
8613           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8614             {
8615               initializer = cp_parser_braced_list (parser, &non_constant_p);
8616               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8617               flags = 0;
8618             }
8619           else
8620             {
8621               /* Consume the `='.  */
8622               cp_parser_require (parser, CPP_EQ, RT_EQ);
8623               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8624             }
8625           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8626             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8627
8628           if (!non_constant_p)
8629             initializer = fold_non_dependent_expr (initializer);
8630
8631           /* Process the initializer.  */
8632           cp_finish_decl (decl,
8633                           initializer, !non_constant_p,
8634                           asm_specification,
8635                           flags);
8636
8637           if (pushed_scope)
8638             pop_scope (pushed_scope);
8639
8640           return convert_from_reference (decl);
8641         }
8642     }
8643   /* If we didn't even get past the declarator successfully, we are
8644      definitely not looking at a declaration.  */
8645   else
8646     cp_parser_abort_tentative_parse (parser);
8647
8648   /* Otherwise, we are looking at an expression.  */
8649   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8650 }
8651
8652 /* Parses a traditional for-statement until the closing ')', not included. */
8653
8654 static tree
8655 cp_parser_c_for (cp_parser *parser)
8656 {
8657   /* Normal for loop */
8658   tree stmt;
8659   tree condition = NULL_TREE;
8660   tree expression = NULL_TREE;
8661
8662   /* Begin the for-statement.  */
8663   stmt = begin_for_stmt ();
8664
8665   /* Parse the initialization.  */
8666   cp_parser_for_init_statement (parser);
8667   finish_for_init_stmt (stmt);
8668
8669   /* If there's a condition, process it.  */
8670   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8671     condition = cp_parser_condition (parser);
8672   finish_for_cond (condition, stmt);
8673   /* Look for the `;'.  */
8674   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8675
8676   /* If there's an expression, process it.  */
8677   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8678     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8679   finish_for_expr (expression, stmt);
8680
8681   return stmt;
8682 }
8683
8684 /* Tries to parse a range-based for-statement:
8685
8686   range-based-for:
8687     type-specifier-seq declarator : expression
8688
8689   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8690   expression. Note that the *DECL is returned unfinished, so
8691   later you should call cp_finish_decl().
8692
8693   Returns TRUE iff a range-based for is parsed. */
8694
8695 static tree
8696 cp_parser_range_for (cp_parser *parser)
8697 {
8698   tree stmt, range_decl, range_expr;
8699   cp_decl_specifier_seq type_specifiers;
8700   cp_declarator *declarator;
8701   const char *saved_message;
8702   tree attributes, pushed_scope;
8703
8704   cp_parser_parse_tentatively (parser);
8705   /* New types are not allowed in the type-specifier-seq for a
8706      range-based for loop.  */
8707   saved_message = parser->type_definition_forbidden_message;
8708   parser->type_definition_forbidden_message
8709     = G_("types may not be defined in range-based for loops");
8710   /* Parse the type-specifier-seq.  */
8711   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8712                                 /*is_trailing_return=*/false,
8713                                 &type_specifiers);
8714   /* Restore the saved message.  */
8715   parser->type_definition_forbidden_message = saved_message;
8716   /* If all is well, we might be looking at a declaration.  */
8717   if (cp_parser_error_occurred (parser))
8718     {
8719       cp_parser_abort_tentative_parse (parser);
8720       return NULL_TREE;
8721     }
8722   /* Parse the declarator.  */
8723   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8724                                      /*ctor_dtor_or_conv_p=*/NULL,
8725                                      /*parenthesized_p=*/NULL,
8726                                      /*member_p=*/false);
8727   /* Parse the attributes.  */
8728   attributes = cp_parser_attributes_opt (parser);
8729   /* The next token should be `:'. */
8730   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8731     cp_parser_simulate_error (parser);
8732
8733   /* Check if it is a range-based for */
8734   if (!cp_parser_parse_definitely (parser))
8735     return NULL_TREE;
8736
8737   cp_parser_require (parser, CPP_COLON, RT_COLON);
8738   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8739     {
8740       bool expr_non_constant_p;
8741       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8742     }
8743   else
8744     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8745
8746   /* If in template, STMT is converted to a normal for-statements
8747      at instantiation. If not, it is done just ahead. */
8748   if (processing_template_decl)
8749     stmt = begin_range_for_stmt ();
8750   else
8751     stmt = begin_for_stmt ();
8752
8753   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8754   range_decl = start_decl (declarator, &type_specifiers,
8755                            /*initialized_p=*/SD_INITIALIZED,
8756                            attributes, /*prefix_attributes=*/NULL_TREE,
8757                            &pushed_scope);
8758   /* No scope allowed here */
8759   pop_scope (pushed_scope);
8760
8761   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8762     finish_range_for_decl (stmt, range_decl, range_expr);
8763   else
8764     /* Convert the range-based for loop into a normal for-statement. */
8765     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8766
8767   return stmt;
8768 }
8769
8770 /* Converts a range-based for-statement into a normal
8771    for-statement, as per the definition.
8772
8773       for (RANGE_DECL : RANGE_EXPR)
8774         BLOCK
8775
8776    should be equivalent to:
8777
8778       {
8779         auto &&__range = RANGE_EXPR;
8780         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8781               __begin != __end;
8782               ++__begin)
8783           {
8784               RANGE_DECL = *__begin;
8785               BLOCK
8786           }
8787       }
8788
8789    If RANGE_EXPR is an array:
8790        BEGIN_EXPR = __range
8791        END_EXPR = __range + ARRAY_SIZE(__range)
8792    Else:
8793         BEGIN_EXPR = begin(__range)
8794         END_EXPR = end(__range);
8795
8796    When calling begin()/end() we must use argument dependent
8797    lookup, but always considering 'std' as an associated namespace.  */
8798
8799 tree
8800 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8801 {
8802   tree range_type, range_temp;
8803   tree begin, end;
8804   tree iter_type, begin_expr, end_expr;
8805   tree condition, expression;
8806
8807   /* Find out the type deduced by the declaration
8808    * `auto &&__range = range_expr' */
8809   range_type = cp_build_reference_type (make_auto (), true);
8810   range_type = do_auto_deduction (range_type, range_expr,
8811                                   type_uses_auto (range_type));
8812
8813   /* Create the __range variable */
8814   range_temp = build_decl (input_location, VAR_DECL,
8815                            get_identifier ("__for_range"), range_type);
8816   TREE_USED (range_temp) = 1;
8817   DECL_ARTIFICIAL (range_temp) = 1;
8818   pushdecl (range_temp);
8819   cp_finish_decl (range_temp, range_expr,
8820                   /*is_constant_init*/false, NULL_TREE,
8821                   LOOKUP_ONLYCONVERTING);
8822
8823   range_temp = convert_from_reference (range_temp);
8824
8825   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8826     {
8827       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8828       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8829       begin_expr = range_temp;
8830       end_expr
8831         = build_binary_op (input_location, PLUS_EXPR,
8832                            range_temp,
8833                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8834     }
8835   else
8836     {
8837       /* If it is not an array, we must call begin(__range)/end__range() */
8838       VEC(tree,gc) *vec;
8839
8840       begin_expr = get_identifier ("begin");
8841       vec = make_tree_vector ();
8842       VEC_safe_push (tree, gc, vec, range_temp);
8843       begin_expr = perform_koenig_lookup (begin_expr, vec,
8844                                           /*include_std=*/true);
8845       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8846                                      tf_warning_or_error);
8847       release_tree_vector (vec);
8848
8849       end_expr = get_identifier ("end");
8850       vec = make_tree_vector ();
8851       VEC_safe_push (tree, gc, vec, range_temp);
8852       end_expr = perform_koenig_lookup (end_expr, vec,
8853                                         /*include_std=*/true);
8854       end_expr = finish_call_expr (end_expr, &vec, false, true,
8855                                    tf_warning_or_error);
8856       release_tree_vector (vec);
8857
8858       /* The unqualified type of the __begin and __end temporaries should
8859       * be the same as required by the multiple auto declaration */
8860       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8861       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8862         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8863                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8864     }
8865
8866   /* The new for initialization statement */
8867   begin = build_decl (input_location, VAR_DECL,
8868                       get_identifier ("__for_begin"), iter_type);
8869   TREE_USED (begin) = 1;
8870   DECL_ARTIFICIAL (begin) = 1;
8871   pushdecl (begin);
8872   cp_finish_decl (begin, begin_expr,
8873                   /*is_constant_init*/false, NULL_TREE,
8874                   LOOKUP_ONLYCONVERTING);
8875
8876   end = build_decl (input_location, VAR_DECL,
8877                     get_identifier ("__for_end"), iter_type);
8878   TREE_USED (end) = 1;
8879   DECL_ARTIFICIAL (end) = 1;
8880   pushdecl (end);
8881   cp_finish_decl (end, end_expr,
8882                   /*is_constant_init*/false, NULL_TREE,
8883                   LOOKUP_ONLYCONVERTING);
8884
8885   finish_for_init_stmt (statement);
8886
8887 /* The new for condition */
8888   condition = build_x_binary_op (NE_EXPR,
8889                                  begin, ERROR_MARK,
8890                                  end, ERROR_MARK,
8891                                  NULL, tf_warning_or_error);
8892   finish_for_cond (condition, statement);
8893
8894   /* The new increment expression */
8895   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8896   finish_for_expr (expression, statement);
8897
8898   /* The declaration is initialized with *__begin inside the loop body */
8899   cp_finish_decl (range_decl,
8900                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8901                   /*is_constant_init*/false, NULL_TREE,
8902                   LOOKUP_ONLYCONVERTING);
8903
8904   return statement;
8905 }
8906
8907
8908 /* Parse an iteration-statement.
8909
8910    iteration-statement:
8911      while ( condition ) statement
8912      do statement while ( expression ) ;
8913      for ( for-init-statement condition [opt] ; expression [opt] )
8914        statement
8915
8916    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8917
8918 static tree
8919 cp_parser_iteration_statement (cp_parser* parser)
8920 {
8921   cp_token *token;
8922   enum rid keyword;
8923   tree statement;
8924   unsigned char in_statement;
8925
8926   /* Peek at the next token.  */
8927   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8928   if (!token)
8929     return error_mark_node;
8930
8931   /* Remember whether or not we are already within an iteration
8932      statement.  */
8933   in_statement = parser->in_statement;
8934
8935   /* See what kind of keyword it is.  */
8936   keyword = token->keyword;
8937   switch (keyword)
8938     {
8939     case RID_WHILE:
8940       {
8941         tree condition;
8942
8943         /* Begin the while-statement.  */
8944         statement = begin_while_stmt ();
8945         /* Look for the `('.  */
8946         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8947         /* Parse the condition.  */
8948         condition = cp_parser_condition (parser);
8949         finish_while_stmt_cond (condition, statement);
8950         /* Look for the `)'.  */
8951         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8952         /* Parse the dependent statement.  */
8953         parser->in_statement = IN_ITERATION_STMT;
8954         cp_parser_already_scoped_statement (parser);
8955         parser->in_statement = in_statement;
8956         /* We're done with the while-statement.  */
8957         finish_while_stmt (statement);
8958       }
8959       break;
8960
8961     case RID_DO:
8962       {
8963         tree expression;
8964
8965         /* Begin the do-statement.  */
8966         statement = begin_do_stmt ();
8967         /* Parse the body of the do-statement.  */
8968         parser->in_statement = IN_ITERATION_STMT;
8969         cp_parser_implicitly_scoped_statement (parser, NULL);
8970         parser->in_statement = in_statement;
8971         finish_do_body (statement);
8972         /* Look for the `while' keyword.  */
8973         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8974         /* Look for the `('.  */
8975         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8976         /* Parse the expression.  */
8977         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8978         /* We're done with the do-statement.  */
8979         finish_do_stmt (expression, statement);
8980         /* Look for the `)'.  */
8981         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8982         /* Look for the `;'.  */
8983         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8984       }
8985       break;
8986
8987     case RID_FOR:
8988       {
8989         /* Look for the `('.  */
8990         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8991
8992         if (cxx_dialect == cxx0x)
8993           statement = cp_parser_range_for (parser);
8994         else
8995           statement = NULL_TREE;
8996         if (statement == NULL_TREE)
8997           statement = cp_parser_c_for (parser);
8998
8999         /* Look for the `)'.  */
9000         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9001
9002         /* Parse the body of the for-statement.  */
9003         parser->in_statement = IN_ITERATION_STMT;
9004         cp_parser_already_scoped_statement (parser);
9005         parser->in_statement = in_statement;
9006
9007         /* We're done with the for-statement.  */
9008         finish_for_stmt (statement);
9009       }
9010       break;
9011
9012     default:
9013       cp_parser_error (parser, "expected iteration-statement");
9014       statement = error_mark_node;
9015       break;
9016     }
9017
9018   return statement;
9019 }
9020
9021 /* Parse a for-init-statement.
9022
9023    for-init-statement:
9024      expression-statement
9025      simple-declaration  */
9026
9027 static void
9028 cp_parser_for_init_statement (cp_parser* parser)
9029 {
9030   /* If the next token is a `;', then we have an empty
9031      expression-statement.  Grammatically, this is also a
9032      simple-declaration, but an invalid one, because it does not
9033      declare anything.  Therefore, if we did not handle this case
9034      specially, we would issue an error message about an invalid
9035      declaration.  */
9036   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9037     {
9038       /* We're going to speculatively look for a declaration, falling back
9039          to an expression, if necessary.  */
9040       cp_parser_parse_tentatively (parser);
9041       /* Parse the declaration.  */
9042       cp_parser_simple_declaration (parser,
9043                                     /*function_definition_allowed_p=*/false);
9044       /* If the tentative parse failed, then we shall need to look for an
9045          expression-statement.  */
9046       if (cp_parser_parse_definitely (parser))
9047         return;
9048     }
9049
9050   cp_parser_expression_statement (parser, NULL_TREE);
9051 }
9052
9053 /* Parse a jump-statement.
9054
9055    jump-statement:
9056      break ;
9057      continue ;
9058      return expression [opt] ;
9059      return braced-init-list ;
9060      goto identifier ;
9061
9062    GNU extension:
9063
9064    jump-statement:
9065      goto * expression ;
9066
9067    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9068
9069 static tree
9070 cp_parser_jump_statement (cp_parser* parser)
9071 {
9072   tree statement = error_mark_node;
9073   cp_token *token;
9074   enum rid keyword;
9075   unsigned char in_statement;
9076
9077   /* Peek at the next token.  */
9078   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9079   if (!token)
9080     return error_mark_node;
9081
9082   /* See what kind of keyword it is.  */
9083   keyword = token->keyword;
9084   switch (keyword)
9085     {
9086     case RID_BREAK:
9087       in_statement = parser->in_statement & ~IN_IF_STMT;      
9088       switch (in_statement)
9089         {
9090         case 0:
9091           error_at (token->location, "break statement not within loop or switch");
9092           break;
9093         default:
9094           gcc_assert ((in_statement & IN_SWITCH_STMT)
9095                       || in_statement == IN_ITERATION_STMT);
9096           statement = finish_break_stmt ();
9097           break;
9098         case IN_OMP_BLOCK:
9099           error_at (token->location, "invalid exit from OpenMP structured block");
9100           break;
9101         case IN_OMP_FOR:
9102           error_at (token->location, "break statement used with OpenMP for loop");
9103           break;
9104         }
9105       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9106       break;
9107
9108     case RID_CONTINUE:
9109       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9110         {
9111         case 0:
9112           error_at (token->location, "continue statement not within a loop");
9113           break;
9114         case IN_ITERATION_STMT:
9115         case IN_OMP_FOR:
9116           statement = finish_continue_stmt ();
9117           break;
9118         case IN_OMP_BLOCK:
9119           error_at (token->location, "invalid exit from OpenMP structured block");
9120           break;
9121         default:
9122           gcc_unreachable ();
9123         }
9124       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9125       break;
9126
9127     case RID_RETURN:
9128       {
9129         tree expr;
9130         bool expr_non_constant_p;
9131
9132         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9133           {
9134             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9135             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9136           }
9137         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9138           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9139         else
9140           /* If the next token is a `;', then there is no
9141              expression.  */
9142           expr = NULL_TREE;
9143         /* Build the return-statement.  */
9144         statement = finish_return_stmt (expr);
9145         /* Look for the final `;'.  */
9146         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9147       }
9148       break;
9149
9150     case RID_GOTO:
9151       /* Create the goto-statement.  */
9152       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9153         {
9154           /* Issue a warning about this use of a GNU extension.  */
9155           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9156           /* Consume the '*' token.  */
9157           cp_lexer_consume_token (parser->lexer);
9158           /* Parse the dependent expression.  */
9159           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9160         }
9161       else
9162         finish_goto_stmt (cp_parser_identifier (parser));
9163       /* Look for the final `;'.  */
9164       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9165       break;
9166
9167     default:
9168       cp_parser_error (parser, "expected jump-statement");
9169       break;
9170     }
9171
9172   return statement;
9173 }
9174
9175 /* Parse a declaration-statement.
9176
9177    declaration-statement:
9178      block-declaration  */
9179
9180 static void
9181 cp_parser_declaration_statement (cp_parser* parser)
9182 {
9183   void *p;
9184
9185   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9186   p = obstack_alloc (&declarator_obstack, 0);
9187
9188  /* Parse the block-declaration.  */
9189   cp_parser_block_declaration (parser, /*statement_p=*/true);
9190
9191   /* Free any declarators allocated.  */
9192   obstack_free (&declarator_obstack, p);
9193
9194   /* Finish off the statement.  */
9195   finish_stmt ();
9196 }
9197
9198 /* Some dependent statements (like `if (cond) statement'), are
9199    implicitly in their own scope.  In other words, if the statement is
9200    a single statement (as opposed to a compound-statement), it is
9201    none-the-less treated as if it were enclosed in braces.  Any
9202    declarations appearing in the dependent statement are out of scope
9203    after control passes that point.  This function parses a statement,
9204    but ensures that is in its own scope, even if it is not a
9205    compound-statement.
9206
9207    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9208    is a (possibly labeled) if statement which is not enclosed in
9209    braces and has an else clause.  This is used to implement
9210    -Wparentheses.
9211
9212    Returns the new statement.  */
9213
9214 static tree
9215 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9216 {
9217   tree statement;
9218
9219   if (if_p != NULL)
9220     *if_p = false;
9221
9222   /* Mark if () ; with a special NOP_EXPR.  */
9223   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9224     {
9225       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9226       cp_lexer_consume_token (parser->lexer);
9227       statement = add_stmt (build_empty_stmt (loc));
9228     }
9229   /* if a compound is opened, we simply parse the statement directly.  */
9230   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9231     statement = cp_parser_compound_statement (parser, NULL, false);
9232   /* If the token is not a `{', then we must take special action.  */
9233   else
9234     {
9235       /* Create a compound-statement.  */
9236       statement = begin_compound_stmt (0);
9237       /* Parse the dependent-statement.  */
9238       cp_parser_statement (parser, NULL_TREE, false, if_p);
9239       /* Finish the dummy compound-statement.  */
9240       finish_compound_stmt (statement);
9241     }
9242
9243   /* Return the statement.  */
9244   return statement;
9245 }
9246
9247 /* For some dependent statements (like `while (cond) statement'), we
9248    have already created a scope.  Therefore, even if the dependent
9249    statement is a compound-statement, we do not want to create another
9250    scope.  */
9251
9252 static void
9253 cp_parser_already_scoped_statement (cp_parser* parser)
9254 {
9255   /* If the token is a `{', then we must take special action.  */
9256   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9257     cp_parser_statement (parser, NULL_TREE, false, NULL);
9258   else
9259     {
9260       /* Avoid calling cp_parser_compound_statement, so that we
9261          don't create a new scope.  Do everything else by hand.  */
9262       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9263       /* If the next keyword is `__label__' we have a label declaration.  */
9264       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9265         cp_parser_label_declaration (parser);
9266       /* Parse an (optional) statement-seq.  */
9267       cp_parser_statement_seq_opt (parser, NULL_TREE);
9268       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9269     }
9270 }
9271
9272 /* Declarations [gram.dcl.dcl] */
9273
9274 /* Parse an optional declaration-sequence.
9275
9276    declaration-seq:
9277      declaration
9278      declaration-seq declaration  */
9279
9280 static void
9281 cp_parser_declaration_seq_opt (cp_parser* parser)
9282 {
9283   while (true)
9284     {
9285       cp_token *token;
9286
9287       token = cp_lexer_peek_token (parser->lexer);
9288
9289       if (token->type == CPP_CLOSE_BRACE
9290           || token->type == CPP_EOF
9291           || token->type == CPP_PRAGMA_EOL)
9292         break;
9293
9294       if (token->type == CPP_SEMICOLON)
9295         {
9296           /* A declaration consisting of a single semicolon is
9297              invalid.  Allow it unless we're being pedantic.  */
9298           cp_lexer_consume_token (parser->lexer);
9299           if (!in_system_header)
9300             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9301           continue;
9302         }
9303
9304       /* If we're entering or exiting a region that's implicitly
9305          extern "C", modify the lang context appropriately.  */
9306       if (!parser->implicit_extern_c && token->implicit_extern_c)
9307         {
9308           push_lang_context (lang_name_c);
9309           parser->implicit_extern_c = true;
9310         }
9311       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9312         {
9313           pop_lang_context ();
9314           parser->implicit_extern_c = false;
9315         }
9316
9317       if (token->type == CPP_PRAGMA)
9318         {
9319           /* A top-level declaration can consist solely of a #pragma.
9320              A nested declaration cannot, so this is done here and not
9321              in cp_parser_declaration.  (A #pragma at block scope is
9322              handled in cp_parser_statement.)  */
9323           cp_parser_pragma (parser, pragma_external);
9324           continue;
9325         }
9326
9327       /* Parse the declaration itself.  */
9328       cp_parser_declaration (parser);
9329     }
9330 }
9331
9332 /* Parse a declaration.
9333
9334    declaration:
9335      block-declaration
9336      function-definition
9337      template-declaration
9338      explicit-instantiation
9339      explicit-specialization
9340      linkage-specification
9341      namespace-definition
9342
9343    GNU extension:
9344
9345    declaration:
9346       __extension__ declaration */
9347
9348 static void
9349 cp_parser_declaration (cp_parser* parser)
9350 {
9351   cp_token token1;
9352   cp_token token2;
9353   int saved_pedantic;
9354   void *p;
9355   tree attributes = NULL_TREE;
9356
9357   /* Check for the `__extension__' keyword.  */
9358   if (cp_parser_extension_opt (parser, &saved_pedantic))
9359     {
9360       /* Parse the qualified declaration.  */
9361       cp_parser_declaration (parser);
9362       /* Restore the PEDANTIC flag.  */
9363       pedantic = saved_pedantic;
9364
9365       return;
9366     }
9367
9368   /* Try to figure out what kind of declaration is present.  */
9369   token1 = *cp_lexer_peek_token (parser->lexer);
9370
9371   if (token1.type != CPP_EOF)
9372     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9373   else
9374     {
9375       token2.type = CPP_EOF;
9376       token2.keyword = RID_MAX;
9377     }
9378
9379   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9380   p = obstack_alloc (&declarator_obstack, 0);
9381
9382   /* If the next token is `extern' and the following token is a string
9383      literal, then we have a linkage specification.  */
9384   if (token1.keyword == RID_EXTERN
9385       && cp_parser_is_string_literal (&token2))
9386     cp_parser_linkage_specification (parser);
9387   /* If the next token is `template', then we have either a template
9388      declaration, an explicit instantiation, or an explicit
9389      specialization.  */
9390   else if (token1.keyword == RID_TEMPLATE)
9391     {
9392       /* `template <>' indicates a template specialization.  */
9393       if (token2.type == CPP_LESS
9394           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9395         cp_parser_explicit_specialization (parser);
9396       /* `template <' indicates a template declaration.  */
9397       else if (token2.type == CPP_LESS)
9398         cp_parser_template_declaration (parser, /*member_p=*/false);
9399       /* Anything else must be an explicit instantiation.  */
9400       else
9401         cp_parser_explicit_instantiation (parser);
9402     }
9403   /* If the next token is `export', then we have a template
9404      declaration.  */
9405   else if (token1.keyword == RID_EXPORT)
9406     cp_parser_template_declaration (parser, /*member_p=*/false);
9407   /* If the next token is `extern', 'static' or 'inline' and the one
9408      after that is `template', we have a GNU extended explicit
9409      instantiation directive.  */
9410   else if (cp_parser_allow_gnu_extensions_p (parser)
9411            && (token1.keyword == RID_EXTERN
9412                || token1.keyword == RID_STATIC
9413                || token1.keyword == RID_INLINE)
9414            && token2.keyword == RID_TEMPLATE)
9415     cp_parser_explicit_instantiation (parser);
9416   /* If the next token is `namespace', check for a named or unnamed
9417      namespace definition.  */
9418   else if (token1.keyword == RID_NAMESPACE
9419            && (/* A named namespace definition.  */
9420                (token2.type == CPP_NAME
9421                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9422                     != CPP_EQ))
9423                /* An unnamed namespace definition.  */
9424                || token2.type == CPP_OPEN_BRACE
9425                || token2.keyword == RID_ATTRIBUTE))
9426     cp_parser_namespace_definition (parser);
9427   /* An inline (associated) namespace definition.  */
9428   else if (token1.keyword == RID_INLINE
9429            && token2.keyword == RID_NAMESPACE)
9430     cp_parser_namespace_definition (parser);
9431   /* Objective-C++ declaration/definition.  */
9432   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9433     cp_parser_objc_declaration (parser, NULL_TREE);
9434   else if (c_dialect_objc ()
9435            && token1.keyword == RID_ATTRIBUTE
9436            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9437     cp_parser_objc_declaration (parser, attributes);
9438   /* We must have either a block declaration or a function
9439      definition.  */
9440   else
9441     /* Try to parse a block-declaration, or a function-definition.  */
9442     cp_parser_block_declaration (parser, /*statement_p=*/false);
9443
9444   /* Free any declarators allocated.  */
9445   obstack_free (&declarator_obstack, p);
9446 }
9447
9448 /* Parse a block-declaration.
9449
9450    block-declaration:
9451      simple-declaration
9452      asm-definition
9453      namespace-alias-definition
9454      using-declaration
9455      using-directive
9456
9457    GNU Extension:
9458
9459    block-declaration:
9460      __extension__ block-declaration
9461
9462    C++0x Extension:
9463
9464    block-declaration:
9465      static_assert-declaration
9466
9467    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9468    part of a declaration-statement.  */
9469
9470 static void
9471 cp_parser_block_declaration (cp_parser *parser,
9472                              bool      statement_p)
9473 {
9474   cp_token *token1;
9475   int saved_pedantic;
9476
9477   /* Check for the `__extension__' keyword.  */
9478   if (cp_parser_extension_opt (parser, &saved_pedantic))
9479     {
9480       /* Parse the qualified declaration.  */
9481       cp_parser_block_declaration (parser, statement_p);
9482       /* Restore the PEDANTIC flag.  */
9483       pedantic = saved_pedantic;
9484
9485       return;
9486     }
9487
9488   /* Peek at the next token to figure out which kind of declaration is
9489      present.  */
9490   token1 = cp_lexer_peek_token (parser->lexer);
9491
9492   /* If the next keyword is `asm', we have an asm-definition.  */
9493   if (token1->keyword == RID_ASM)
9494     {
9495       if (statement_p)
9496         cp_parser_commit_to_tentative_parse (parser);
9497       cp_parser_asm_definition (parser);
9498     }
9499   /* If the next keyword is `namespace', we have a
9500      namespace-alias-definition.  */
9501   else if (token1->keyword == RID_NAMESPACE)
9502     cp_parser_namespace_alias_definition (parser);
9503   /* If the next keyword is `using', we have either a
9504      using-declaration or a using-directive.  */
9505   else if (token1->keyword == RID_USING)
9506     {
9507       cp_token *token2;
9508
9509       if (statement_p)
9510         cp_parser_commit_to_tentative_parse (parser);
9511       /* If the token after `using' is `namespace', then we have a
9512          using-directive.  */
9513       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9514       if (token2->keyword == RID_NAMESPACE)
9515         cp_parser_using_directive (parser);
9516       /* Otherwise, it's a using-declaration.  */
9517       else
9518         cp_parser_using_declaration (parser,
9519                                      /*access_declaration_p=*/false);
9520     }
9521   /* If the next keyword is `__label__' we have a misplaced label
9522      declaration.  */
9523   else if (token1->keyword == RID_LABEL)
9524     {
9525       cp_lexer_consume_token (parser->lexer);
9526       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9527       cp_parser_skip_to_end_of_statement (parser);
9528       /* If the next token is now a `;', consume it.  */
9529       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9530         cp_lexer_consume_token (parser->lexer);
9531     }
9532   /* If the next token is `static_assert' we have a static assertion.  */
9533   else if (token1->keyword == RID_STATIC_ASSERT)
9534     cp_parser_static_assert (parser, /*member_p=*/false);
9535   /* Anything else must be a simple-declaration.  */
9536   else
9537     cp_parser_simple_declaration (parser, !statement_p);
9538 }
9539
9540 /* Parse a simple-declaration.
9541
9542    simple-declaration:
9543      decl-specifier-seq [opt] init-declarator-list [opt] ;
9544
9545    init-declarator-list:
9546      init-declarator
9547      init-declarator-list , init-declarator
9548
9549    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9550    function-definition as a simple-declaration.  */
9551
9552 static void
9553 cp_parser_simple_declaration (cp_parser* parser,
9554                               bool function_definition_allowed_p)
9555 {
9556   cp_decl_specifier_seq decl_specifiers;
9557   int declares_class_or_enum;
9558   bool saw_declarator;
9559
9560   /* Defer access checks until we know what is being declared; the
9561      checks for names appearing in the decl-specifier-seq should be
9562      done as if we were in the scope of the thing being declared.  */
9563   push_deferring_access_checks (dk_deferred);
9564
9565   /* Parse the decl-specifier-seq.  We have to keep track of whether
9566      or not the decl-specifier-seq declares a named class or
9567      enumeration type, since that is the only case in which the
9568      init-declarator-list is allowed to be empty.
9569
9570      [dcl.dcl]
9571
9572      In a simple-declaration, the optional init-declarator-list can be
9573      omitted only when declaring a class or enumeration, that is when
9574      the decl-specifier-seq contains either a class-specifier, an
9575      elaborated-type-specifier, or an enum-specifier.  */
9576   cp_parser_decl_specifier_seq (parser,
9577                                 CP_PARSER_FLAGS_OPTIONAL,
9578                                 &decl_specifiers,
9579                                 &declares_class_or_enum);
9580   /* We no longer need to defer access checks.  */
9581   stop_deferring_access_checks ();
9582
9583   /* In a block scope, a valid declaration must always have a
9584      decl-specifier-seq.  By not trying to parse declarators, we can
9585      resolve the declaration/expression ambiguity more quickly.  */
9586   if (!function_definition_allowed_p
9587       && !decl_specifiers.any_specifiers_p)
9588     {
9589       cp_parser_error (parser, "expected declaration");
9590       goto done;
9591     }
9592
9593   /* If the next two tokens are both identifiers, the code is
9594      erroneous. The usual cause of this situation is code like:
9595
9596        T t;
9597
9598      where "T" should name a type -- but does not.  */
9599   if (!decl_specifiers.any_type_specifiers_p
9600       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9601     {
9602       /* If parsing tentatively, we should commit; we really are
9603          looking at a declaration.  */
9604       cp_parser_commit_to_tentative_parse (parser);
9605       /* Give up.  */
9606       goto done;
9607     }
9608
9609   /* If we have seen at least one decl-specifier, and the next token
9610      is not a parenthesis, then we must be looking at a declaration.
9611      (After "int (" we might be looking at a functional cast.)  */
9612   if (decl_specifiers.any_specifiers_p
9613       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9614       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9615       && !cp_parser_error_occurred (parser))
9616     cp_parser_commit_to_tentative_parse (parser);
9617
9618   /* Keep going until we hit the `;' at the end of the simple
9619      declaration.  */
9620   saw_declarator = false;
9621   while (cp_lexer_next_token_is_not (parser->lexer,
9622                                      CPP_SEMICOLON))
9623     {
9624       cp_token *token;
9625       bool function_definition_p;
9626       tree decl;
9627
9628       if (saw_declarator)
9629         {
9630           /* If we are processing next declarator, coma is expected */
9631           token = cp_lexer_peek_token (parser->lexer);
9632           gcc_assert (token->type == CPP_COMMA);
9633           cp_lexer_consume_token (parser->lexer);
9634         }
9635       else
9636         saw_declarator = true;
9637
9638       /* Parse the init-declarator.  */
9639       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9640                                         /*checks=*/NULL,
9641                                         function_definition_allowed_p,
9642                                         /*member_p=*/false,
9643                                         declares_class_or_enum,
9644                                         &function_definition_p);
9645       /* If an error occurred while parsing tentatively, exit quickly.
9646          (That usually happens when in the body of a function; each
9647          statement is treated as a declaration-statement until proven
9648          otherwise.)  */
9649       if (cp_parser_error_occurred (parser))
9650         goto done;
9651       /* Handle function definitions specially.  */
9652       if (function_definition_p)
9653         {
9654           /* If the next token is a `,', then we are probably
9655              processing something like:
9656
9657                void f() {}, *p;
9658
9659              which is erroneous.  */
9660           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9661             {
9662               cp_token *token = cp_lexer_peek_token (parser->lexer);
9663               error_at (token->location,
9664                         "mixing"
9665                         " declarations and function-definitions is forbidden");
9666             }
9667           /* Otherwise, we're done with the list of declarators.  */
9668           else
9669             {
9670               pop_deferring_access_checks ();
9671               return;
9672             }
9673         }
9674       /* The next token should be either a `,' or a `;'.  */
9675       token = cp_lexer_peek_token (parser->lexer);
9676       /* If it's a `,', there are more declarators to come.  */
9677       if (token->type == CPP_COMMA)
9678         /* will be consumed next time around */;
9679       /* If it's a `;', we are done.  */
9680       else if (token->type == CPP_SEMICOLON)
9681         break;
9682       /* Anything else is an error.  */
9683       else
9684         {
9685           /* If we have already issued an error message we don't need
9686              to issue another one.  */
9687           if (decl != error_mark_node
9688               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9689             cp_parser_error (parser, "expected %<,%> or %<;%>");
9690           /* Skip tokens until we reach the end of the statement.  */
9691           cp_parser_skip_to_end_of_statement (parser);
9692           /* If the next token is now a `;', consume it.  */
9693           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9694             cp_lexer_consume_token (parser->lexer);
9695           goto done;
9696         }
9697       /* After the first time around, a function-definition is not
9698          allowed -- even if it was OK at first.  For example:
9699
9700            int i, f() {}
9701
9702          is not valid.  */
9703       function_definition_allowed_p = false;
9704     }
9705
9706   /* Issue an error message if no declarators are present, and the
9707      decl-specifier-seq does not itself declare a class or
9708      enumeration.  */
9709   if (!saw_declarator)
9710     {
9711       if (cp_parser_declares_only_class_p (parser))
9712         shadow_tag (&decl_specifiers);
9713       /* Perform any deferred access checks.  */
9714       perform_deferred_access_checks ();
9715     }
9716
9717   /* Consume the `;'.  */
9718   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9719
9720  done:
9721   pop_deferring_access_checks ();
9722 }
9723
9724 /* Parse a decl-specifier-seq.
9725
9726    decl-specifier-seq:
9727      decl-specifier-seq [opt] decl-specifier
9728
9729    decl-specifier:
9730      storage-class-specifier
9731      type-specifier
9732      function-specifier
9733      friend
9734      typedef
9735
9736    GNU Extension:
9737
9738    decl-specifier:
9739      attributes
9740
9741    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9742
9743    The parser flags FLAGS is used to control type-specifier parsing.
9744
9745    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9746    flags:
9747
9748      1: one of the decl-specifiers is an elaborated-type-specifier
9749         (i.e., a type declaration)
9750      2: one of the decl-specifiers is an enum-specifier or a
9751         class-specifier (i.e., a type definition)
9752
9753    */
9754
9755 static void
9756 cp_parser_decl_specifier_seq (cp_parser* parser,
9757                               cp_parser_flags flags,
9758                               cp_decl_specifier_seq *decl_specs,
9759                               int* declares_class_or_enum)
9760 {
9761   bool constructor_possible_p = !parser->in_declarator_p;
9762   cp_token *start_token = NULL;
9763
9764   /* Clear DECL_SPECS.  */
9765   clear_decl_specs (decl_specs);
9766
9767   /* Assume no class or enumeration type is declared.  */
9768   *declares_class_or_enum = 0;
9769
9770   /* Keep reading specifiers until there are no more to read.  */
9771   while (true)
9772     {
9773       bool constructor_p;
9774       bool found_decl_spec;
9775       cp_token *token;
9776
9777       /* Peek at the next token.  */
9778       token = cp_lexer_peek_token (parser->lexer);
9779
9780       /* Save the first token of the decl spec list for error
9781          reporting.  */
9782       if (!start_token)
9783         start_token = token;
9784       /* Handle attributes.  */
9785       if (token->keyword == RID_ATTRIBUTE)
9786         {
9787           /* Parse the attributes.  */
9788           decl_specs->attributes
9789             = chainon (decl_specs->attributes,
9790                        cp_parser_attributes_opt (parser));
9791           continue;
9792         }
9793       /* Assume we will find a decl-specifier keyword.  */
9794       found_decl_spec = true;
9795       /* If the next token is an appropriate keyword, we can simply
9796          add it to the list.  */
9797       switch (token->keyword)
9798         {
9799           /* decl-specifier:
9800                friend
9801                constexpr */
9802         case RID_FRIEND:
9803           if (!at_class_scope_p ())
9804             {
9805               error_at (token->location, "%<friend%> used outside of class");
9806               cp_lexer_purge_token (parser->lexer);
9807             }
9808           else
9809             {
9810               ++decl_specs->specs[(int) ds_friend];
9811               /* Consume the token.  */
9812               cp_lexer_consume_token (parser->lexer);
9813             }
9814           break;
9815
9816         case RID_CONSTEXPR:
9817           ++decl_specs->specs[(int) ds_constexpr];
9818           cp_lexer_consume_token (parser->lexer);
9819           break;
9820
9821           /* function-specifier:
9822                inline
9823                virtual
9824                explicit  */
9825         case RID_INLINE:
9826         case RID_VIRTUAL:
9827         case RID_EXPLICIT:
9828           cp_parser_function_specifier_opt (parser, decl_specs);
9829           break;
9830
9831           /* decl-specifier:
9832                typedef  */
9833         case RID_TYPEDEF:
9834           ++decl_specs->specs[(int) ds_typedef];
9835           /* Consume the token.  */
9836           cp_lexer_consume_token (parser->lexer);
9837           /* A constructor declarator cannot appear in a typedef.  */
9838           constructor_possible_p = false;
9839           /* The "typedef" keyword can only occur in a declaration; we
9840              may as well commit at this point.  */
9841           cp_parser_commit_to_tentative_parse (parser);
9842
9843           if (decl_specs->storage_class != sc_none)
9844             decl_specs->conflicting_specifiers_p = true;
9845           break;
9846
9847           /* storage-class-specifier:
9848                auto
9849                register
9850                static
9851                extern
9852                mutable
9853
9854              GNU Extension:
9855                thread  */
9856         case RID_AUTO:
9857           if (cxx_dialect == cxx98) 
9858             {
9859               /* Consume the token.  */
9860               cp_lexer_consume_token (parser->lexer);
9861
9862               /* Complain about `auto' as a storage specifier, if
9863                  we're complaining about C++0x compatibility.  */
9864               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9865                           " will change meaning in C++0x; please remove it");
9866
9867               /* Set the storage class anyway.  */
9868               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9869                                            token->location);
9870             }
9871           else
9872             /* C++0x auto type-specifier.  */
9873             found_decl_spec = false;
9874           break;
9875
9876         case RID_REGISTER:
9877         case RID_STATIC:
9878         case RID_EXTERN:
9879         case RID_MUTABLE:
9880           /* Consume the token.  */
9881           cp_lexer_consume_token (parser->lexer);
9882           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9883                                        token->location);
9884           break;
9885         case RID_THREAD:
9886           /* Consume the token.  */
9887           cp_lexer_consume_token (parser->lexer);
9888           ++decl_specs->specs[(int) ds_thread];
9889           break;
9890
9891         default:
9892           /* We did not yet find a decl-specifier yet.  */
9893           found_decl_spec = false;
9894           break;
9895         }
9896
9897       if (found_decl_spec
9898           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9899           && token->keyword != RID_CONSTEXPR)
9900         error ("decl-specifier invalid in condition");
9901
9902       /* Constructors are a special case.  The `S' in `S()' is not a
9903          decl-specifier; it is the beginning of the declarator.  */
9904       constructor_p
9905         = (!found_decl_spec
9906            && constructor_possible_p
9907            && (cp_parser_constructor_declarator_p
9908                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9909
9910       /* If we don't have a DECL_SPEC yet, then we must be looking at
9911          a type-specifier.  */
9912       if (!found_decl_spec && !constructor_p)
9913         {
9914           int decl_spec_declares_class_or_enum;
9915           bool is_cv_qualifier;
9916           tree type_spec;
9917
9918           type_spec
9919             = cp_parser_type_specifier (parser, flags,
9920                                         decl_specs,
9921                                         /*is_declaration=*/true,
9922                                         &decl_spec_declares_class_or_enum,
9923                                         &is_cv_qualifier);
9924           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9925
9926           /* If this type-specifier referenced a user-defined type
9927              (a typedef, class-name, etc.), then we can't allow any
9928              more such type-specifiers henceforth.
9929
9930              [dcl.spec]
9931
9932              The longest sequence of decl-specifiers that could
9933              possibly be a type name is taken as the
9934              decl-specifier-seq of a declaration.  The sequence shall
9935              be self-consistent as described below.
9936
9937              [dcl.type]
9938
9939              As a general rule, at most one type-specifier is allowed
9940              in the complete decl-specifier-seq of a declaration.  The
9941              only exceptions are the following:
9942
9943              -- const or volatile can be combined with any other
9944                 type-specifier.
9945
9946              -- signed or unsigned can be combined with char, long,
9947                 short, or int.
9948
9949              -- ..
9950
9951              Example:
9952
9953                typedef char* Pc;
9954                void g (const int Pc);
9955
9956              Here, Pc is *not* part of the decl-specifier seq; it's
9957              the declarator.  Therefore, once we see a type-specifier
9958              (other than a cv-qualifier), we forbid any additional
9959              user-defined types.  We *do* still allow things like `int
9960              int' to be considered a decl-specifier-seq, and issue the
9961              error message later.  */
9962           if (type_spec && !is_cv_qualifier)
9963             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9964           /* A constructor declarator cannot follow a type-specifier.  */
9965           if (type_spec)
9966             {
9967               constructor_possible_p = false;
9968               found_decl_spec = true;
9969               if (!is_cv_qualifier)
9970                 decl_specs->any_type_specifiers_p = true;
9971             }
9972         }
9973
9974       /* If we still do not have a DECL_SPEC, then there are no more
9975          decl-specifiers.  */
9976       if (!found_decl_spec)
9977         break;
9978
9979       decl_specs->any_specifiers_p = true;
9980       /* After we see one decl-specifier, further decl-specifiers are
9981          always optional.  */
9982       flags |= CP_PARSER_FLAGS_OPTIONAL;
9983     }
9984
9985   cp_parser_check_decl_spec (decl_specs, start_token->location);
9986
9987   /* Don't allow a friend specifier with a class definition.  */
9988   if (decl_specs->specs[(int) ds_friend] != 0
9989       && (*declares_class_or_enum & 2))
9990     error_at (start_token->location,
9991               "class definition may not be declared a friend");
9992 }
9993
9994 /* Parse an (optional) storage-class-specifier.
9995
9996    storage-class-specifier:
9997      auto
9998      register
9999      static
10000      extern
10001      mutable
10002
10003    GNU Extension:
10004
10005    storage-class-specifier:
10006      thread
10007
10008    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10009
10010 static tree
10011 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10012 {
10013   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10014     {
10015     case RID_AUTO:
10016       if (cxx_dialect != cxx98)
10017         return NULL_TREE;
10018       /* Fall through for C++98.  */
10019
10020     case RID_REGISTER:
10021     case RID_STATIC:
10022     case RID_EXTERN:
10023     case RID_MUTABLE:
10024     case RID_THREAD:
10025       /* Consume the token.  */
10026       return cp_lexer_consume_token (parser->lexer)->u.value;
10027
10028     default:
10029       return NULL_TREE;
10030     }
10031 }
10032
10033 /* Parse an (optional) function-specifier.
10034
10035    function-specifier:
10036      inline
10037      virtual
10038      explicit
10039
10040    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10041    Updates DECL_SPECS, if it is non-NULL.  */
10042
10043 static tree
10044 cp_parser_function_specifier_opt (cp_parser* parser,
10045                                   cp_decl_specifier_seq *decl_specs)
10046 {
10047   cp_token *token = cp_lexer_peek_token (parser->lexer);
10048   switch (token->keyword)
10049     {
10050     case RID_INLINE:
10051       if (decl_specs)
10052         ++decl_specs->specs[(int) ds_inline];
10053       break;
10054
10055     case RID_VIRTUAL:
10056       /* 14.5.2.3 [temp.mem]
10057
10058          A member function template shall not be virtual.  */
10059       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10060         error_at (token->location, "templates may not be %<virtual%>");
10061       else if (decl_specs)
10062         ++decl_specs->specs[(int) ds_virtual];
10063       break;
10064
10065     case RID_EXPLICIT:
10066       if (decl_specs)
10067         ++decl_specs->specs[(int) ds_explicit];
10068       break;
10069
10070     default:
10071       return NULL_TREE;
10072     }
10073
10074   /* Consume the token.  */
10075   return cp_lexer_consume_token (parser->lexer)->u.value;
10076 }
10077
10078 /* Parse a linkage-specification.
10079
10080    linkage-specification:
10081      extern string-literal { declaration-seq [opt] }
10082      extern string-literal declaration  */
10083
10084 static void
10085 cp_parser_linkage_specification (cp_parser* parser)
10086 {
10087   tree linkage;
10088
10089   /* Look for the `extern' keyword.  */
10090   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10091
10092   /* Look for the string-literal.  */
10093   linkage = cp_parser_string_literal (parser, false, false);
10094
10095   /* Transform the literal into an identifier.  If the literal is a
10096      wide-character string, or contains embedded NULs, then we can't
10097      handle it as the user wants.  */
10098   if (strlen (TREE_STRING_POINTER (linkage))
10099       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10100     {
10101       cp_parser_error (parser, "invalid linkage-specification");
10102       /* Assume C++ linkage.  */
10103       linkage = lang_name_cplusplus;
10104     }
10105   else
10106     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10107
10108   /* We're now using the new linkage.  */
10109   push_lang_context (linkage);
10110
10111   /* If the next token is a `{', then we're using the first
10112      production.  */
10113   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10114     {
10115       /* Consume the `{' token.  */
10116       cp_lexer_consume_token (parser->lexer);
10117       /* Parse the declarations.  */
10118       cp_parser_declaration_seq_opt (parser);
10119       /* Look for the closing `}'.  */
10120       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10121     }
10122   /* Otherwise, there's just one declaration.  */
10123   else
10124     {
10125       bool saved_in_unbraced_linkage_specification_p;
10126
10127       saved_in_unbraced_linkage_specification_p
10128         = parser->in_unbraced_linkage_specification_p;
10129       parser->in_unbraced_linkage_specification_p = true;
10130       cp_parser_declaration (parser);
10131       parser->in_unbraced_linkage_specification_p
10132         = saved_in_unbraced_linkage_specification_p;
10133     }
10134
10135   /* We're done with the linkage-specification.  */
10136   pop_lang_context ();
10137 }
10138
10139 /* Parse a static_assert-declaration.
10140
10141    static_assert-declaration:
10142      static_assert ( constant-expression , string-literal ) ; 
10143
10144    If MEMBER_P, this static_assert is a class member.  */
10145
10146 static void 
10147 cp_parser_static_assert(cp_parser *parser, bool member_p)
10148 {
10149   tree condition;
10150   tree message;
10151   cp_token *token;
10152   location_t saved_loc;
10153
10154   /* Peek at the `static_assert' token so we can keep track of exactly
10155      where the static assertion started.  */
10156   token = cp_lexer_peek_token (parser->lexer);
10157   saved_loc = token->location;
10158
10159   /* Look for the `static_assert' keyword.  */
10160   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10161                                   RT_STATIC_ASSERT))
10162     return;
10163
10164   /*  We know we are in a static assertion; commit to any tentative
10165       parse.  */
10166   if (cp_parser_parsing_tentatively (parser))
10167     cp_parser_commit_to_tentative_parse (parser);
10168
10169   /* Parse the `(' starting the static assertion condition.  */
10170   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10171
10172   /* Parse the constant-expression.  */
10173   condition = 
10174     cp_parser_constant_expression (parser,
10175                                    /*allow_non_constant_p=*/false,
10176                                    /*non_constant_p=*/NULL);
10177
10178   /* Parse the separating `,'.  */
10179   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10180
10181   /* Parse the string-literal message.  */
10182   message = cp_parser_string_literal (parser, 
10183                                       /*translate=*/false,
10184                                       /*wide_ok=*/true);
10185
10186   /* A `)' completes the static assertion.  */
10187   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10188     cp_parser_skip_to_closing_parenthesis (parser, 
10189                                            /*recovering=*/true, 
10190                                            /*or_comma=*/false,
10191                                            /*consume_paren=*/true);
10192
10193   /* A semicolon terminates the declaration.  */
10194   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10195
10196   /* Complete the static assertion, which may mean either processing 
10197      the static assert now or saving it for template instantiation.  */
10198   finish_static_assert (condition, message, saved_loc, member_p);
10199 }
10200
10201 /* Parse a `decltype' type. Returns the type. 
10202
10203    simple-type-specifier:
10204      decltype ( expression )  */
10205
10206 static tree
10207 cp_parser_decltype (cp_parser *parser)
10208 {
10209   tree expr;
10210   bool id_expression_or_member_access_p = false;
10211   const char *saved_message;
10212   bool saved_integral_constant_expression_p;
10213   bool saved_non_integral_constant_expression_p;
10214   cp_token *id_expr_start_token;
10215
10216   /* Look for the `decltype' token.  */
10217   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10218     return error_mark_node;
10219
10220   /* Types cannot be defined in a `decltype' expression.  Save away the
10221      old message.  */
10222   saved_message = parser->type_definition_forbidden_message;
10223
10224   /* And create the new one.  */
10225   parser->type_definition_forbidden_message
10226     = G_("types may not be defined in %<decltype%> expressions");
10227
10228   /* The restrictions on constant-expressions do not apply inside
10229      decltype expressions.  */
10230   saved_integral_constant_expression_p
10231     = parser->integral_constant_expression_p;
10232   saved_non_integral_constant_expression_p
10233     = parser->non_integral_constant_expression_p;
10234   parser->integral_constant_expression_p = false;
10235
10236   /* Do not actually evaluate the expression.  */
10237   ++cp_unevaluated_operand;
10238
10239   /* Do not warn about problems with the expression.  */
10240   ++c_inhibit_evaluation_warnings;
10241
10242   /* Parse the opening `('.  */
10243   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10244     return error_mark_node;
10245   
10246   /* First, try parsing an id-expression.  */
10247   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10248   cp_parser_parse_tentatively (parser);
10249   expr = cp_parser_id_expression (parser,
10250                                   /*template_keyword_p=*/false,
10251                                   /*check_dependency_p=*/true,
10252                                   /*template_p=*/NULL,
10253                                   /*declarator_p=*/false,
10254                                   /*optional_p=*/false);
10255
10256   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10257     {
10258       bool non_integral_constant_expression_p = false;
10259       tree id_expression = expr;
10260       cp_id_kind idk;
10261       const char *error_msg;
10262
10263       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10264         /* Lookup the name we got back from the id-expression.  */
10265         expr = cp_parser_lookup_name (parser, expr,
10266                                       none_type,
10267                                       /*is_template=*/false,
10268                                       /*is_namespace=*/false,
10269                                       /*check_dependency=*/true,
10270                                       /*ambiguous_decls=*/NULL,
10271                                       id_expr_start_token->location);
10272
10273       if (expr
10274           && expr != error_mark_node
10275           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10276           && TREE_CODE (expr) != TYPE_DECL
10277           && (TREE_CODE (expr) != BIT_NOT_EXPR
10278               || !TYPE_P (TREE_OPERAND (expr, 0)))
10279           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10280         {
10281           /* Complete lookup of the id-expression.  */
10282           expr = (finish_id_expression
10283                   (id_expression, expr, parser->scope, &idk,
10284                    /*integral_constant_expression_p=*/false,
10285                    /*allow_non_integral_constant_expression_p=*/true,
10286                    &non_integral_constant_expression_p,
10287                    /*template_p=*/false,
10288                    /*done=*/true,
10289                    /*address_p=*/false,
10290                    /*template_arg_p=*/false,
10291                    &error_msg,
10292                    id_expr_start_token->location));
10293
10294           if (expr == error_mark_node)
10295             /* We found an id-expression, but it was something that we
10296                should not have found. This is an error, not something
10297                we can recover from, so note that we found an
10298                id-expression and we'll recover as gracefully as
10299                possible.  */
10300             id_expression_or_member_access_p = true;
10301         }
10302
10303       if (expr 
10304           && expr != error_mark_node
10305           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10306         /* We have an id-expression.  */
10307         id_expression_or_member_access_p = true;
10308     }
10309
10310   if (!id_expression_or_member_access_p)
10311     {
10312       /* Abort the id-expression parse.  */
10313       cp_parser_abort_tentative_parse (parser);
10314
10315       /* Parsing tentatively, again.  */
10316       cp_parser_parse_tentatively (parser);
10317
10318       /* Parse a class member access.  */
10319       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10320                                            /*cast_p=*/false,
10321                                            /*member_access_only_p=*/true, NULL);
10322
10323       if (expr 
10324           && expr != error_mark_node
10325           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10326         /* We have an id-expression.  */
10327         id_expression_or_member_access_p = true;
10328     }
10329
10330   if (id_expression_or_member_access_p)
10331     /* We have parsed the complete id-expression or member access.  */
10332     cp_parser_parse_definitely (parser);
10333   else
10334     {
10335       bool saved_greater_than_is_operator_p;
10336
10337       /* Abort our attempt to parse an id-expression or member access
10338          expression.  */
10339       cp_parser_abort_tentative_parse (parser);
10340
10341       /* Within a parenthesized expression, a `>' token is always
10342          the greater-than operator.  */
10343       saved_greater_than_is_operator_p
10344         = parser->greater_than_is_operator_p;
10345       parser->greater_than_is_operator_p = true;
10346
10347       /* Parse a full expression.  */
10348       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10349
10350       /* The `>' token might be the end of a template-id or
10351          template-parameter-list now.  */
10352       parser->greater_than_is_operator_p
10353         = saved_greater_than_is_operator_p;
10354     }
10355
10356   /* Go back to evaluating expressions.  */
10357   --cp_unevaluated_operand;
10358   --c_inhibit_evaluation_warnings;
10359
10360   /* Restore the old message and the integral constant expression
10361      flags.  */
10362   parser->type_definition_forbidden_message = saved_message;
10363   parser->integral_constant_expression_p
10364     = saved_integral_constant_expression_p;
10365   parser->non_integral_constant_expression_p
10366     = saved_non_integral_constant_expression_p;
10367
10368   if (expr == error_mark_node)
10369     {
10370       /* Skip everything up to the closing `)'.  */
10371       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10372                                              /*consume_paren=*/true);
10373       return error_mark_node;
10374     }
10375   
10376   /* Parse to the closing `)'.  */
10377   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10378     {
10379       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10380                                              /*consume_paren=*/true);
10381       return error_mark_node;
10382     }
10383
10384   return finish_decltype_type (expr, id_expression_or_member_access_p);
10385 }
10386
10387 /* Special member functions [gram.special] */
10388
10389 /* Parse a conversion-function-id.
10390
10391    conversion-function-id:
10392      operator conversion-type-id
10393
10394    Returns an IDENTIFIER_NODE representing the operator.  */
10395
10396 static tree
10397 cp_parser_conversion_function_id (cp_parser* parser)
10398 {
10399   tree type;
10400   tree saved_scope;
10401   tree saved_qualifying_scope;
10402   tree saved_object_scope;
10403   tree pushed_scope = NULL_TREE;
10404
10405   /* Look for the `operator' token.  */
10406   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10407     return error_mark_node;
10408   /* When we parse the conversion-type-id, the current scope will be
10409      reset.  However, we need that information in able to look up the
10410      conversion function later, so we save it here.  */
10411   saved_scope = parser->scope;
10412   saved_qualifying_scope = parser->qualifying_scope;
10413   saved_object_scope = parser->object_scope;
10414   /* We must enter the scope of the class so that the names of
10415      entities declared within the class are available in the
10416      conversion-type-id.  For example, consider:
10417
10418        struct S {
10419          typedef int I;
10420          operator I();
10421        };
10422
10423        S::operator I() { ... }
10424
10425      In order to see that `I' is a type-name in the definition, we
10426      must be in the scope of `S'.  */
10427   if (saved_scope)
10428     pushed_scope = push_scope (saved_scope);
10429   /* Parse the conversion-type-id.  */
10430   type = cp_parser_conversion_type_id (parser);
10431   /* Leave the scope of the class, if any.  */
10432   if (pushed_scope)
10433     pop_scope (pushed_scope);
10434   /* Restore the saved scope.  */
10435   parser->scope = saved_scope;
10436   parser->qualifying_scope = saved_qualifying_scope;
10437   parser->object_scope = saved_object_scope;
10438   /* If the TYPE is invalid, indicate failure.  */
10439   if (type == error_mark_node)
10440     return error_mark_node;
10441   return mangle_conv_op_name_for_type (type);
10442 }
10443
10444 /* Parse a conversion-type-id:
10445
10446    conversion-type-id:
10447      type-specifier-seq conversion-declarator [opt]
10448
10449    Returns the TYPE specified.  */
10450
10451 static tree
10452 cp_parser_conversion_type_id (cp_parser* parser)
10453 {
10454   tree attributes;
10455   cp_decl_specifier_seq type_specifiers;
10456   cp_declarator *declarator;
10457   tree type_specified;
10458
10459   /* Parse the attributes.  */
10460   attributes = cp_parser_attributes_opt (parser);
10461   /* Parse the type-specifiers.  */
10462   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10463                                 /*is_trailing_return=*/false,
10464                                 &type_specifiers);
10465   /* If that didn't work, stop.  */
10466   if (type_specifiers.type == error_mark_node)
10467     return error_mark_node;
10468   /* Parse the conversion-declarator.  */
10469   declarator = cp_parser_conversion_declarator_opt (parser);
10470
10471   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10472                                     /*initialized=*/0, &attributes);
10473   if (attributes)
10474     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10475
10476   /* Don't give this error when parsing tentatively.  This happens to
10477      work because we always parse this definitively once.  */
10478   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10479       && type_uses_auto (type_specified))
10480     {
10481       error ("invalid use of %<auto%> in conversion operator");
10482       return error_mark_node;
10483     }
10484
10485   return type_specified;
10486 }
10487
10488 /* Parse an (optional) conversion-declarator.
10489
10490    conversion-declarator:
10491      ptr-operator conversion-declarator [opt]
10492
10493    */
10494
10495 static cp_declarator *
10496 cp_parser_conversion_declarator_opt (cp_parser* parser)
10497 {
10498   enum tree_code code;
10499   tree class_type;
10500   cp_cv_quals cv_quals;
10501
10502   /* We don't know if there's a ptr-operator next, or not.  */
10503   cp_parser_parse_tentatively (parser);
10504   /* Try the ptr-operator.  */
10505   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10506   /* If it worked, look for more conversion-declarators.  */
10507   if (cp_parser_parse_definitely (parser))
10508     {
10509       cp_declarator *declarator;
10510
10511       /* Parse another optional declarator.  */
10512       declarator = cp_parser_conversion_declarator_opt (parser);
10513
10514       return cp_parser_make_indirect_declarator
10515         (code, class_type, cv_quals, declarator);
10516    }
10517
10518   return NULL;
10519 }
10520
10521 /* Parse an (optional) ctor-initializer.
10522
10523    ctor-initializer:
10524      : mem-initializer-list
10525
10526    Returns TRUE iff the ctor-initializer was actually present.  */
10527
10528 static bool
10529 cp_parser_ctor_initializer_opt (cp_parser* parser)
10530 {
10531   /* If the next token is not a `:', then there is no
10532      ctor-initializer.  */
10533   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10534     {
10535       /* Do default initialization of any bases and members.  */
10536       if (DECL_CONSTRUCTOR_P (current_function_decl))
10537         finish_mem_initializers (NULL_TREE);
10538
10539       return false;
10540     }
10541
10542   /* Consume the `:' token.  */
10543   cp_lexer_consume_token (parser->lexer);
10544   /* And the mem-initializer-list.  */
10545   cp_parser_mem_initializer_list (parser);
10546
10547   return true;
10548 }
10549
10550 /* Parse a mem-initializer-list.
10551
10552    mem-initializer-list:
10553      mem-initializer ... [opt]
10554      mem-initializer ... [opt] , mem-initializer-list  */
10555
10556 static void
10557 cp_parser_mem_initializer_list (cp_parser* parser)
10558 {
10559   tree mem_initializer_list = NULL_TREE;
10560   cp_token *token = cp_lexer_peek_token (parser->lexer);
10561
10562   /* Let the semantic analysis code know that we are starting the
10563      mem-initializer-list.  */
10564   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10565     error_at (token->location,
10566               "only constructors take member initializers");
10567
10568   /* Loop through the list.  */
10569   while (true)
10570     {
10571       tree mem_initializer;
10572
10573       token = cp_lexer_peek_token (parser->lexer);
10574       /* Parse the mem-initializer.  */
10575       mem_initializer = cp_parser_mem_initializer (parser);
10576       /* If the next token is a `...', we're expanding member initializers. */
10577       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10578         {
10579           /* Consume the `...'. */
10580           cp_lexer_consume_token (parser->lexer);
10581
10582           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10583              can be expanded but members cannot. */
10584           if (mem_initializer != error_mark_node
10585               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10586             {
10587               error_at (token->location,
10588                         "cannot expand initializer for member %<%D%>",
10589                         TREE_PURPOSE (mem_initializer));
10590               mem_initializer = error_mark_node;
10591             }
10592
10593           /* Construct the pack expansion type. */
10594           if (mem_initializer != error_mark_node)
10595             mem_initializer = make_pack_expansion (mem_initializer);
10596         }
10597       /* Add it to the list, unless it was erroneous.  */
10598       if (mem_initializer != error_mark_node)
10599         {
10600           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10601           mem_initializer_list = mem_initializer;
10602         }
10603       /* If the next token is not a `,', we're done.  */
10604       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10605         break;
10606       /* Consume the `,' token.  */
10607       cp_lexer_consume_token (parser->lexer);
10608     }
10609
10610   /* Perform semantic analysis.  */
10611   if (DECL_CONSTRUCTOR_P (current_function_decl))
10612     finish_mem_initializers (mem_initializer_list);
10613 }
10614
10615 /* Parse a mem-initializer.
10616
10617    mem-initializer:
10618      mem-initializer-id ( expression-list [opt] )
10619      mem-initializer-id braced-init-list
10620
10621    GNU extension:
10622
10623    mem-initializer:
10624      ( expression-list [opt] )
10625
10626    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10627    class) or FIELD_DECL (for a non-static data member) to initialize;
10628    the TREE_VALUE is the expression-list.  An empty initialization
10629    list is represented by void_list_node.  */
10630
10631 static tree
10632 cp_parser_mem_initializer (cp_parser* parser)
10633 {
10634   tree mem_initializer_id;
10635   tree expression_list;
10636   tree member;
10637   cp_token *token = cp_lexer_peek_token (parser->lexer);
10638
10639   /* Find out what is being initialized.  */
10640   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10641     {
10642       permerror (token->location,
10643                  "anachronistic old-style base class initializer");
10644       mem_initializer_id = NULL_TREE;
10645     }
10646   else
10647     {
10648       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10649       if (mem_initializer_id == error_mark_node)
10650         return mem_initializer_id;
10651     }
10652   member = expand_member_init (mem_initializer_id);
10653   if (member && !DECL_P (member))
10654     in_base_initializer = 1;
10655
10656   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10657     {
10658       bool expr_non_constant_p;
10659       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10660       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10661       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10662       expression_list = build_tree_list (NULL_TREE, expression_list);
10663     }
10664   else
10665     {
10666       VEC(tree,gc)* vec;
10667       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10668                                                      /*cast_p=*/false,
10669                                                      /*allow_expansion_p=*/true,
10670                                                      /*non_constant_p=*/NULL);
10671       if (vec == NULL)
10672         return error_mark_node;
10673       expression_list = build_tree_list_vec (vec);
10674       release_tree_vector (vec);
10675     }
10676
10677   if (expression_list == error_mark_node)
10678     return error_mark_node;
10679   if (!expression_list)
10680     expression_list = void_type_node;
10681
10682   in_base_initializer = 0;
10683
10684   return member ? build_tree_list (member, expression_list) : error_mark_node;
10685 }
10686
10687 /* Parse a mem-initializer-id.
10688
10689    mem-initializer-id:
10690      :: [opt] nested-name-specifier [opt] class-name
10691      identifier
10692
10693    Returns a TYPE indicating the class to be initializer for the first
10694    production.  Returns an IDENTIFIER_NODE indicating the data member
10695    to be initialized for the second production.  */
10696
10697 static tree
10698 cp_parser_mem_initializer_id (cp_parser* parser)
10699 {
10700   bool global_scope_p;
10701   bool nested_name_specifier_p;
10702   bool template_p = false;
10703   tree id;
10704
10705   cp_token *token = cp_lexer_peek_token (parser->lexer);
10706
10707   /* `typename' is not allowed in this context ([temp.res]).  */
10708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10709     {
10710       error_at (token->location, 
10711                 "keyword %<typename%> not allowed in this context (a qualified "
10712                 "member initializer is implicitly a type)");
10713       cp_lexer_consume_token (parser->lexer);
10714     }
10715   /* Look for the optional `::' operator.  */
10716   global_scope_p
10717     = (cp_parser_global_scope_opt (parser,
10718                                    /*current_scope_valid_p=*/false)
10719        != NULL_TREE);
10720   /* Look for the optional nested-name-specifier.  The simplest way to
10721      implement:
10722
10723        [temp.res]
10724
10725        The keyword `typename' is not permitted in a base-specifier or
10726        mem-initializer; in these contexts a qualified name that
10727        depends on a template-parameter is implicitly assumed to be a
10728        type name.
10729
10730      is to assume that we have seen the `typename' keyword at this
10731      point.  */
10732   nested_name_specifier_p
10733     = (cp_parser_nested_name_specifier_opt (parser,
10734                                             /*typename_keyword_p=*/true,
10735                                             /*check_dependency_p=*/true,
10736                                             /*type_p=*/true,
10737                                             /*is_declaration=*/true)
10738        != NULL_TREE);
10739   if (nested_name_specifier_p)
10740     template_p = cp_parser_optional_template_keyword (parser);
10741   /* If there is a `::' operator or a nested-name-specifier, then we
10742      are definitely looking for a class-name.  */
10743   if (global_scope_p || nested_name_specifier_p)
10744     return cp_parser_class_name (parser,
10745                                  /*typename_keyword_p=*/true,
10746                                  /*template_keyword_p=*/template_p,
10747                                  typename_type,
10748                                  /*check_dependency_p=*/true,
10749                                  /*class_head_p=*/false,
10750                                  /*is_declaration=*/true);
10751   /* Otherwise, we could also be looking for an ordinary identifier.  */
10752   cp_parser_parse_tentatively (parser);
10753   /* Try a class-name.  */
10754   id = cp_parser_class_name (parser,
10755                              /*typename_keyword_p=*/true,
10756                              /*template_keyword_p=*/false,
10757                              none_type,
10758                              /*check_dependency_p=*/true,
10759                              /*class_head_p=*/false,
10760                              /*is_declaration=*/true);
10761   /* If we found one, we're done.  */
10762   if (cp_parser_parse_definitely (parser))
10763     return id;
10764   /* Otherwise, look for an ordinary identifier.  */
10765   return cp_parser_identifier (parser);
10766 }
10767
10768 /* Overloading [gram.over] */
10769
10770 /* Parse an operator-function-id.
10771
10772    operator-function-id:
10773      operator operator
10774
10775    Returns an IDENTIFIER_NODE for the operator which is a
10776    human-readable spelling of the identifier, e.g., `operator +'.  */
10777
10778 static tree
10779 cp_parser_operator_function_id (cp_parser* parser)
10780 {
10781   /* Look for the `operator' keyword.  */
10782   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10783     return error_mark_node;
10784   /* And then the name of the operator itself.  */
10785   return cp_parser_operator (parser);
10786 }
10787
10788 /* Parse an operator.
10789
10790    operator:
10791      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10792      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10793      || ++ -- , ->* -> () []
10794
10795    GNU Extensions:
10796
10797    operator:
10798      <? >? <?= >?=
10799
10800    Returns an IDENTIFIER_NODE for the operator which is a
10801    human-readable spelling of the identifier, e.g., `operator +'.  */
10802
10803 static tree
10804 cp_parser_operator (cp_parser* parser)
10805 {
10806   tree id = NULL_TREE;
10807   cp_token *token;
10808
10809   /* Peek at the next token.  */
10810   token = cp_lexer_peek_token (parser->lexer);
10811   /* Figure out which operator we have.  */
10812   switch (token->type)
10813     {
10814     case CPP_KEYWORD:
10815       {
10816         enum tree_code op;
10817
10818         /* The keyword should be either `new' or `delete'.  */
10819         if (token->keyword == RID_NEW)
10820           op = NEW_EXPR;
10821         else if (token->keyword == RID_DELETE)
10822           op = DELETE_EXPR;
10823         else
10824           break;
10825
10826         /* Consume the `new' or `delete' token.  */
10827         cp_lexer_consume_token (parser->lexer);
10828
10829         /* Peek at the next token.  */
10830         token = cp_lexer_peek_token (parser->lexer);
10831         /* If it's a `[' token then this is the array variant of the
10832            operator.  */
10833         if (token->type == CPP_OPEN_SQUARE)
10834           {
10835             /* Consume the `[' token.  */
10836             cp_lexer_consume_token (parser->lexer);
10837             /* Look for the `]' token.  */
10838             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10839             id = ansi_opname (op == NEW_EXPR
10840                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10841           }
10842         /* Otherwise, we have the non-array variant.  */
10843         else
10844           id = ansi_opname (op);
10845
10846         return id;
10847       }
10848
10849     case CPP_PLUS:
10850       id = ansi_opname (PLUS_EXPR);
10851       break;
10852
10853     case CPP_MINUS:
10854       id = ansi_opname (MINUS_EXPR);
10855       break;
10856
10857     case CPP_MULT:
10858       id = ansi_opname (MULT_EXPR);
10859       break;
10860
10861     case CPP_DIV:
10862       id = ansi_opname (TRUNC_DIV_EXPR);
10863       break;
10864
10865     case CPP_MOD:
10866       id = ansi_opname (TRUNC_MOD_EXPR);
10867       break;
10868
10869     case CPP_XOR:
10870       id = ansi_opname (BIT_XOR_EXPR);
10871       break;
10872
10873     case CPP_AND:
10874       id = ansi_opname (BIT_AND_EXPR);
10875       break;
10876
10877     case CPP_OR:
10878       id = ansi_opname (BIT_IOR_EXPR);
10879       break;
10880
10881     case CPP_COMPL:
10882       id = ansi_opname (BIT_NOT_EXPR);
10883       break;
10884
10885     case CPP_NOT:
10886       id = ansi_opname (TRUTH_NOT_EXPR);
10887       break;
10888
10889     case CPP_EQ:
10890       id = ansi_assopname (NOP_EXPR);
10891       break;
10892
10893     case CPP_LESS:
10894       id = ansi_opname (LT_EXPR);
10895       break;
10896
10897     case CPP_GREATER:
10898       id = ansi_opname (GT_EXPR);
10899       break;
10900
10901     case CPP_PLUS_EQ:
10902       id = ansi_assopname (PLUS_EXPR);
10903       break;
10904
10905     case CPP_MINUS_EQ:
10906       id = ansi_assopname (MINUS_EXPR);
10907       break;
10908
10909     case CPP_MULT_EQ:
10910       id = ansi_assopname (MULT_EXPR);
10911       break;
10912
10913     case CPP_DIV_EQ:
10914       id = ansi_assopname (TRUNC_DIV_EXPR);
10915       break;
10916
10917     case CPP_MOD_EQ:
10918       id = ansi_assopname (TRUNC_MOD_EXPR);
10919       break;
10920
10921     case CPP_XOR_EQ:
10922       id = ansi_assopname (BIT_XOR_EXPR);
10923       break;
10924
10925     case CPP_AND_EQ:
10926       id = ansi_assopname (BIT_AND_EXPR);
10927       break;
10928
10929     case CPP_OR_EQ:
10930       id = ansi_assopname (BIT_IOR_EXPR);
10931       break;
10932
10933     case CPP_LSHIFT:
10934       id = ansi_opname (LSHIFT_EXPR);
10935       break;
10936
10937     case CPP_RSHIFT:
10938       id = ansi_opname (RSHIFT_EXPR);
10939       break;
10940
10941     case CPP_LSHIFT_EQ:
10942       id = ansi_assopname (LSHIFT_EXPR);
10943       break;
10944
10945     case CPP_RSHIFT_EQ:
10946       id = ansi_assopname (RSHIFT_EXPR);
10947       break;
10948
10949     case CPP_EQ_EQ:
10950       id = ansi_opname (EQ_EXPR);
10951       break;
10952
10953     case CPP_NOT_EQ:
10954       id = ansi_opname (NE_EXPR);
10955       break;
10956
10957     case CPP_LESS_EQ:
10958       id = ansi_opname (LE_EXPR);
10959       break;
10960
10961     case CPP_GREATER_EQ:
10962       id = ansi_opname (GE_EXPR);
10963       break;
10964
10965     case CPP_AND_AND:
10966       id = ansi_opname (TRUTH_ANDIF_EXPR);
10967       break;
10968
10969     case CPP_OR_OR:
10970       id = ansi_opname (TRUTH_ORIF_EXPR);
10971       break;
10972
10973     case CPP_PLUS_PLUS:
10974       id = ansi_opname (POSTINCREMENT_EXPR);
10975       break;
10976
10977     case CPP_MINUS_MINUS:
10978       id = ansi_opname (PREDECREMENT_EXPR);
10979       break;
10980
10981     case CPP_COMMA:
10982       id = ansi_opname (COMPOUND_EXPR);
10983       break;
10984
10985     case CPP_DEREF_STAR:
10986       id = ansi_opname (MEMBER_REF);
10987       break;
10988
10989     case CPP_DEREF:
10990       id = ansi_opname (COMPONENT_REF);
10991       break;
10992
10993     case CPP_OPEN_PAREN:
10994       /* Consume the `('.  */
10995       cp_lexer_consume_token (parser->lexer);
10996       /* Look for the matching `)'.  */
10997       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10998       return ansi_opname (CALL_EXPR);
10999
11000     case CPP_OPEN_SQUARE:
11001       /* Consume the `['.  */
11002       cp_lexer_consume_token (parser->lexer);
11003       /* Look for the matching `]'.  */
11004       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11005       return ansi_opname (ARRAY_REF);
11006
11007     default:
11008       /* Anything else is an error.  */
11009       break;
11010     }
11011
11012   /* If we have selected an identifier, we need to consume the
11013      operator token.  */
11014   if (id)
11015     cp_lexer_consume_token (parser->lexer);
11016   /* Otherwise, no valid operator name was present.  */
11017   else
11018     {
11019       cp_parser_error (parser, "expected operator");
11020       id = error_mark_node;
11021     }
11022
11023   return id;
11024 }
11025
11026 /* Parse a template-declaration.
11027
11028    template-declaration:
11029      export [opt] template < template-parameter-list > declaration
11030
11031    If MEMBER_P is TRUE, this template-declaration occurs within a
11032    class-specifier.
11033
11034    The grammar rule given by the standard isn't correct.  What
11035    is really meant is:
11036
11037    template-declaration:
11038      export [opt] template-parameter-list-seq
11039        decl-specifier-seq [opt] init-declarator [opt] ;
11040      export [opt] template-parameter-list-seq
11041        function-definition
11042
11043    template-parameter-list-seq:
11044      template-parameter-list-seq [opt]
11045      template < template-parameter-list >  */
11046
11047 static void
11048 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11049 {
11050   /* Check for `export'.  */
11051   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11052     {
11053       /* Consume the `export' token.  */
11054       cp_lexer_consume_token (parser->lexer);
11055       /* Warn that we do not support `export'.  */
11056       warning (0, "keyword %<export%> not implemented, and will be ignored");
11057     }
11058
11059   cp_parser_template_declaration_after_export (parser, member_p);
11060 }
11061
11062 /* Parse a template-parameter-list.
11063
11064    template-parameter-list:
11065      template-parameter
11066      template-parameter-list , template-parameter
11067
11068    Returns a TREE_LIST.  Each node represents a template parameter.
11069    The nodes are connected via their TREE_CHAINs.  */
11070
11071 static tree
11072 cp_parser_template_parameter_list (cp_parser* parser)
11073 {
11074   tree parameter_list = NULL_TREE;
11075
11076   begin_template_parm_list ();
11077
11078   /* The loop below parses the template parms.  We first need to know
11079      the total number of template parms to be able to compute proper
11080      canonical types of each dependent type. So after the loop, when
11081      we know the total number of template parms,
11082      end_template_parm_list computes the proper canonical types and
11083      fixes up the dependent types accordingly.  */
11084   while (true)
11085     {
11086       tree parameter;
11087       bool is_non_type;
11088       bool is_parameter_pack;
11089       location_t parm_loc;
11090
11091       /* Parse the template-parameter.  */
11092       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11093       parameter = cp_parser_template_parameter (parser, 
11094                                                 &is_non_type,
11095                                                 &is_parameter_pack);
11096       /* Add it to the list.  */
11097       if (parameter != error_mark_node)
11098         parameter_list = process_template_parm (parameter_list,
11099                                                 parm_loc,
11100                                                 parameter,
11101                                                 is_non_type,
11102                                                 is_parameter_pack,
11103                                                 0);
11104       else
11105        {
11106          tree err_parm = build_tree_list (parameter, parameter);
11107          parameter_list = chainon (parameter_list, err_parm);
11108        }
11109
11110       /* If the next token is not a `,', we're done.  */
11111       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11112         break;
11113       /* Otherwise, consume the `,' token.  */
11114       cp_lexer_consume_token (parser->lexer);
11115     }
11116
11117   return end_template_parm_list (parameter_list);
11118 }
11119
11120 /* Parse a template-parameter.
11121
11122    template-parameter:
11123      type-parameter
11124      parameter-declaration
11125
11126    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11127    the parameter.  The TREE_PURPOSE is the default value, if any.
11128    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11129    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11130    set to true iff this parameter is a parameter pack. */
11131
11132 static tree
11133 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11134                               bool *is_parameter_pack)
11135 {
11136   cp_token *token;
11137   cp_parameter_declarator *parameter_declarator;
11138   cp_declarator *id_declarator;
11139   tree parm;
11140
11141   /* Assume it is a type parameter or a template parameter.  */
11142   *is_non_type = false;
11143   /* Assume it not a parameter pack. */
11144   *is_parameter_pack = false;
11145   /* Peek at the next token.  */
11146   token = cp_lexer_peek_token (parser->lexer);
11147   /* If it is `class' or `template', we have a type-parameter.  */
11148   if (token->keyword == RID_TEMPLATE)
11149     return cp_parser_type_parameter (parser, is_parameter_pack);
11150   /* If it is `class' or `typename' we do not know yet whether it is a
11151      type parameter or a non-type parameter.  Consider:
11152
11153        template <typename T, typename T::X X> ...
11154
11155      or:
11156
11157        template <class C, class D*> ...
11158
11159      Here, the first parameter is a type parameter, and the second is
11160      a non-type parameter.  We can tell by looking at the token after
11161      the identifier -- if it is a `,', `=', or `>' then we have a type
11162      parameter.  */
11163   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11164     {
11165       /* Peek at the token after `class' or `typename'.  */
11166       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11167       /* If it's an ellipsis, we have a template type parameter
11168          pack. */
11169       if (token->type == CPP_ELLIPSIS)
11170         return cp_parser_type_parameter (parser, is_parameter_pack);
11171       /* If it's an identifier, skip it.  */
11172       if (token->type == CPP_NAME)
11173         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11174       /* Now, see if the token looks like the end of a template
11175          parameter.  */
11176       if (token->type == CPP_COMMA
11177           || token->type == CPP_EQ
11178           || token->type == CPP_GREATER)
11179         return cp_parser_type_parameter (parser, is_parameter_pack);
11180     }
11181
11182   /* Otherwise, it is a non-type parameter.
11183
11184      [temp.param]
11185
11186      When parsing a default template-argument for a non-type
11187      template-parameter, the first non-nested `>' is taken as the end
11188      of the template parameter-list rather than a greater-than
11189      operator.  */
11190   *is_non_type = true;
11191   parameter_declarator
11192      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11193                                         /*parenthesized_p=*/NULL);
11194
11195   /* If the parameter declaration is marked as a parameter pack, set
11196      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11197      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11198      grokdeclarator. */
11199   if (parameter_declarator
11200       && parameter_declarator->declarator
11201       && parameter_declarator->declarator->parameter_pack_p)
11202     {
11203       *is_parameter_pack = true;
11204       parameter_declarator->declarator->parameter_pack_p = false;
11205     }
11206
11207   /* If the next token is an ellipsis, and we don't already have it
11208      marked as a parameter pack, then we have a parameter pack (that
11209      has no declarator).  */
11210   if (!*is_parameter_pack
11211       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11212       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11213     {
11214       /* Consume the `...'.  */
11215       cp_lexer_consume_token (parser->lexer);
11216       maybe_warn_variadic_templates ();
11217       
11218       *is_parameter_pack = true;
11219     }
11220   /* We might end up with a pack expansion as the type of the non-type
11221      template parameter, in which case this is a non-type template
11222      parameter pack.  */
11223   else if (parameter_declarator
11224            && parameter_declarator->decl_specifiers.type
11225            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11226     {
11227       *is_parameter_pack = true;
11228       parameter_declarator->decl_specifiers.type = 
11229         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11230     }
11231
11232   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11233     {
11234       /* Parameter packs cannot have default arguments.  However, a
11235          user may try to do so, so we'll parse them and give an
11236          appropriate diagnostic here.  */
11237
11238       /* Consume the `='.  */
11239       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11240       cp_lexer_consume_token (parser->lexer);
11241       
11242       /* Find the name of the parameter pack.  */     
11243       id_declarator = parameter_declarator->declarator;
11244       while (id_declarator && id_declarator->kind != cdk_id)
11245         id_declarator = id_declarator->declarator;
11246       
11247       if (id_declarator && id_declarator->kind == cdk_id)
11248         error_at (start_token->location,
11249                   "template parameter pack %qD cannot have a default argument",
11250                   id_declarator->u.id.unqualified_name);
11251       else
11252         error_at (start_token->location,
11253                   "template parameter pack cannot have a default argument");
11254       
11255       /* Parse the default argument, but throw away the result.  */
11256       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11257     }
11258
11259   parm = grokdeclarator (parameter_declarator->declarator,
11260                          &parameter_declarator->decl_specifiers,
11261                          TPARM, /*initialized=*/0,
11262                          /*attrlist=*/NULL);
11263   if (parm == error_mark_node)
11264     return error_mark_node;
11265
11266   return build_tree_list (parameter_declarator->default_argument, parm);
11267 }
11268
11269 /* Parse a type-parameter.
11270
11271    type-parameter:
11272      class identifier [opt]
11273      class identifier [opt] = type-id
11274      typename identifier [opt]
11275      typename identifier [opt] = type-id
11276      template < template-parameter-list > class identifier [opt]
11277      template < template-parameter-list > class identifier [opt]
11278        = id-expression
11279
11280    GNU Extension (variadic templates):
11281
11282    type-parameter:
11283      class ... identifier [opt]
11284      typename ... identifier [opt]
11285
11286    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11287    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11288    the declaration of the parameter.
11289
11290    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11291
11292 static tree
11293 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11294 {
11295   cp_token *token;
11296   tree parameter;
11297
11298   /* Look for a keyword to tell us what kind of parameter this is.  */
11299   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11300   if (!token)
11301     return error_mark_node;
11302
11303   switch (token->keyword)
11304     {
11305     case RID_CLASS:
11306     case RID_TYPENAME:
11307       {
11308         tree identifier;
11309         tree default_argument;
11310
11311         /* If the next token is an ellipsis, we have a template
11312            argument pack. */
11313         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11314           {
11315             /* Consume the `...' token. */
11316             cp_lexer_consume_token (parser->lexer);
11317             maybe_warn_variadic_templates ();
11318
11319             *is_parameter_pack = true;
11320           }
11321
11322         /* If the next token is an identifier, then it names the
11323            parameter.  */
11324         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11325           identifier = cp_parser_identifier (parser);
11326         else
11327           identifier = NULL_TREE;
11328
11329         /* Create the parameter.  */
11330         parameter = finish_template_type_parm (class_type_node, identifier);
11331
11332         /* If the next token is an `=', we have a default argument.  */
11333         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11334           {
11335             /* Consume the `=' token.  */
11336             cp_lexer_consume_token (parser->lexer);
11337             /* Parse the default-argument.  */
11338             push_deferring_access_checks (dk_no_deferred);
11339             default_argument = cp_parser_type_id (parser);
11340
11341             /* Template parameter packs cannot have default
11342                arguments. */
11343             if (*is_parameter_pack)
11344               {
11345                 if (identifier)
11346                   error_at (token->location,
11347                             "template parameter pack %qD cannot have a "
11348                             "default argument", identifier);
11349                 else
11350                   error_at (token->location,
11351                             "template parameter packs cannot have "
11352                             "default arguments");
11353                 default_argument = NULL_TREE;
11354               }
11355             pop_deferring_access_checks ();
11356           }
11357         else
11358           default_argument = NULL_TREE;
11359
11360         /* Create the combined representation of the parameter and the
11361            default argument.  */
11362         parameter = build_tree_list (default_argument, parameter);
11363       }
11364       break;
11365
11366     case RID_TEMPLATE:
11367       {
11368         tree identifier;
11369         tree default_argument;
11370
11371         /* Look for the `<'.  */
11372         cp_parser_require (parser, CPP_LESS, RT_LESS);
11373         /* Parse the template-parameter-list.  */
11374         cp_parser_template_parameter_list (parser);
11375         /* Look for the `>'.  */
11376         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11377         /* Look for the `class' keyword.  */
11378         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11379         /* If the next token is an ellipsis, we have a template
11380            argument pack. */
11381         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11382           {
11383             /* Consume the `...' token. */
11384             cp_lexer_consume_token (parser->lexer);
11385             maybe_warn_variadic_templates ();
11386
11387             *is_parameter_pack = true;
11388           }
11389         /* If the next token is an `=', then there is a
11390            default-argument.  If the next token is a `>', we are at
11391            the end of the parameter-list.  If the next token is a `,',
11392            then we are at the end of this parameter.  */
11393         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11394             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11395             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11396           {
11397             identifier = cp_parser_identifier (parser);
11398             /* Treat invalid names as if the parameter were nameless.  */
11399             if (identifier == error_mark_node)
11400               identifier = NULL_TREE;
11401           }
11402         else
11403           identifier = NULL_TREE;
11404
11405         /* Create the template parameter.  */
11406         parameter = finish_template_template_parm (class_type_node,
11407                                                    identifier);
11408
11409         /* If the next token is an `=', then there is a
11410            default-argument.  */
11411         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11412           {
11413             bool is_template;
11414
11415             /* Consume the `='.  */
11416             cp_lexer_consume_token (parser->lexer);
11417             /* Parse the id-expression.  */
11418             push_deferring_access_checks (dk_no_deferred);
11419             /* save token before parsing the id-expression, for error
11420                reporting */
11421             token = cp_lexer_peek_token (parser->lexer);
11422             default_argument
11423               = cp_parser_id_expression (parser,
11424                                          /*template_keyword_p=*/false,
11425                                          /*check_dependency_p=*/true,
11426                                          /*template_p=*/&is_template,
11427                                          /*declarator_p=*/false,
11428                                          /*optional_p=*/false);
11429             if (TREE_CODE (default_argument) == TYPE_DECL)
11430               /* If the id-expression was a template-id that refers to
11431                  a template-class, we already have the declaration here,
11432                  so no further lookup is needed.  */
11433                  ;
11434             else
11435               /* Look up the name.  */
11436               default_argument
11437                 = cp_parser_lookup_name (parser, default_argument,
11438                                          none_type,
11439                                          /*is_template=*/is_template,
11440                                          /*is_namespace=*/false,
11441                                          /*check_dependency=*/true,
11442                                          /*ambiguous_decls=*/NULL,
11443                                          token->location);
11444             /* See if the default argument is valid.  */
11445             default_argument
11446               = check_template_template_default_arg (default_argument);
11447
11448             /* Template parameter packs cannot have default
11449                arguments. */
11450             if (*is_parameter_pack)
11451               {
11452                 if (identifier)
11453                   error_at (token->location,
11454                             "template parameter pack %qD cannot "
11455                             "have a default argument",
11456                             identifier);
11457                 else
11458                   error_at (token->location, "template parameter packs cannot "
11459                             "have default arguments");
11460                 default_argument = NULL_TREE;
11461               }
11462             pop_deferring_access_checks ();
11463           }
11464         else
11465           default_argument = NULL_TREE;
11466
11467         /* Create the combined representation of the parameter and the
11468            default argument.  */
11469         parameter = build_tree_list (default_argument, parameter);
11470       }
11471       break;
11472
11473     default:
11474       gcc_unreachable ();
11475       break;
11476     }
11477
11478   return parameter;
11479 }
11480
11481 /* Parse a template-id.
11482
11483    template-id:
11484      template-name < template-argument-list [opt] >
11485
11486    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11487    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11488    returned.  Otherwise, if the template-name names a function, or set
11489    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11490    names a class, returns a TYPE_DECL for the specialization.
11491
11492    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11493    uninstantiated templates.  */
11494
11495 static tree
11496 cp_parser_template_id (cp_parser *parser,
11497                        bool template_keyword_p,
11498                        bool check_dependency_p,
11499                        bool is_declaration)
11500 {
11501   int i;
11502   tree templ;
11503   tree arguments;
11504   tree template_id;
11505   cp_token_position start_of_id = 0;
11506   deferred_access_check *chk;
11507   VEC (deferred_access_check,gc) *access_check;
11508   cp_token *next_token = NULL, *next_token_2 = NULL;
11509   bool is_identifier;
11510
11511   /* If the next token corresponds to a template-id, there is no need
11512      to reparse it.  */
11513   next_token = cp_lexer_peek_token (parser->lexer);
11514   if (next_token->type == CPP_TEMPLATE_ID)
11515     {
11516       struct tree_check *check_value;
11517
11518       /* Get the stored value.  */
11519       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11520       /* Perform any access checks that were deferred.  */
11521       access_check = check_value->checks;
11522       if (access_check)
11523         {
11524           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11525             perform_or_defer_access_check (chk->binfo,
11526                                            chk->decl,
11527                                            chk->diag_decl);
11528         }
11529       /* Return the stored value.  */
11530       return check_value->value;
11531     }
11532
11533   /* Avoid performing name lookup if there is no possibility of
11534      finding a template-id.  */
11535   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11536       || (next_token->type == CPP_NAME
11537           && !cp_parser_nth_token_starts_template_argument_list_p
11538                (parser, 2)))
11539     {
11540       cp_parser_error (parser, "expected template-id");
11541       return error_mark_node;
11542     }
11543
11544   /* Remember where the template-id starts.  */
11545   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11546     start_of_id = cp_lexer_token_position (parser->lexer, false);
11547
11548   push_deferring_access_checks (dk_deferred);
11549
11550   /* Parse the template-name.  */
11551   is_identifier = false;
11552   templ = cp_parser_template_name (parser, template_keyword_p,
11553                                    check_dependency_p,
11554                                    is_declaration,
11555                                    &is_identifier);
11556   if (templ == error_mark_node || is_identifier)
11557     {
11558       pop_deferring_access_checks ();
11559       return templ;
11560     }
11561
11562   /* If we find the sequence `[:' after a template-name, it's probably
11563      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11564      parse correctly the argument list.  */
11565   next_token = cp_lexer_peek_token (parser->lexer);
11566   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11567   if (next_token->type == CPP_OPEN_SQUARE
11568       && next_token->flags & DIGRAPH
11569       && next_token_2->type == CPP_COLON
11570       && !(next_token_2->flags & PREV_WHITE))
11571     {
11572       cp_parser_parse_tentatively (parser);
11573       /* Change `:' into `::'.  */
11574       next_token_2->type = CPP_SCOPE;
11575       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11576          CPP_LESS.  */
11577       cp_lexer_consume_token (parser->lexer);
11578
11579       /* Parse the arguments.  */
11580       arguments = cp_parser_enclosed_template_argument_list (parser);
11581       if (!cp_parser_parse_definitely (parser))
11582         {
11583           /* If we couldn't parse an argument list, then we revert our changes
11584              and return simply an error. Maybe this is not a template-id
11585              after all.  */
11586           next_token_2->type = CPP_COLON;
11587           cp_parser_error (parser, "expected %<<%>");
11588           pop_deferring_access_checks ();
11589           return error_mark_node;
11590         }
11591       /* Otherwise, emit an error about the invalid digraph, but continue
11592          parsing because we got our argument list.  */
11593       if (permerror (next_token->location,
11594                      "%<<::%> cannot begin a template-argument list"))
11595         {
11596           static bool hint = false;
11597           inform (next_token->location,
11598                   "%<<:%> is an alternate spelling for %<[%>."
11599                   " Insert whitespace between %<<%> and %<::%>");
11600           if (!hint && !flag_permissive)
11601             {
11602               inform (next_token->location, "(if you use %<-fpermissive%>"
11603                       " G++ will accept your code)");
11604               hint = true;
11605             }
11606         }
11607     }
11608   else
11609     {
11610       /* Look for the `<' that starts the template-argument-list.  */
11611       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11612         {
11613           pop_deferring_access_checks ();
11614           return error_mark_node;
11615         }
11616       /* Parse the arguments.  */
11617       arguments = cp_parser_enclosed_template_argument_list (parser);
11618     }
11619
11620   /* Build a representation of the specialization.  */
11621   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11622     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11623   else if (DECL_CLASS_TEMPLATE_P (templ)
11624            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11625     {
11626       bool entering_scope;
11627       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11628          template (rather than some instantiation thereof) only if
11629          is not nested within some other construct.  For example, in
11630          "template <typename T> void f(T) { A<T>::", A<T> is just an
11631          instantiation of A.  */
11632       entering_scope = (template_parm_scope_p ()
11633                         && cp_lexer_next_token_is (parser->lexer,
11634                                                    CPP_SCOPE));
11635       template_id
11636         = finish_template_type (templ, arguments, entering_scope);
11637     }
11638   else
11639     {
11640       /* If it's not a class-template or a template-template, it should be
11641          a function-template.  */
11642       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11643                    || TREE_CODE (templ) == OVERLOAD
11644                    || BASELINK_P (templ)));
11645
11646       template_id = lookup_template_function (templ, arguments);
11647     }
11648
11649   /* If parsing tentatively, replace the sequence of tokens that makes
11650      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11651      should we re-parse the token stream, we will not have to repeat
11652      the effort required to do the parse, nor will we issue duplicate
11653      error messages about problems during instantiation of the
11654      template.  */
11655   if (start_of_id)
11656     {
11657       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11658
11659       /* Reset the contents of the START_OF_ID token.  */
11660       token->type = CPP_TEMPLATE_ID;
11661       /* Retrieve any deferred checks.  Do not pop this access checks yet
11662          so the memory will not be reclaimed during token replacing below.  */
11663       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11664       token->u.tree_check_value->value = template_id;
11665       token->u.tree_check_value->checks = get_deferred_access_checks ();
11666       token->keyword = RID_MAX;
11667
11668       /* Purge all subsequent tokens.  */
11669       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11670
11671       /* ??? Can we actually assume that, if template_id ==
11672          error_mark_node, we will have issued a diagnostic to the
11673          user, as opposed to simply marking the tentative parse as
11674          failed?  */
11675       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11676         error_at (token->location, "parse error in template argument list");
11677     }
11678
11679   pop_deferring_access_checks ();
11680   return template_id;
11681 }
11682
11683 /* Parse a template-name.
11684
11685    template-name:
11686      identifier
11687
11688    The standard should actually say:
11689
11690    template-name:
11691      identifier
11692      operator-function-id
11693
11694    A defect report has been filed about this issue.
11695
11696    A conversion-function-id cannot be a template name because they cannot
11697    be part of a template-id. In fact, looking at this code:
11698
11699    a.operator K<int>()
11700
11701    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11702    It is impossible to call a templated conversion-function-id with an
11703    explicit argument list, since the only allowed template parameter is
11704    the type to which it is converting.
11705
11706    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11707    `template' keyword, in a construction like:
11708
11709      T::template f<3>()
11710
11711    In that case `f' is taken to be a template-name, even though there
11712    is no way of knowing for sure.
11713
11714    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11715    name refers to a set of overloaded functions, at least one of which
11716    is a template, or an IDENTIFIER_NODE with the name of the template,
11717    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11718    names are looked up inside uninstantiated templates.  */
11719
11720 static tree
11721 cp_parser_template_name (cp_parser* parser,
11722                          bool template_keyword_p,
11723                          bool check_dependency_p,
11724                          bool is_declaration,
11725                          bool *is_identifier)
11726 {
11727   tree identifier;
11728   tree decl;
11729   tree fns;
11730   cp_token *token = cp_lexer_peek_token (parser->lexer);
11731
11732   /* If the next token is `operator', then we have either an
11733      operator-function-id or a conversion-function-id.  */
11734   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11735     {
11736       /* We don't know whether we're looking at an
11737          operator-function-id or a conversion-function-id.  */
11738       cp_parser_parse_tentatively (parser);
11739       /* Try an operator-function-id.  */
11740       identifier = cp_parser_operator_function_id (parser);
11741       /* If that didn't work, try a conversion-function-id.  */
11742       if (!cp_parser_parse_definitely (parser))
11743         {
11744           cp_parser_error (parser, "expected template-name");
11745           return error_mark_node;
11746         }
11747     }
11748   /* Look for the identifier.  */
11749   else
11750     identifier = cp_parser_identifier (parser);
11751
11752   /* If we didn't find an identifier, we don't have a template-id.  */
11753   if (identifier == error_mark_node)
11754     return error_mark_node;
11755
11756   /* If the name immediately followed the `template' keyword, then it
11757      is a template-name.  However, if the next token is not `<', then
11758      we do not treat it as a template-name, since it is not being used
11759      as part of a template-id.  This enables us to handle constructs
11760      like:
11761
11762        template <typename T> struct S { S(); };
11763        template <typename T> S<T>::S();
11764
11765      correctly.  We would treat `S' as a template -- if it were `S<T>'
11766      -- but we do not if there is no `<'.  */
11767
11768   if (processing_template_decl
11769       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11770     {
11771       /* In a declaration, in a dependent context, we pretend that the
11772          "template" keyword was present in order to improve error
11773          recovery.  For example, given:
11774
11775            template <typename T> void f(T::X<int>);
11776
11777          we want to treat "X<int>" as a template-id.  */
11778       if (is_declaration
11779           && !template_keyword_p
11780           && parser->scope && TYPE_P (parser->scope)
11781           && check_dependency_p
11782           && dependent_scope_p (parser->scope)
11783           /* Do not do this for dtors (or ctors), since they never
11784              need the template keyword before their name.  */
11785           && !constructor_name_p (identifier, parser->scope))
11786         {
11787           cp_token_position start = 0;
11788
11789           /* Explain what went wrong.  */
11790           error_at (token->location, "non-template %qD used as template",
11791                     identifier);
11792           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11793                   parser->scope, identifier);
11794           /* If parsing tentatively, find the location of the "<" token.  */
11795           if (cp_parser_simulate_error (parser))
11796             start = cp_lexer_token_position (parser->lexer, true);
11797           /* Parse the template arguments so that we can issue error
11798              messages about them.  */
11799           cp_lexer_consume_token (parser->lexer);
11800           cp_parser_enclosed_template_argument_list (parser);
11801           /* Skip tokens until we find a good place from which to
11802              continue parsing.  */
11803           cp_parser_skip_to_closing_parenthesis (parser,
11804                                                  /*recovering=*/true,
11805                                                  /*or_comma=*/true,
11806                                                  /*consume_paren=*/false);
11807           /* If parsing tentatively, permanently remove the
11808              template argument list.  That will prevent duplicate
11809              error messages from being issued about the missing
11810              "template" keyword.  */
11811           if (start)
11812             cp_lexer_purge_tokens_after (parser->lexer, start);
11813           if (is_identifier)
11814             *is_identifier = true;
11815           return identifier;
11816         }
11817
11818       /* If the "template" keyword is present, then there is generally
11819          no point in doing name-lookup, so we just return IDENTIFIER.
11820          But, if the qualifying scope is non-dependent then we can
11821          (and must) do name-lookup normally.  */
11822       if (template_keyword_p
11823           && (!parser->scope
11824               || (TYPE_P (parser->scope)
11825                   && dependent_type_p (parser->scope))))
11826         return identifier;
11827     }
11828
11829   /* Look up the name.  */
11830   decl = cp_parser_lookup_name (parser, identifier,
11831                                 none_type,
11832                                 /*is_template=*/true,
11833                                 /*is_namespace=*/false,
11834                                 check_dependency_p,
11835                                 /*ambiguous_decls=*/NULL,
11836                                 token->location);
11837
11838   /* If DECL is a template, then the name was a template-name.  */
11839   if (TREE_CODE (decl) == TEMPLATE_DECL)
11840     ;
11841   else
11842     {
11843       tree fn = NULL_TREE;
11844
11845       /* The standard does not explicitly indicate whether a name that
11846          names a set of overloaded declarations, some of which are
11847          templates, is a template-name.  However, such a name should
11848          be a template-name; otherwise, there is no way to form a
11849          template-id for the overloaded templates.  */
11850       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11851       if (TREE_CODE (fns) == OVERLOAD)
11852         for (fn = fns; fn; fn = OVL_NEXT (fn))
11853           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11854             break;
11855
11856       if (!fn)
11857         {
11858           /* The name does not name a template.  */
11859           cp_parser_error (parser, "expected template-name");
11860           return error_mark_node;
11861         }
11862     }
11863
11864   /* If DECL is dependent, and refers to a function, then just return
11865      its name; we will look it up again during template instantiation.  */
11866   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11867     {
11868       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11869       if (TYPE_P (scope) && dependent_type_p (scope))
11870         return identifier;
11871     }
11872
11873   return decl;
11874 }
11875
11876 /* Parse a template-argument-list.
11877
11878    template-argument-list:
11879      template-argument ... [opt]
11880      template-argument-list , template-argument ... [opt]
11881
11882    Returns a TREE_VEC containing the arguments.  */
11883
11884 static tree
11885 cp_parser_template_argument_list (cp_parser* parser)
11886 {
11887   tree fixed_args[10];
11888   unsigned n_args = 0;
11889   unsigned alloced = 10;
11890   tree *arg_ary = fixed_args;
11891   tree vec;
11892   bool saved_in_template_argument_list_p;
11893   bool saved_ice_p;
11894   bool saved_non_ice_p;
11895
11896   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11897   parser->in_template_argument_list_p = true;
11898   /* Even if the template-id appears in an integral
11899      constant-expression, the contents of the argument list do
11900      not.  */
11901   saved_ice_p = parser->integral_constant_expression_p;
11902   parser->integral_constant_expression_p = false;
11903   saved_non_ice_p = parser->non_integral_constant_expression_p;
11904   parser->non_integral_constant_expression_p = false;
11905   /* Parse the arguments.  */
11906   do
11907     {
11908       tree argument;
11909
11910       if (n_args)
11911         /* Consume the comma.  */
11912         cp_lexer_consume_token (parser->lexer);
11913
11914       /* Parse the template-argument.  */
11915       argument = cp_parser_template_argument (parser);
11916
11917       /* If the next token is an ellipsis, we're expanding a template
11918          argument pack. */
11919       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11920         {
11921           if (argument == error_mark_node)
11922             {
11923               cp_token *token = cp_lexer_peek_token (parser->lexer);
11924               error_at (token->location,
11925                         "expected parameter pack before %<...%>");
11926             }
11927           /* Consume the `...' token. */
11928           cp_lexer_consume_token (parser->lexer);
11929
11930           /* Make the argument into a TYPE_PACK_EXPANSION or
11931              EXPR_PACK_EXPANSION. */
11932           argument = make_pack_expansion (argument);
11933         }
11934
11935       if (n_args == alloced)
11936         {
11937           alloced *= 2;
11938
11939           if (arg_ary == fixed_args)
11940             {
11941               arg_ary = XNEWVEC (tree, alloced);
11942               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11943             }
11944           else
11945             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11946         }
11947       arg_ary[n_args++] = argument;
11948     }
11949   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11950
11951   vec = make_tree_vec (n_args);
11952
11953   while (n_args--)
11954     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11955
11956   if (arg_ary != fixed_args)
11957     free (arg_ary);
11958   parser->non_integral_constant_expression_p = saved_non_ice_p;
11959   parser->integral_constant_expression_p = saved_ice_p;
11960   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11961 #ifdef ENABLE_CHECKING
11962   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11963 #endif
11964   return vec;
11965 }
11966
11967 /* Parse a template-argument.
11968
11969    template-argument:
11970      assignment-expression
11971      type-id
11972      id-expression
11973
11974    The representation is that of an assignment-expression, type-id, or
11975    id-expression -- except that the qualified id-expression is
11976    evaluated, so that the value returned is either a DECL or an
11977    OVERLOAD.
11978
11979    Although the standard says "assignment-expression", it forbids
11980    throw-expressions or assignments in the template argument.
11981    Therefore, we use "conditional-expression" instead.  */
11982
11983 static tree
11984 cp_parser_template_argument (cp_parser* parser)
11985 {
11986   tree argument;
11987   bool template_p;
11988   bool address_p;
11989   bool maybe_type_id = false;
11990   cp_token *token = NULL, *argument_start_token = NULL;
11991   cp_id_kind idk;
11992
11993   /* There's really no way to know what we're looking at, so we just
11994      try each alternative in order.
11995
11996        [temp.arg]
11997
11998        In a template-argument, an ambiguity between a type-id and an
11999        expression is resolved to a type-id, regardless of the form of
12000        the corresponding template-parameter.
12001
12002      Therefore, we try a type-id first.  */
12003   cp_parser_parse_tentatively (parser);
12004   argument = cp_parser_template_type_arg (parser);
12005   /* If there was no error parsing the type-id but the next token is a
12006      '>>', our behavior depends on which dialect of C++ we're
12007      parsing. In C++98, we probably found a typo for '> >'. But there
12008      are type-id which are also valid expressions. For instance:
12009
12010      struct X { int operator >> (int); };
12011      template <int V> struct Foo {};
12012      Foo<X () >> 5> r;
12013
12014      Here 'X()' is a valid type-id of a function type, but the user just
12015      wanted to write the expression "X() >> 5". Thus, we remember that we
12016      found a valid type-id, but we still try to parse the argument as an
12017      expression to see what happens. 
12018
12019      In C++0x, the '>>' will be considered two separate '>'
12020      tokens.  */
12021   if (!cp_parser_error_occurred (parser)
12022       && cxx_dialect == cxx98
12023       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12024     {
12025       maybe_type_id = true;
12026       cp_parser_abort_tentative_parse (parser);
12027     }
12028   else
12029     {
12030       /* If the next token isn't a `,' or a `>', then this argument wasn't
12031       really finished. This means that the argument is not a valid
12032       type-id.  */
12033       if (!cp_parser_next_token_ends_template_argument_p (parser))
12034         cp_parser_error (parser, "expected template-argument");
12035       /* If that worked, we're done.  */
12036       if (cp_parser_parse_definitely (parser))
12037         return argument;
12038     }
12039   /* We're still not sure what the argument will be.  */
12040   cp_parser_parse_tentatively (parser);
12041   /* Try a template.  */
12042   argument_start_token = cp_lexer_peek_token (parser->lexer);
12043   argument = cp_parser_id_expression (parser,
12044                                       /*template_keyword_p=*/false,
12045                                       /*check_dependency_p=*/true,
12046                                       &template_p,
12047                                       /*declarator_p=*/false,
12048                                       /*optional_p=*/false);
12049   /* If the next token isn't a `,' or a `>', then this argument wasn't
12050      really finished.  */
12051   if (!cp_parser_next_token_ends_template_argument_p (parser))
12052     cp_parser_error (parser, "expected template-argument");
12053   if (!cp_parser_error_occurred (parser))
12054     {
12055       /* Figure out what is being referred to.  If the id-expression
12056          was for a class template specialization, then we will have a
12057          TYPE_DECL at this point.  There is no need to do name lookup
12058          at this point in that case.  */
12059       if (TREE_CODE (argument) != TYPE_DECL)
12060         argument = cp_parser_lookup_name (parser, argument,
12061                                           none_type,
12062                                           /*is_template=*/template_p,
12063                                           /*is_namespace=*/false,
12064                                           /*check_dependency=*/true,
12065                                           /*ambiguous_decls=*/NULL,
12066                                           argument_start_token->location);
12067       if (TREE_CODE (argument) != TEMPLATE_DECL
12068           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12069         cp_parser_error (parser, "expected template-name");
12070     }
12071   if (cp_parser_parse_definitely (parser))
12072     return argument;
12073   /* It must be a non-type argument.  There permitted cases are given
12074      in [temp.arg.nontype]:
12075
12076      -- an integral constant-expression of integral or enumeration
12077         type; or
12078
12079      -- the name of a non-type template-parameter; or
12080
12081      -- the name of an object or function with external linkage...
12082
12083      -- the address of an object or function with external linkage...
12084
12085      -- a pointer to member...  */
12086   /* Look for a non-type template parameter.  */
12087   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12088     {
12089       cp_parser_parse_tentatively (parser);
12090       argument = cp_parser_primary_expression (parser,
12091                                                /*address_p=*/false,
12092                                                /*cast_p=*/false,
12093                                                /*template_arg_p=*/true,
12094                                                &idk);
12095       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12096           || !cp_parser_next_token_ends_template_argument_p (parser))
12097         cp_parser_simulate_error (parser);
12098       if (cp_parser_parse_definitely (parser))
12099         return argument;
12100     }
12101
12102   /* If the next token is "&", the argument must be the address of an
12103      object or function with external linkage.  */
12104   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12105   if (address_p)
12106     cp_lexer_consume_token (parser->lexer);
12107   /* See if we might have an id-expression.  */
12108   token = cp_lexer_peek_token (parser->lexer);
12109   if (token->type == CPP_NAME
12110       || token->keyword == RID_OPERATOR
12111       || token->type == CPP_SCOPE
12112       || token->type == CPP_TEMPLATE_ID
12113       || token->type == CPP_NESTED_NAME_SPECIFIER)
12114     {
12115       cp_parser_parse_tentatively (parser);
12116       argument = cp_parser_primary_expression (parser,
12117                                                address_p,
12118                                                /*cast_p=*/false,
12119                                                /*template_arg_p=*/true,
12120                                                &idk);
12121       if (cp_parser_error_occurred (parser)
12122           || !cp_parser_next_token_ends_template_argument_p (parser))
12123         cp_parser_abort_tentative_parse (parser);
12124       else
12125         {
12126           tree probe;
12127
12128           if (TREE_CODE (argument) == INDIRECT_REF)
12129             {
12130               gcc_assert (REFERENCE_REF_P (argument));
12131               argument = TREE_OPERAND (argument, 0);
12132             }
12133
12134           /* If we're in a template, we represent a qualified-id referring
12135              to a static data member as a SCOPE_REF even if the scope isn't
12136              dependent so that we can check access control later.  */
12137           probe = argument;
12138           if (TREE_CODE (probe) == SCOPE_REF)
12139             probe = TREE_OPERAND (probe, 1);
12140           if (TREE_CODE (probe) == VAR_DECL)
12141             {
12142               /* A variable without external linkage might still be a
12143                  valid constant-expression, so no error is issued here
12144                  if the external-linkage check fails.  */
12145               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12146                 cp_parser_simulate_error (parser);
12147             }
12148           else if (is_overloaded_fn (argument))
12149             /* All overloaded functions are allowed; if the external
12150                linkage test does not pass, an error will be issued
12151                later.  */
12152             ;
12153           else if (address_p
12154                    && (TREE_CODE (argument) == OFFSET_REF
12155                        || TREE_CODE (argument) == SCOPE_REF))
12156             /* A pointer-to-member.  */
12157             ;
12158           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12159             ;
12160           else
12161             cp_parser_simulate_error (parser);
12162
12163           if (cp_parser_parse_definitely (parser))
12164             {
12165               if (address_p)
12166                 argument = build_x_unary_op (ADDR_EXPR, argument,
12167                                              tf_warning_or_error);
12168               return argument;
12169             }
12170         }
12171     }
12172   /* If the argument started with "&", there are no other valid
12173      alternatives at this point.  */
12174   if (address_p)
12175     {
12176       cp_parser_error (parser, "invalid non-type template argument");
12177       return error_mark_node;
12178     }
12179
12180   /* If the argument wasn't successfully parsed as a type-id followed
12181      by '>>', the argument can only be a constant expression now.
12182      Otherwise, we try parsing the constant-expression tentatively,
12183      because the argument could really be a type-id.  */
12184   if (maybe_type_id)
12185     cp_parser_parse_tentatively (parser);
12186   argument = cp_parser_constant_expression (parser,
12187                                             /*allow_non_constant_p=*/false,
12188                                             /*non_constant_p=*/NULL);
12189   argument = fold_non_dependent_expr (argument);
12190   if (!maybe_type_id)
12191     return argument;
12192   if (!cp_parser_next_token_ends_template_argument_p (parser))
12193     cp_parser_error (parser, "expected template-argument");
12194   if (cp_parser_parse_definitely (parser))
12195     return argument;
12196   /* We did our best to parse the argument as a non type-id, but that
12197      was the only alternative that matched (albeit with a '>' after
12198      it). We can assume it's just a typo from the user, and a
12199      diagnostic will then be issued.  */
12200   return cp_parser_template_type_arg (parser);
12201 }
12202
12203 /* Parse an explicit-instantiation.
12204
12205    explicit-instantiation:
12206      template declaration
12207
12208    Although the standard says `declaration', what it really means is:
12209
12210    explicit-instantiation:
12211      template decl-specifier-seq [opt] declarator [opt] ;
12212
12213    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12214    supposed to be allowed.  A defect report has been filed about this
12215    issue.
12216
12217    GNU Extension:
12218
12219    explicit-instantiation:
12220      storage-class-specifier template
12221        decl-specifier-seq [opt] declarator [opt] ;
12222      function-specifier template
12223        decl-specifier-seq [opt] declarator [opt] ;  */
12224
12225 static void
12226 cp_parser_explicit_instantiation (cp_parser* parser)
12227 {
12228   int declares_class_or_enum;
12229   cp_decl_specifier_seq decl_specifiers;
12230   tree extension_specifier = NULL_TREE;
12231
12232   /* Look for an (optional) storage-class-specifier or
12233      function-specifier.  */
12234   if (cp_parser_allow_gnu_extensions_p (parser))
12235     {
12236       extension_specifier
12237         = cp_parser_storage_class_specifier_opt (parser);
12238       if (!extension_specifier)
12239         extension_specifier
12240           = cp_parser_function_specifier_opt (parser,
12241                                               /*decl_specs=*/NULL);
12242     }
12243
12244   /* Look for the `template' keyword.  */
12245   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12246   /* Let the front end know that we are processing an explicit
12247      instantiation.  */
12248   begin_explicit_instantiation ();
12249   /* [temp.explicit] says that we are supposed to ignore access
12250      control while processing explicit instantiation directives.  */
12251   push_deferring_access_checks (dk_no_check);
12252   /* Parse a decl-specifier-seq.  */
12253   cp_parser_decl_specifier_seq (parser,
12254                                 CP_PARSER_FLAGS_OPTIONAL,
12255                                 &decl_specifiers,
12256                                 &declares_class_or_enum);
12257   /* If there was exactly one decl-specifier, and it declared a class,
12258      and there's no declarator, then we have an explicit type
12259      instantiation.  */
12260   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12261     {
12262       tree type;
12263
12264       type = check_tag_decl (&decl_specifiers);
12265       /* Turn access control back on for names used during
12266          template instantiation.  */
12267       pop_deferring_access_checks ();
12268       if (type)
12269         do_type_instantiation (type, extension_specifier,
12270                                /*complain=*/tf_error);
12271     }
12272   else
12273     {
12274       cp_declarator *declarator;
12275       tree decl;
12276
12277       /* Parse the declarator.  */
12278       declarator
12279         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12280                                 /*ctor_dtor_or_conv_p=*/NULL,
12281                                 /*parenthesized_p=*/NULL,
12282                                 /*member_p=*/false);
12283       if (declares_class_or_enum & 2)
12284         cp_parser_check_for_definition_in_return_type (declarator,
12285                                                        decl_specifiers.type,
12286                                                        decl_specifiers.type_location);
12287       if (declarator != cp_error_declarator)
12288         {
12289           if (decl_specifiers.specs[(int)ds_inline])
12290             permerror (input_location, "explicit instantiation shall not use"
12291                        " %<inline%> specifier");
12292           if (decl_specifiers.specs[(int)ds_constexpr])
12293             permerror (input_location, "explicit instantiation shall not use"
12294                        " %<constexpr%> specifier");
12295
12296           decl = grokdeclarator (declarator, &decl_specifiers,
12297                                  NORMAL, 0, &decl_specifiers.attributes);
12298           /* Turn access control back on for names used during
12299              template instantiation.  */
12300           pop_deferring_access_checks ();
12301           /* Do the explicit instantiation.  */
12302           do_decl_instantiation (decl, extension_specifier);
12303         }
12304       else
12305         {
12306           pop_deferring_access_checks ();
12307           /* Skip the body of the explicit instantiation.  */
12308           cp_parser_skip_to_end_of_statement (parser);
12309         }
12310     }
12311   /* We're done with the instantiation.  */
12312   end_explicit_instantiation ();
12313
12314   cp_parser_consume_semicolon_at_end_of_statement (parser);
12315 }
12316
12317 /* Parse an explicit-specialization.
12318
12319    explicit-specialization:
12320      template < > declaration
12321
12322    Although the standard says `declaration', what it really means is:
12323
12324    explicit-specialization:
12325      template <> decl-specifier [opt] init-declarator [opt] ;
12326      template <> function-definition
12327      template <> explicit-specialization
12328      template <> template-declaration  */
12329
12330 static void
12331 cp_parser_explicit_specialization (cp_parser* parser)
12332 {
12333   bool need_lang_pop;
12334   cp_token *token = cp_lexer_peek_token (parser->lexer);
12335
12336   /* Look for the `template' keyword.  */
12337   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12338   /* Look for the `<'.  */
12339   cp_parser_require (parser, CPP_LESS, RT_LESS);
12340   /* Look for the `>'.  */
12341   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12342   /* We have processed another parameter list.  */
12343   ++parser->num_template_parameter_lists;
12344   /* [temp]
12345
12346      A template ... explicit specialization ... shall not have C
12347      linkage.  */
12348   if (current_lang_name == lang_name_c)
12349     {
12350       error_at (token->location, "template specialization with C linkage");
12351       /* Give it C++ linkage to avoid confusing other parts of the
12352          front end.  */
12353       push_lang_context (lang_name_cplusplus);
12354       need_lang_pop = true;
12355     }
12356   else
12357     need_lang_pop = false;
12358   /* Let the front end know that we are beginning a specialization.  */
12359   if (!begin_specialization ())
12360     {
12361       end_specialization ();
12362       return;
12363     }
12364
12365   /* If the next keyword is `template', we need to figure out whether
12366      or not we're looking a template-declaration.  */
12367   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12368     {
12369       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12370           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12371         cp_parser_template_declaration_after_export (parser,
12372                                                      /*member_p=*/false);
12373       else
12374         cp_parser_explicit_specialization (parser);
12375     }
12376   else
12377     /* Parse the dependent declaration.  */
12378     cp_parser_single_declaration (parser,
12379                                   /*checks=*/NULL,
12380                                   /*member_p=*/false,
12381                                   /*explicit_specialization_p=*/true,
12382                                   /*friend_p=*/NULL);
12383   /* We're done with the specialization.  */
12384   end_specialization ();
12385   /* For the erroneous case of a template with C linkage, we pushed an
12386      implicit C++ linkage scope; exit that scope now.  */
12387   if (need_lang_pop)
12388     pop_lang_context ();
12389   /* We're done with this parameter list.  */
12390   --parser->num_template_parameter_lists;
12391 }
12392
12393 /* Parse a type-specifier.
12394
12395    type-specifier:
12396      simple-type-specifier
12397      class-specifier
12398      enum-specifier
12399      elaborated-type-specifier
12400      cv-qualifier
12401
12402    GNU Extension:
12403
12404    type-specifier:
12405      __complex__
12406
12407    Returns a representation of the type-specifier.  For a
12408    class-specifier, enum-specifier, or elaborated-type-specifier, a
12409    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12410
12411    The parser flags FLAGS is used to control type-specifier parsing.
12412
12413    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12414    in a decl-specifier-seq.
12415
12416    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12417    class-specifier, enum-specifier, or elaborated-type-specifier, then
12418    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12419    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12420    zero.
12421
12422    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12423    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12424    is set to FALSE.  */
12425
12426 static tree
12427 cp_parser_type_specifier (cp_parser* parser,
12428                           cp_parser_flags flags,
12429                           cp_decl_specifier_seq *decl_specs,
12430                           bool is_declaration,
12431                           int* declares_class_or_enum,
12432                           bool* is_cv_qualifier)
12433 {
12434   tree type_spec = NULL_TREE;
12435   cp_token *token;
12436   enum rid keyword;
12437   cp_decl_spec ds = ds_last;
12438
12439   /* Assume this type-specifier does not declare a new type.  */
12440   if (declares_class_or_enum)
12441     *declares_class_or_enum = 0;
12442   /* And that it does not specify a cv-qualifier.  */
12443   if (is_cv_qualifier)
12444     *is_cv_qualifier = false;
12445   /* Peek at the next token.  */
12446   token = cp_lexer_peek_token (parser->lexer);
12447
12448   /* If we're looking at a keyword, we can use that to guide the
12449      production we choose.  */
12450   keyword = token->keyword;
12451   switch (keyword)
12452     {
12453     case RID_ENUM:
12454       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12455         goto elaborated_type_specifier;
12456
12457       /* Look for the enum-specifier.  */
12458       type_spec = cp_parser_enum_specifier (parser);
12459       /* If that worked, we're done.  */
12460       if (type_spec)
12461         {
12462           if (declares_class_or_enum)
12463             *declares_class_or_enum = 2;
12464           if (decl_specs)
12465             cp_parser_set_decl_spec_type (decl_specs,
12466                                           type_spec,
12467                                           token->location,
12468                                           /*user_defined_p=*/true);
12469           return type_spec;
12470         }
12471       else
12472         goto elaborated_type_specifier;
12473
12474       /* Any of these indicate either a class-specifier, or an
12475          elaborated-type-specifier.  */
12476     case RID_CLASS:
12477     case RID_STRUCT:
12478     case RID_UNION:
12479       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12480         goto elaborated_type_specifier;
12481
12482       /* Parse tentatively so that we can back up if we don't find a
12483          class-specifier.  */
12484       cp_parser_parse_tentatively (parser);
12485       /* Look for the class-specifier.  */
12486       type_spec = cp_parser_class_specifier (parser);
12487       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12488       /* If that worked, we're done.  */
12489       if (cp_parser_parse_definitely (parser))
12490         {
12491           if (declares_class_or_enum)
12492             *declares_class_or_enum = 2;
12493           if (decl_specs)
12494             cp_parser_set_decl_spec_type (decl_specs,
12495                                           type_spec,
12496                                           token->location,
12497                                           /*user_defined_p=*/true);
12498           return type_spec;
12499         }
12500
12501       /* Fall through.  */
12502     elaborated_type_specifier:
12503       /* We're declaring (not defining) a class or enum.  */
12504       if (declares_class_or_enum)
12505         *declares_class_or_enum = 1;
12506
12507       /* Fall through.  */
12508     case RID_TYPENAME:
12509       /* Look for an elaborated-type-specifier.  */
12510       type_spec
12511         = (cp_parser_elaborated_type_specifier
12512            (parser,
12513             decl_specs && decl_specs->specs[(int) ds_friend],
12514             is_declaration));
12515       if (decl_specs)
12516         cp_parser_set_decl_spec_type (decl_specs,
12517                                       type_spec,
12518                                       token->location,
12519                                       /*user_defined_p=*/true);
12520       return type_spec;
12521
12522     case RID_CONST:
12523       ds = ds_const;
12524       if (is_cv_qualifier)
12525         *is_cv_qualifier = true;
12526       break;
12527
12528     case RID_VOLATILE:
12529       ds = ds_volatile;
12530       if (is_cv_qualifier)
12531         *is_cv_qualifier = true;
12532       break;
12533
12534     case RID_RESTRICT:
12535       ds = ds_restrict;
12536       if (is_cv_qualifier)
12537         *is_cv_qualifier = true;
12538       break;
12539
12540     case RID_COMPLEX:
12541       /* The `__complex__' keyword is a GNU extension.  */
12542       ds = ds_complex;
12543       break;
12544
12545     default:
12546       break;
12547     }
12548
12549   /* Handle simple keywords.  */
12550   if (ds != ds_last)
12551     {
12552       if (decl_specs)
12553         {
12554           ++decl_specs->specs[(int)ds];
12555           decl_specs->any_specifiers_p = true;
12556         }
12557       return cp_lexer_consume_token (parser->lexer)->u.value;
12558     }
12559
12560   /* If we do not already have a type-specifier, assume we are looking
12561      at a simple-type-specifier.  */
12562   type_spec = cp_parser_simple_type_specifier (parser,
12563                                                decl_specs,
12564                                                flags);
12565
12566   /* If we didn't find a type-specifier, and a type-specifier was not
12567      optional in this context, issue an error message.  */
12568   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12569     {
12570       cp_parser_error (parser, "expected type specifier");
12571       return error_mark_node;
12572     }
12573
12574   return type_spec;
12575 }
12576
12577 /* Parse a simple-type-specifier.
12578
12579    simple-type-specifier:
12580      :: [opt] nested-name-specifier [opt] type-name
12581      :: [opt] nested-name-specifier template template-id
12582      char
12583      wchar_t
12584      bool
12585      short
12586      int
12587      long
12588      signed
12589      unsigned
12590      float
12591      double
12592      void
12593
12594    C++0x Extension:
12595
12596    simple-type-specifier:
12597      auto
12598      decltype ( expression )   
12599      char16_t
12600      char32_t
12601
12602    GNU Extension:
12603
12604    simple-type-specifier:
12605      __int128
12606      __typeof__ unary-expression
12607      __typeof__ ( type-id )
12608
12609    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12610    appropriately updated.  */
12611
12612 static tree
12613 cp_parser_simple_type_specifier (cp_parser* parser,
12614                                  cp_decl_specifier_seq *decl_specs,
12615                                  cp_parser_flags flags)
12616 {
12617   tree type = NULL_TREE;
12618   cp_token *token;
12619
12620   /* Peek at the next token.  */
12621   token = cp_lexer_peek_token (parser->lexer);
12622
12623   /* If we're looking at a keyword, things are easy.  */
12624   switch (token->keyword)
12625     {
12626     case RID_CHAR:
12627       if (decl_specs)
12628         decl_specs->explicit_char_p = true;
12629       type = char_type_node;
12630       break;
12631     case RID_CHAR16:
12632       type = char16_type_node;
12633       break;
12634     case RID_CHAR32:
12635       type = char32_type_node;
12636       break;
12637     case RID_WCHAR:
12638       type = wchar_type_node;
12639       break;
12640     case RID_BOOL:
12641       type = boolean_type_node;
12642       break;
12643     case RID_SHORT:
12644       if (decl_specs)
12645         ++decl_specs->specs[(int) ds_short];
12646       type = short_integer_type_node;
12647       break;
12648     case RID_INT:
12649       if (decl_specs)
12650         decl_specs->explicit_int_p = true;
12651       type = integer_type_node;
12652       break;
12653     case RID_INT128:
12654       if (!int128_integer_type_node)
12655         break;
12656       if (decl_specs)
12657         decl_specs->explicit_int128_p = true;
12658       type = int128_integer_type_node;
12659       break;
12660     case RID_LONG:
12661       if (decl_specs)
12662         ++decl_specs->specs[(int) ds_long];
12663       type = long_integer_type_node;
12664       break;
12665     case RID_SIGNED:
12666       if (decl_specs)
12667         ++decl_specs->specs[(int) ds_signed];
12668       type = integer_type_node;
12669       break;
12670     case RID_UNSIGNED:
12671       if (decl_specs)
12672         ++decl_specs->specs[(int) ds_unsigned];
12673       type = unsigned_type_node;
12674       break;
12675     case RID_FLOAT:
12676       type = float_type_node;
12677       break;
12678     case RID_DOUBLE:
12679       type = double_type_node;
12680       break;
12681     case RID_VOID:
12682       type = void_type_node;
12683       break;
12684       
12685     case RID_AUTO:
12686       maybe_warn_cpp0x (CPP0X_AUTO);
12687       type = make_auto ();
12688       break;
12689
12690     case RID_DECLTYPE:
12691       /* Parse the `decltype' type.  */
12692       type = cp_parser_decltype (parser);
12693
12694       if (decl_specs)
12695         cp_parser_set_decl_spec_type (decl_specs, type,
12696                                       token->location,
12697                                       /*user_defined_p=*/true);
12698
12699       return type;
12700
12701     case RID_TYPEOF:
12702       /* Consume the `typeof' token.  */
12703       cp_lexer_consume_token (parser->lexer);
12704       /* Parse the operand to `typeof'.  */
12705       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12706       /* If it is not already a TYPE, take its type.  */
12707       if (!TYPE_P (type))
12708         type = finish_typeof (type);
12709
12710       if (decl_specs)
12711         cp_parser_set_decl_spec_type (decl_specs, type,
12712                                       token->location,
12713                                       /*user_defined_p=*/true);
12714
12715       return type;
12716
12717     default:
12718       break;
12719     }
12720
12721   /* If the type-specifier was for a built-in type, we're done.  */
12722   if (type)
12723     {
12724       /* Record the type.  */
12725       if (decl_specs
12726           && (token->keyword != RID_SIGNED
12727               && token->keyword != RID_UNSIGNED
12728               && token->keyword != RID_SHORT
12729               && token->keyword != RID_LONG))
12730         cp_parser_set_decl_spec_type (decl_specs,
12731                                       type,
12732                                       token->location,
12733                                       /*user_defined=*/false);
12734       if (decl_specs)
12735         decl_specs->any_specifiers_p = true;
12736
12737       /* Consume the token.  */
12738       cp_lexer_consume_token (parser->lexer);
12739
12740       /* There is no valid C++ program where a non-template type is
12741          followed by a "<".  That usually indicates that the user thought
12742          that the type was a template.  */
12743       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12744
12745       return TYPE_NAME (type);
12746     }
12747
12748   /* The type-specifier must be a user-defined type.  */
12749   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12750     {
12751       bool qualified_p;
12752       bool global_p;
12753
12754       /* Don't gobble tokens or issue error messages if this is an
12755          optional type-specifier.  */
12756       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12757         cp_parser_parse_tentatively (parser);
12758
12759       /* Look for the optional `::' operator.  */
12760       global_p
12761         = (cp_parser_global_scope_opt (parser,
12762                                        /*current_scope_valid_p=*/false)
12763            != NULL_TREE);
12764       /* Look for the nested-name specifier.  */
12765       qualified_p
12766         = (cp_parser_nested_name_specifier_opt (parser,
12767                                                 /*typename_keyword_p=*/false,
12768                                                 /*check_dependency_p=*/true,
12769                                                 /*type_p=*/false,
12770                                                 /*is_declaration=*/false)
12771            != NULL_TREE);
12772       token = cp_lexer_peek_token (parser->lexer);
12773       /* If we have seen a nested-name-specifier, and the next token
12774          is `template', then we are using the template-id production.  */
12775       if (parser->scope
12776           && cp_parser_optional_template_keyword (parser))
12777         {
12778           /* Look for the template-id.  */
12779           type = cp_parser_template_id (parser,
12780                                         /*template_keyword_p=*/true,
12781                                         /*check_dependency_p=*/true,
12782                                         /*is_declaration=*/false);
12783           /* If the template-id did not name a type, we are out of
12784              luck.  */
12785           if (TREE_CODE (type) != TYPE_DECL)
12786             {
12787               cp_parser_error (parser, "expected template-id for type");
12788               type = NULL_TREE;
12789             }
12790         }
12791       /* Otherwise, look for a type-name.  */
12792       else
12793         type = cp_parser_type_name (parser);
12794       /* Keep track of all name-lookups performed in class scopes.  */
12795       if (type
12796           && !global_p
12797           && !qualified_p
12798           && TREE_CODE (type) == TYPE_DECL
12799           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12800         maybe_note_name_used_in_class (DECL_NAME (type), type);
12801       /* If it didn't work out, we don't have a TYPE.  */
12802       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12803           && !cp_parser_parse_definitely (parser))
12804         type = NULL_TREE;
12805       if (type && decl_specs)
12806         cp_parser_set_decl_spec_type (decl_specs, type,
12807                                       token->location,
12808                                       /*user_defined=*/true);
12809     }
12810
12811   /* If we didn't get a type-name, issue an error message.  */
12812   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12813     {
12814       cp_parser_error (parser, "expected type-name");
12815       return error_mark_node;
12816     }
12817
12818   if (type && type != error_mark_node)
12819     {
12820       /* See if TYPE is an Objective-C type, and if so, parse and
12821          accept any protocol references following it.  Do this before
12822          the cp_parser_check_for_invalid_template_id() call, because
12823          Objective-C types can be followed by '<...>' which would
12824          enclose protocol names rather than template arguments, and so
12825          everything is fine.  */
12826       if (c_dialect_objc () && !parser->scope
12827           && (objc_is_id (type) || objc_is_class_name (type)))
12828         {
12829           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12830           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12831
12832           /* Clobber the "unqualified" type previously entered into
12833              DECL_SPECS with the new, improved protocol-qualified version.  */
12834           if (decl_specs)
12835             decl_specs->type = qual_type;
12836
12837           return qual_type;
12838         }
12839
12840       /* There is no valid C++ program where a non-template type is
12841          followed by a "<".  That usually indicates that the user
12842          thought that the type was a template.  */
12843       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12844                                                token->location);
12845     }
12846
12847   return type;
12848 }
12849
12850 /* Parse a type-name.
12851
12852    type-name:
12853      class-name
12854      enum-name
12855      typedef-name
12856
12857    enum-name:
12858      identifier
12859
12860    typedef-name:
12861      identifier
12862
12863    Returns a TYPE_DECL for the type.  */
12864
12865 static tree
12866 cp_parser_type_name (cp_parser* parser)
12867 {
12868   tree type_decl;
12869
12870   /* We can't know yet whether it is a class-name or not.  */
12871   cp_parser_parse_tentatively (parser);
12872   /* Try a class-name.  */
12873   type_decl = cp_parser_class_name (parser,
12874                                     /*typename_keyword_p=*/false,
12875                                     /*template_keyword_p=*/false,
12876                                     none_type,
12877                                     /*check_dependency_p=*/true,
12878                                     /*class_head_p=*/false,
12879                                     /*is_declaration=*/false);
12880   /* If it's not a class-name, keep looking.  */
12881   if (!cp_parser_parse_definitely (parser))
12882     {
12883       /* It must be a typedef-name or an enum-name.  */
12884       return cp_parser_nonclass_name (parser);
12885     }
12886
12887   return type_decl;
12888 }
12889
12890 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12891
12892    enum-name:
12893      identifier
12894
12895    typedef-name:
12896      identifier
12897
12898    Returns a TYPE_DECL for the type.  */
12899
12900 static tree
12901 cp_parser_nonclass_name (cp_parser* parser)
12902 {
12903   tree type_decl;
12904   tree identifier;
12905
12906   cp_token *token = cp_lexer_peek_token (parser->lexer);
12907   identifier = cp_parser_identifier (parser);
12908   if (identifier == error_mark_node)
12909     return error_mark_node;
12910
12911   /* Look up the type-name.  */
12912   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12913
12914   if (TREE_CODE (type_decl) != TYPE_DECL
12915       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12916     {
12917       /* See if this is an Objective-C type.  */
12918       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12919       tree type = objc_get_protocol_qualified_type (identifier, protos);
12920       if (type)
12921         type_decl = TYPE_NAME (type);
12922     }
12923
12924   /* Issue an error if we did not find a type-name.  */
12925   if (TREE_CODE (type_decl) != TYPE_DECL
12926       /* In Objective-C, we have the complication that class names are
12927          normally type names and start declarations (eg, the
12928          "NSObject" in "NSObject *object;"), but can be used in an
12929          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12930          is an expression.  So, a classname followed by a dot is not a
12931          valid type-name.  */
12932       || (objc_is_class_name (TREE_TYPE (type_decl))
12933           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12934     {
12935       if (!cp_parser_simulate_error (parser))
12936         cp_parser_name_lookup_error (parser, identifier, type_decl,
12937                                      NLE_TYPE, token->location);
12938       return error_mark_node;
12939     }
12940   /* Remember that the name was used in the definition of the
12941      current class so that we can check later to see if the
12942      meaning would have been different after the class was
12943      entirely defined.  */
12944   else if (type_decl != error_mark_node
12945            && !parser->scope)
12946     maybe_note_name_used_in_class (identifier, type_decl);
12947   
12948   return type_decl;
12949 }
12950
12951 /* Parse an elaborated-type-specifier.  Note that the grammar given
12952    here incorporates the resolution to DR68.
12953
12954    elaborated-type-specifier:
12955      class-key :: [opt] nested-name-specifier [opt] identifier
12956      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12957      enum-key :: [opt] nested-name-specifier [opt] identifier
12958      typename :: [opt] nested-name-specifier identifier
12959      typename :: [opt] nested-name-specifier template [opt]
12960        template-id
12961
12962    GNU extension:
12963
12964    elaborated-type-specifier:
12965      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12966      class-key attributes :: [opt] nested-name-specifier [opt]
12967                template [opt] template-id
12968      enum attributes :: [opt] nested-name-specifier [opt] identifier
12969
12970    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12971    declared `friend'.  If IS_DECLARATION is TRUE, then this
12972    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12973    something is being declared.
12974
12975    Returns the TYPE specified.  */
12976
12977 static tree
12978 cp_parser_elaborated_type_specifier (cp_parser* parser,
12979                                      bool is_friend,
12980                                      bool is_declaration)
12981 {
12982   enum tag_types tag_type;
12983   tree identifier;
12984   tree type = NULL_TREE;
12985   tree attributes = NULL_TREE;
12986   tree globalscope;
12987   cp_token *token = NULL;
12988
12989   /* See if we're looking at the `enum' keyword.  */
12990   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12991     {
12992       /* Consume the `enum' token.  */
12993       cp_lexer_consume_token (parser->lexer);
12994       /* Remember that it's an enumeration type.  */
12995       tag_type = enum_type;
12996       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12997          enums) is used here.  */
12998       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12999           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13000         {
13001             pedwarn (input_location, 0, "elaborated-type-specifier "
13002                       "for a scoped enum must not use the %<%D%> keyword",
13003                       cp_lexer_peek_token (parser->lexer)->u.value);
13004           /* Consume the `struct' or `class' and parse it anyway.  */
13005           cp_lexer_consume_token (parser->lexer);
13006         }
13007       /* Parse the attributes.  */
13008       attributes = cp_parser_attributes_opt (parser);
13009     }
13010   /* Or, it might be `typename'.  */
13011   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13012                                            RID_TYPENAME))
13013     {
13014       /* Consume the `typename' token.  */
13015       cp_lexer_consume_token (parser->lexer);
13016       /* Remember that it's a `typename' type.  */
13017       tag_type = typename_type;
13018     }
13019   /* Otherwise it must be a class-key.  */
13020   else
13021     {
13022       tag_type = cp_parser_class_key (parser);
13023       if (tag_type == none_type)
13024         return error_mark_node;
13025       /* Parse the attributes.  */
13026       attributes = cp_parser_attributes_opt (parser);
13027     }
13028
13029   /* Look for the `::' operator.  */
13030   globalscope =  cp_parser_global_scope_opt (parser,
13031                                              /*current_scope_valid_p=*/false);
13032   /* Look for the nested-name-specifier.  */
13033   if (tag_type == typename_type && !globalscope)
13034     {
13035       if (!cp_parser_nested_name_specifier (parser,
13036                                            /*typename_keyword_p=*/true,
13037                                            /*check_dependency_p=*/true,
13038                                            /*type_p=*/true,
13039                                             is_declaration))
13040         return error_mark_node;
13041     }
13042   else
13043     /* Even though `typename' is not present, the proposed resolution
13044        to Core Issue 180 says that in `class A<T>::B', `B' should be
13045        considered a type-name, even if `A<T>' is dependent.  */
13046     cp_parser_nested_name_specifier_opt (parser,
13047                                          /*typename_keyword_p=*/true,
13048                                          /*check_dependency_p=*/true,
13049                                          /*type_p=*/true,
13050                                          is_declaration);
13051  /* For everything but enumeration types, consider a template-id.
13052     For an enumeration type, consider only a plain identifier.  */
13053   if (tag_type != enum_type)
13054     {
13055       bool template_p = false;
13056       tree decl;
13057
13058       /* Allow the `template' keyword.  */
13059       template_p = cp_parser_optional_template_keyword (parser);
13060       /* If we didn't see `template', we don't know if there's a
13061          template-id or not.  */
13062       if (!template_p)
13063         cp_parser_parse_tentatively (parser);
13064       /* Parse the template-id.  */
13065       token = cp_lexer_peek_token (parser->lexer);
13066       decl = cp_parser_template_id (parser, template_p,
13067                                     /*check_dependency_p=*/true,
13068                                     is_declaration);
13069       /* If we didn't find a template-id, look for an ordinary
13070          identifier.  */
13071       if (!template_p && !cp_parser_parse_definitely (parser))
13072         ;
13073       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13074          in effect, then we must assume that, upon instantiation, the
13075          template will correspond to a class.  */
13076       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13077                && tag_type == typename_type)
13078         type = make_typename_type (parser->scope, decl,
13079                                    typename_type,
13080                                    /*complain=*/tf_error);
13081       /* If the `typename' keyword is in effect and DECL is not a type
13082          decl. Then type is non existant.   */
13083       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13084         type = NULL_TREE; 
13085       else 
13086         type = TREE_TYPE (decl);
13087     }
13088
13089   if (!type)
13090     {
13091       token = cp_lexer_peek_token (parser->lexer);
13092       identifier = cp_parser_identifier (parser);
13093
13094       if (identifier == error_mark_node)
13095         {
13096           parser->scope = NULL_TREE;
13097           return error_mark_node;
13098         }
13099
13100       /* For a `typename', we needn't call xref_tag.  */
13101       if (tag_type == typename_type
13102           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13103         return cp_parser_make_typename_type (parser, parser->scope,
13104                                              identifier,
13105                                              token->location);
13106       /* Look up a qualified name in the usual way.  */
13107       if (parser->scope)
13108         {
13109           tree decl;
13110           tree ambiguous_decls;
13111
13112           decl = cp_parser_lookup_name (parser, identifier,
13113                                         tag_type,
13114                                         /*is_template=*/false,
13115                                         /*is_namespace=*/false,
13116                                         /*check_dependency=*/true,
13117                                         &ambiguous_decls,
13118                                         token->location);
13119
13120           /* If the lookup was ambiguous, an error will already have been
13121              issued.  */
13122           if (ambiguous_decls)
13123             return error_mark_node;
13124
13125           /* If we are parsing friend declaration, DECL may be a
13126              TEMPLATE_DECL tree node here.  However, we need to check
13127              whether this TEMPLATE_DECL results in valid code.  Consider
13128              the following example:
13129
13130                namespace N {
13131                  template <class T> class C {};
13132                }
13133                class X {
13134                  template <class T> friend class N::C; // #1, valid code
13135                };
13136                template <class T> class Y {
13137                  friend class N::C;                    // #2, invalid code
13138                };
13139
13140              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13141              name lookup of `N::C'.  We see that friend declaration must
13142              be template for the code to be valid.  Note that
13143              processing_template_decl does not work here since it is
13144              always 1 for the above two cases.  */
13145
13146           decl = (cp_parser_maybe_treat_template_as_class
13147                   (decl, /*tag_name_p=*/is_friend
13148                          && parser->num_template_parameter_lists));
13149
13150           if (TREE_CODE (decl) != TYPE_DECL)
13151             {
13152               cp_parser_diagnose_invalid_type_name (parser,
13153                                                     parser->scope,
13154                                                     identifier,
13155                                                     token->location);
13156               return error_mark_node;
13157             }
13158
13159           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13160             {
13161               bool allow_template = (parser->num_template_parameter_lists
13162                                       || DECL_SELF_REFERENCE_P (decl));
13163               type = check_elaborated_type_specifier (tag_type, decl, 
13164                                                       allow_template);
13165
13166               if (type == error_mark_node)
13167                 return error_mark_node;
13168             }
13169
13170           /* Forward declarations of nested types, such as
13171
13172                class C1::C2;
13173                class C1::C2::C3;
13174
13175              are invalid unless all components preceding the final '::'
13176              are complete.  If all enclosing types are complete, these
13177              declarations become merely pointless.
13178
13179              Invalid forward declarations of nested types are errors
13180              caught elsewhere in parsing.  Those that are pointless arrive
13181              here.  */
13182
13183           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13184               && !is_friend && !processing_explicit_instantiation)
13185             warning (0, "declaration %qD does not declare anything", decl);
13186
13187           type = TREE_TYPE (decl);
13188         }
13189       else
13190         {
13191           /* An elaborated-type-specifier sometimes introduces a new type and
13192              sometimes names an existing type.  Normally, the rule is that it
13193              introduces a new type only if there is not an existing type of
13194              the same name already in scope.  For example, given:
13195
13196                struct S {};
13197                void f() { struct S s; }
13198
13199              the `struct S' in the body of `f' is the same `struct S' as in
13200              the global scope; the existing definition is used.  However, if
13201              there were no global declaration, this would introduce a new
13202              local class named `S'.
13203
13204              An exception to this rule applies to the following code:
13205
13206                namespace N { struct S; }
13207
13208              Here, the elaborated-type-specifier names a new type
13209              unconditionally; even if there is already an `S' in the
13210              containing scope this declaration names a new type.
13211              This exception only applies if the elaborated-type-specifier
13212              forms the complete declaration:
13213
13214                [class.name]
13215
13216                A declaration consisting solely of `class-key identifier ;' is
13217                either a redeclaration of the name in the current scope or a
13218                forward declaration of the identifier as a class name.  It
13219                introduces the name into the current scope.
13220
13221              We are in this situation precisely when the next token is a `;'.
13222
13223              An exception to the exception is that a `friend' declaration does
13224              *not* name a new type; i.e., given:
13225
13226                struct S { friend struct T; };
13227
13228              `T' is not a new type in the scope of `S'.
13229
13230              Also, `new struct S' or `sizeof (struct S)' never results in the
13231              definition of a new type; a new type can only be declared in a
13232              declaration context.  */
13233
13234           tag_scope ts;
13235           bool template_p;
13236
13237           if (is_friend)
13238             /* Friends have special name lookup rules.  */
13239             ts = ts_within_enclosing_non_class;
13240           else if (is_declaration
13241                    && cp_lexer_next_token_is (parser->lexer,
13242                                               CPP_SEMICOLON))
13243             /* This is a `class-key identifier ;' */
13244             ts = ts_current;
13245           else
13246             ts = ts_global;
13247
13248           template_p =
13249             (parser->num_template_parameter_lists
13250              && (cp_parser_next_token_starts_class_definition_p (parser)
13251                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13252           /* An unqualified name was used to reference this type, so
13253              there were no qualifying templates.  */
13254           if (!cp_parser_check_template_parameters (parser,
13255                                                     /*num_templates=*/0,
13256                                                     token->location,
13257                                                     /*declarator=*/NULL))
13258             return error_mark_node;
13259           type = xref_tag (tag_type, identifier, ts, template_p);
13260         }
13261     }
13262
13263   if (type == error_mark_node)
13264     return error_mark_node;
13265
13266   /* Allow attributes on forward declarations of classes.  */
13267   if (attributes)
13268     {
13269       if (TREE_CODE (type) == TYPENAME_TYPE)
13270         warning (OPT_Wattributes,
13271                  "attributes ignored on uninstantiated type");
13272       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13273                && ! processing_explicit_instantiation)
13274         warning (OPT_Wattributes,
13275                  "attributes ignored on template instantiation");
13276       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13277         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13278       else
13279         warning (OPT_Wattributes,
13280                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13281     }
13282
13283   if (tag_type != enum_type)
13284     cp_parser_check_class_key (tag_type, type);
13285
13286   /* A "<" cannot follow an elaborated type specifier.  If that
13287      happens, the user was probably trying to form a template-id.  */
13288   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13289
13290   return type;
13291 }
13292
13293 /* Parse an enum-specifier.
13294
13295    enum-specifier:
13296      enum-head { enumerator-list [opt] }
13297
13298    enum-head:
13299      enum-key identifier [opt] enum-base [opt]
13300      enum-key nested-name-specifier identifier enum-base [opt]
13301
13302    enum-key:
13303      enum
13304      enum class   [C++0x]
13305      enum struct  [C++0x]
13306
13307    enum-base:   [C++0x]
13308      : type-specifier-seq
13309
13310    opaque-enum-specifier:
13311      enum-key identifier enum-base [opt] ;
13312
13313    GNU Extensions:
13314      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13315        { enumerator-list [opt] }attributes[opt]
13316
13317    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13318    if the token stream isn't an enum-specifier after all.  */
13319
13320 static tree
13321 cp_parser_enum_specifier (cp_parser* parser)
13322 {
13323   tree identifier;
13324   tree type = NULL_TREE;
13325   tree prev_scope;
13326   tree nested_name_specifier = NULL_TREE;
13327   tree attributes;
13328   bool scoped_enum_p = false;
13329   bool has_underlying_type = false;
13330   bool nested_being_defined = false;
13331   bool new_value_list = false;
13332   bool is_new_type = false;
13333   bool is_anonymous = false;
13334   tree underlying_type = NULL_TREE;
13335   cp_token *type_start_token = NULL;
13336
13337   /* Parse tentatively so that we can back up if we don't find a
13338      enum-specifier.  */
13339   cp_parser_parse_tentatively (parser);
13340
13341   /* Caller guarantees that the current token is 'enum', an identifier
13342      possibly follows, and the token after that is an opening brace.
13343      If we don't have an identifier, fabricate an anonymous name for
13344      the enumeration being defined.  */
13345   cp_lexer_consume_token (parser->lexer);
13346
13347   /* Parse the "class" or "struct", which indicates a scoped
13348      enumeration type in C++0x.  */
13349   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13350       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13351     {
13352       if (cxx_dialect < cxx0x)
13353         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13354
13355       /* Consume the `struct' or `class' token.  */
13356       cp_lexer_consume_token (parser->lexer);
13357
13358       scoped_enum_p = true;
13359     }
13360
13361   attributes = cp_parser_attributes_opt (parser);
13362
13363   /* Clear the qualification.  */
13364   parser->scope = NULL_TREE;
13365   parser->qualifying_scope = NULL_TREE;
13366   parser->object_scope = NULL_TREE;
13367
13368   /* Figure out in what scope the declaration is being placed.  */
13369   prev_scope = current_scope ();
13370
13371   type_start_token = cp_lexer_peek_token (parser->lexer);
13372
13373   push_deferring_access_checks (dk_no_check);
13374   nested_name_specifier
13375       = cp_parser_nested_name_specifier_opt (parser,
13376                                              /*typename_keyword_p=*/true,
13377                                              /*check_dependency_p=*/false,
13378                                              /*type_p=*/false,
13379                                              /*is_declaration=*/false);
13380
13381   if (nested_name_specifier)
13382     {
13383       tree name;
13384
13385       identifier = cp_parser_identifier (parser);
13386       name =  cp_parser_lookup_name (parser, identifier,
13387                                      enum_type,
13388                                      /*is_template=*/false,
13389                                      /*is_namespace=*/false,
13390                                      /*check_dependency=*/true,
13391                                      /*ambiguous_decls=*/NULL,
13392                                      input_location);
13393       if (name)
13394         {
13395           type = TREE_TYPE (name);
13396           if (TREE_CODE (type) == TYPENAME_TYPE)
13397             {
13398               /* Are template enums allowed in ISO? */
13399               if (template_parm_scope_p ())
13400                 pedwarn (type_start_token->location, OPT_pedantic,
13401                          "%qD is an enumeration template", name);
13402               /* ignore a typename reference, for it will be solved by name
13403                  in start_enum.  */
13404               type = NULL_TREE;
13405             }
13406         }
13407       else
13408         error_at (type_start_token->location,
13409                   "%qD is not an enumerator-name", identifier);
13410     }
13411   else
13412     {
13413       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13414         identifier = cp_parser_identifier (parser);
13415       else
13416         {
13417           identifier = make_anon_name ();
13418           is_anonymous = true;
13419         }
13420     }
13421   pop_deferring_access_checks ();
13422
13423   /* Check for the `:' that denotes a specified underlying type in C++0x.
13424      Note that a ':' could also indicate a bitfield width, however.  */
13425   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13426     {
13427       cp_decl_specifier_seq type_specifiers;
13428
13429       /* Consume the `:'.  */
13430       cp_lexer_consume_token (parser->lexer);
13431
13432       /* Parse the type-specifier-seq.  */
13433       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13434                                     /*is_trailing_return=*/false,
13435                                     &type_specifiers);
13436
13437       /* At this point this is surely not elaborated type specifier.  */
13438       if (!cp_parser_parse_definitely (parser))
13439         return NULL_TREE;
13440
13441       if (cxx_dialect < cxx0x)
13442         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13443
13444       has_underlying_type = true;
13445
13446       /* If that didn't work, stop.  */
13447       if (type_specifiers.type != error_mark_node)
13448         {
13449           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13450                                             /*initialized=*/0, NULL);
13451           if (underlying_type == error_mark_node)
13452             underlying_type = NULL_TREE;
13453         }
13454     }
13455
13456   /* Look for the `{' but don't consume it yet.  */
13457   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13458     {
13459       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13460         {
13461           cp_parser_error (parser, "expected %<{%>");
13462           if (has_underlying_type)
13463             return NULL_TREE;
13464         }
13465       /* An opaque-enum-specifier must have a ';' here.  */
13466       if ((scoped_enum_p || underlying_type)
13467           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13468         {
13469           cp_parser_error (parser, "expected %<;%> or %<{%>");
13470           if (has_underlying_type)
13471             return NULL_TREE;
13472         }
13473     }
13474
13475   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13476     return NULL_TREE;
13477
13478   if (nested_name_specifier)
13479     {
13480       if (CLASS_TYPE_P (nested_name_specifier))
13481         {
13482           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13483           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13484           push_scope (nested_name_specifier);
13485         }
13486       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13487         {
13488           push_nested_namespace (nested_name_specifier);
13489         }
13490     }
13491
13492   /* Issue an error message if type-definitions are forbidden here.  */
13493   if (!cp_parser_check_type_definition (parser))
13494     type = error_mark_node;
13495   else
13496     /* Create the new type.  We do this before consuming the opening
13497        brace so the enum will be recorded as being on the line of its
13498        tag (or the 'enum' keyword, if there is no tag).  */
13499     type = start_enum (identifier, type, underlying_type,
13500                        scoped_enum_p, &is_new_type);
13501
13502   /* If the next token is not '{' it is an opaque-enum-specifier or an
13503      elaborated-type-specifier.  */
13504   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13505     {
13506       if (nested_name_specifier)
13507         {
13508           /* The following catches invalid code such as:
13509              enum class S<int>::E { A, B, C }; */
13510           if (!processing_specialization
13511               && CLASS_TYPE_P (nested_name_specifier)
13512               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13513             error_at (type_start_token->location, "cannot add an enumerator "
13514                       "list to a template instantiation");
13515
13516           /* If that scope does not contain the scope in which the
13517              class was originally declared, the program is invalid.  */
13518           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13519             {
13520               if (at_namespace_scope_p ())
13521                 error_at (type_start_token->location,
13522                           "declaration of %qD in namespace %qD which does not "
13523                           "enclose %qD",
13524                           type, prev_scope, nested_name_specifier);
13525               else
13526                 error_at (type_start_token->location,
13527                           "declaration of %qD in %qD which does not enclose %qD",
13528                           type, prev_scope, nested_name_specifier);
13529               type = error_mark_node;
13530             }
13531         }
13532
13533       if (scoped_enum_p)
13534         begin_scope (sk_scoped_enum, type);
13535
13536       /* Consume the opening brace.  */
13537       cp_lexer_consume_token (parser->lexer);
13538
13539       if (type == error_mark_node)
13540         ; /* Nothing to add */
13541       else if (OPAQUE_ENUM_P (type)
13542                || (cxx_dialect > cxx98 && processing_specialization))
13543         {
13544           new_value_list = true;
13545           SET_OPAQUE_ENUM_P (type, false);
13546           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13547         }
13548       else
13549         {
13550           error_at (type_start_token->location, "multiple definition of %q#T", type);
13551           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13552                     "previous definition here");
13553           type = error_mark_node;
13554         }
13555
13556       if (type == error_mark_node)
13557         cp_parser_skip_to_end_of_block_or_statement (parser);
13558       /* If the next token is not '}', then there are some enumerators.  */
13559       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13560         cp_parser_enumerator_list (parser, type);
13561
13562       /* Consume the final '}'.  */
13563       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13564
13565       if (scoped_enum_p)
13566         finish_scope ();
13567     }
13568   else
13569     {
13570       /* If a ';' follows, then it is an opaque-enum-specifier
13571         and additional restrictions apply.  */
13572       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13573         {
13574           if (is_anonymous)
13575             error_at (type_start_token->location,
13576                       "opaque-enum-specifier without name");
13577           else if (nested_name_specifier)
13578             error_at (type_start_token->location,
13579                       "opaque-enum-specifier must use a simple identifier");
13580         }
13581     }
13582
13583   /* Look for trailing attributes to apply to this enumeration, and
13584      apply them if appropriate.  */
13585   if (cp_parser_allow_gnu_extensions_p (parser))
13586     {
13587       tree trailing_attr = cp_parser_attributes_opt (parser);
13588       trailing_attr = chainon (trailing_attr, attributes);
13589       cplus_decl_attributes (&type,
13590                              trailing_attr,
13591                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13592     }
13593
13594   /* Finish up the enumeration.  */
13595   if (type != error_mark_node)
13596     {
13597       if (new_value_list)
13598         finish_enum_value_list (type);
13599       if (is_new_type)
13600         finish_enum (type);
13601     }
13602
13603   if (nested_name_specifier)
13604     {
13605       if (CLASS_TYPE_P (nested_name_specifier))
13606         {
13607           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13608           pop_scope (nested_name_specifier);
13609         }
13610       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13611         {
13612           pop_nested_namespace (nested_name_specifier);
13613         }
13614     }
13615   return type;
13616 }
13617
13618 /* Parse an enumerator-list.  The enumerators all have the indicated
13619    TYPE.
13620
13621    enumerator-list:
13622      enumerator-definition
13623      enumerator-list , enumerator-definition  */
13624
13625 static void
13626 cp_parser_enumerator_list (cp_parser* parser, tree type)
13627 {
13628   while (true)
13629     {
13630       /* Parse an enumerator-definition.  */
13631       cp_parser_enumerator_definition (parser, type);
13632
13633       /* If the next token is not a ',', we've reached the end of
13634          the list.  */
13635       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13636         break;
13637       /* Otherwise, consume the `,' and keep going.  */
13638       cp_lexer_consume_token (parser->lexer);
13639       /* If the next token is a `}', there is a trailing comma.  */
13640       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13641         {
13642           if (!in_system_header)
13643             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13644           break;
13645         }
13646     }
13647 }
13648
13649 /* Parse an enumerator-definition.  The enumerator has the indicated
13650    TYPE.
13651
13652    enumerator-definition:
13653      enumerator
13654      enumerator = constant-expression
13655
13656    enumerator:
13657      identifier  */
13658
13659 static void
13660 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13661 {
13662   tree identifier;
13663   tree value;
13664   location_t loc;
13665
13666   /* Save the input location because we are interested in the location
13667      of the identifier and not the location of the explicit value.  */
13668   loc = cp_lexer_peek_token (parser->lexer)->location;
13669
13670   /* Look for the identifier.  */
13671   identifier = cp_parser_identifier (parser);
13672   if (identifier == error_mark_node)
13673     return;
13674
13675   /* If the next token is an '=', then there is an explicit value.  */
13676   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13677     {
13678       /* Consume the `=' token.  */
13679       cp_lexer_consume_token (parser->lexer);
13680       /* Parse the value.  */
13681       value = cp_parser_constant_expression (parser,
13682                                              /*allow_non_constant_p=*/false,
13683                                              NULL);
13684     }
13685   else
13686     value = NULL_TREE;
13687
13688   /* If we are processing a template, make sure the initializer of the
13689      enumerator doesn't contain any bare template parameter pack.  */
13690   if (check_for_bare_parameter_packs (value))
13691     value = error_mark_node;
13692
13693   /* Create the enumerator.  */
13694   build_enumerator (identifier, value, type, loc);
13695 }
13696
13697 /* Parse a namespace-name.
13698
13699    namespace-name:
13700      original-namespace-name
13701      namespace-alias
13702
13703    Returns the NAMESPACE_DECL for the namespace.  */
13704
13705 static tree
13706 cp_parser_namespace_name (cp_parser* parser)
13707 {
13708   tree identifier;
13709   tree namespace_decl;
13710
13711   cp_token *token = cp_lexer_peek_token (parser->lexer);
13712
13713   /* Get the name of the namespace.  */
13714   identifier = cp_parser_identifier (parser);
13715   if (identifier == error_mark_node)
13716     return error_mark_node;
13717
13718   /* Look up the identifier in the currently active scope.  Look only
13719      for namespaces, due to:
13720
13721        [basic.lookup.udir]
13722
13723        When looking up a namespace-name in a using-directive or alias
13724        definition, only namespace names are considered.
13725
13726      And:
13727
13728        [basic.lookup.qual]
13729
13730        During the lookup of a name preceding the :: scope resolution
13731        operator, object, function, and enumerator names are ignored.
13732
13733      (Note that cp_parser_qualifying_entity only calls this
13734      function if the token after the name is the scope resolution
13735      operator.)  */
13736   namespace_decl = cp_parser_lookup_name (parser, identifier,
13737                                           none_type,
13738                                           /*is_template=*/false,
13739                                           /*is_namespace=*/true,
13740                                           /*check_dependency=*/true,
13741                                           /*ambiguous_decls=*/NULL,
13742                                           token->location);
13743   /* If it's not a namespace, issue an error.  */
13744   if (namespace_decl == error_mark_node
13745       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13746     {
13747       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13748         error_at (token->location, "%qD is not a namespace-name", identifier);
13749       cp_parser_error (parser, "expected namespace-name");
13750       namespace_decl = error_mark_node;
13751     }
13752
13753   return namespace_decl;
13754 }
13755
13756 /* Parse a namespace-definition.
13757
13758    namespace-definition:
13759      named-namespace-definition
13760      unnamed-namespace-definition
13761
13762    named-namespace-definition:
13763      original-namespace-definition
13764      extension-namespace-definition
13765
13766    original-namespace-definition:
13767      namespace identifier { namespace-body }
13768
13769    extension-namespace-definition:
13770      namespace original-namespace-name { namespace-body }
13771
13772    unnamed-namespace-definition:
13773      namespace { namespace-body } */
13774
13775 static void
13776 cp_parser_namespace_definition (cp_parser* parser)
13777 {
13778   tree identifier, attribs;
13779   bool has_visibility;
13780   bool is_inline;
13781
13782   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13783     {
13784       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13785       is_inline = true;
13786       cp_lexer_consume_token (parser->lexer);
13787     }
13788   else
13789     is_inline = false;
13790
13791   /* Look for the `namespace' keyword.  */
13792   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13793
13794   /* Get the name of the namespace.  We do not attempt to distinguish
13795      between an original-namespace-definition and an
13796      extension-namespace-definition at this point.  The semantic
13797      analysis routines are responsible for that.  */
13798   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13799     identifier = cp_parser_identifier (parser);
13800   else
13801     identifier = NULL_TREE;
13802
13803   /* Parse any specified attributes.  */
13804   attribs = cp_parser_attributes_opt (parser);
13805
13806   /* Look for the `{' to start the namespace.  */
13807   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13808   /* Start the namespace.  */
13809   push_namespace (identifier);
13810
13811   /* "inline namespace" is equivalent to a stub namespace definition
13812      followed by a strong using directive.  */
13813   if (is_inline)
13814     {
13815       tree name_space = current_namespace;
13816       /* Set up namespace association.  */
13817       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13818         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13819                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13820       /* Import the contents of the inline namespace.  */
13821       pop_namespace ();
13822       do_using_directive (name_space);
13823       push_namespace (identifier);
13824     }
13825
13826   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13827
13828   /* Parse the body of the namespace.  */
13829   cp_parser_namespace_body (parser);
13830
13831 #ifdef HANDLE_PRAGMA_VISIBILITY
13832   if (has_visibility)
13833     pop_visibility (1);
13834 #endif
13835
13836   /* Finish the namespace.  */
13837   pop_namespace ();
13838   /* Look for the final `}'.  */
13839   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13840 }
13841
13842 /* Parse a namespace-body.
13843
13844    namespace-body:
13845      declaration-seq [opt]  */
13846
13847 static void
13848 cp_parser_namespace_body (cp_parser* parser)
13849 {
13850   cp_parser_declaration_seq_opt (parser);
13851 }
13852
13853 /* Parse a namespace-alias-definition.
13854
13855    namespace-alias-definition:
13856      namespace identifier = qualified-namespace-specifier ;  */
13857
13858 static void
13859 cp_parser_namespace_alias_definition (cp_parser* parser)
13860 {
13861   tree identifier;
13862   tree namespace_specifier;
13863
13864   cp_token *token = cp_lexer_peek_token (parser->lexer);
13865
13866   /* Look for the `namespace' keyword.  */
13867   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13868   /* Look for the identifier.  */
13869   identifier = cp_parser_identifier (parser);
13870   if (identifier == error_mark_node)
13871     return;
13872   /* Look for the `=' token.  */
13873   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13874       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13875     {
13876       error_at (token->location, "%<namespace%> definition is not allowed here");
13877       /* Skip the definition.  */
13878       cp_lexer_consume_token (parser->lexer);
13879       if (cp_parser_skip_to_closing_brace (parser))
13880         cp_lexer_consume_token (parser->lexer);
13881       return;
13882     }
13883   cp_parser_require (parser, CPP_EQ, RT_EQ);
13884   /* Look for the qualified-namespace-specifier.  */
13885   namespace_specifier
13886     = cp_parser_qualified_namespace_specifier (parser);
13887   /* Look for the `;' token.  */
13888   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13889
13890   /* Register the alias in the symbol table.  */
13891   do_namespace_alias (identifier, namespace_specifier);
13892 }
13893
13894 /* Parse a qualified-namespace-specifier.
13895
13896    qualified-namespace-specifier:
13897      :: [opt] nested-name-specifier [opt] namespace-name
13898
13899    Returns a NAMESPACE_DECL corresponding to the specified
13900    namespace.  */
13901
13902 static tree
13903 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13904 {
13905   /* Look for the optional `::'.  */
13906   cp_parser_global_scope_opt (parser,
13907                               /*current_scope_valid_p=*/false);
13908
13909   /* Look for the optional nested-name-specifier.  */
13910   cp_parser_nested_name_specifier_opt (parser,
13911                                        /*typename_keyword_p=*/false,
13912                                        /*check_dependency_p=*/true,
13913                                        /*type_p=*/false,
13914                                        /*is_declaration=*/true);
13915
13916   return cp_parser_namespace_name (parser);
13917 }
13918
13919 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13920    access declaration.
13921
13922    using-declaration:
13923      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13924      using :: unqualified-id ;  
13925
13926    access-declaration:
13927      qualified-id ;  
13928
13929    */
13930
13931 static bool
13932 cp_parser_using_declaration (cp_parser* parser, 
13933                              bool access_declaration_p)
13934 {
13935   cp_token *token;
13936   bool typename_p = false;
13937   bool global_scope_p;
13938   tree decl;
13939   tree identifier;
13940   tree qscope;
13941
13942   if (access_declaration_p)
13943     cp_parser_parse_tentatively (parser);
13944   else
13945     {
13946       /* Look for the `using' keyword.  */
13947       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13948       
13949       /* Peek at the next token.  */
13950       token = cp_lexer_peek_token (parser->lexer);
13951       /* See if it's `typename'.  */
13952       if (token->keyword == RID_TYPENAME)
13953         {
13954           /* Remember that we've seen it.  */
13955           typename_p = true;
13956           /* Consume the `typename' token.  */
13957           cp_lexer_consume_token (parser->lexer);
13958         }
13959     }
13960
13961   /* Look for the optional global scope qualification.  */
13962   global_scope_p
13963     = (cp_parser_global_scope_opt (parser,
13964                                    /*current_scope_valid_p=*/false)
13965        != NULL_TREE);
13966
13967   /* If we saw `typename', or didn't see `::', then there must be a
13968      nested-name-specifier present.  */
13969   if (typename_p || !global_scope_p)
13970     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13971                                               /*check_dependency_p=*/true,
13972                                               /*type_p=*/false,
13973                                               /*is_declaration=*/true);
13974   /* Otherwise, we could be in either of the two productions.  In that
13975      case, treat the nested-name-specifier as optional.  */
13976   else
13977     qscope = cp_parser_nested_name_specifier_opt (parser,
13978                                                   /*typename_keyword_p=*/false,
13979                                                   /*check_dependency_p=*/true,
13980                                                   /*type_p=*/false,
13981                                                   /*is_declaration=*/true);
13982   if (!qscope)
13983     qscope = global_namespace;
13984
13985   if (access_declaration_p && cp_parser_error_occurred (parser))
13986     /* Something has already gone wrong; there's no need to parse
13987        further.  Since an error has occurred, the return value of
13988        cp_parser_parse_definitely will be false, as required.  */
13989     return cp_parser_parse_definitely (parser);
13990
13991   token = cp_lexer_peek_token (parser->lexer);
13992   /* Parse the unqualified-id.  */
13993   identifier = cp_parser_unqualified_id (parser,
13994                                          /*template_keyword_p=*/false,
13995                                          /*check_dependency_p=*/true,
13996                                          /*declarator_p=*/true,
13997                                          /*optional_p=*/false);
13998
13999   if (access_declaration_p)
14000     {
14001       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14002         cp_parser_simulate_error (parser);
14003       if (!cp_parser_parse_definitely (parser))
14004         return false;
14005     }
14006
14007   /* The function we call to handle a using-declaration is different
14008      depending on what scope we are in.  */
14009   if (qscope == error_mark_node || identifier == error_mark_node)
14010     ;
14011   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14012            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14013     /* [namespace.udecl]
14014
14015        A using declaration shall not name a template-id.  */
14016     error_at (token->location,
14017               "a template-id may not appear in a using-declaration");
14018   else
14019     {
14020       if (at_class_scope_p ())
14021         {
14022           /* Create the USING_DECL.  */
14023           decl = do_class_using_decl (parser->scope, identifier);
14024
14025           if (check_for_bare_parameter_packs (decl))
14026             return false;
14027           else
14028             /* Add it to the list of members in this class.  */
14029             finish_member_declaration (decl);
14030         }
14031       else
14032         {
14033           decl = cp_parser_lookup_name_simple (parser,
14034                                                identifier,
14035                                                token->location);
14036           if (decl == error_mark_node)
14037             cp_parser_name_lookup_error (parser, identifier,
14038                                          decl, NLE_NULL,
14039                                          token->location);
14040           else if (check_for_bare_parameter_packs (decl))
14041             return false;
14042           else if (!at_namespace_scope_p ())
14043             do_local_using_decl (decl, qscope, identifier);
14044           else
14045             do_toplevel_using_decl (decl, qscope, identifier);
14046         }
14047     }
14048
14049   /* Look for the final `;'.  */
14050   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14051   
14052   return true;
14053 }
14054
14055 /* Parse a using-directive.
14056
14057    using-directive:
14058      using namespace :: [opt] nested-name-specifier [opt]
14059        namespace-name ;  */
14060
14061 static void
14062 cp_parser_using_directive (cp_parser* parser)
14063 {
14064   tree namespace_decl;
14065   tree attribs;
14066
14067   /* Look for the `using' keyword.  */
14068   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14069   /* And the `namespace' keyword.  */
14070   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14071   /* Look for the optional `::' operator.  */
14072   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14073   /* And the optional nested-name-specifier.  */
14074   cp_parser_nested_name_specifier_opt (parser,
14075                                        /*typename_keyword_p=*/false,
14076                                        /*check_dependency_p=*/true,
14077                                        /*type_p=*/false,
14078                                        /*is_declaration=*/true);
14079   /* Get the namespace being used.  */
14080   namespace_decl = cp_parser_namespace_name (parser);
14081   /* And any specified attributes.  */
14082   attribs = cp_parser_attributes_opt (parser);
14083   /* Update the symbol table.  */
14084   parse_using_directive (namespace_decl, attribs);
14085   /* Look for the final `;'.  */
14086   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14087 }
14088
14089 /* Parse an asm-definition.
14090
14091    asm-definition:
14092      asm ( string-literal ) ;
14093
14094    GNU Extension:
14095
14096    asm-definition:
14097      asm volatile [opt] ( string-literal ) ;
14098      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14099      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14100                           : asm-operand-list [opt] ) ;
14101      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14102                           : asm-operand-list [opt]
14103                           : asm-clobber-list [opt] ) ;
14104      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14105                                : asm-clobber-list [opt]
14106                                : asm-goto-list ) ;  */
14107
14108 static void
14109 cp_parser_asm_definition (cp_parser* parser)
14110 {
14111   tree string;
14112   tree outputs = NULL_TREE;
14113   tree inputs = NULL_TREE;
14114   tree clobbers = NULL_TREE;
14115   tree labels = NULL_TREE;
14116   tree asm_stmt;
14117   bool volatile_p = false;
14118   bool extended_p = false;
14119   bool invalid_inputs_p = false;
14120   bool invalid_outputs_p = false;
14121   bool goto_p = false;
14122   required_token missing = RT_NONE;
14123
14124   /* Look for the `asm' keyword.  */
14125   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14126   /* See if the next token is `volatile'.  */
14127   if (cp_parser_allow_gnu_extensions_p (parser)
14128       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14129     {
14130       /* Remember that we saw the `volatile' keyword.  */
14131       volatile_p = true;
14132       /* Consume the token.  */
14133       cp_lexer_consume_token (parser->lexer);
14134     }
14135   if (cp_parser_allow_gnu_extensions_p (parser)
14136       && parser->in_function_body
14137       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14138     {
14139       /* Remember that we saw the `goto' keyword.  */
14140       goto_p = true;
14141       /* Consume the token.  */
14142       cp_lexer_consume_token (parser->lexer);
14143     }
14144   /* Look for the opening `('.  */
14145   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14146     return;
14147   /* Look for the string.  */
14148   string = cp_parser_string_literal (parser, false, false);
14149   if (string == error_mark_node)
14150     {
14151       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14152                                              /*consume_paren=*/true);
14153       return;
14154     }
14155
14156   /* If we're allowing GNU extensions, check for the extended assembly
14157      syntax.  Unfortunately, the `:' tokens need not be separated by
14158      a space in C, and so, for compatibility, we tolerate that here
14159      too.  Doing that means that we have to treat the `::' operator as
14160      two `:' tokens.  */
14161   if (cp_parser_allow_gnu_extensions_p (parser)
14162       && parser->in_function_body
14163       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14164           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14165     {
14166       bool inputs_p = false;
14167       bool clobbers_p = false;
14168       bool labels_p = false;
14169
14170       /* The extended syntax was used.  */
14171       extended_p = true;
14172
14173       /* Look for outputs.  */
14174       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14175         {
14176           /* Consume the `:'.  */
14177           cp_lexer_consume_token (parser->lexer);
14178           /* Parse the output-operands.  */
14179           if (cp_lexer_next_token_is_not (parser->lexer,
14180                                           CPP_COLON)
14181               && cp_lexer_next_token_is_not (parser->lexer,
14182                                              CPP_SCOPE)
14183               && cp_lexer_next_token_is_not (parser->lexer,
14184                                              CPP_CLOSE_PAREN)
14185               && !goto_p)
14186             outputs = cp_parser_asm_operand_list (parser);
14187
14188             if (outputs == error_mark_node)
14189               invalid_outputs_p = true;
14190         }
14191       /* If the next token is `::', there are no outputs, and the
14192          next token is the beginning of the inputs.  */
14193       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14194         /* The inputs are coming next.  */
14195         inputs_p = true;
14196
14197       /* Look for inputs.  */
14198       if (inputs_p
14199           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14200         {
14201           /* Consume the `:' or `::'.  */
14202           cp_lexer_consume_token (parser->lexer);
14203           /* Parse the output-operands.  */
14204           if (cp_lexer_next_token_is_not (parser->lexer,
14205                                           CPP_COLON)
14206               && cp_lexer_next_token_is_not (parser->lexer,
14207                                              CPP_SCOPE)
14208               && cp_lexer_next_token_is_not (parser->lexer,
14209                                              CPP_CLOSE_PAREN))
14210             inputs = cp_parser_asm_operand_list (parser);
14211
14212             if (inputs == error_mark_node)
14213               invalid_inputs_p = true;
14214         }
14215       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14216         /* The clobbers are coming next.  */
14217         clobbers_p = true;
14218
14219       /* Look for clobbers.  */
14220       if (clobbers_p
14221           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14222         {
14223           clobbers_p = true;
14224           /* Consume the `:' or `::'.  */
14225           cp_lexer_consume_token (parser->lexer);
14226           /* Parse the clobbers.  */
14227           if (cp_lexer_next_token_is_not (parser->lexer,
14228                                           CPP_COLON)
14229               && cp_lexer_next_token_is_not (parser->lexer,
14230                                              CPP_CLOSE_PAREN))
14231             clobbers = cp_parser_asm_clobber_list (parser);
14232         }
14233       else if (goto_p
14234                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14235         /* The labels are coming next.  */
14236         labels_p = true;
14237
14238       /* Look for labels.  */
14239       if (labels_p
14240           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14241         {
14242           labels_p = true;
14243           /* Consume the `:' or `::'.  */
14244           cp_lexer_consume_token (parser->lexer);
14245           /* Parse the labels.  */
14246           labels = cp_parser_asm_label_list (parser);
14247         }
14248
14249       if (goto_p && !labels_p)
14250         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14251     }
14252   else if (goto_p)
14253     missing = RT_COLON_SCOPE;
14254
14255   /* Look for the closing `)'.  */
14256   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14257                           missing ? missing : RT_CLOSE_PAREN))
14258     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14259                                            /*consume_paren=*/true);
14260   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14261
14262   if (!invalid_inputs_p && !invalid_outputs_p)
14263     {
14264       /* Create the ASM_EXPR.  */
14265       if (parser->in_function_body)
14266         {
14267           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14268                                       inputs, clobbers, labels);
14269           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14270           if (!extended_p)
14271             {
14272               tree temp = asm_stmt;
14273               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14274                 temp = TREE_OPERAND (temp, 0);
14275
14276               ASM_INPUT_P (temp) = 1;
14277             }
14278         }
14279       else
14280         cgraph_add_asm_node (string);
14281     }
14282 }
14283
14284 /* Declarators [gram.dcl.decl] */
14285
14286 /* Parse an init-declarator.
14287
14288    init-declarator:
14289      declarator initializer [opt]
14290
14291    GNU Extension:
14292
14293    init-declarator:
14294      declarator asm-specification [opt] attributes [opt] initializer [opt]
14295
14296    function-definition:
14297      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14298        function-body
14299      decl-specifier-seq [opt] declarator function-try-block
14300
14301    GNU Extension:
14302
14303    function-definition:
14304      __extension__ function-definition
14305
14306    The DECL_SPECIFIERS apply to this declarator.  Returns a
14307    representation of the entity declared.  If MEMBER_P is TRUE, then
14308    this declarator appears in a class scope.  The new DECL created by
14309    this declarator is returned.
14310
14311    The CHECKS are access checks that should be performed once we know
14312    what entity is being declared (and, therefore, what classes have
14313    befriended it).
14314
14315    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14316    for a function-definition here as well.  If the declarator is a
14317    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14318    be TRUE upon return.  By that point, the function-definition will
14319    have been completely parsed.
14320
14321    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14322    is FALSE.  */
14323
14324 static tree
14325 cp_parser_init_declarator (cp_parser* parser,
14326                            cp_decl_specifier_seq *decl_specifiers,
14327                            VEC (deferred_access_check,gc)* checks,
14328                            bool function_definition_allowed_p,
14329                            bool member_p,
14330                            int declares_class_or_enum,
14331                            bool* function_definition_p)
14332 {
14333   cp_token *token = NULL, *asm_spec_start_token = NULL,
14334            *attributes_start_token = NULL;
14335   cp_declarator *declarator;
14336   tree prefix_attributes;
14337   tree attributes;
14338   tree asm_specification;
14339   tree initializer;
14340   tree decl = NULL_TREE;
14341   tree scope;
14342   int is_initialized;
14343   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14344      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14345      "(...)".  */
14346   enum cpp_ttype initialization_kind;
14347   bool is_direct_init = false;
14348   bool is_non_constant_init;
14349   int ctor_dtor_or_conv_p;
14350   bool friend_p;
14351   tree pushed_scope = NULL;
14352
14353   /* Gather the attributes that were provided with the
14354      decl-specifiers.  */
14355   prefix_attributes = decl_specifiers->attributes;
14356
14357   /* Assume that this is not the declarator for a function
14358      definition.  */
14359   if (function_definition_p)
14360     *function_definition_p = false;
14361
14362   /* Defer access checks while parsing the declarator; we cannot know
14363      what names are accessible until we know what is being
14364      declared.  */
14365   resume_deferring_access_checks ();
14366
14367   /* Parse the declarator.  */
14368   token = cp_lexer_peek_token (parser->lexer);
14369   declarator
14370     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14371                             &ctor_dtor_or_conv_p,
14372                             /*parenthesized_p=*/NULL,
14373                             /*member_p=*/false);
14374   /* Gather up the deferred checks.  */
14375   stop_deferring_access_checks ();
14376
14377   /* If the DECLARATOR was erroneous, there's no need to go
14378      further.  */
14379   if (declarator == cp_error_declarator)
14380     return error_mark_node;
14381
14382   /* Check that the number of template-parameter-lists is OK.  */
14383   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14384                                                        token->location))
14385     return error_mark_node;
14386
14387   if (declares_class_or_enum & 2)
14388     cp_parser_check_for_definition_in_return_type (declarator,
14389                                                    decl_specifiers->type,
14390                                                    decl_specifiers->type_location);
14391
14392   /* Figure out what scope the entity declared by the DECLARATOR is
14393      located in.  `grokdeclarator' sometimes changes the scope, so
14394      we compute it now.  */
14395   scope = get_scope_of_declarator (declarator);
14396
14397   /* Perform any lookups in the declared type which were thought to be
14398      dependent, but are not in the scope of the declarator.  */
14399   decl_specifiers->type
14400     = maybe_update_decl_type (decl_specifiers->type, scope);
14401
14402   /* If we're allowing GNU extensions, look for an asm-specification
14403      and attributes.  */
14404   if (cp_parser_allow_gnu_extensions_p (parser))
14405     {
14406       /* Look for an asm-specification.  */
14407       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14408       asm_specification = cp_parser_asm_specification_opt (parser);
14409       /* And attributes.  */
14410       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14411       attributes = cp_parser_attributes_opt (parser);
14412     }
14413   else
14414     {
14415       asm_specification = NULL_TREE;
14416       attributes = NULL_TREE;
14417     }
14418
14419   /* Peek at the next token.  */
14420   token = cp_lexer_peek_token (parser->lexer);
14421   /* Check to see if the token indicates the start of a
14422      function-definition.  */
14423   if (function_declarator_p (declarator)
14424       && cp_parser_token_starts_function_definition_p (token))
14425     {
14426       if (!function_definition_allowed_p)
14427         {
14428           /* If a function-definition should not appear here, issue an
14429              error message.  */
14430           cp_parser_error (parser,
14431                            "a function-definition is not allowed here");
14432           return error_mark_node;
14433         }
14434       else
14435         {
14436           location_t func_brace_location
14437             = cp_lexer_peek_token (parser->lexer)->location;
14438
14439           /* Neither attributes nor an asm-specification are allowed
14440              on a function-definition.  */
14441           if (asm_specification)
14442             error_at (asm_spec_start_token->location,
14443                       "an asm-specification is not allowed "
14444                       "on a function-definition");
14445           if (attributes)
14446             error_at (attributes_start_token->location,
14447                       "attributes are not allowed on a function-definition");
14448           /* This is a function-definition.  */
14449           *function_definition_p = true;
14450
14451           /* Parse the function definition.  */
14452           if (member_p)
14453             decl = cp_parser_save_member_function_body (parser,
14454                                                         decl_specifiers,
14455                                                         declarator,
14456                                                         prefix_attributes);
14457           else
14458             decl
14459               = (cp_parser_function_definition_from_specifiers_and_declarator
14460                  (parser, decl_specifiers, prefix_attributes, declarator));
14461
14462           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14463             {
14464               /* This is where the prologue starts...  */
14465               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14466                 = func_brace_location;
14467             }
14468
14469           return decl;
14470         }
14471     }
14472
14473   /* [dcl.dcl]
14474
14475      Only in function declarations for constructors, destructors, and
14476      type conversions can the decl-specifier-seq be omitted.
14477
14478      We explicitly postpone this check past the point where we handle
14479      function-definitions because we tolerate function-definitions
14480      that are missing their return types in some modes.  */
14481   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14482     {
14483       cp_parser_error (parser,
14484                        "expected constructor, destructor, or type conversion");
14485       return error_mark_node;
14486     }
14487
14488   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14489   if (token->type == CPP_EQ
14490       || token->type == CPP_OPEN_PAREN
14491       || token->type == CPP_OPEN_BRACE)
14492     {
14493       is_initialized = SD_INITIALIZED;
14494       initialization_kind = token->type;
14495
14496       if (token->type == CPP_EQ
14497           && function_declarator_p (declarator))
14498         {
14499           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14500           if (t2->keyword == RID_DEFAULT)
14501             is_initialized = SD_DEFAULTED;
14502           else if (t2->keyword == RID_DELETE)
14503             is_initialized = SD_DELETED;
14504         }
14505     }
14506   else
14507     {
14508       /* If the init-declarator isn't initialized and isn't followed by a
14509          `,' or `;', it's not a valid init-declarator.  */
14510       if (token->type != CPP_COMMA
14511           && token->type != CPP_SEMICOLON)
14512         {
14513           cp_parser_error (parser, "expected initializer");
14514           return error_mark_node;
14515         }
14516       is_initialized = SD_UNINITIALIZED;
14517       initialization_kind = CPP_EOF;
14518     }
14519
14520   /* Because start_decl has side-effects, we should only call it if we
14521      know we're going ahead.  By this point, we know that we cannot
14522      possibly be looking at any other construct.  */
14523   cp_parser_commit_to_tentative_parse (parser);
14524
14525   /* If the decl specifiers were bad, issue an error now that we're
14526      sure this was intended to be a declarator.  Then continue
14527      declaring the variable(s), as int, to try to cut down on further
14528      errors.  */
14529   if (decl_specifiers->any_specifiers_p
14530       && decl_specifiers->type == error_mark_node)
14531     {
14532       cp_parser_error (parser, "invalid type in declaration");
14533       decl_specifiers->type = integer_type_node;
14534     }
14535
14536   /* Check to see whether or not this declaration is a friend.  */
14537   friend_p = cp_parser_friend_p (decl_specifiers);
14538
14539   /* Enter the newly declared entry in the symbol table.  If we're
14540      processing a declaration in a class-specifier, we wait until
14541      after processing the initializer.  */
14542   if (!member_p)
14543     {
14544       if (parser->in_unbraced_linkage_specification_p)
14545         decl_specifiers->storage_class = sc_extern;
14546       decl = start_decl (declarator, decl_specifiers,
14547                          is_initialized, attributes, prefix_attributes,
14548                          &pushed_scope);
14549       /* Adjust location of decl if declarator->id_loc is more appropriate:
14550          set, and decl wasn't merged with another decl, in which case its
14551          location would be different from input_location, and more accurate.  */
14552       if (DECL_P (decl)
14553           && declarator->id_loc != UNKNOWN_LOCATION
14554           && DECL_SOURCE_LOCATION (decl) == input_location)
14555         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14556     }
14557   else if (scope)
14558     /* Enter the SCOPE.  That way unqualified names appearing in the
14559        initializer will be looked up in SCOPE.  */
14560     pushed_scope = push_scope (scope);
14561
14562   /* Perform deferred access control checks, now that we know in which
14563      SCOPE the declared entity resides.  */
14564   if (!member_p && decl)
14565     {
14566       tree saved_current_function_decl = NULL_TREE;
14567
14568       /* If the entity being declared is a function, pretend that we
14569          are in its scope.  If it is a `friend', it may have access to
14570          things that would not otherwise be accessible.  */
14571       if (TREE_CODE (decl) == FUNCTION_DECL)
14572         {
14573           saved_current_function_decl = current_function_decl;
14574           current_function_decl = decl;
14575         }
14576
14577       /* Perform access checks for template parameters.  */
14578       cp_parser_perform_template_parameter_access_checks (checks);
14579
14580       /* Perform the access control checks for the declarator and the
14581          decl-specifiers.  */
14582       perform_deferred_access_checks ();
14583
14584       /* Restore the saved value.  */
14585       if (TREE_CODE (decl) == FUNCTION_DECL)
14586         current_function_decl = saved_current_function_decl;
14587     }
14588
14589   /* Parse the initializer.  */
14590   initializer = NULL_TREE;
14591   is_direct_init = false;
14592   is_non_constant_init = true;
14593   if (is_initialized)
14594     {
14595       if (function_declarator_p (declarator))
14596         {
14597           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14598            if (initialization_kind == CPP_EQ)
14599              initializer = cp_parser_pure_specifier (parser);
14600            else
14601              {
14602                /* If the declaration was erroneous, we don't really
14603                   know what the user intended, so just silently
14604                   consume the initializer.  */
14605                if (decl != error_mark_node)
14606                  error_at (initializer_start_token->location,
14607                            "initializer provided for function");
14608                cp_parser_skip_to_closing_parenthesis (parser,
14609                                                       /*recovering=*/true,
14610                                                       /*or_comma=*/false,
14611                                                       /*consume_paren=*/true);
14612              }
14613         }
14614       else
14615         {
14616           /* We want to record the extra mangling scope for in-class
14617              initializers of class members and initializers of static data
14618              member templates.  The former is a C++0x feature which isn't
14619              implemented yet, and I expect it will involve deferring
14620              parsing of the initializer until end of class as with default
14621              arguments.  So right here we only handle the latter.  */
14622           if (!member_p && processing_template_decl)
14623             start_lambda_scope (decl);
14624           initializer = cp_parser_initializer (parser,
14625                                                &is_direct_init,
14626                                                &is_non_constant_init);
14627           if (!member_p && processing_template_decl)
14628             finish_lambda_scope ();
14629         }
14630     }
14631
14632   /* The old parser allows attributes to appear after a parenthesized
14633      initializer.  Mark Mitchell proposed removing this functionality
14634      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14635      attributes -- but ignores them.  */
14636   if (cp_parser_allow_gnu_extensions_p (parser)
14637       && initialization_kind == CPP_OPEN_PAREN)
14638     if (cp_parser_attributes_opt (parser))
14639       warning (OPT_Wattributes,
14640                "attributes after parenthesized initializer ignored");
14641
14642   /* For an in-class declaration, use `grokfield' to create the
14643      declaration.  */
14644   if (member_p)
14645     {
14646       if (pushed_scope)
14647         {
14648           pop_scope (pushed_scope);
14649           pushed_scope = false;
14650         }
14651       decl = grokfield (declarator, decl_specifiers,
14652                         initializer, !is_non_constant_init,
14653                         /*asmspec=*/NULL_TREE,
14654                         prefix_attributes);
14655       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14656         cp_parser_save_default_args (parser, decl);
14657     }
14658
14659   /* Finish processing the declaration.  But, skip friend
14660      declarations.  */
14661   if (!friend_p && decl && decl != error_mark_node)
14662     {
14663       cp_finish_decl (decl,
14664                       initializer, !is_non_constant_init,
14665                       asm_specification,
14666                       /* If the initializer is in parentheses, then this is
14667                          a direct-initialization, which means that an
14668                          `explicit' constructor is OK.  Otherwise, an
14669                          `explicit' constructor cannot be used.  */
14670                       ((is_direct_init || !is_initialized)
14671                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14672     }
14673   else if ((cxx_dialect != cxx98) && friend_p
14674            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14675     /* Core issue #226 (C++0x only): A default template-argument
14676        shall not be specified in a friend class template
14677        declaration. */
14678     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14679                              /*is_partial=*/0, /*is_friend_decl=*/1);
14680
14681   if (!friend_p && pushed_scope)
14682     pop_scope (pushed_scope);
14683
14684   return decl;
14685 }
14686
14687 /* Parse a declarator.
14688
14689    declarator:
14690      direct-declarator
14691      ptr-operator declarator
14692
14693    abstract-declarator:
14694      ptr-operator abstract-declarator [opt]
14695      direct-abstract-declarator
14696
14697    GNU Extensions:
14698
14699    declarator:
14700      attributes [opt] direct-declarator
14701      attributes [opt] ptr-operator declarator
14702
14703    abstract-declarator:
14704      attributes [opt] ptr-operator abstract-declarator [opt]
14705      attributes [opt] direct-abstract-declarator
14706
14707    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14708    detect constructor, destructor or conversion operators. It is set
14709    to -1 if the declarator is a name, and +1 if it is a
14710    function. Otherwise it is set to zero. Usually you just want to
14711    test for >0, but internally the negative value is used.
14712
14713    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14714    a decl-specifier-seq unless it declares a constructor, destructor,
14715    or conversion.  It might seem that we could check this condition in
14716    semantic analysis, rather than parsing, but that makes it difficult
14717    to handle something like `f()'.  We want to notice that there are
14718    no decl-specifiers, and therefore realize that this is an
14719    expression, not a declaration.)
14720
14721    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14722    the declarator is a direct-declarator of the form "(...)".
14723
14724    MEMBER_P is true iff this declarator is a member-declarator.  */
14725
14726 static cp_declarator *
14727 cp_parser_declarator (cp_parser* parser,
14728                       cp_parser_declarator_kind dcl_kind,
14729                       int* ctor_dtor_or_conv_p,
14730                       bool* parenthesized_p,
14731                       bool member_p)
14732 {
14733   cp_declarator *declarator;
14734   enum tree_code code;
14735   cp_cv_quals cv_quals;
14736   tree class_type;
14737   tree attributes = NULL_TREE;
14738
14739   /* Assume this is not a constructor, destructor, or type-conversion
14740      operator.  */
14741   if (ctor_dtor_or_conv_p)
14742     *ctor_dtor_or_conv_p = 0;
14743
14744   if (cp_parser_allow_gnu_extensions_p (parser))
14745     attributes = cp_parser_attributes_opt (parser);
14746
14747   /* Check for the ptr-operator production.  */
14748   cp_parser_parse_tentatively (parser);
14749   /* Parse the ptr-operator.  */
14750   code = cp_parser_ptr_operator (parser,
14751                                  &class_type,
14752                                  &cv_quals);
14753   /* If that worked, then we have a ptr-operator.  */
14754   if (cp_parser_parse_definitely (parser))
14755     {
14756       /* If a ptr-operator was found, then this declarator was not
14757          parenthesized.  */
14758       if (parenthesized_p)
14759         *parenthesized_p = true;
14760       /* The dependent declarator is optional if we are parsing an
14761          abstract-declarator.  */
14762       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14763         cp_parser_parse_tentatively (parser);
14764
14765       /* Parse the dependent declarator.  */
14766       declarator = cp_parser_declarator (parser, dcl_kind,
14767                                          /*ctor_dtor_or_conv_p=*/NULL,
14768                                          /*parenthesized_p=*/NULL,
14769                                          /*member_p=*/false);
14770
14771       /* If we are parsing an abstract-declarator, we must handle the
14772          case where the dependent declarator is absent.  */
14773       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14774           && !cp_parser_parse_definitely (parser))
14775         declarator = NULL;
14776
14777       declarator = cp_parser_make_indirect_declarator
14778         (code, class_type, cv_quals, declarator);
14779     }
14780   /* Everything else is a direct-declarator.  */
14781   else
14782     {
14783       if (parenthesized_p)
14784         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14785                                                    CPP_OPEN_PAREN);
14786       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14787                                                 ctor_dtor_or_conv_p,
14788                                                 member_p);
14789     }
14790
14791   if (attributes && declarator && declarator != cp_error_declarator)
14792     declarator->attributes = attributes;
14793
14794   return declarator;
14795 }
14796
14797 /* Parse a direct-declarator or direct-abstract-declarator.
14798
14799    direct-declarator:
14800      declarator-id
14801      direct-declarator ( parameter-declaration-clause )
14802        cv-qualifier-seq [opt]
14803        exception-specification [opt]
14804      direct-declarator [ constant-expression [opt] ]
14805      ( declarator )
14806
14807    direct-abstract-declarator:
14808      direct-abstract-declarator [opt]
14809        ( parameter-declaration-clause )
14810        cv-qualifier-seq [opt]
14811        exception-specification [opt]
14812      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14813      ( abstract-declarator )
14814
14815    Returns a representation of the declarator.  DCL_KIND is
14816    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14817    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14818    we are parsing a direct-declarator.  It is
14819    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14820    of ambiguity we prefer an abstract declarator, as per
14821    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14822    cp_parser_declarator.  */
14823
14824 static cp_declarator *
14825 cp_parser_direct_declarator (cp_parser* parser,
14826                              cp_parser_declarator_kind dcl_kind,
14827                              int* ctor_dtor_or_conv_p,
14828                              bool member_p)
14829 {
14830   cp_token *token;
14831   cp_declarator *declarator = NULL;
14832   tree scope = NULL_TREE;
14833   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14834   bool saved_in_declarator_p = parser->in_declarator_p;
14835   bool first = true;
14836   tree pushed_scope = NULL_TREE;
14837
14838   while (true)
14839     {
14840       /* Peek at the next token.  */
14841       token = cp_lexer_peek_token (parser->lexer);
14842       if (token->type == CPP_OPEN_PAREN)
14843         {
14844           /* This is either a parameter-declaration-clause, or a
14845              parenthesized declarator. When we know we are parsing a
14846              named declarator, it must be a parenthesized declarator
14847              if FIRST is true. For instance, `(int)' is a
14848              parameter-declaration-clause, with an omitted
14849              direct-abstract-declarator. But `((*))', is a
14850              parenthesized abstract declarator. Finally, when T is a
14851              template parameter `(T)' is a
14852              parameter-declaration-clause, and not a parenthesized
14853              named declarator.
14854
14855              We first try and parse a parameter-declaration-clause,
14856              and then try a nested declarator (if FIRST is true).
14857
14858              It is not an error for it not to be a
14859              parameter-declaration-clause, even when FIRST is
14860              false. Consider,
14861
14862                int i (int);
14863                int i (3);
14864
14865              The first is the declaration of a function while the
14866              second is the definition of a variable, including its
14867              initializer.
14868
14869              Having seen only the parenthesis, we cannot know which of
14870              these two alternatives should be selected.  Even more
14871              complex are examples like:
14872
14873                int i (int (a));
14874                int i (int (3));
14875
14876              The former is a function-declaration; the latter is a
14877              variable initialization.
14878
14879              Thus again, we try a parameter-declaration-clause, and if
14880              that fails, we back out and return.  */
14881
14882           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14883             {
14884               tree params;
14885               unsigned saved_num_template_parameter_lists;
14886               bool is_declarator = false;
14887               tree t;
14888
14889               /* In a member-declarator, the only valid interpretation
14890                  of a parenthesis is the start of a
14891                  parameter-declaration-clause.  (It is invalid to
14892                  initialize a static data member with a parenthesized
14893                  initializer; only the "=" form of initialization is
14894                  permitted.)  */
14895               if (!member_p)
14896                 cp_parser_parse_tentatively (parser);
14897
14898               /* Consume the `('.  */
14899               cp_lexer_consume_token (parser->lexer);
14900               if (first)
14901                 {
14902                   /* If this is going to be an abstract declarator, we're
14903                      in a declarator and we can't have default args.  */
14904                   parser->default_arg_ok_p = false;
14905                   parser->in_declarator_p = true;
14906                 }
14907
14908               /* Inside the function parameter list, surrounding
14909                  template-parameter-lists do not apply.  */
14910               saved_num_template_parameter_lists
14911                 = parser->num_template_parameter_lists;
14912               parser->num_template_parameter_lists = 0;
14913
14914               begin_scope (sk_function_parms, NULL_TREE);
14915
14916               /* Parse the parameter-declaration-clause.  */
14917               params = cp_parser_parameter_declaration_clause (parser);
14918
14919               parser->num_template_parameter_lists
14920                 = saved_num_template_parameter_lists;
14921
14922               /* If all went well, parse the cv-qualifier-seq and the
14923                  exception-specification.  */
14924               if (member_p || cp_parser_parse_definitely (parser))
14925                 {
14926                   cp_cv_quals cv_quals;
14927                   tree exception_specification;
14928                   tree late_return;
14929
14930                   is_declarator = true;
14931
14932                   if (ctor_dtor_or_conv_p)
14933                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14934                   first = false;
14935                   /* Consume the `)'.  */
14936                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14937
14938                   /* Parse the cv-qualifier-seq.  */
14939                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14940                   /* And the exception-specification.  */
14941                   exception_specification
14942                     = cp_parser_exception_specification_opt (parser);
14943
14944                   late_return
14945                     = cp_parser_late_return_type_opt (parser);
14946
14947                   /* Create the function-declarator.  */
14948                   declarator = make_call_declarator (declarator,
14949                                                      params,
14950                                                      cv_quals,
14951                                                      exception_specification,
14952                                                      late_return);
14953                   /* Any subsequent parameter lists are to do with
14954                      return type, so are not those of the declared
14955                      function.  */
14956                   parser->default_arg_ok_p = false;
14957                 }
14958
14959               /* Remove the function parms from scope.  */
14960               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14961                 pop_binding (DECL_NAME (t), t);
14962               leave_scope();
14963
14964               if (is_declarator)
14965                 /* Repeat the main loop.  */
14966                 continue;
14967             }
14968
14969           /* If this is the first, we can try a parenthesized
14970              declarator.  */
14971           if (first)
14972             {
14973               bool saved_in_type_id_in_expr_p;
14974
14975               parser->default_arg_ok_p = saved_default_arg_ok_p;
14976               parser->in_declarator_p = saved_in_declarator_p;
14977
14978               /* Consume the `('.  */
14979               cp_lexer_consume_token (parser->lexer);
14980               /* Parse the nested declarator.  */
14981               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14982               parser->in_type_id_in_expr_p = true;
14983               declarator
14984                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14985                                         /*parenthesized_p=*/NULL,
14986                                         member_p);
14987               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14988               first = false;
14989               /* Expect a `)'.  */
14990               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14991                 declarator = cp_error_declarator;
14992               if (declarator == cp_error_declarator)
14993                 break;
14994
14995               goto handle_declarator;
14996             }
14997           /* Otherwise, we must be done.  */
14998           else
14999             break;
15000         }
15001       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15002                && token->type == CPP_OPEN_SQUARE)
15003         {
15004           /* Parse an array-declarator.  */
15005           tree bounds;
15006
15007           if (ctor_dtor_or_conv_p)
15008             *ctor_dtor_or_conv_p = 0;
15009
15010           first = false;
15011           parser->default_arg_ok_p = false;
15012           parser->in_declarator_p = true;
15013           /* Consume the `['.  */
15014           cp_lexer_consume_token (parser->lexer);
15015           /* Peek at the next token.  */
15016           token = cp_lexer_peek_token (parser->lexer);
15017           /* If the next token is `]', then there is no
15018              constant-expression.  */
15019           if (token->type != CPP_CLOSE_SQUARE)
15020             {
15021               bool non_constant_p;
15022
15023               bounds
15024                 = cp_parser_constant_expression (parser,
15025                                                  /*allow_non_constant=*/true,
15026                                                  &non_constant_p);
15027               if (!non_constant_p || cxx_dialect >= cxx0x)
15028                 /* OK */;
15029               /* Normally, the array bound must be an integral constant
15030                  expression.  However, as an extension, we allow VLAs
15031                  in function scopes as long as they aren't part of a
15032                  parameter declaration.  */
15033               else if (!parser->in_function_body
15034                        || current_binding_level->kind == sk_function_parms)
15035                 {
15036                   cp_parser_error (parser,
15037                                    "array bound is not an integer constant");
15038                   bounds = error_mark_node;
15039                 }
15040               else if (processing_template_decl && !error_operand_p (bounds))
15041                 {
15042                   /* Remember this wasn't a constant-expression.  */
15043                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15044                   TREE_SIDE_EFFECTS (bounds) = 1;
15045                 }
15046             }
15047           else
15048             bounds = NULL_TREE;
15049           /* Look for the closing `]'.  */
15050           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15051             {
15052               declarator = cp_error_declarator;
15053               break;
15054             }
15055
15056           declarator = make_array_declarator (declarator, bounds);
15057         }
15058       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15059         {
15060           {
15061             tree qualifying_scope;
15062             tree unqualified_name;
15063             special_function_kind sfk;
15064             bool abstract_ok;
15065             bool pack_expansion_p = false;
15066             cp_token *declarator_id_start_token;
15067
15068             /* Parse a declarator-id */
15069             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15070             if (abstract_ok)
15071               {
15072                 cp_parser_parse_tentatively (parser);
15073
15074                 /* If we see an ellipsis, we should be looking at a
15075                    parameter pack. */
15076                 if (token->type == CPP_ELLIPSIS)
15077                   {
15078                     /* Consume the `...' */
15079                     cp_lexer_consume_token (parser->lexer);
15080
15081                     pack_expansion_p = true;
15082                   }
15083               }
15084
15085             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15086             unqualified_name
15087               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15088             qualifying_scope = parser->scope;
15089             if (abstract_ok)
15090               {
15091                 bool okay = false;
15092
15093                 if (!unqualified_name && pack_expansion_p)
15094                   {
15095                     /* Check whether an error occurred. */
15096                     okay = !cp_parser_error_occurred (parser);
15097
15098                     /* We already consumed the ellipsis to mark a
15099                        parameter pack, but we have no way to report it,
15100                        so abort the tentative parse. We will be exiting
15101                        immediately anyway. */
15102                     cp_parser_abort_tentative_parse (parser);
15103                   }
15104                 else
15105                   okay = cp_parser_parse_definitely (parser);
15106
15107                 if (!okay)
15108                   unqualified_name = error_mark_node;
15109                 else if (unqualified_name
15110                          && (qualifying_scope
15111                              || (TREE_CODE (unqualified_name)
15112                                  != IDENTIFIER_NODE)))
15113                   {
15114                     cp_parser_error (parser, "expected unqualified-id");
15115                     unqualified_name = error_mark_node;
15116                   }
15117               }
15118
15119             if (!unqualified_name)
15120               return NULL;
15121             if (unqualified_name == error_mark_node)
15122               {
15123                 declarator = cp_error_declarator;
15124                 pack_expansion_p = false;
15125                 declarator->parameter_pack_p = false;
15126                 break;
15127               }
15128
15129             if (qualifying_scope && at_namespace_scope_p ()
15130                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15131               {
15132                 /* In the declaration of a member of a template class
15133                    outside of the class itself, the SCOPE will sometimes
15134                    be a TYPENAME_TYPE.  For example, given:
15135
15136                    template <typename T>
15137                    int S<T>::R::i = 3;
15138
15139                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15140                    this context, we must resolve S<T>::R to an ordinary
15141                    type, rather than a typename type.
15142
15143                    The reason we normally avoid resolving TYPENAME_TYPEs
15144                    is that a specialization of `S' might render
15145                    `S<T>::R' not a type.  However, if `S' is
15146                    specialized, then this `i' will not be used, so there
15147                    is no harm in resolving the types here.  */
15148                 tree type;
15149
15150                 /* Resolve the TYPENAME_TYPE.  */
15151                 type = resolve_typename_type (qualifying_scope,
15152                                               /*only_current_p=*/false);
15153                 /* If that failed, the declarator is invalid.  */
15154                 if (TREE_CODE (type) == TYPENAME_TYPE)
15155                   {
15156                     if (typedef_variant_p (type))
15157                       error_at (declarator_id_start_token->location,
15158                                 "cannot define member of dependent typedef "
15159                                 "%qT", type);
15160                     else
15161                       error_at (declarator_id_start_token->location,
15162                                 "%<%T::%E%> is not a type",
15163                                 TYPE_CONTEXT (qualifying_scope),
15164                                 TYPE_IDENTIFIER (qualifying_scope));
15165                   }
15166                 qualifying_scope = type;
15167               }
15168
15169             sfk = sfk_none;
15170
15171             if (unqualified_name)
15172               {
15173                 tree class_type;
15174
15175                 if (qualifying_scope
15176                     && CLASS_TYPE_P (qualifying_scope))
15177                   class_type = qualifying_scope;
15178                 else
15179                   class_type = current_class_type;
15180
15181                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15182                   {
15183                     tree name_type = TREE_TYPE (unqualified_name);
15184                     if (class_type && same_type_p (name_type, class_type))
15185                       {
15186                         if (qualifying_scope
15187                             && CLASSTYPE_USE_TEMPLATE (name_type))
15188                           {
15189                             error_at (declarator_id_start_token->location,
15190                                       "invalid use of constructor as a template");
15191                             inform (declarator_id_start_token->location,
15192                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15193                                     "name the constructor in a qualified name",
15194                                     class_type,
15195                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15196                                     class_type, name_type);
15197                             declarator = cp_error_declarator;
15198                             break;
15199                           }
15200                         else
15201                           unqualified_name = constructor_name (class_type);
15202                       }
15203                     else
15204                       {
15205                         /* We do not attempt to print the declarator
15206                            here because we do not have enough
15207                            information about its original syntactic
15208                            form.  */
15209                         cp_parser_error (parser, "invalid declarator");
15210                         declarator = cp_error_declarator;
15211                         break;
15212                       }
15213                   }
15214
15215                 if (class_type)
15216                   {
15217                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15218                       sfk = sfk_destructor;
15219                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15220                       sfk = sfk_conversion;
15221                     else if (/* There's no way to declare a constructor
15222                                 for an anonymous type, even if the type
15223                                 got a name for linkage purposes.  */
15224                              !TYPE_WAS_ANONYMOUS (class_type)
15225                              && constructor_name_p (unqualified_name,
15226                                                     class_type))
15227                       {
15228                         unqualified_name = constructor_name (class_type);
15229                         sfk = sfk_constructor;
15230                       }
15231                     else if (is_overloaded_fn (unqualified_name)
15232                              && DECL_CONSTRUCTOR_P (get_first_fn
15233                                                     (unqualified_name)))
15234                       sfk = sfk_constructor;
15235
15236                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15237                       *ctor_dtor_or_conv_p = -1;
15238                   }
15239               }
15240             declarator = make_id_declarator (qualifying_scope,
15241                                              unqualified_name,
15242                                              sfk);
15243             declarator->id_loc = token->location;
15244             declarator->parameter_pack_p = pack_expansion_p;
15245
15246             if (pack_expansion_p)
15247               maybe_warn_variadic_templates ();
15248           }
15249
15250         handle_declarator:;
15251           scope = get_scope_of_declarator (declarator);
15252           if (scope)
15253             /* Any names that appear after the declarator-id for a
15254                member are looked up in the containing scope.  */
15255             pushed_scope = push_scope (scope);
15256           parser->in_declarator_p = true;
15257           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15258               || (declarator && declarator->kind == cdk_id))
15259             /* Default args are only allowed on function
15260                declarations.  */
15261             parser->default_arg_ok_p = saved_default_arg_ok_p;
15262           else
15263             parser->default_arg_ok_p = false;
15264
15265           first = false;
15266         }
15267       /* We're done.  */
15268       else
15269         break;
15270     }
15271
15272   /* For an abstract declarator, we might wind up with nothing at this
15273      point.  That's an error; the declarator is not optional.  */
15274   if (!declarator)
15275     cp_parser_error (parser, "expected declarator");
15276
15277   /* If we entered a scope, we must exit it now.  */
15278   if (pushed_scope)
15279     pop_scope (pushed_scope);
15280
15281   parser->default_arg_ok_p = saved_default_arg_ok_p;
15282   parser->in_declarator_p = saved_in_declarator_p;
15283
15284   return declarator;
15285 }
15286
15287 /* Parse a ptr-operator.
15288
15289    ptr-operator:
15290      * cv-qualifier-seq [opt]
15291      &
15292      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15293
15294    GNU Extension:
15295
15296    ptr-operator:
15297      & cv-qualifier-seq [opt]
15298
15299    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15300    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15301    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15302    filled in with the TYPE containing the member.  *CV_QUALS is
15303    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15304    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15305    Note that the tree codes returned by this function have nothing
15306    to do with the types of trees that will be eventually be created
15307    to represent the pointer or reference type being parsed. They are
15308    just constants with suggestive names. */
15309 static enum tree_code
15310 cp_parser_ptr_operator (cp_parser* parser,
15311                         tree* type,
15312                         cp_cv_quals *cv_quals)
15313 {
15314   enum tree_code code = ERROR_MARK;
15315   cp_token *token;
15316
15317   /* Assume that it's not a pointer-to-member.  */
15318   *type = NULL_TREE;
15319   /* And that there are no cv-qualifiers.  */
15320   *cv_quals = TYPE_UNQUALIFIED;
15321
15322   /* Peek at the next token.  */
15323   token = cp_lexer_peek_token (parser->lexer);
15324
15325   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15326   if (token->type == CPP_MULT)
15327     code = INDIRECT_REF;
15328   else if (token->type == CPP_AND)
15329     code = ADDR_EXPR;
15330   else if ((cxx_dialect != cxx98) &&
15331            token->type == CPP_AND_AND) /* C++0x only */
15332     code = NON_LVALUE_EXPR;
15333
15334   if (code != ERROR_MARK)
15335     {
15336       /* Consume the `*', `&' or `&&'.  */
15337       cp_lexer_consume_token (parser->lexer);
15338
15339       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15340          `&', if we are allowing GNU extensions.  (The only qualifier
15341          that can legally appear after `&' is `restrict', but that is
15342          enforced during semantic analysis.  */
15343       if (code == INDIRECT_REF
15344           || cp_parser_allow_gnu_extensions_p (parser))
15345         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15346     }
15347   else
15348     {
15349       /* Try the pointer-to-member case.  */
15350       cp_parser_parse_tentatively (parser);
15351       /* Look for the optional `::' operator.  */
15352       cp_parser_global_scope_opt (parser,
15353                                   /*current_scope_valid_p=*/false);
15354       /* Look for the nested-name specifier.  */
15355       token = cp_lexer_peek_token (parser->lexer);
15356       cp_parser_nested_name_specifier (parser,
15357                                        /*typename_keyword_p=*/false,
15358                                        /*check_dependency_p=*/true,
15359                                        /*type_p=*/false,
15360                                        /*is_declaration=*/false);
15361       /* If we found it, and the next token is a `*', then we are
15362          indeed looking at a pointer-to-member operator.  */
15363       if (!cp_parser_error_occurred (parser)
15364           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15365         {
15366           /* Indicate that the `*' operator was used.  */
15367           code = INDIRECT_REF;
15368
15369           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15370             error_at (token->location, "%qD is a namespace", parser->scope);
15371           else
15372             {
15373               /* The type of which the member is a member is given by the
15374                  current SCOPE.  */
15375               *type = parser->scope;
15376               /* The next name will not be qualified.  */
15377               parser->scope = NULL_TREE;
15378               parser->qualifying_scope = NULL_TREE;
15379               parser->object_scope = NULL_TREE;
15380               /* Look for the optional cv-qualifier-seq.  */
15381               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15382             }
15383         }
15384       /* If that didn't work we don't have a ptr-operator.  */
15385       if (!cp_parser_parse_definitely (parser))
15386         cp_parser_error (parser, "expected ptr-operator");
15387     }
15388
15389   return code;
15390 }
15391
15392 /* Parse an (optional) cv-qualifier-seq.
15393
15394    cv-qualifier-seq:
15395      cv-qualifier cv-qualifier-seq [opt]
15396
15397    cv-qualifier:
15398      const
15399      volatile
15400
15401    GNU Extension:
15402
15403    cv-qualifier:
15404      __restrict__
15405
15406    Returns a bitmask representing the cv-qualifiers.  */
15407
15408 static cp_cv_quals
15409 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15410 {
15411   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15412
15413   while (true)
15414     {
15415       cp_token *token;
15416       cp_cv_quals cv_qualifier;
15417
15418       /* Peek at the next token.  */
15419       token = cp_lexer_peek_token (parser->lexer);
15420       /* See if it's a cv-qualifier.  */
15421       switch (token->keyword)
15422         {
15423         case RID_CONST:
15424           cv_qualifier = TYPE_QUAL_CONST;
15425           break;
15426
15427         case RID_VOLATILE:
15428           cv_qualifier = TYPE_QUAL_VOLATILE;
15429           break;
15430
15431         case RID_RESTRICT:
15432           cv_qualifier = TYPE_QUAL_RESTRICT;
15433           break;
15434
15435         default:
15436           cv_qualifier = TYPE_UNQUALIFIED;
15437           break;
15438         }
15439
15440       if (!cv_qualifier)
15441         break;
15442
15443       if (cv_quals & cv_qualifier)
15444         {
15445           error_at (token->location, "duplicate cv-qualifier");
15446           cp_lexer_purge_token (parser->lexer);
15447         }
15448       else
15449         {
15450           cp_lexer_consume_token (parser->lexer);
15451           cv_quals |= cv_qualifier;
15452         }
15453     }
15454
15455   return cv_quals;
15456 }
15457
15458 /* Parse a late-specified return type, if any.  This is not a separate
15459    non-terminal, but part of a function declarator, which looks like
15460
15461    -> trailing-type-specifier-seq abstract-declarator(opt)
15462
15463    Returns the type indicated by the type-id.  */
15464
15465 static tree
15466 cp_parser_late_return_type_opt (cp_parser* parser)
15467 {
15468   cp_token *token;
15469
15470   /* Peek at the next token.  */
15471   token = cp_lexer_peek_token (parser->lexer);
15472   /* A late-specified return type is indicated by an initial '->'. */
15473   if (token->type != CPP_DEREF)
15474     return NULL_TREE;
15475
15476   /* Consume the ->.  */
15477   cp_lexer_consume_token (parser->lexer);
15478
15479   return cp_parser_trailing_type_id (parser);
15480 }
15481
15482 /* Parse a declarator-id.
15483
15484    declarator-id:
15485      id-expression
15486      :: [opt] nested-name-specifier [opt] type-name
15487
15488    In the `id-expression' case, the value returned is as for
15489    cp_parser_id_expression if the id-expression was an unqualified-id.
15490    If the id-expression was a qualified-id, then a SCOPE_REF is
15491    returned.  The first operand is the scope (either a NAMESPACE_DECL
15492    or TREE_TYPE), but the second is still just a representation of an
15493    unqualified-id.  */
15494
15495 static tree
15496 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15497 {
15498   tree id;
15499   /* The expression must be an id-expression.  Assume that qualified
15500      names are the names of types so that:
15501
15502        template <class T>
15503        int S<T>::R::i = 3;
15504
15505      will work; we must treat `S<T>::R' as the name of a type.
15506      Similarly, assume that qualified names are templates, where
15507      required, so that:
15508
15509        template <class T>
15510        int S<T>::R<T>::i = 3;
15511
15512      will work, too.  */
15513   id = cp_parser_id_expression (parser,
15514                                 /*template_keyword_p=*/false,
15515                                 /*check_dependency_p=*/false,
15516                                 /*template_p=*/NULL,
15517                                 /*declarator_p=*/true,
15518                                 optional_p);
15519   if (id && BASELINK_P (id))
15520     id = BASELINK_FUNCTIONS (id);
15521   return id;
15522 }
15523
15524 /* Parse a type-id.
15525
15526    type-id:
15527      type-specifier-seq abstract-declarator [opt]
15528
15529    Returns the TYPE specified.  */
15530
15531 static tree
15532 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15533                      bool is_trailing_return)
15534 {
15535   cp_decl_specifier_seq type_specifier_seq;
15536   cp_declarator *abstract_declarator;
15537
15538   /* Parse the type-specifier-seq.  */
15539   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15540                                 is_trailing_return,
15541                                 &type_specifier_seq);
15542   if (type_specifier_seq.type == error_mark_node)
15543     return error_mark_node;
15544
15545   /* There might or might not be an abstract declarator.  */
15546   cp_parser_parse_tentatively (parser);
15547   /* Look for the declarator.  */
15548   abstract_declarator
15549     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15550                             /*parenthesized_p=*/NULL,
15551                             /*member_p=*/false);
15552   /* Check to see if there really was a declarator.  */
15553   if (!cp_parser_parse_definitely (parser))
15554     abstract_declarator = NULL;
15555
15556   if (type_specifier_seq.type
15557       && type_uses_auto (type_specifier_seq.type))
15558     {
15559       /* A type-id with type 'auto' is only ok if the abstract declarator
15560          is a function declarator with a late-specified return type.  */
15561       if (abstract_declarator
15562           && abstract_declarator->kind == cdk_function
15563           && abstract_declarator->u.function.late_return_type)
15564         /* OK */;
15565       else
15566         {
15567           error ("invalid use of %<auto%>");
15568           return error_mark_node;
15569         }
15570     }
15571   
15572   return groktypename (&type_specifier_seq, abstract_declarator,
15573                        is_template_arg);
15574 }
15575
15576 static tree cp_parser_type_id (cp_parser *parser)
15577 {
15578   return cp_parser_type_id_1 (parser, false, false);
15579 }
15580
15581 static tree cp_parser_template_type_arg (cp_parser *parser)
15582 {
15583   return cp_parser_type_id_1 (parser, true, false);
15584 }
15585
15586 static tree cp_parser_trailing_type_id (cp_parser *parser)
15587 {
15588   return cp_parser_type_id_1 (parser, false, true);
15589 }
15590
15591 /* Parse a type-specifier-seq.
15592
15593    type-specifier-seq:
15594      type-specifier type-specifier-seq [opt]
15595
15596    GNU extension:
15597
15598    type-specifier-seq:
15599      attributes type-specifier-seq [opt]
15600
15601    If IS_DECLARATION is true, we are at the start of a "condition" or
15602    exception-declaration, so we might be followed by a declarator-id.
15603
15604    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15605    i.e. we've just seen "->".
15606
15607    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15608
15609 static void
15610 cp_parser_type_specifier_seq (cp_parser* parser,
15611                               bool is_declaration,
15612                               bool is_trailing_return,
15613                               cp_decl_specifier_seq *type_specifier_seq)
15614 {
15615   bool seen_type_specifier = false;
15616   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15617   cp_token *start_token = NULL;
15618
15619   /* Clear the TYPE_SPECIFIER_SEQ.  */
15620   clear_decl_specs (type_specifier_seq);
15621
15622   /* In the context of a trailing return type, enum E { } is an
15623      elaborated-type-specifier followed by a function-body, not an
15624      enum-specifier.  */
15625   if (is_trailing_return)
15626     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15627
15628   /* Parse the type-specifiers and attributes.  */
15629   while (true)
15630     {
15631       tree type_specifier;
15632       bool is_cv_qualifier;
15633
15634       /* Check for attributes first.  */
15635       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15636         {
15637           type_specifier_seq->attributes =
15638             chainon (type_specifier_seq->attributes,
15639                      cp_parser_attributes_opt (parser));
15640           continue;
15641         }
15642
15643       /* record the token of the beginning of the type specifier seq,
15644          for error reporting purposes*/
15645      if (!start_token)
15646        start_token = cp_lexer_peek_token (parser->lexer);
15647
15648       /* Look for the type-specifier.  */
15649       type_specifier = cp_parser_type_specifier (parser,
15650                                                  flags,
15651                                                  type_specifier_seq,
15652                                                  /*is_declaration=*/false,
15653                                                  NULL,
15654                                                  &is_cv_qualifier);
15655       if (!type_specifier)
15656         {
15657           /* If the first type-specifier could not be found, this is not a
15658              type-specifier-seq at all.  */
15659           if (!seen_type_specifier)
15660             {
15661               cp_parser_error (parser, "expected type-specifier");
15662               type_specifier_seq->type = error_mark_node;
15663               return;
15664             }
15665           /* If subsequent type-specifiers could not be found, the
15666              type-specifier-seq is complete.  */
15667           break;
15668         }
15669
15670       seen_type_specifier = true;
15671       /* The standard says that a condition can be:
15672
15673             type-specifier-seq declarator = assignment-expression
15674
15675          However, given:
15676
15677            struct S {};
15678            if (int S = ...)
15679
15680          we should treat the "S" as a declarator, not as a
15681          type-specifier.  The standard doesn't say that explicitly for
15682          type-specifier-seq, but it does say that for
15683          decl-specifier-seq in an ordinary declaration.  Perhaps it
15684          would be clearer just to allow a decl-specifier-seq here, and
15685          then add a semantic restriction that if any decl-specifiers
15686          that are not type-specifiers appear, the program is invalid.  */
15687       if (is_declaration && !is_cv_qualifier)
15688         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15689     }
15690
15691   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15692 }
15693
15694 /* Parse a parameter-declaration-clause.
15695
15696    parameter-declaration-clause:
15697      parameter-declaration-list [opt] ... [opt]
15698      parameter-declaration-list , ...
15699
15700    Returns a representation for the parameter declarations.  A return
15701    value of NULL indicates a parameter-declaration-clause consisting
15702    only of an ellipsis.  */
15703
15704 static tree
15705 cp_parser_parameter_declaration_clause (cp_parser* parser)
15706 {
15707   tree parameters;
15708   cp_token *token;
15709   bool ellipsis_p;
15710   bool is_error;
15711
15712   /* Peek at the next token.  */
15713   token = cp_lexer_peek_token (parser->lexer);
15714   /* Check for trivial parameter-declaration-clauses.  */
15715   if (token->type == CPP_ELLIPSIS)
15716     {
15717       /* Consume the `...' token.  */
15718       cp_lexer_consume_token (parser->lexer);
15719       return NULL_TREE;
15720     }
15721   else if (token->type == CPP_CLOSE_PAREN)
15722     /* There are no parameters.  */
15723     {
15724 #ifndef NO_IMPLICIT_EXTERN_C
15725       if (in_system_header && current_class_type == NULL
15726           && current_lang_name == lang_name_c)
15727         return NULL_TREE;
15728       else
15729 #endif
15730         return void_list_node;
15731     }
15732   /* Check for `(void)', too, which is a special case.  */
15733   else if (token->keyword == RID_VOID
15734            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15735                == CPP_CLOSE_PAREN))
15736     {
15737       /* Consume the `void' token.  */
15738       cp_lexer_consume_token (parser->lexer);
15739       /* There are no parameters.  */
15740       return void_list_node;
15741     }
15742
15743   /* Parse the parameter-declaration-list.  */
15744   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15745   /* If a parse error occurred while parsing the
15746      parameter-declaration-list, then the entire
15747      parameter-declaration-clause is erroneous.  */
15748   if (is_error)
15749     return NULL;
15750
15751   /* Peek at the next token.  */
15752   token = cp_lexer_peek_token (parser->lexer);
15753   /* If it's a `,', the clause should terminate with an ellipsis.  */
15754   if (token->type == CPP_COMMA)
15755     {
15756       /* Consume the `,'.  */
15757       cp_lexer_consume_token (parser->lexer);
15758       /* Expect an ellipsis.  */
15759       ellipsis_p
15760         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15761     }
15762   /* It might also be `...' if the optional trailing `,' was
15763      omitted.  */
15764   else if (token->type == CPP_ELLIPSIS)
15765     {
15766       /* Consume the `...' token.  */
15767       cp_lexer_consume_token (parser->lexer);
15768       /* And remember that we saw it.  */
15769       ellipsis_p = true;
15770     }
15771   else
15772     ellipsis_p = false;
15773
15774   /* Finish the parameter list.  */
15775   if (!ellipsis_p)
15776     parameters = chainon (parameters, void_list_node);
15777
15778   return parameters;
15779 }
15780
15781 /* Parse a parameter-declaration-list.
15782
15783    parameter-declaration-list:
15784      parameter-declaration
15785      parameter-declaration-list , parameter-declaration
15786
15787    Returns a representation of the parameter-declaration-list, as for
15788    cp_parser_parameter_declaration_clause.  However, the
15789    `void_list_node' is never appended to the list.  Upon return,
15790    *IS_ERROR will be true iff an error occurred.  */
15791
15792 static tree
15793 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15794 {
15795   tree parameters = NULL_TREE;
15796   tree *tail = &parameters; 
15797   bool saved_in_unbraced_linkage_specification_p;
15798   int index = 0;
15799
15800   /* Assume all will go well.  */
15801   *is_error = false;
15802   /* The special considerations that apply to a function within an
15803      unbraced linkage specifications do not apply to the parameters
15804      to the function.  */
15805   saved_in_unbraced_linkage_specification_p 
15806     = parser->in_unbraced_linkage_specification_p;
15807   parser->in_unbraced_linkage_specification_p = false;
15808
15809   /* Look for more parameters.  */
15810   while (true)
15811     {
15812       cp_parameter_declarator *parameter;
15813       tree decl = error_mark_node;
15814       bool parenthesized_p;
15815       /* Parse the parameter.  */
15816       parameter
15817         = cp_parser_parameter_declaration (parser,
15818                                            /*template_parm_p=*/false,
15819                                            &parenthesized_p);
15820
15821       /* We don't know yet if the enclosing context is deprecated, so wait
15822          and warn in grokparms if appropriate.  */
15823       deprecated_state = DEPRECATED_SUPPRESS;
15824
15825       if (parameter)
15826         decl = grokdeclarator (parameter->declarator,
15827                                &parameter->decl_specifiers,
15828                                PARM,
15829                                parameter->default_argument != NULL_TREE,
15830                                &parameter->decl_specifiers.attributes);
15831
15832       deprecated_state = DEPRECATED_NORMAL;
15833
15834       /* If a parse error occurred parsing the parameter declaration,
15835          then the entire parameter-declaration-list is erroneous.  */
15836       if (decl == error_mark_node)
15837         {
15838           *is_error = true;
15839           parameters = error_mark_node;
15840           break;
15841         }
15842
15843       if (parameter->decl_specifiers.attributes)
15844         cplus_decl_attributes (&decl,
15845                                parameter->decl_specifiers.attributes,
15846                                0);
15847       if (DECL_NAME (decl))
15848         decl = pushdecl (decl);
15849
15850       if (decl != error_mark_node)
15851         {
15852           retrofit_lang_decl (decl);
15853           DECL_PARM_INDEX (decl) = ++index;
15854         }
15855
15856       /* Add the new parameter to the list.  */
15857       *tail = build_tree_list (parameter->default_argument, decl);
15858       tail = &TREE_CHAIN (*tail);
15859
15860       /* Peek at the next token.  */
15861       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15862           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15863           /* These are for Objective-C++ */
15864           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15865           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15866         /* The parameter-declaration-list is complete.  */
15867         break;
15868       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15869         {
15870           cp_token *token;
15871
15872           /* Peek at the next token.  */
15873           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15874           /* If it's an ellipsis, then the list is complete.  */
15875           if (token->type == CPP_ELLIPSIS)
15876             break;
15877           /* Otherwise, there must be more parameters.  Consume the
15878              `,'.  */
15879           cp_lexer_consume_token (parser->lexer);
15880           /* When parsing something like:
15881
15882                 int i(float f, double d)
15883
15884              we can tell after seeing the declaration for "f" that we
15885              are not looking at an initialization of a variable "i",
15886              but rather at the declaration of a function "i".
15887
15888              Due to the fact that the parsing of template arguments
15889              (as specified to a template-id) requires backtracking we
15890              cannot use this technique when inside a template argument
15891              list.  */
15892           if (!parser->in_template_argument_list_p
15893               && !parser->in_type_id_in_expr_p
15894               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15895               /* However, a parameter-declaration of the form
15896                  "foat(f)" (which is a valid declaration of a
15897                  parameter "f") can also be interpreted as an
15898                  expression (the conversion of "f" to "float").  */
15899               && !parenthesized_p)
15900             cp_parser_commit_to_tentative_parse (parser);
15901         }
15902       else
15903         {
15904           cp_parser_error (parser, "expected %<,%> or %<...%>");
15905           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15906             cp_parser_skip_to_closing_parenthesis (parser,
15907                                                    /*recovering=*/true,
15908                                                    /*or_comma=*/false,
15909                                                    /*consume_paren=*/false);
15910           break;
15911         }
15912     }
15913
15914   parser->in_unbraced_linkage_specification_p
15915     = saved_in_unbraced_linkage_specification_p;
15916
15917   return parameters;
15918 }
15919
15920 /* Parse a parameter declaration.
15921
15922    parameter-declaration:
15923      decl-specifier-seq ... [opt] declarator
15924      decl-specifier-seq declarator = assignment-expression
15925      decl-specifier-seq ... [opt] abstract-declarator [opt]
15926      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15927
15928    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15929    declares a template parameter.  (In that case, a non-nested `>'
15930    token encountered during the parsing of the assignment-expression
15931    is not interpreted as a greater-than operator.)
15932
15933    Returns a representation of the parameter, or NULL if an error
15934    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15935    true iff the declarator is of the form "(p)".  */
15936
15937 static cp_parameter_declarator *
15938 cp_parser_parameter_declaration (cp_parser *parser,
15939                                  bool template_parm_p,
15940                                  bool *parenthesized_p)
15941 {
15942   int declares_class_or_enum;
15943   cp_decl_specifier_seq decl_specifiers;
15944   cp_declarator *declarator;
15945   tree default_argument;
15946   cp_token *token = NULL, *declarator_token_start = NULL;
15947   const char *saved_message;
15948
15949   /* In a template parameter, `>' is not an operator.
15950
15951      [temp.param]
15952
15953      When parsing a default template-argument for a non-type
15954      template-parameter, the first non-nested `>' is taken as the end
15955      of the template parameter-list rather than a greater-than
15956      operator.  */
15957
15958   /* Type definitions may not appear in parameter types.  */
15959   saved_message = parser->type_definition_forbidden_message;
15960   parser->type_definition_forbidden_message
15961     = G_("types may not be defined in parameter types");
15962
15963   /* Parse the declaration-specifiers.  */
15964   cp_parser_decl_specifier_seq (parser,
15965                                 CP_PARSER_FLAGS_NONE,
15966                                 &decl_specifiers,
15967                                 &declares_class_or_enum);
15968
15969   /* Complain about missing 'typename' or other invalid type names.  */
15970   if (!decl_specifiers.any_type_specifiers_p)
15971     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15972
15973   /* If an error occurred, there's no reason to attempt to parse the
15974      rest of the declaration.  */
15975   if (cp_parser_error_occurred (parser))
15976     {
15977       parser->type_definition_forbidden_message = saved_message;
15978       return NULL;
15979     }
15980
15981   /* Peek at the next token.  */
15982   token = cp_lexer_peek_token (parser->lexer);
15983
15984   /* If the next token is a `)', `,', `=', `>', or `...', then there
15985      is no declarator. However, when variadic templates are enabled,
15986      there may be a declarator following `...'.  */
15987   if (token->type == CPP_CLOSE_PAREN
15988       || token->type == CPP_COMMA
15989       || token->type == CPP_EQ
15990       || token->type == CPP_GREATER)
15991     {
15992       declarator = NULL;
15993       if (parenthesized_p)
15994         *parenthesized_p = false;
15995     }
15996   /* Otherwise, there should be a declarator.  */
15997   else
15998     {
15999       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16000       parser->default_arg_ok_p = false;
16001
16002       /* After seeing a decl-specifier-seq, if the next token is not a
16003          "(", there is no possibility that the code is a valid
16004          expression.  Therefore, if parsing tentatively, we commit at
16005          this point.  */
16006       if (!parser->in_template_argument_list_p
16007           /* In an expression context, having seen:
16008
16009                (int((char ...
16010
16011              we cannot be sure whether we are looking at a
16012              function-type (taking a "char" as a parameter) or a cast
16013              of some object of type "char" to "int".  */
16014           && !parser->in_type_id_in_expr_p
16015           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16016           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16017         cp_parser_commit_to_tentative_parse (parser);
16018       /* Parse the declarator.  */
16019       declarator_token_start = token;
16020       declarator = cp_parser_declarator (parser,
16021                                          CP_PARSER_DECLARATOR_EITHER,
16022                                          /*ctor_dtor_or_conv_p=*/NULL,
16023                                          parenthesized_p,
16024                                          /*member_p=*/false);
16025       parser->default_arg_ok_p = saved_default_arg_ok_p;
16026       /* After the declarator, allow more attributes.  */
16027       decl_specifiers.attributes
16028         = chainon (decl_specifiers.attributes,
16029                    cp_parser_attributes_opt (parser));
16030     }
16031
16032   /* If the next token is an ellipsis, and we have not seen a
16033      declarator name, and the type of the declarator contains parameter
16034      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16035      a parameter pack expansion expression. Otherwise, leave the
16036      ellipsis for a C-style variadic function. */
16037   token = cp_lexer_peek_token (parser->lexer);
16038   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16039     {
16040       tree type = decl_specifiers.type;
16041
16042       if (type && DECL_P (type))
16043         type = TREE_TYPE (type);
16044
16045       if (type
16046           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16047           && declarator_can_be_parameter_pack (declarator)
16048           && (!declarator || !declarator->parameter_pack_p)
16049           && uses_parameter_packs (type))
16050         {
16051           /* Consume the `...'. */
16052           cp_lexer_consume_token (parser->lexer);
16053           maybe_warn_variadic_templates ();
16054           
16055           /* Build a pack expansion type */
16056           if (declarator)
16057             declarator->parameter_pack_p = true;
16058           else
16059             decl_specifiers.type = make_pack_expansion (type);
16060         }
16061     }
16062
16063   /* The restriction on defining new types applies only to the type
16064      of the parameter, not to the default argument.  */
16065   parser->type_definition_forbidden_message = saved_message;
16066
16067   /* If the next token is `=', then process a default argument.  */
16068   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16069     {
16070       /* Consume the `='.  */
16071       cp_lexer_consume_token (parser->lexer);
16072
16073       /* If we are defining a class, then the tokens that make up the
16074          default argument must be saved and processed later.  */
16075       if (!template_parm_p && at_class_scope_p ()
16076           && TYPE_BEING_DEFINED (current_class_type)
16077           && !LAMBDA_TYPE_P (current_class_type))
16078         {
16079           unsigned depth = 0;
16080           int maybe_template_id = 0;
16081           cp_token *first_token;
16082           cp_token *token;
16083
16084           /* Add tokens until we have processed the entire default
16085              argument.  We add the range [first_token, token).  */
16086           first_token = cp_lexer_peek_token (parser->lexer);
16087           while (true)
16088             {
16089               bool done = false;
16090
16091               /* Peek at the next token.  */
16092               token = cp_lexer_peek_token (parser->lexer);
16093               /* What we do depends on what token we have.  */
16094               switch (token->type)
16095                 {
16096                   /* In valid code, a default argument must be
16097                      immediately followed by a `,' `)', or `...'.  */
16098                 case CPP_COMMA:
16099                   if (depth == 0 && maybe_template_id)
16100                     {
16101                       /* If we've seen a '<', we might be in a
16102                          template-argument-list.  Until Core issue 325 is
16103                          resolved, we don't know how this situation ought
16104                          to be handled, so try to DTRT.  We check whether
16105                          what comes after the comma is a valid parameter
16106                          declaration list.  If it is, then the comma ends
16107                          the default argument; otherwise the default
16108                          argument continues.  */
16109                       bool error = false;
16110                       tree t;
16111
16112                       /* Set ITALP so cp_parser_parameter_declaration_list
16113                          doesn't decide to commit to this parse.  */
16114                       bool saved_italp = parser->in_template_argument_list_p;
16115                       parser->in_template_argument_list_p = true;
16116
16117                       cp_parser_parse_tentatively (parser);
16118                       cp_lexer_consume_token (parser->lexer);
16119                       begin_scope (sk_function_parms, NULL_TREE);
16120                       cp_parser_parameter_declaration_list (parser, &error);
16121                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16122                         pop_binding (DECL_NAME (t), t);
16123                       leave_scope ();
16124                       if (!cp_parser_error_occurred (parser) && !error)
16125                         done = true;
16126                       cp_parser_abort_tentative_parse (parser);
16127
16128                       parser->in_template_argument_list_p = saved_italp;
16129                       break;
16130                     }
16131                 case CPP_CLOSE_PAREN:
16132                 case CPP_ELLIPSIS:
16133                   /* If we run into a non-nested `;', `}', or `]',
16134                      then the code is invalid -- but the default
16135                      argument is certainly over.  */
16136                 case CPP_SEMICOLON:
16137                 case CPP_CLOSE_BRACE:
16138                 case CPP_CLOSE_SQUARE:
16139                   if (depth == 0)
16140                     done = true;
16141                   /* Update DEPTH, if necessary.  */
16142                   else if (token->type == CPP_CLOSE_PAREN
16143                            || token->type == CPP_CLOSE_BRACE
16144                            || token->type == CPP_CLOSE_SQUARE)
16145                     --depth;
16146                   break;
16147
16148                 case CPP_OPEN_PAREN:
16149                 case CPP_OPEN_SQUARE:
16150                 case CPP_OPEN_BRACE:
16151                   ++depth;
16152                   break;
16153
16154                 case CPP_LESS:
16155                   if (depth == 0)
16156                     /* This might be the comparison operator, or it might
16157                        start a template argument list.  */
16158                     ++maybe_template_id;
16159                   break;
16160
16161                 case CPP_RSHIFT:
16162                   if (cxx_dialect == cxx98)
16163                     break;
16164                   /* Fall through for C++0x, which treats the `>>'
16165                      operator like two `>' tokens in certain
16166                      cases.  */
16167
16168                 case CPP_GREATER:
16169                   if (depth == 0)
16170                     {
16171                       /* This might be an operator, or it might close a
16172                          template argument list.  But if a previous '<'
16173                          started a template argument list, this will have
16174                          closed it, so we can't be in one anymore.  */
16175                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16176                       if (maybe_template_id < 0)
16177                         maybe_template_id = 0;
16178                     }
16179                   break;
16180
16181                   /* If we run out of tokens, issue an error message.  */
16182                 case CPP_EOF:
16183                 case CPP_PRAGMA_EOL:
16184                   error_at (token->location, "file ends in default argument");
16185                   done = true;
16186                   break;
16187
16188                 case CPP_NAME:
16189                 case CPP_SCOPE:
16190                   /* In these cases, we should look for template-ids.
16191                      For example, if the default argument is
16192                      `X<int, double>()', we need to do name lookup to
16193                      figure out whether or not `X' is a template; if
16194                      so, the `,' does not end the default argument.
16195
16196                      That is not yet done.  */
16197                   break;
16198
16199                 default:
16200                   break;
16201                 }
16202
16203               /* If we've reached the end, stop.  */
16204               if (done)
16205                 break;
16206
16207               /* Add the token to the token block.  */
16208               token = cp_lexer_consume_token (parser->lexer);
16209             }
16210
16211           /* Create a DEFAULT_ARG to represent the unparsed default
16212              argument.  */
16213           default_argument = make_node (DEFAULT_ARG);
16214           DEFARG_TOKENS (default_argument)
16215             = cp_token_cache_new (first_token, token);
16216           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16217         }
16218       /* Outside of a class definition, we can just parse the
16219          assignment-expression.  */
16220       else
16221         {
16222           token = cp_lexer_peek_token (parser->lexer);
16223           default_argument 
16224             = cp_parser_default_argument (parser, template_parm_p);
16225         }
16226
16227       if (!parser->default_arg_ok_p)
16228         {
16229           if (flag_permissive)
16230             warning (0, "deprecated use of default argument for parameter of non-function");
16231           else
16232             {
16233               error_at (token->location,
16234                         "default arguments are only "
16235                         "permitted for function parameters");
16236               default_argument = NULL_TREE;
16237             }
16238         }
16239       else if ((declarator && declarator->parameter_pack_p)
16240                || (decl_specifiers.type
16241                    && PACK_EXPANSION_P (decl_specifiers.type)))
16242         {
16243           /* Find the name of the parameter pack.  */     
16244           cp_declarator *id_declarator = declarator;
16245           while (id_declarator && id_declarator->kind != cdk_id)
16246             id_declarator = id_declarator->declarator;
16247           
16248           if (id_declarator && id_declarator->kind == cdk_id)
16249             error_at (declarator_token_start->location,
16250                       template_parm_p 
16251                       ? "template parameter pack %qD"
16252                       " cannot have a default argument"
16253                       : "parameter pack %qD cannot have a default argument",
16254                       id_declarator->u.id.unqualified_name);
16255           else
16256             error_at (declarator_token_start->location,
16257                       template_parm_p 
16258                       ? "template parameter pack cannot have a default argument"
16259                       : "parameter pack cannot have a default argument");
16260           
16261           default_argument = NULL_TREE;
16262         }
16263     }
16264   else
16265     default_argument = NULL_TREE;
16266
16267   return make_parameter_declarator (&decl_specifiers,
16268                                     declarator,
16269                                     default_argument);
16270 }
16271
16272 /* Parse a default argument and return it.
16273
16274    TEMPLATE_PARM_P is true if this is a default argument for a
16275    non-type template parameter.  */
16276 static tree
16277 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16278 {
16279   tree default_argument = NULL_TREE;
16280   bool saved_greater_than_is_operator_p;
16281   bool saved_local_variables_forbidden_p;
16282
16283   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16284      set correctly.  */
16285   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16286   parser->greater_than_is_operator_p = !template_parm_p;
16287   /* Local variable names (and the `this' keyword) may not
16288      appear in a default argument.  */
16289   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16290   parser->local_variables_forbidden_p = true;
16291   /* Parse the assignment-expression.  */
16292   if (template_parm_p)
16293     push_deferring_access_checks (dk_no_deferred);
16294   default_argument
16295     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16296   if (template_parm_p)
16297     pop_deferring_access_checks ();
16298   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16299   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16300
16301   return default_argument;
16302 }
16303
16304 /* Parse a function-body.
16305
16306    function-body:
16307      compound_statement  */
16308
16309 static void
16310 cp_parser_function_body (cp_parser *parser)
16311 {
16312   cp_parser_compound_statement (parser, NULL, false);
16313 }
16314
16315 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16316    true if a ctor-initializer was present.  */
16317
16318 static bool
16319 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16320 {
16321   tree body, list;
16322   bool ctor_initializer_p;
16323   const bool check_body_p =
16324      DECL_CONSTRUCTOR_P (current_function_decl)
16325      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16326   tree last = NULL;
16327
16328   /* Begin the function body.  */
16329   body = begin_function_body ();
16330   /* Parse the optional ctor-initializer.  */
16331   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16332
16333   /* If we're parsing a constexpr constructor definition, we need
16334      to check that the constructor body is indeed empty.  However,
16335      before we get to cp_parser_function_body lot of junk has been
16336      generated, so we can't just check that we have an empty block.
16337      Rather we take a snapshot of the outermost block, and check whether
16338      cp_parser_function_body changed its state.  */
16339   if (check_body_p)
16340     {
16341       list = body;
16342       if (TREE_CODE (list) == BIND_EXPR)
16343         list = BIND_EXPR_BODY (list);
16344       if (TREE_CODE (list) == STATEMENT_LIST
16345           && STATEMENT_LIST_TAIL (list) != NULL)
16346         last = STATEMENT_LIST_TAIL (list)->stmt;
16347     }
16348   /* Parse the function-body.  */
16349   cp_parser_function_body (parser);
16350   if (check_body_p
16351       && (TREE_CODE (list) != STATEMENT_LIST
16352           || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL)
16353           || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt)))
16354     {
16355       error ("constexpr constructor does not have empty body");
16356       DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
16357     }
16358   /* Finish the function body.  */
16359   finish_function_body (body);
16360
16361   return ctor_initializer_p;
16362 }
16363
16364 /* Parse an initializer.
16365
16366    initializer:
16367      = initializer-clause
16368      ( expression-list )
16369
16370    Returns an expression representing the initializer.  If no
16371    initializer is present, NULL_TREE is returned.
16372
16373    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16374    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16375    set to TRUE if there is no initializer present.  If there is an
16376    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16377    is set to true; otherwise it is set to false.  */
16378
16379 static tree
16380 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16381                        bool* non_constant_p)
16382 {
16383   cp_token *token;
16384   tree init;
16385
16386   /* Peek at the next token.  */
16387   token = cp_lexer_peek_token (parser->lexer);
16388
16389   /* Let our caller know whether or not this initializer was
16390      parenthesized.  */
16391   *is_direct_init = (token->type != CPP_EQ);
16392   /* Assume that the initializer is constant.  */
16393   *non_constant_p = false;
16394
16395   if (token->type == CPP_EQ)
16396     {
16397       /* Consume the `='.  */
16398       cp_lexer_consume_token (parser->lexer);
16399       /* Parse the initializer-clause.  */
16400       init = cp_parser_initializer_clause (parser, non_constant_p);
16401     }
16402   else if (token->type == CPP_OPEN_PAREN)
16403     {
16404       VEC(tree,gc) *vec;
16405       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16406                                                      /*cast_p=*/false,
16407                                                      /*allow_expansion_p=*/true,
16408                                                      non_constant_p);
16409       if (vec == NULL)
16410         return error_mark_node;
16411       init = build_tree_list_vec (vec);
16412       release_tree_vector (vec);
16413     }
16414   else if (token->type == CPP_OPEN_BRACE)
16415     {
16416       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16417       init = cp_parser_braced_list (parser, non_constant_p);
16418       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16419     }
16420   else
16421     {
16422       /* Anything else is an error.  */
16423       cp_parser_error (parser, "expected initializer");
16424       init = error_mark_node;
16425     }
16426
16427   return init;
16428 }
16429
16430 /* Parse an initializer-clause.
16431
16432    initializer-clause:
16433      assignment-expression
16434      braced-init-list
16435
16436    Returns an expression representing the initializer.
16437
16438    If the `assignment-expression' production is used the value
16439    returned is simply a representation for the expression.
16440
16441    Otherwise, calls cp_parser_braced_list.  */
16442
16443 static tree
16444 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16445 {
16446   tree initializer;
16447
16448   /* Assume the expression is constant.  */
16449   *non_constant_p = false;
16450
16451   /* If it is not a `{', then we are looking at an
16452      assignment-expression.  */
16453   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16454     {
16455       initializer
16456         = cp_parser_constant_expression (parser,
16457                                         /*allow_non_constant_p=*/true,
16458                                         non_constant_p);
16459       if (!*non_constant_p)
16460         {
16461           /* We only want to fold if this is really a constant
16462              expression.  FIXME Actually, we don't want to fold here, but in
16463              cp_finish_decl.  */
16464           tree folded = fold_non_dependent_expr (initializer);
16465           folded = maybe_constant_value (folded);
16466           if (TREE_CONSTANT (folded))
16467             initializer = folded;
16468         }
16469     }
16470   else
16471     initializer = cp_parser_braced_list (parser, non_constant_p);
16472
16473   return initializer;
16474 }
16475
16476 /* Parse a brace-enclosed initializer list.
16477
16478    braced-init-list:
16479      { initializer-list , [opt] }
16480      { }
16481
16482    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16483    the elements of the initializer-list (or NULL, if the last
16484    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16485    NULL_TREE.  There is no way to detect whether or not the optional
16486    trailing `,' was provided.  NON_CONSTANT_P is as for
16487    cp_parser_initializer.  */     
16488
16489 static tree
16490 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16491 {
16492   tree initializer;
16493
16494   /* Consume the `{' token.  */
16495   cp_lexer_consume_token (parser->lexer);
16496   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16497   initializer = make_node (CONSTRUCTOR);
16498   /* If it's not a `}', then there is a non-trivial initializer.  */
16499   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16500     {
16501       /* Parse the initializer list.  */
16502       CONSTRUCTOR_ELTS (initializer)
16503         = cp_parser_initializer_list (parser, non_constant_p);
16504       /* A trailing `,' token is allowed.  */
16505       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16506         cp_lexer_consume_token (parser->lexer);
16507     }
16508   /* Now, there should be a trailing `}'.  */
16509   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16510   TREE_TYPE (initializer) = init_list_type_node;
16511   return initializer;
16512 }
16513
16514 /* Parse an initializer-list.
16515
16516    initializer-list:
16517      initializer-clause ... [opt]
16518      initializer-list , initializer-clause ... [opt]
16519
16520    GNU Extension:
16521
16522    initializer-list:
16523      identifier : initializer-clause
16524      initializer-list, identifier : initializer-clause
16525
16526    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16527    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16528    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16529    as for cp_parser_initializer.  */
16530
16531 static VEC(constructor_elt,gc) *
16532 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16533 {
16534   VEC(constructor_elt,gc) *v = NULL;
16535
16536   /* Assume all of the expressions are constant.  */
16537   *non_constant_p = false;
16538
16539   /* Parse the rest of the list.  */
16540   while (true)
16541     {
16542       cp_token *token;
16543       tree identifier;
16544       tree initializer;
16545       bool clause_non_constant_p;
16546
16547       /* If the next token is an identifier and the following one is a
16548          colon, we are looking at the GNU designated-initializer
16549          syntax.  */
16550       if (cp_parser_allow_gnu_extensions_p (parser)
16551           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16552           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16553         {
16554           /* Warn the user that they are using an extension.  */
16555           pedwarn (input_location, OPT_pedantic, 
16556                    "ISO C++ does not allow designated initializers");
16557           /* Consume the identifier.  */
16558           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16559           /* Consume the `:'.  */
16560           cp_lexer_consume_token (parser->lexer);
16561         }
16562       else
16563         identifier = NULL_TREE;
16564
16565       /* Parse the initializer.  */
16566       initializer = cp_parser_initializer_clause (parser,
16567                                                   &clause_non_constant_p);
16568       /* If any clause is non-constant, so is the entire initializer.  */
16569       if (clause_non_constant_p)
16570         *non_constant_p = true;
16571
16572       /* If we have an ellipsis, this is an initializer pack
16573          expansion.  */
16574       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16575         {
16576           /* Consume the `...'.  */
16577           cp_lexer_consume_token (parser->lexer);
16578
16579           /* Turn the initializer into an initializer expansion.  */
16580           initializer = make_pack_expansion (initializer);
16581         }
16582
16583       /* Add it to the vector.  */
16584       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16585
16586       /* If the next token is not a comma, we have reached the end of
16587          the list.  */
16588       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16589         break;
16590
16591       /* Peek at the next token.  */
16592       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16593       /* If the next token is a `}', then we're still done.  An
16594          initializer-clause can have a trailing `,' after the
16595          initializer-list and before the closing `}'.  */
16596       if (token->type == CPP_CLOSE_BRACE)
16597         break;
16598
16599       /* Consume the `,' token.  */
16600       cp_lexer_consume_token (parser->lexer);
16601     }
16602
16603   return v;
16604 }
16605
16606 /* Classes [gram.class] */
16607
16608 /* Parse a class-name.
16609
16610    class-name:
16611      identifier
16612      template-id
16613
16614    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16615    to indicate that names looked up in dependent types should be
16616    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16617    keyword has been used to indicate that the name that appears next
16618    is a template.  TAG_TYPE indicates the explicit tag given before
16619    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16620    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16621    is the class being defined in a class-head.
16622
16623    Returns the TYPE_DECL representing the class.  */
16624
16625 static tree
16626 cp_parser_class_name (cp_parser *parser,
16627                       bool typename_keyword_p,
16628                       bool template_keyword_p,
16629                       enum tag_types tag_type,
16630                       bool check_dependency_p,
16631                       bool class_head_p,
16632                       bool is_declaration)
16633 {
16634   tree decl;
16635   tree scope;
16636   bool typename_p;
16637   cp_token *token;
16638   tree identifier = NULL_TREE;
16639
16640   /* All class-names start with an identifier.  */
16641   token = cp_lexer_peek_token (parser->lexer);
16642   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16643     {
16644       cp_parser_error (parser, "expected class-name");
16645       return error_mark_node;
16646     }
16647
16648   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16649      to a template-id, so we save it here.  */
16650   scope = parser->scope;
16651   if (scope == error_mark_node)
16652     return error_mark_node;
16653
16654   /* Any name names a type if we're following the `typename' keyword
16655      in a qualified name where the enclosing scope is type-dependent.  */
16656   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16657                 && dependent_type_p (scope));
16658   /* Handle the common case (an identifier, but not a template-id)
16659      efficiently.  */
16660   if (token->type == CPP_NAME
16661       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16662     {
16663       cp_token *identifier_token;
16664       bool ambiguous_p;
16665
16666       /* Look for the identifier.  */
16667       identifier_token = cp_lexer_peek_token (parser->lexer);
16668       ambiguous_p = identifier_token->ambiguous_p;
16669       identifier = cp_parser_identifier (parser);
16670       /* If the next token isn't an identifier, we are certainly not
16671          looking at a class-name.  */
16672       if (identifier == error_mark_node)
16673         decl = error_mark_node;
16674       /* If we know this is a type-name, there's no need to look it
16675          up.  */
16676       else if (typename_p)
16677         decl = identifier;
16678       else
16679         {
16680           tree ambiguous_decls;
16681           /* If we already know that this lookup is ambiguous, then
16682              we've already issued an error message; there's no reason
16683              to check again.  */
16684           if (ambiguous_p)
16685             {
16686               cp_parser_simulate_error (parser);
16687               return error_mark_node;
16688             }
16689           /* If the next token is a `::', then the name must be a type
16690              name.
16691
16692              [basic.lookup.qual]
16693
16694              During the lookup for a name preceding the :: scope
16695              resolution operator, object, function, and enumerator
16696              names are ignored.  */
16697           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16698             tag_type = typename_type;
16699           /* Look up the name.  */
16700           decl = cp_parser_lookup_name (parser, identifier,
16701                                         tag_type,
16702                                         /*is_template=*/false,
16703                                         /*is_namespace=*/false,
16704                                         check_dependency_p,
16705                                         &ambiguous_decls,
16706                                         identifier_token->location);
16707           if (ambiguous_decls)
16708             {
16709               if (cp_parser_parsing_tentatively (parser))
16710                 cp_parser_simulate_error (parser);
16711               return error_mark_node;
16712             }
16713         }
16714     }
16715   else
16716     {
16717       /* Try a template-id.  */
16718       decl = cp_parser_template_id (parser, template_keyword_p,
16719                                     check_dependency_p,
16720                                     is_declaration);
16721       if (decl == error_mark_node)
16722         return error_mark_node;
16723     }
16724
16725   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16726
16727   /* If this is a typename, create a TYPENAME_TYPE.  */
16728   if (typename_p && decl != error_mark_node)
16729     {
16730       decl = make_typename_type (scope, decl, typename_type,
16731                                  /*complain=*/tf_error);
16732       if (decl != error_mark_node)
16733         decl = TYPE_NAME (decl);
16734     }
16735
16736   /* Check to see that it is really the name of a class.  */
16737   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16738       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16739       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16740     /* Situations like this:
16741
16742          template <typename T> struct A {
16743            typename T::template X<int>::I i;
16744          };
16745
16746        are problematic.  Is `T::template X<int>' a class-name?  The
16747        standard does not seem to be definitive, but there is no other
16748        valid interpretation of the following `::'.  Therefore, those
16749        names are considered class-names.  */
16750     {
16751       decl = make_typename_type (scope, decl, tag_type, tf_error);
16752       if (decl != error_mark_node)
16753         decl = TYPE_NAME (decl);
16754     }
16755   else if (TREE_CODE (decl) != TYPE_DECL
16756            || TREE_TYPE (decl) == error_mark_node
16757            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16758            /* In Objective-C 2.0, a classname followed by '.' starts a
16759               dot-syntax expression, and it's not a type-name.  */
16760            || (c_dialect_objc ()
16761                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16762                && objc_is_class_name (decl)))
16763     decl = error_mark_node;
16764
16765   if (decl == error_mark_node)
16766     cp_parser_error (parser, "expected class-name");
16767   else if (identifier && !parser->scope)
16768     maybe_note_name_used_in_class (identifier, decl);
16769
16770   return decl;
16771 }
16772
16773 /* Parse a class-specifier.
16774
16775    class-specifier:
16776      class-head { member-specification [opt] }
16777
16778    Returns the TREE_TYPE representing the class.  */
16779
16780 static tree
16781 cp_parser_class_specifier (cp_parser* parser)
16782 {
16783   tree type;
16784   tree attributes = NULL_TREE;
16785   bool nested_name_specifier_p;
16786   unsigned saved_num_template_parameter_lists;
16787   bool saved_in_function_body;
16788   bool saved_in_unbraced_linkage_specification_p;
16789   tree old_scope = NULL_TREE;
16790   tree scope = NULL_TREE;
16791   tree bases;
16792
16793   push_deferring_access_checks (dk_no_deferred);
16794
16795   /* Parse the class-head.  */
16796   type = cp_parser_class_head (parser,
16797                                &nested_name_specifier_p,
16798                                &attributes,
16799                                &bases);
16800   /* If the class-head was a semantic disaster, skip the entire body
16801      of the class.  */
16802   if (!type)
16803     {
16804       cp_parser_skip_to_end_of_block_or_statement (parser);
16805       pop_deferring_access_checks ();
16806       return error_mark_node;
16807     }
16808
16809   /* Look for the `{'.  */
16810   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16811     {
16812       pop_deferring_access_checks ();
16813       return error_mark_node;
16814     }
16815
16816   /* Process the base classes. If they're invalid, skip the 
16817      entire class body.  */
16818   if (!xref_basetypes (type, bases))
16819     {
16820       /* Consuming the closing brace yields better error messages
16821          later on.  */
16822       if (cp_parser_skip_to_closing_brace (parser))
16823         cp_lexer_consume_token (parser->lexer);
16824       pop_deferring_access_checks ();
16825       return error_mark_node;
16826     }
16827
16828   /* Issue an error message if type-definitions are forbidden here.  */
16829   cp_parser_check_type_definition (parser);
16830   /* Remember that we are defining one more class.  */
16831   ++parser->num_classes_being_defined;
16832   /* Inside the class, surrounding template-parameter-lists do not
16833      apply.  */
16834   saved_num_template_parameter_lists
16835     = parser->num_template_parameter_lists;
16836   parser->num_template_parameter_lists = 0;
16837   /* We are not in a function body.  */
16838   saved_in_function_body = parser->in_function_body;
16839   parser->in_function_body = false;
16840   /* We are not immediately inside an extern "lang" block.  */
16841   saved_in_unbraced_linkage_specification_p
16842     = parser->in_unbraced_linkage_specification_p;
16843   parser->in_unbraced_linkage_specification_p = false;
16844
16845   /* Start the class.  */
16846   if (nested_name_specifier_p)
16847     {
16848       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16849       old_scope = push_inner_scope (scope);
16850     }
16851   type = begin_class_definition (type, attributes);
16852
16853   if (type == error_mark_node)
16854     /* If the type is erroneous, skip the entire body of the class.  */
16855     cp_parser_skip_to_closing_brace (parser);
16856   else
16857     /* Parse the member-specification.  */
16858     cp_parser_member_specification_opt (parser);
16859
16860   /* Look for the trailing `}'.  */
16861   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16862   /* Look for trailing attributes to apply to this class.  */
16863   if (cp_parser_allow_gnu_extensions_p (parser))
16864     attributes = cp_parser_attributes_opt (parser);
16865   if (type != error_mark_node)
16866     type = finish_struct (type, attributes);
16867   if (nested_name_specifier_p)
16868     pop_inner_scope (old_scope, scope);
16869   /* If this class is not itself within the scope of another class,
16870      then we need to parse the bodies of all of the queued function
16871      definitions.  Note that the queued functions defined in a class
16872      are not always processed immediately following the
16873      class-specifier for that class.  Consider:
16874
16875        struct A {
16876          struct B { void f() { sizeof (A); } };
16877        };
16878
16879      If `f' were processed before the processing of `A' were
16880      completed, there would be no way to compute the size of `A'.
16881      Note that the nesting we are interested in here is lexical --
16882      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16883      for:
16884
16885        struct A { struct B; };
16886        struct A::B { void f() { } };
16887
16888      there is no need to delay the parsing of `A::B::f'.  */
16889   if (--parser->num_classes_being_defined == 0)
16890     {
16891       tree fn;
16892       tree class_type = NULL_TREE;
16893       tree pushed_scope = NULL_TREE;
16894       unsigned ix;
16895       cp_default_arg_entry *e;
16896
16897       /* In a first pass, parse default arguments to the functions.
16898          Then, in a second pass, parse the bodies of the functions.
16899          This two-phased approach handles cases like:
16900
16901             struct S {
16902               void f() { g(); }
16903               void g(int i = 3);
16904             };
16905
16906          */
16907       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16908                         ix, e)
16909         {
16910           fn = e->decl;
16911           /* If there are default arguments that have not yet been processed,
16912              take care of them now.  */
16913           if (class_type != e->class_type)
16914             {
16915               if (pushed_scope)
16916                 pop_scope (pushed_scope);
16917               class_type = e->class_type;
16918               pushed_scope = push_scope (class_type);
16919             }
16920           /* Make sure that any template parameters are in scope.  */
16921           maybe_begin_member_template_processing (fn);
16922           /* Parse the default argument expressions.  */
16923           cp_parser_late_parsing_default_args (parser, fn);
16924           /* Remove any template parameters from the symbol table.  */
16925           maybe_end_member_template_processing ();
16926         }
16927       if (pushed_scope)
16928         pop_scope (pushed_scope);
16929       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16930       /* Now parse the body of the functions.  */
16931       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16932         cp_parser_late_parsing_for_member (parser, fn);
16933       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16934     }
16935
16936   /* Put back any saved access checks.  */
16937   pop_deferring_access_checks ();
16938
16939   /* Restore saved state.  */
16940   parser->in_function_body = saved_in_function_body;
16941   parser->num_template_parameter_lists
16942     = saved_num_template_parameter_lists;
16943   parser->in_unbraced_linkage_specification_p
16944     = saved_in_unbraced_linkage_specification_p;
16945
16946   return type;
16947 }
16948
16949 /* Parse a class-head.
16950
16951    class-head:
16952      class-key identifier [opt] base-clause [opt]
16953      class-key nested-name-specifier identifier base-clause [opt]
16954      class-key nested-name-specifier [opt] template-id
16955        base-clause [opt]
16956
16957    GNU Extensions:
16958      class-key attributes identifier [opt] base-clause [opt]
16959      class-key attributes nested-name-specifier identifier base-clause [opt]
16960      class-key attributes nested-name-specifier [opt] template-id
16961        base-clause [opt]
16962
16963    Upon return BASES is initialized to the list of base classes (or
16964    NULL, if there are none) in the same form returned by
16965    cp_parser_base_clause.
16966
16967    Returns the TYPE of the indicated class.  Sets
16968    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16969    involving a nested-name-specifier was used, and FALSE otherwise.
16970
16971    Returns error_mark_node if this is not a class-head.
16972
16973    Returns NULL_TREE if the class-head is syntactically valid, but
16974    semantically invalid in a way that means we should skip the entire
16975    body of the class.  */
16976
16977 static tree
16978 cp_parser_class_head (cp_parser* parser,
16979                       bool* nested_name_specifier_p,
16980                       tree *attributes_p,
16981                       tree *bases)
16982 {
16983   tree nested_name_specifier;
16984   enum tag_types class_key;
16985   tree id = NULL_TREE;
16986   tree type = NULL_TREE;
16987   tree attributes;
16988   bool template_id_p = false;
16989   bool qualified_p = false;
16990   bool invalid_nested_name_p = false;
16991   bool invalid_explicit_specialization_p = false;
16992   tree pushed_scope = NULL_TREE;
16993   unsigned num_templates;
16994   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16995   /* Assume no nested-name-specifier will be present.  */
16996   *nested_name_specifier_p = false;
16997   /* Assume no template parameter lists will be used in defining the
16998      type.  */
16999   num_templates = 0;
17000
17001   *bases = NULL_TREE;
17002
17003   /* Look for the class-key.  */
17004   class_key = cp_parser_class_key (parser);
17005   if (class_key == none_type)
17006     return error_mark_node;
17007
17008   /* Parse the attributes.  */
17009   attributes = cp_parser_attributes_opt (parser);
17010
17011   /* If the next token is `::', that is invalid -- but sometimes
17012      people do try to write:
17013
17014        struct ::S {};
17015
17016      Handle this gracefully by accepting the extra qualifier, and then
17017      issuing an error about it later if this really is a
17018      class-head.  If it turns out just to be an elaborated type
17019      specifier, remain silent.  */
17020   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17021     qualified_p = true;
17022
17023   push_deferring_access_checks (dk_no_check);
17024
17025   /* Determine the name of the class.  Begin by looking for an
17026      optional nested-name-specifier.  */
17027   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17028   nested_name_specifier
17029     = cp_parser_nested_name_specifier_opt (parser,
17030                                            /*typename_keyword_p=*/false,
17031                                            /*check_dependency_p=*/false,
17032                                            /*type_p=*/false,
17033                                            /*is_declaration=*/false);
17034   /* If there was a nested-name-specifier, then there *must* be an
17035      identifier.  */
17036   if (nested_name_specifier)
17037     {
17038       type_start_token = cp_lexer_peek_token (parser->lexer);
17039       /* Although the grammar says `identifier', it really means
17040          `class-name' or `template-name'.  You are only allowed to
17041          define a class that has already been declared with this
17042          syntax.
17043
17044          The proposed resolution for Core Issue 180 says that wherever
17045          you see `class T::X' you should treat `X' as a type-name.
17046
17047          It is OK to define an inaccessible class; for example:
17048
17049            class A { class B; };
17050            class A::B {};
17051
17052          We do not know if we will see a class-name, or a
17053          template-name.  We look for a class-name first, in case the
17054          class-name is a template-id; if we looked for the
17055          template-name first we would stop after the template-name.  */
17056       cp_parser_parse_tentatively (parser);
17057       type = cp_parser_class_name (parser,
17058                                    /*typename_keyword_p=*/false,
17059                                    /*template_keyword_p=*/false,
17060                                    class_type,
17061                                    /*check_dependency_p=*/false,
17062                                    /*class_head_p=*/true,
17063                                    /*is_declaration=*/false);
17064       /* If that didn't work, ignore the nested-name-specifier.  */
17065       if (!cp_parser_parse_definitely (parser))
17066         {
17067           invalid_nested_name_p = true;
17068           type_start_token = cp_lexer_peek_token (parser->lexer);
17069           id = cp_parser_identifier (parser);
17070           if (id == error_mark_node)
17071             id = NULL_TREE;
17072         }
17073       /* If we could not find a corresponding TYPE, treat this
17074          declaration like an unqualified declaration.  */
17075       if (type == error_mark_node)
17076         nested_name_specifier = NULL_TREE;
17077       /* Otherwise, count the number of templates used in TYPE and its
17078          containing scopes.  */
17079       else
17080         {
17081           tree scope;
17082
17083           for (scope = TREE_TYPE (type);
17084                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17085                scope = (TYPE_P (scope)
17086                         ? TYPE_CONTEXT (scope)
17087                         : DECL_CONTEXT (scope)))
17088             if (TYPE_P (scope)
17089                 && CLASS_TYPE_P (scope)
17090                 && CLASSTYPE_TEMPLATE_INFO (scope)
17091                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17092                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17093               ++num_templates;
17094         }
17095     }
17096   /* Otherwise, the identifier is optional.  */
17097   else
17098     {
17099       /* We don't know whether what comes next is a template-id,
17100          an identifier, or nothing at all.  */
17101       cp_parser_parse_tentatively (parser);
17102       /* Check for a template-id.  */
17103       type_start_token = cp_lexer_peek_token (parser->lexer);
17104       id = cp_parser_template_id (parser,
17105                                   /*template_keyword_p=*/false,
17106                                   /*check_dependency_p=*/true,
17107                                   /*is_declaration=*/true);
17108       /* If that didn't work, it could still be an identifier.  */
17109       if (!cp_parser_parse_definitely (parser))
17110         {
17111           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17112             {
17113               type_start_token = cp_lexer_peek_token (parser->lexer);
17114               id = cp_parser_identifier (parser);
17115             }
17116           else
17117             id = NULL_TREE;
17118         }
17119       else
17120         {
17121           template_id_p = true;
17122           ++num_templates;
17123         }
17124     }
17125
17126   pop_deferring_access_checks ();
17127
17128   if (id)
17129     cp_parser_check_for_invalid_template_id (parser, id,
17130                                              type_start_token->location);
17131
17132   /* If it's not a `:' or a `{' then we can't really be looking at a
17133      class-head, since a class-head only appears as part of a
17134      class-specifier.  We have to detect this situation before calling
17135      xref_tag, since that has irreversible side-effects.  */
17136   if (!cp_parser_next_token_starts_class_definition_p (parser))
17137     {
17138       cp_parser_error (parser, "expected %<{%> or %<:%>");
17139       return error_mark_node;
17140     }
17141
17142   /* At this point, we're going ahead with the class-specifier, even
17143      if some other problem occurs.  */
17144   cp_parser_commit_to_tentative_parse (parser);
17145   /* Issue the error about the overly-qualified name now.  */
17146   if (qualified_p)
17147     {
17148       cp_parser_error (parser,
17149                        "global qualification of class name is invalid");
17150       return error_mark_node;
17151     }
17152   else if (invalid_nested_name_p)
17153     {
17154       cp_parser_error (parser,
17155                        "qualified name does not name a class");
17156       return error_mark_node;
17157     }
17158   else if (nested_name_specifier)
17159     {
17160       tree scope;
17161
17162       /* Reject typedef-names in class heads.  */
17163       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17164         {
17165           error_at (type_start_token->location,
17166                     "invalid class name in declaration of %qD",
17167                     type);
17168           type = NULL_TREE;
17169           goto done;
17170         }
17171
17172       /* Figure out in what scope the declaration is being placed.  */
17173       scope = current_scope ();
17174       /* If that scope does not contain the scope in which the
17175          class was originally declared, the program is invalid.  */
17176       if (scope && !is_ancestor (scope, nested_name_specifier))
17177         {
17178           if (at_namespace_scope_p ())
17179             error_at (type_start_token->location,
17180                       "declaration of %qD in namespace %qD which does not "
17181                       "enclose %qD",
17182                       type, scope, nested_name_specifier);
17183           else
17184             error_at (type_start_token->location,
17185                       "declaration of %qD in %qD which does not enclose %qD",
17186                       type, scope, nested_name_specifier);
17187           type = NULL_TREE;
17188           goto done;
17189         }
17190       /* [dcl.meaning]
17191
17192          A declarator-id shall not be qualified except for the
17193          definition of a ... nested class outside of its class
17194          ... [or] the definition or explicit instantiation of a
17195          class member of a namespace outside of its namespace.  */
17196       if (scope == nested_name_specifier)
17197         {
17198           permerror (nested_name_specifier_token_start->location,
17199                      "extra qualification not allowed");
17200           nested_name_specifier = NULL_TREE;
17201           num_templates = 0;
17202         }
17203     }
17204   /* An explicit-specialization must be preceded by "template <>".  If
17205      it is not, try to recover gracefully.  */
17206   if (at_namespace_scope_p ()
17207       && parser->num_template_parameter_lists == 0
17208       && template_id_p)
17209     {
17210       error_at (type_start_token->location,
17211                 "an explicit specialization must be preceded by %<template <>%>");
17212       invalid_explicit_specialization_p = true;
17213       /* Take the same action that would have been taken by
17214          cp_parser_explicit_specialization.  */
17215       ++parser->num_template_parameter_lists;
17216       begin_specialization ();
17217     }
17218   /* There must be no "return" statements between this point and the
17219      end of this function; set "type "to the correct return value and
17220      use "goto done;" to return.  */
17221   /* Make sure that the right number of template parameters were
17222      present.  */
17223   if (!cp_parser_check_template_parameters (parser, num_templates,
17224                                             type_start_token->location,
17225                                             /*declarator=*/NULL))
17226     {
17227       /* If something went wrong, there is no point in even trying to
17228          process the class-definition.  */
17229       type = NULL_TREE;
17230       goto done;
17231     }
17232
17233   /* Look up the type.  */
17234   if (template_id_p)
17235     {
17236       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17237           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17238               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17239         {
17240           error_at (type_start_token->location,
17241                     "function template %qD redeclared as a class template", id);
17242           type = error_mark_node;
17243         }
17244       else
17245         {
17246           type = TREE_TYPE (id);
17247           type = maybe_process_partial_specialization (type);
17248         }
17249       if (nested_name_specifier)
17250         pushed_scope = push_scope (nested_name_specifier);
17251     }
17252   else if (nested_name_specifier)
17253     {
17254       tree class_type;
17255
17256       /* Given:
17257
17258             template <typename T> struct S { struct T };
17259             template <typename T> struct S<T>::T { };
17260
17261          we will get a TYPENAME_TYPE when processing the definition of
17262          `S::T'.  We need to resolve it to the actual type before we
17263          try to define it.  */
17264       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17265         {
17266           class_type = resolve_typename_type (TREE_TYPE (type),
17267                                               /*only_current_p=*/false);
17268           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17269             type = TYPE_NAME (class_type);
17270           else
17271             {
17272               cp_parser_error (parser, "could not resolve typename type");
17273               type = error_mark_node;
17274             }
17275         }
17276
17277       if (maybe_process_partial_specialization (TREE_TYPE (type))
17278           == error_mark_node)
17279         {
17280           type = NULL_TREE;
17281           goto done;
17282         }
17283
17284       class_type = current_class_type;
17285       /* Enter the scope indicated by the nested-name-specifier.  */
17286       pushed_scope = push_scope (nested_name_specifier);
17287       /* Get the canonical version of this type.  */
17288       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17289       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17290           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17291         {
17292           type = push_template_decl (type);
17293           if (type == error_mark_node)
17294             {
17295               type = NULL_TREE;
17296               goto done;
17297             }
17298         }
17299
17300       type = TREE_TYPE (type);
17301       *nested_name_specifier_p = true;
17302     }
17303   else      /* The name is not a nested name.  */
17304     {
17305       /* If the class was unnamed, create a dummy name.  */
17306       if (!id)
17307         id = make_anon_name ();
17308       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17309                        parser->num_template_parameter_lists);
17310     }
17311
17312   /* Indicate whether this class was declared as a `class' or as a
17313      `struct'.  */
17314   if (TREE_CODE (type) == RECORD_TYPE)
17315     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17316   cp_parser_check_class_key (class_key, type);
17317
17318   /* If this type was already complete, and we see another definition,
17319      that's an error.  */
17320   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17321     {
17322       error_at (type_start_token->location, "redefinition of %q#T",
17323                 type);
17324       error_at (type_start_token->location, "previous definition of %q+#T",
17325                 type);
17326       type = NULL_TREE;
17327       goto done;
17328     }
17329   else if (type == error_mark_node)
17330     type = NULL_TREE;
17331
17332   /* We will have entered the scope containing the class; the names of
17333      base classes should be looked up in that context.  For example:
17334
17335        struct A { struct B {}; struct C; };
17336        struct A::C : B {};
17337
17338      is valid.  */
17339
17340   /* Get the list of base-classes, if there is one.  */
17341   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17342     *bases = cp_parser_base_clause (parser);
17343
17344  done:
17345   /* Leave the scope given by the nested-name-specifier.  We will
17346      enter the class scope itself while processing the members.  */
17347   if (pushed_scope)
17348     pop_scope (pushed_scope);
17349
17350   if (invalid_explicit_specialization_p)
17351     {
17352       end_specialization ();
17353       --parser->num_template_parameter_lists;
17354     }
17355
17356   if (type)
17357     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17358   *attributes_p = attributes;
17359   return type;
17360 }
17361
17362 /* Parse a class-key.
17363
17364    class-key:
17365      class
17366      struct
17367      union
17368
17369    Returns the kind of class-key specified, or none_type to indicate
17370    error.  */
17371
17372 static enum tag_types
17373 cp_parser_class_key (cp_parser* parser)
17374 {
17375   cp_token *token;
17376   enum tag_types tag_type;
17377
17378   /* Look for the class-key.  */
17379   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17380   if (!token)
17381     return none_type;
17382
17383   /* Check to see if the TOKEN is a class-key.  */
17384   tag_type = cp_parser_token_is_class_key (token);
17385   if (!tag_type)
17386     cp_parser_error (parser, "expected class-key");
17387   return tag_type;
17388 }
17389
17390 /* Parse an (optional) member-specification.
17391
17392    member-specification:
17393      member-declaration member-specification [opt]
17394      access-specifier : member-specification [opt]  */
17395
17396 static void
17397 cp_parser_member_specification_opt (cp_parser* parser)
17398 {
17399   while (true)
17400     {
17401       cp_token *token;
17402       enum rid keyword;
17403
17404       /* Peek at the next token.  */
17405       token = cp_lexer_peek_token (parser->lexer);
17406       /* If it's a `}', or EOF then we've seen all the members.  */
17407       if (token->type == CPP_CLOSE_BRACE
17408           || token->type == CPP_EOF
17409           || token->type == CPP_PRAGMA_EOL)
17410         break;
17411
17412       /* See if this token is a keyword.  */
17413       keyword = token->keyword;
17414       switch (keyword)
17415         {
17416         case RID_PUBLIC:
17417         case RID_PROTECTED:
17418         case RID_PRIVATE:
17419           /* Consume the access-specifier.  */
17420           cp_lexer_consume_token (parser->lexer);
17421           /* Remember which access-specifier is active.  */
17422           current_access_specifier = token->u.value;
17423           /* Look for the `:'.  */
17424           cp_parser_require (parser, CPP_COLON, RT_COLON);
17425           break;
17426
17427         default:
17428           /* Accept #pragmas at class scope.  */
17429           if (token->type == CPP_PRAGMA)
17430             {
17431               cp_parser_pragma (parser, pragma_external);
17432               break;
17433             }
17434
17435           /* Otherwise, the next construction must be a
17436              member-declaration.  */
17437           cp_parser_member_declaration (parser);
17438         }
17439     }
17440 }
17441
17442 /* Parse a member-declaration.
17443
17444    member-declaration:
17445      decl-specifier-seq [opt] member-declarator-list [opt] ;
17446      function-definition ; [opt]
17447      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17448      using-declaration
17449      template-declaration
17450
17451    member-declarator-list:
17452      member-declarator
17453      member-declarator-list , member-declarator
17454
17455    member-declarator:
17456      declarator pure-specifier [opt]
17457      declarator constant-initializer [opt]
17458      identifier [opt] : constant-expression
17459
17460    GNU Extensions:
17461
17462    member-declaration:
17463      __extension__ member-declaration
17464
17465    member-declarator:
17466      declarator attributes [opt] pure-specifier [opt]
17467      declarator attributes [opt] constant-initializer [opt]
17468      identifier [opt] attributes [opt] : constant-expression  
17469
17470    C++0x Extensions:
17471
17472    member-declaration:
17473      static_assert-declaration  */
17474
17475 static void
17476 cp_parser_member_declaration (cp_parser* parser)
17477 {
17478   cp_decl_specifier_seq decl_specifiers;
17479   tree prefix_attributes;
17480   tree decl;
17481   int declares_class_or_enum;
17482   bool friend_p;
17483   cp_token *token = NULL;
17484   cp_token *decl_spec_token_start = NULL;
17485   cp_token *initializer_token_start = NULL;
17486   int saved_pedantic;
17487
17488   /* Check for the `__extension__' keyword.  */
17489   if (cp_parser_extension_opt (parser, &saved_pedantic))
17490     {
17491       /* Recurse.  */
17492       cp_parser_member_declaration (parser);
17493       /* Restore the old value of the PEDANTIC flag.  */
17494       pedantic = saved_pedantic;
17495
17496       return;
17497     }
17498
17499   /* Check for a template-declaration.  */
17500   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17501     {
17502       /* An explicit specialization here is an error condition, and we
17503          expect the specialization handler to detect and report this.  */
17504       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17505           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17506         cp_parser_explicit_specialization (parser);
17507       else
17508         cp_parser_template_declaration (parser, /*member_p=*/true);
17509
17510       return;
17511     }
17512
17513   /* Check for a using-declaration.  */
17514   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17515     {
17516       /* Parse the using-declaration.  */
17517       cp_parser_using_declaration (parser,
17518                                    /*access_declaration_p=*/false);
17519       return;
17520     }
17521
17522   /* Check for @defs.  */
17523   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17524     {
17525       tree ivar, member;
17526       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17527       ivar = ivar_chains;
17528       while (ivar)
17529         {
17530           member = ivar;
17531           ivar = TREE_CHAIN (member);
17532           TREE_CHAIN (member) = NULL_TREE;
17533           finish_member_declaration (member);
17534         }
17535       return;
17536     }
17537
17538   /* If the next token is `static_assert' we have a static assertion.  */
17539   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17540     {
17541       cp_parser_static_assert (parser, /*member_p=*/true);
17542       return;
17543     }
17544
17545   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17546     return;
17547
17548   /* Parse the decl-specifier-seq.  */
17549   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17550   cp_parser_decl_specifier_seq (parser,
17551                                 CP_PARSER_FLAGS_OPTIONAL,
17552                                 &decl_specifiers,
17553                                 &declares_class_or_enum);
17554   prefix_attributes = decl_specifiers.attributes;
17555   decl_specifiers.attributes = NULL_TREE;
17556   /* Check for an invalid type-name.  */
17557   if (!decl_specifiers.any_type_specifiers_p
17558       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17559     return;
17560   /* If there is no declarator, then the decl-specifier-seq should
17561      specify a type.  */
17562   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17563     {
17564       /* If there was no decl-specifier-seq, and the next token is a
17565          `;', then we have something like:
17566
17567            struct S { ; };
17568
17569          [class.mem]
17570
17571          Each member-declaration shall declare at least one member
17572          name of the class.  */
17573       if (!decl_specifiers.any_specifiers_p)
17574         {
17575           cp_token *token = cp_lexer_peek_token (parser->lexer);
17576           if (!in_system_header_at (token->location))
17577             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17578         }
17579       else
17580         {
17581           tree type;
17582
17583           /* See if this declaration is a friend.  */
17584           friend_p = cp_parser_friend_p (&decl_specifiers);
17585           /* If there were decl-specifiers, check to see if there was
17586              a class-declaration.  */
17587           type = check_tag_decl (&decl_specifiers);
17588           /* Nested classes have already been added to the class, but
17589              a `friend' needs to be explicitly registered.  */
17590           if (friend_p)
17591             {
17592               /* If the `friend' keyword was present, the friend must
17593                  be introduced with a class-key.  */
17594                if (!declares_class_or_enum)
17595                  error_at (decl_spec_token_start->location,
17596                            "a class-key must be used when declaring a friend");
17597                /* In this case:
17598
17599                     template <typename T> struct A {
17600                       friend struct A<T>::B;
17601                     };
17602
17603                   A<T>::B will be represented by a TYPENAME_TYPE, and
17604                   therefore not recognized by check_tag_decl.  */
17605                if (!type
17606                    && decl_specifiers.type
17607                    && TYPE_P (decl_specifiers.type))
17608                  type = decl_specifiers.type;
17609                if (!type || !TYPE_P (type))
17610                  error_at (decl_spec_token_start->location,
17611                            "friend declaration does not name a class or "
17612                            "function");
17613                else
17614                  make_friend_class (current_class_type, type,
17615                                     /*complain=*/true);
17616             }
17617           /* If there is no TYPE, an error message will already have
17618              been issued.  */
17619           else if (!type || type == error_mark_node)
17620             ;
17621           /* An anonymous aggregate has to be handled specially; such
17622              a declaration really declares a data member (with a
17623              particular type), as opposed to a nested class.  */
17624           else if (ANON_AGGR_TYPE_P (type))
17625             {
17626               /* Remove constructors and such from TYPE, now that we
17627                  know it is an anonymous aggregate.  */
17628               fixup_anonymous_aggr (type);
17629               /* And make the corresponding data member.  */
17630               decl = build_decl (decl_spec_token_start->location,
17631                                  FIELD_DECL, NULL_TREE, type);
17632               /* Add it to the class.  */
17633               finish_member_declaration (decl);
17634             }
17635           else
17636             cp_parser_check_access_in_redeclaration
17637                                               (TYPE_NAME (type),
17638                                                decl_spec_token_start->location);
17639         }
17640     }
17641   else
17642     {
17643       bool assume_semicolon = false;
17644
17645       /* See if these declarations will be friends.  */
17646       friend_p = cp_parser_friend_p (&decl_specifiers);
17647
17648       /* Keep going until we hit the `;' at the end of the
17649          declaration.  */
17650       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17651         {
17652           tree attributes = NULL_TREE;
17653           tree first_attribute;
17654
17655           /* Peek at the next token.  */
17656           token = cp_lexer_peek_token (parser->lexer);
17657
17658           /* Check for a bitfield declaration.  */
17659           if (token->type == CPP_COLON
17660               || (token->type == CPP_NAME
17661                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17662                   == CPP_COLON))
17663             {
17664               tree identifier;
17665               tree width;
17666
17667               /* Get the name of the bitfield.  Note that we cannot just
17668                  check TOKEN here because it may have been invalidated by
17669                  the call to cp_lexer_peek_nth_token above.  */
17670               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17671                 identifier = cp_parser_identifier (parser);
17672               else
17673                 identifier = NULL_TREE;
17674
17675               /* Consume the `:' token.  */
17676               cp_lexer_consume_token (parser->lexer);
17677               /* Get the width of the bitfield.  */
17678               width
17679                 = cp_parser_constant_expression (parser,
17680                                                  /*allow_non_constant=*/false,
17681                                                  NULL);
17682
17683               /* Look for attributes that apply to the bitfield.  */
17684               attributes = cp_parser_attributes_opt (parser);
17685               /* Remember which attributes are prefix attributes and
17686                  which are not.  */
17687               first_attribute = attributes;
17688               /* Combine the attributes.  */
17689               attributes = chainon (prefix_attributes, attributes);
17690
17691               /* Create the bitfield declaration.  */
17692               decl = grokbitfield (identifier
17693                                    ? make_id_declarator (NULL_TREE,
17694                                                          identifier,
17695                                                          sfk_none)
17696                                    : NULL,
17697                                    &decl_specifiers,
17698                                    width,
17699                                    attributes);
17700             }
17701           else
17702             {
17703               cp_declarator *declarator;
17704               tree initializer;
17705               tree asm_specification;
17706               int ctor_dtor_or_conv_p;
17707
17708               /* Parse the declarator.  */
17709               declarator
17710                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17711                                         &ctor_dtor_or_conv_p,
17712                                         /*parenthesized_p=*/NULL,
17713                                         /*member_p=*/true);
17714
17715               /* If something went wrong parsing the declarator, make sure
17716                  that we at least consume some tokens.  */
17717               if (declarator == cp_error_declarator)
17718                 {
17719                   /* Skip to the end of the statement.  */
17720                   cp_parser_skip_to_end_of_statement (parser);
17721                   /* If the next token is not a semicolon, that is
17722                      probably because we just skipped over the body of
17723                      a function.  So, we consume a semicolon if
17724                      present, but do not issue an error message if it
17725                      is not present.  */
17726                   if (cp_lexer_next_token_is (parser->lexer,
17727                                               CPP_SEMICOLON))
17728                     cp_lexer_consume_token (parser->lexer);
17729                   return;
17730                 }
17731
17732               if (declares_class_or_enum & 2)
17733                 cp_parser_check_for_definition_in_return_type
17734                                             (declarator, decl_specifiers.type,
17735                                              decl_specifiers.type_location);
17736
17737               /* Look for an asm-specification.  */
17738               asm_specification = cp_parser_asm_specification_opt (parser);
17739               /* Look for attributes that apply to the declaration.  */
17740               attributes = cp_parser_attributes_opt (parser);
17741               /* Remember which attributes are prefix attributes and
17742                  which are not.  */
17743               first_attribute = attributes;
17744               /* Combine the attributes.  */
17745               attributes = chainon (prefix_attributes, attributes);
17746
17747               /* If it's an `=', then we have a constant-initializer or a
17748                  pure-specifier.  It is not correct to parse the
17749                  initializer before registering the member declaration
17750                  since the member declaration should be in scope while
17751                  its initializer is processed.  However, the rest of the
17752                  front end does not yet provide an interface that allows
17753                  us to handle this correctly.  */
17754               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17755                 {
17756                   /* In [class.mem]:
17757
17758                      A pure-specifier shall be used only in the declaration of
17759                      a virtual function.
17760
17761                      A member-declarator can contain a constant-initializer
17762                      only if it declares a static member of integral or
17763                      enumeration type.
17764
17765                      Therefore, if the DECLARATOR is for a function, we look
17766                      for a pure-specifier; otherwise, we look for a
17767                      constant-initializer.  When we call `grokfield', it will
17768                      perform more stringent semantics checks.  */
17769                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17770                   if (function_declarator_p (declarator))
17771                     initializer = cp_parser_pure_specifier (parser);
17772                   else
17773                     /* Parse the initializer.  */
17774                     initializer = cp_parser_constant_initializer (parser);
17775                 }
17776               /* Otherwise, there is no initializer.  */
17777               else
17778                 initializer = NULL_TREE;
17779
17780               /* See if we are probably looking at a function
17781                  definition.  We are certainly not looking at a
17782                  member-declarator.  Calling `grokfield' has
17783                  side-effects, so we must not do it unless we are sure
17784                  that we are looking at a member-declarator.  */
17785               if (cp_parser_token_starts_function_definition_p
17786                   (cp_lexer_peek_token (parser->lexer)))
17787                 {
17788                   /* The grammar does not allow a pure-specifier to be
17789                      used when a member function is defined.  (It is
17790                      possible that this fact is an oversight in the
17791                      standard, since a pure function may be defined
17792                      outside of the class-specifier.  */
17793                   if (initializer)
17794                     error_at (initializer_token_start->location,
17795                               "pure-specifier on function-definition");
17796                   decl = cp_parser_save_member_function_body (parser,
17797                                                               &decl_specifiers,
17798                                                               declarator,
17799                                                               attributes);
17800                   /* If the member was not a friend, declare it here.  */
17801                   if (!friend_p)
17802                     finish_member_declaration (decl);
17803                   /* Peek at the next token.  */
17804                   token = cp_lexer_peek_token (parser->lexer);
17805                   /* If the next token is a semicolon, consume it.  */
17806                   if (token->type == CPP_SEMICOLON)
17807                     cp_lexer_consume_token (parser->lexer);
17808                   return;
17809                 }
17810               else
17811                 if (declarator->kind == cdk_function)
17812                   declarator->id_loc = token->location;
17813                 /* Create the declaration.  */
17814                 decl = grokfield (declarator, &decl_specifiers,
17815                                   initializer, /*init_const_expr_p=*/true,
17816                                   asm_specification,
17817                                   attributes);
17818             }
17819
17820           /* Reset PREFIX_ATTRIBUTES.  */
17821           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17822             attributes = TREE_CHAIN (attributes);
17823           if (attributes)
17824             TREE_CHAIN (attributes) = NULL_TREE;
17825
17826           /* If there is any qualification still in effect, clear it
17827              now; we will be starting fresh with the next declarator.  */
17828           parser->scope = NULL_TREE;
17829           parser->qualifying_scope = NULL_TREE;
17830           parser->object_scope = NULL_TREE;
17831           /* If it's a `,', then there are more declarators.  */
17832           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17833             cp_lexer_consume_token (parser->lexer);
17834           /* If the next token isn't a `;', then we have a parse error.  */
17835           else if (cp_lexer_next_token_is_not (parser->lexer,
17836                                                CPP_SEMICOLON))
17837             {
17838               /* The next token might be a ways away from where the
17839                  actual semicolon is missing.  Find the previous token
17840                  and use that for our error position.  */
17841               cp_token *token = cp_lexer_previous_token (parser->lexer);
17842               error_at (token->location,
17843                         "expected %<;%> at end of member declaration");
17844
17845               /* Assume that the user meant to provide a semicolon.  If
17846                  we were to cp_parser_skip_to_end_of_statement, we might
17847                  skip to a semicolon inside a member function definition
17848                  and issue nonsensical error messages.  */
17849               assume_semicolon = true;
17850             }
17851
17852           if (decl)
17853             {
17854               /* Add DECL to the list of members.  */
17855               if (!friend_p)
17856                 finish_member_declaration (decl);
17857
17858               if (TREE_CODE (decl) == FUNCTION_DECL)
17859                 cp_parser_save_default_args (parser, decl);
17860             }
17861
17862           if (assume_semicolon)
17863             return;
17864         }
17865     }
17866
17867   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17868 }
17869
17870 /* Parse a pure-specifier.
17871
17872    pure-specifier:
17873      = 0
17874
17875    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17876    Otherwise, ERROR_MARK_NODE is returned.  */
17877
17878 static tree
17879 cp_parser_pure_specifier (cp_parser* parser)
17880 {
17881   cp_token *token;
17882
17883   /* Look for the `=' token.  */
17884   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17885     return error_mark_node;
17886   /* Look for the `0' token.  */
17887   token = cp_lexer_peek_token (parser->lexer);
17888
17889   if (token->type == CPP_EOF
17890       || token->type == CPP_PRAGMA_EOL)
17891     return error_mark_node;
17892
17893   cp_lexer_consume_token (parser->lexer);
17894
17895   /* Accept = default or = delete in c++0x mode.  */
17896   if (token->keyword == RID_DEFAULT
17897       || token->keyword == RID_DELETE)
17898     {
17899       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17900       return token->u.value;
17901     }
17902
17903   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17904   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17905     {
17906       cp_parser_error (parser,
17907                        "invalid pure specifier (only %<= 0%> is allowed)");
17908       cp_parser_skip_to_end_of_statement (parser);
17909       return error_mark_node;
17910     }
17911   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17912     {
17913       error_at (token->location, "templates may not be %<virtual%>");
17914       return error_mark_node;
17915     }
17916
17917   return integer_zero_node;
17918 }
17919
17920 /* Parse a constant-initializer.
17921
17922    constant-initializer:
17923      = constant-expression
17924
17925    Returns a representation of the constant-expression.  */
17926
17927 static tree
17928 cp_parser_constant_initializer (cp_parser* parser)
17929 {
17930   /* Look for the `=' token.  */
17931   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17932     return error_mark_node;
17933
17934   /* It is invalid to write:
17935
17936        struct S { static const int i = { 7 }; };
17937
17938      */
17939   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17940     {
17941       cp_parser_error (parser,
17942                        "a brace-enclosed initializer is not allowed here");
17943       /* Consume the opening brace.  */
17944       cp_lexer_consume_token (parser->lexer);
17945       /* Skip the initializer.  */
17946       cp_parser_skip_to_closing_brace (parser);
17947       /* Look for the trailing `}'.  */
17948       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17949
17950       return error_mark_node;
17951     }
17952
17953   return cp_parser_constant_expression (parser,
17954                                         /*allow_non_constant=*/false,
17955                                         NULL);
17956 }
17957
17958 /* Derived classes [gram.class.derived] */
17959
17960 /* Parse a base-clause.
17961
17962    base-clause:
17963      : base-specifier-list
17964
17965    base-specifier-list:
17966      base-specifier ... [opt]
17967      base-specifier-list , base-specifier ... [opt]
17968
17969    Returns a TREE_LIST representing the base-classes, in the order in
17970    which they were declared.  The representation of each node is as
17971    described by cp_parser_base_specifier.
17972
17973    In the case that no bases are specified, this function will return
17974    NULL_TREE, not ERROR_MARK_NODE.  */
17975
17976 static tree
17977 cp_parser_base_clause (cp_parser* parser)
17978 {
17979   tree bases = NULL_TREE;
17980
17981   /* Look for the `:' that begins the list.  */
17982   cp_parser_require (parser, CPP_COLON, RT_COLON);
17983
17984   /* Scan the base-specifier-list.  */
17985   while (true)
17986     {
17987       cp_token *token;
17988       tree base;
17989       bool pack_expansion_p = false;
17990
17991       /* Look for the base-specifier.  */
17992       base = cp_parser_base_specifier (parser);
17993       /* Look for the (optional) ellipsis. */
17994       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17995         {
17996           /* Consume the `...'. */
17997           cp_lexer_consume_token (parser->lexer);
17998
17999           pack_expansion_p = true;
18000         }
18001
18002       /* Add BASE to the front of the list.  */
18003       if (base != error_mark_node)
18004         {
18005           if (pack_expansion_p)
18006             /* Make this a pack expansion type. */
18007             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18008           
18009
18010           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18011             {
18012               TREE_CHAIN (base) = bases;
18013               bases = base;
18014             }
18015         }
18016       /* Peek at the next token.  */
18017       token = cp_lexer_peek_token (parser->lexer);
18018       /* If it's not a comma, then the list is complete.  */
18019       if (token->type != CPP_COMMA)
18020         break;
18021       /* Consume the `,'.  */
18022       cp_lexer_consume_token (parser->lexer);
18023     }
18024
18025   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18026      base class had a qualified name.  However, the next name that
18027      appears is certainly not qualified.  */
18028   parser->scope = NULL_TREE;
18029   parser->qualifying_scope = NULL_TREE;
18030   parser->object_scope = NULL_TREE;
18031
18032   return nreverse (bases);
18033 }
18034
18035 /* Parse a base-specifier.
18036
18037    base-specifier:
18038      :: [opt] nested-name-specifier [opt] class-name
18039      virtual access-specifier [opt] :: [opt] nested-name-specifier
18040        [opt] class-name
18041      access-specifier virtual [opt] :: [opt] nested-name-specifier
18042        [opt] class-name
18043
18044    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18045    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18046    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18047    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18048
18049 static tree
18050 cp_parser_base_specifier (cp_parser* parser)
18051 {
18052   cp_token *token;
18053   bool done = false;
18054   bool virtual_p = false;
18055   bool duplicate_virtual_error_issued_p = false;
18056   bool duplicate_access_error_issued_p = false;
18057   bool class_scope_p, template_p;
18058   tree access = access_default_node;
18059   tree type;
18060
18061   /* Process the optional `virtual' and `access-specifier'.  */
18062   while (!done)
18063     {
18064       /* Peek at the next token.  */
18065       token = cp_lexer_peek_token (parser->lexer);
18066       /* Process `virtual'.  */
18067       switch (token->keyword)
18068         {
18069         case RID_VIRTUAL:
18070           /* If `virtual' appears more than once, issue an error.  */
18071           if (virtual_p && !duplicate_virtual_error_issued_p)
18072             {
18073               cp_parser_error (parser,
18074                                "%<virtual%> specified more than once in base-specified");
18075               duplicate_virtual_error_issued_p = true;
18076             }
18077
18078           virtual_p = true;
18079
18080           /* Consume the `virtual' token.  */
18081           cp_lexer_consume_token (parser->lexer);
18082
18083           break;
18084
18085         case RID_PUBLIC:
18086         case RID_PROTECTED:
18087         case RID_PRIVATE:
18088           /* If more than one access specifier appears, issue an
18089              error.  */
18090           if (access != access_default_node
18091               && !duplicate_access_error_issued_p)
18092             {
18093               cp_parser_error (parser,
18094                                "more than one access specifier in base-specified");
18095               duplicate_access_error_issued_p = true;
18096             }
18097
18098           access = ridpointers[(int) token->keyword];
18099
18100           /* Consume the access-specifier.  */
18101           cp_lexer_consume_token (parser->lexer);
18102
18103           break;
18104
18105         default:
18106           done = true;
18107           break;
18108         }
18109     }
18110   /* It is not uncommon to see programs mechanically, erroneously, use
18111      the 'typename' keyword to denote (dependent) qualified types
18112      as base classes.  */
18113   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18114     {
18115       token = cp_lexer_peek_token (parser->lexer);
18116       if (!processing_template_decl)
18117         error_at (token->location,
18118                   "keyword %<typename%> not allowed outside of templates");
18119       else
18120         error_at (token->location,
18121                   "keyword %<typename%> not allowed in this context "
18122                   "(the base class is implicitly a type)");
18123       cp_lexer_consume_token (parser->lexer);
18124     }
18125
18126   /* Look for the optional `::' operator.  */
18127   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18128   /* Look for the nested-name-specifier.  The simplest way to
18129      implement:
18130
18131        [temp.res]
18132
18133        The keyword `typename' is not permitted in a base-specifier or
18134        mem-initializer; in these contexts a qualified name that
18135        depends on a template-parameter is implicitly assumed to be a
18136        type name.
18137
18138      is to pretend that we have seen the `typename' keyword at this
18139      point.  */
18140   cp_parser_nested_name_specifier_opt (parser,
18141                                        /*typename_keyword_p=*/true,
18142                                        /*check_dependency_p=*/true,
18143                                        typename_type,
18144                                        /*is_declaration=*/true);
18145   /* If the base class is given by a qualified name, assume that names
18146      we see are type names or templates, as appropriate.  */
18147   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18148   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18149
18150   /* Finally, look for the class-name.  */
18151   type = cp_parser_class_name (parser,
18152                                class_scope_p,
18153                                template_p,
18154                                typename_type,
18155                                /*check_dependency_p=*/true,
18156                                /*class_head_p=*/false,
18157                                /*is_declaration=*/true);
18158
18159   if (type == error_mark_node)
18160     return error_mark_node;
18161
18162   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18163 }
18164
18165 /* Exception handling [gram.exception] */
18166
18167 /* Parse an (optional) exception-specification.
18168
18169    exception-specification:
18170      throw ( type-id-list [opt] )
18171
18172    Returns a TREE_LIST representing the exception-specification.  The
18173    TREE_VALUE of each node is a type.  */
18174
18175 static tree
18176 cp_parser_exception_specification_opt (cp_parser* parser)
18177 {
18178   cp_token *token;
18179   tree type_id_list;
18180   const char *saved_message;
18181
18182   /* Peek at the next token.  */
18183   token = cp_lexer_peek_token (parser->lexer);
18184
18185   /* Is it a noexcept-specification?  */
18186   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18187     {
18188       tree expr;
18189       cp_lexer_consume_token (parser->lexer);
18190
18191       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18192         {
18193           cp_lexer_consume_token (parser->lexer);
18194
18195           /* Types may not be defined in an exception-specification.  */
18196           saved_message = parser->type_definition_forbidden_message;
18197           parser->type_definition_forbidden_message
18198             = G_("types may not be defined in an exception-specification");
18199
18200           expr = cp_parser_constant_expression (parser, false, NULL);
18201
18202           /* Restore the saved message.  */
18203           parser->type_definition_forbidden_message = saved_message;
18204
18205           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18206         }
18207       else
18208         expr = boolean_true_node;
18209
18210       return build_noexcept_spec (expr, tf_warning_or_error);
18211     }
18212
18213   /* If it's not `throw', then there's no exception-specification.  */
18214   if (!cp_parser_is_keyword (token, RID_THROW))
18215     return NULL_TREE;
18216
18217 #if 0
18218   /* Enable this once a lot of code has transitioned to noexcept?  */
18219   if (cxx_dialect == cxx0x && !in_system_header)
18220     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18221              "deprecated in C++0x; use %<noexcept%> instead.");
18222 #endif
18223
18224   /* Consume the `throw'.  */
18225   cp_lexer_consume_token (parser->lexer);
18226
18227   /* Look for the `('.  */
18228   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18229
18230   /* Peek at the next token.  */
18231   token = cp_lexer_peek_token (parser->lexer);
18232   /* If it's not a `)', then there is a type-id-list.  */
18233   if (token->type != CPP_CLOSE_PAREN)
18234     {
18235       /* Types may not be defined in an exception-specification.  */
18236       saved_message = parser->type_definition_forbidden_message;
18237       parser->type_definition_forbidden_message
18238         = G_("types may not be defined in an exception-specification");
18239       /* Parse the type-id-list.  */
18240       type_id_list = cp_parser_type_id_list (parser);
18241       /* Restore the saved message.  */
18242       parser->type_definition_forbidden_message = saved_message;
18243     }
18244   else
18245     type_id_list = empty_except_spec;
18246
18247   /* Look for the `)'.  */
18248   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18249
18250   return type_id_list;
18251 }
18252
18253 /* Parse an (optional) type-id-list.
18254
18255    type-id-list:
18256      type-id ... [opt]
18257      type-id-list , type-id ... [opt]
18258
18259    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18260    in the order that the types were presented.  */
18261
18262 static tree
18263 cp_parser_type_id_list (cp_parser* parser)
18264 {
18265   tree types = NULL_TREE;
18266
18267   while (true)
18268     {
18269       cp_token *token;
18270       tree type;
18271
18272       /* Get the next type-id.  */
18273       type = cp_parser_type_id (parser);
18274       /* Parse the optional ellipsis. */
18275       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18276         {
18277           /* Consume the `...'. */
18278           cp_lexer_consume_token (parser->lexer);
18279
18280           /* Turn the type into a pack expansion expression. */
18281           type = make_pack_expansion (type);
18282         }
18283       /* Add it to the list.  */
18284       types = add_exception_specifier (types, type, /*complain=*/1);
18285       /* Peek at the next token.  */
18286       token = cp_lexer_peek_token (parser->lexer);
18287       /* If it is not a `,', we are done.  */
18288       if (token->type != CPP_COMMA)
18289         break;
18290       /* Consume the `,'.  */
18291       cp_lexer_consume_token (parser->lexer);
18292     }
18293
18294   return nreverse (types);
18295 }
18296
18297 /* Parse a try-block.
18298
18299    try-block:
18300      try compound-statement handler-seq  */
18301
18302 static tree
18303 cp_parser_try_block (cp_parser* parser)
18304 {
18305   tree try_block;
18306
18307   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18308   try_block = begin_try_block ();
18309   cp_parser_compound_statement (parser, NULL, true);
18310   finish_try_block (try_block);
18311   cp_parser_handler_seq (parser);
18312   finish_handler_sequence (try_block);
18313
18314   return try_block;
18315 }
18316
18317 /* Parse a function-try-block.
18318
18319    function-try-block:
18320      try ctor-initializer [opt] function-body handler-seq  */
18321
18322 static bool
18323 cp_parser_function_try_block (cp_parser* parser)
18324 {
18325   tree compound_stmt;
18326   tree try_block;
18327   bool ctor_initializer_p;
18328
18329   /* Look for the `try' keyword.  */
18330   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18331     return false;
18332   /* Let the rest of the front end know where we are.  */
18333   try_block = begin_function_try_block (&compound_stmt);
18334   /* Parse the function-body.  */
18335   ctor_initializer_p
18336     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18337   /* We're done with the `try' part.  */
18338   finish_function_try_block (try_block);
18339   /* Parse the handlers.  */
18340   cp_parser_handler_seq (parser);
18341   /* We're done with the handlers.  */
18342   finish_function_handler_sequence (try_block, compound_stmt);
18343
18344   return ctor_initializer_p;
18345 }
18346
18347 /* Parse a handler-seq.
18348
18349    handler-seq:
18350      handler handler-seq [opt]  */
18351
18352 static void
18353 cp_parser_handler_seq (cp_parser* parser)
18354 {
18355   while (true)
18356     {
18357       cp_token *token;
18358
18359       /* Parse the handler.  */
18360       cp_parser_handler (parser);
18361       /* Peek at the next token.  */
18362       token = cp_lexer_peek_token (parser->lexer);
18363       /* If it's not `catch' then there are no more handlers.  */
18364       if (!cp_parser_is_keyword (token, RID_CATCH))
18365         break;
18366     }
18367 }
18368
18369 /* Parse a handler.
18370
18371    handler:
18372      catch ( exception-declaration ) compound-statement  */
18373
18374 static void
18375 cp_parser_handler (cp_parser* parser)
18376 {
18377   tree handler;
18378   tree declaration;
18379
18380   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18381   handler = begin_handler ();
18382   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18383   declaration = cp_parser_exception_declaration (parser);
18384   finish_handler_parms (declaration, handler);
18385   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18386   cp_parser_compound_statement (parser, NULL, false);
18387   finish_handler (handler);
18388 }
18389
18390 /* Parse an exception-declaration.
18391
18392    exception-declaration:
18393      type-specifier-seq declarator
18394      type-specifier-seq abstract-declarator
18395      type-specifier-seq
18396      ...
18397
18398    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18399    ellipsis variant is used.  */
18400
18401 static tree
18402 cp_parser_exception_declaration (cp_parser* parser)
18403 {
18404   cp_decl_specifier_seq type_specifiers;
18405   cp_declarator *declarator;
18406   const char *saved_message;
18407
18408   /* If it's an ellipsis, it's easy to handle.  */
18409   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18410     {
18411       /* Consume the `...' token.  */
18412       cp_lexer_consume_token (parser->lexer);
18413       return NULL_TREE;
18414     }
18415
18416   /* Types may not be defined in exception-declarations.  */
18417   saved_message = parser->type_definition_forbidden_message;
18418   parser->type_definition_forbidden_message
18419     = G_("types may not be defined in exception-declarations");
18420
18421   /* Parse the type-specifier-seq.  */
18422   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18423                                 /*is_trailing_return=*/false,
18424                                 &type_specifiers);
18425   /* If it's a `)', then there is no declarator.  */
18426   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18427     declarator = NULL;
18428   else
18429     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18430                                        /*ctor_dtor_or_conv_p=*/NULL,
18431                                        /*parenthesized_p=*/NULL,
18432                                        /*member_p=*/false);
18433
18434   /* Restore the saved message.  */
18435   parser->type_definition_forbidden_message = saved_message;
18436
18437   if (!type_specifiers.any_specifiers_p)
18438     return error_mark_node;
18439
18440   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18441 }
18442
18443 /* Parse a throw-expression.
18444
18445    throw-expression:
18446      throw assignment-expression [opt]
18447
18448    Returns a THROW_EXPR representing the throw-expression.  */
18449
18450 static tree
18451 cp_parser_throw_expression (cp_parser* parser)
18452 {
18453   tree expression;
18454   cp_token* token;
18455
18456   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18457   token = cp_lexer_peek_token (parser->lexer);
18458   /* Figure out whether or not there is an assignment-expression
18459      following the "throw" keyword.  */
18460   if (token->type == CPP_COMMA
18461       || token->type == CPP_SEMICOLON
18462       || token->type == CPP_CLOSE_PAREN
18463       || token->type == CPP_CLOSE_SQUARE
18464       || token->type == CPP_CLOSE_BRACE
18465       || token->type == CPP_COLON)
18466     expression = NULL_TREE;
18467   else
18468     expression = cp_parser_assignment_expression (parser,
18469                                                   /*cast_p=*/false, NULL);
18470
18471   return build_throw (expression);
18472 }
18473
18474 /* GNU Extensions */
18475
18476 /* Parse an (optional) asm-specification.
18477
18478    asm-specification:
18479      asm ( string-literal )
18480
18481    If the asm-specification is present, returns a STRING_CST
18482    corresponding to the string-literal.  Otherwise, returns
18483    NULL_TREE.  */
18484
18485 static tree
18486 cp_parser_asm_specification_opt (cp_parser* parser)
18487 {
18488   cp_token *token;
18489   tree asm_specification;
18490
18491   /* Peek at the next token.  */
18492   token = cp_lexer_peek_token (parser->lexer);
18493   /* If the next token isn't the `asm' keyword, then there's no
18494      asm-specification.  */
18495   if (!cp_parser_is_keyword (token, RID_ASM))
18496     return NULL_TREE;
18497
18498   /* Consume the `asm' token.  */
18499   cp_lexer_consume_token (parser->lexer);
18500   /* Look for the `('.  */
18501   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18502
18503   /* Look for the string-literal.  */
18504   asm_specification = cp_parser_string_literal (parser, false, false);
18505
18506   /* Look for the `)'.  */
18507   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18508
18509   return asm_specification;
18510 }
18511
18512 /* Parse an asm-operand-list.
18513
18514    asm-operand-list:
18515      asm-operand
18516      asm-operand-list , asm-operand
18517
18518    asm-operand:
18519      string-literal ( expression )
18520      [ string-literal ] string-literal ( expression )
18521
18522    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18523    each node is the expression.  The TREE_PURPOSE is itself a
18524    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18525    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18526    is a STRING_CST for the string literal before the parenthesis. Returns
18527    ERROR_MARK_NODE if any of the operands are invalid.  */
18528
18529 static tree
18530 cp_parser_asm_operand_list (cp_parser* parser)
18531 {
18532   tree asm_operands = NULL_TREE;
18533   bool invalid_operands = false;
18534
18535   while (true)
18536     {
18537       tree string_literal;
18538       tree expression;
18539       tree name;
18540
18541       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18542         {
18543           /* Consume the `[' token.  */
18544           cp_lexer_consume_token (parser->lexer);
18545           /* Read the operand name.  */
18546           name = cp_parser_identifier (parser);
18547           if (name != error_mark_node)
18548             name = build_string (IDENTIFIER_LENGTH (name),
18549                                  IDENTIFIER_POINTER (name));
18550           /* Look for the closing `]'.  */
18551           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18552         }
18553       else
18554         name = NULL_TREE;
18555       /* Look for the string-literal.  */
18556       string_literal = cp_parser_string_literal (parser, false, false);
18557
18558       /* Look for the `('.  */
18559       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18560       /* Parse the expression.  */
18561       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18562       /* Look for the `)'.  */
18563       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18564
18565       if (name == error_mark_node 
18566           || string_literal == error_mark_node 
18567           || expression == error_mark_node)
18568         invalid_operands = true;
18569
18570       /* Add this operand to the list.  */
18571       asm_operands = tree_cons (build_tree_list (name, string_literal),
18572                                 expression,
18573                                 asm_operands);
18574       /* If the next token is not a `,', there are no more
18575          operands.  */
18576       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18577         break;
18578       /* Consume the `,'.  */
18579       cp_lexer_consume_token (parser->lexer);
18580     }
18581
18582   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18583 }
18584
18585 /* Parse an asm-clobber-list.
18586
18587    asm-clobber-list:
18588      string-literal
18589      asm-clobber-list , string-literal
18590
18591    Returns a TREE_LIST, indicating the clobbers in the order that they
18592    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18593
18594 static tree
18595 cp_parser_asm_clobber_list (cp_parser* parser)
18596 {
18597   tree clobbers = NULL_TREE;
18598
18599   while (true)
18600     {
18601       tree string_literal;
18602
18603       /* Look for the string literal.  */
18604       string_literal = cp_parser_string_literal (parser, false, false);
18605       /* Add it to the list.  */
18606       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18607       /* If the next token is not a `,', then the list is
18608          complete.  */
18609       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18610         break;
18611       /* Consume the `,' token.  */
18612       cp_lexer_consume_token (parser->lexer);
18613     }
18614
18615   return clobbers;
18616 }
18617
18618 /* Parse an asm-label-list.
18619
18620    asm-label-list:
18621      identifier
18622      asm-label-list , identifier
18623
18624    Returns a TREE_LIST, indicating the labels in the order that they
18625    appeared.  The TREE_VALUE of each node is a label.  */
18626
18627 static tree
18628 cp_parser_asm_label_list (cp_parser* parser)
18629 {
18630   tree labels = NULL_TREE;
18631
18632   while (true)
18633     {
18634       tree identifier, label, name;
18635
18636       /* Look for the identifier.  */
18637       identifier = cp_parser_identifier (parser);
18638       if (!error_operand_p (identifier))
18639         {
18640           label = lookup_label (identifier);
18641           if (TREE_CODE (label) == LABEL_DECL)
18642             {
18643               TREE_USED (label) = 1;
18644               check_goto (label);
18645               name = build_string (IDENTIFIER_LENGTH (identifier),
18646                                    IDENTIFIER_POINTER (identifier));
18647               labels = tree_cons (name, label, labels);
18648             }
18649         }
18650       /* If the next token is not a `,', then the list is
18651          complete.  */
18652       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18653         break;
18654       /* Consume the `,' token.  */
18655       cp_lexer_consume_token (parser->lexer);
18656     }
18657
18658   return nreverse (labels);
18659 }
18660
18661 /* Parse an (optional) series of attributes.
18662
18663    attributes:
18664      attributes attribute
18665
18666    attribute:
18667      __attribute__ (( attribute-list [opt] ))
18668
18669    The return value is as for cp_parser_attribute_list.  */
18670
18671 static tree
18672 cp_parser_attributes_opt (cp_parser* parser)
18673 {
18674   tree attributes = NULL_TREE;
18675
18676   while (true)
18677     {
18678       cp_token *token;
18679       tree attribute_list;
18680
18681       /* Peek at the next token.  */
18682       token = cp_lexer_peek_token (parser->lexer);
18683       /* If it's not `__attribute__', then we're done.  */
18684       if (token->keyword != RID_ATTRIBUTE)
18685         break;
18686
18687       /* Consume the `__attribute__' keyword.  */
18688       cp_lexer_consume_token (parser->lexer);
18689       /* Look for the two `(' tokens.  */
18690       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18691       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18692
18693       /* Peek at the next token.  */
18694       token = cp_lexer_peek_token (parser->lexer);
18695       if (token->type != CPP_CLOSE_PAREN)
18696         /* Parse the attribute-list.  */
18697         attribute_list = cp_parser_attribute_list (parser);
18698       else
18699         /* If the next token is a `)', then there is no attribute
18700            list.  */
18701         attribute_list = NULL;
18702
18703       /* Look for the two `)' tokens.  */
18704       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18705       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18706
18707       /* Add these new attributes to the list.  */
18708       attributes = chainon (attributes, attribute_list);
18709     }
18710
18711   return attributes;
18712 }
18713
18714 /* Parse an attribute-list.
18715
18716    attribute-list:
18717      attribute
18718      attribute-list , attribute
18719
18720    attribute:
18721      identifier
18722      identifier ( identifier )
18723      identifier ( identifier , expression-list )
18724      identifier ( expression-list )
18725
18726    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18727    to an attribute.  The TREE_PURPOSE of each node is the identifier
18728    indicating which attribute is in use.  The TREE_VALUE represents
18729    the arguments, if any.  */
18730
18731 static tree
18732 cp_parser_attribute_list (cp_parser* parser)
18733 {
18734   tree attribute_list = NULL_TREE;
18735   bool save_translate_strings_p = parser->translate_strings_p;
18736
18737   parser->translate_strings_p = false;
18738   while (true)
18739     {
18740       cp_token *token;
18741       tree identifier;
18742       tree attribute;
18743
18744       /* Look for the identifier.  We also allow keywords here; for
18745          example `__attribute__ ((const))' is legal.  */
18746       token = cp_lexer_peek_token (parser->lexer);
18747       if (token->type == CPP_NAME
18748           || token->type == CPP_KEYWORD)
18749         {
18750           tree arguments = NULL_TREE;
18751
18752           /* Consume the token.  */
18753           token = cp_lexer_consume_token (parser->lexer);
18754
18755           /* Save away the identifier that indicates which attribute
18756              this is.  */
18757           identifier = (token->type == CPP_KEYWORD) 
18758             /* For keywords, use the canonical spelling, not the
18759                parsed identifier.  */
18760             ? ridpointers[(int) token->keyword]
18761             : token->u.value;
18762           
18763           attribute = build_tree_list (identifier, NULL_TREE);
18764
18765           /* Peek at the next token.  */
18766           token = cp_lexer_peek_token (parser->lexer);
18767           /* If it's an `(', then parse the attribute arguments.  */
18768           if (token->type == CPP_OPEN_PAREN)
18769             {
18770               VEC(tree,gc) *vec;
18771               int attr_flag = (attribute_takes_identifier_p (identifier)
18772                                ? id_attr : normal_attr);
18773               vec = cp_parser_parenthesized_expression_list
18774                     (parser, attr_flag, /*cast_p=*/false,
18775                      /*allow_expansion_p=*/false,
18776                      /*non_constant_p=*/NULL);
18777               if (vec == NULL)
18778                 arguments = error_mark_node;
18779               else
18780                 {
18781                   arguments = build_tree_list_vec (vec);
18782                   release_tree_vector (vec);
18783                 }
18784               /* Save the arguments away.  */
18785               TREE_VALUE (attribute) = arguments;
18786             }
18787
18788           if (arguments != error_mark_node)
18789             {
18790               /* Add this attribute to the list.  */
18791               TREE_CHAIN (attribute) = attribute_list;
18792               attribute_list = attribute;
18793             }
18794
18795           token = cp_lexer_peek_token (parser->lexer);
18796         }
18797       /* Now, look for more attributes.  If the next token isn't a
18798          `,', we're done.  */
18799       if (token->type != CPP_COMMA)
18800         break;
18801
18802       /* Consume the comma and keep going.  */
18803       cp_lexer_consume_token (parser->lexer);
18804     }
18805   parser->translate_strings_p = save_translate_strings_p;
18806
18807   /* We built up the list in reverse order.  */
18808   return nreverse (attribute_list);
18809 }
18810
18811 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18812    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18813    current value of the PEDANTIC flag, regardless of whether or not
18814    the `__extension__' keyword is present.  The caller is responsible
18815    for restoring the value of the PEDANTIC flag.  */
18816
18817 static bool
18818 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18819 {
18820   /* Save the old value of the PEDANTIC flag.  */
18821   *saved_pedantic = pedantic;
18822
18823   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18824     {
18825       /* Consume the `__extension__' token.  */
18826       cp_lexer_consume_token (parser->lexer);
18827       /* We're not being pedantic while the `__extension__' keyword is
18828          in effect.  */
18829       pedantic = 0;
18830
18831       return true;
18832     }
18833
18834   return false;
18835 }
18836
18837 /* Parse a label declaration.
18838
18839    label-declaration:
18840      __label__ label-declarator-seq ;
18841
18842    label-declarator-seq:
18843      identifier , label-declarator-seq
18844      identifier  */
18845
18846 static void
18847 cp_parser_label_declaration (cp_parser* parser)
18848 {
18849   /* Look for the `__label__' keyword.  */
18850   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18851
18852   while (true)
18853     {
18854       tree identifier;
18855
18856       /* Look for an identifier.  */
18857       identifier = cp_parser_identifier (parser);
18858       /* If we failed, stop.  */
18859       if (identifier == error_mark_node)
18860         break;
18861       /* Declare it as a label.  */
18862       finish_label_decl (identifier);
18863       /* If the next token is a `;', stop.  */
18864       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18865         break;
18866       /* Look for the `,' separating the label declarations.  */
18867       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18868     }
18869
18870   /* Look for the final `;'.  */
18871   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18872 }
18873
18874 /* Support Functions */
18875
18876 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18877    NAME should have one of the representations used for an
18878    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18879    is returned.  If PARSER->SCOPE is a dependent type, then a
18880    SCOPE_REF is returned.
18881
18882    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18883    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18884    was formed.  Abstractly, such entities should not be passed to this
18885    function, because they do not need to be looked up, but it is
18886    simpler to check for this special case here, rather than at the
18887    call-sites.
18888
18889    In cases not explicitly covered above, this function returns a
18890    DECL, OVERLOAD, or baselink representing the result of the lookup.
18891    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18892    is returned.
18893
18894    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18895    (e.g., "struct") that was used.  In that case bindings that do not
18896    refer to types are ignored.
18897
18898    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18899    ignored.
18900
18901    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18902    are ignored.
18903
18904    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18905    types.
18906
18907    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18908    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18909    NULL_TREE otherwise.  */
18910
18911 static tree
18912 cp_parser_lookup_name (cp_parser *parser, tree name,
18913                        enum tag_types tag_type,
18914                        bool is_template,
18915                        bool is_namespace,
18916                        bool check_dependency,
18917                        tree *ambiguous_decls,
18918                        location_t name_location)
18919 {
18920   int flags = 0;
18921   tree decl;
18922   tree object_type = parser->context->object_type;
18923
18924   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18925     flags |= LOOKUP_COMPLAIN;
18926
18927   /* Assume that the lookup will be unambiguous.  */
18928   if (ambiguous_decls)
18929     *ambiguous_decls = NULL_TREE;
18930
18931   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18932      no longer valid.  Note that if we are parsing tentatively, and
18933      the parse fails, OBJECT_TYPE will be automatically restored.  */
18934   parser->context->object_type = NULL_TREE;
18935
18936   if (name == error_mark_node)
18937     return error_mark_node;
18938
18939   /* A template-id has already been resolved; there is no lookup to
18940      do.  */
18941   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18942     return name;
18943   if (BASELINK_P (name))
18944     {
18945       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18946                   == TEMPLATE_ID_EXPR);
18947       return name;
18948     }
18949
18950   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
18951      it should already have been checked to make sure that the name
18952      used matches the type being destroyed.  */
18953   if (TREE_CODE (name) == BIT_NOT_EXPR)
18954     {
18955       tree type;
18956
18957       /* Figure out to which type this destructor applies.  */
18958       if (parser->scope)
18959         type = parser->scope;
18960       else if (object_type)
18961         type = object_type;
18962       else
18963         type = current_class_type;
18964       /* If that's not a class type, there is no destructor.  */
18965       if (!type || !CLASS_TYPE_P (type))
18966         return error_mark_node;
18967       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18968         lazily_declare_fn (sfk_destructor, type);
18969       if (!CLASSTYPE_DESTRUCTORS (type))
18970           return error_mark_node;
18971       /* If it was a class type, return the destructor.  */
18972       return CLASSTYPE_DESTRUCTORS (type);
18973     }
18974
18975   /* By this point, the NAME should be an ordinary identifier.  If
18976      the id-expression was a qualified name, the qualifying scope is
18977      stored in PARSER->SCOPE at this point.  */
18978   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18979
18980   /* Perform the lookup.  */
18981   if (parser->scope)
18982     {
18983       bool dependent_p;
18984
18985       if (parser->scope == error_mark_node)
18986         return error_mark_node;
18987
18988       /* If the SCOPE is dependent, the lookup must be deferred until
18989          the template is instantiated -- unless we are explicitly
18990          looking up names in uninstantiated templates.  Even then, we
18991          cannot look up the name if the scope is not a class type; it
18992          might, for example, be a template type parameter.  */
18993       dependent_p = (TYPE_P (parser->scope)
18994                      && dependent_scope_p (parser->scope));
18995       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18996           && dependent_p)
18997         /* Defer lookup.  */
18998         decl = error_mark_node;
18999       else
19000         {
19001           tree pushed_scope = NULL_TREE;
19002
19003           /* If PARSER->SCOPE is a dependent type, then it must be a
19004              class type, and we must not be checking dependencies;
19005              otherwise, we would have processed this lookup above.  So
19006              that PARSER->SCOPE is not considered a dependent base by
19007              lookup_member, we must enter the scope here.  */
19008           if (dependent_p)
19009             pushed_scope = push_scope (parser->scope);
19010
19011           /* If the PARSER->SCOPE is a template specialization, it
19012              may be instantiated during name lookup.  In that case,
19013              errors may be issued.  Even if we rollback the current
19014              tentative parse, those errors are valid.  */
19015           decl = lookup_qualified_name (parser->scope, name,
19016                                         tag_type != none_type,
19017                                         /*complain=*/true);
19018
19019           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19020              lookup result and the nested-name-specifier nominates a class C:
19021                * if the name specified after the nested-name-specifier, when
19022                looked up in C, is the injected-class-name of C (Clause 9), or
19023                * if the name specified after the nested-name-specifier is the
19024                same as the identifier or the simple-template-id's template-
19025                name in the last component of the nested-name-specifier,
19026              the name is instead considered to name the constructor of
19027              class C. [ Note: for example, the constructor is not an
19028              acceptable lookup result in an elaborated-type-specifier so
19029              the constructor would not be used in place of the
19030              injected-class-name. --end note ] Such a constructor name
19031              shall be used only in the declarator-id of a declaration that
19032              names a constructor or in a using-declaration.  */
19033           if (tag_type == none_type
19034               && DECL_SELF_REFERENCE_P (decl)
19035               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19036             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19037                                           tag_type != none_type,
19038                                           /*complain=*/true);
19039
19040           /* If we have a single function from a using decl, pull it out.  */
19041           if (TREE_CODE (decl) == OVERLOAD
19042               && !really_overloaded_fn (decl))
19043             decl = OVL_FUNCTION (decl);
19044
19045           if (pushed_scope)
19046             pop_scope (pushed_scope);
19047         }
19048
19049       /* If the scope is a dependent type and either we deferred lookup or
19050          we did lookup but didn't find the name, rememeber the name.  */
19051       if (decl == error_mark_node && TYPE_P (parser->scope)
19052           && dependent_type_p (parser->scope))
19053         {
19054           if (tag_type)
19055             {
19056               tree type;
19057
19058               /* The resolution to Core Issue 180 says that `struct
19059                  A::B' should be considered a type-name, even if `A'
19060                  is dependent.  */
19061               type = make_typename_type (parser->scope, name, tag_type,
19062                                          /*complain=*/tf_error);
19063               decl = TYPE_NAME (type);
19064             }
19065           else if (is_template
19066                    && (cp_parser_next_token_ends_template_argument_p (parser)
19067                        || cp_lexer_next_token_is (parser->lexer,
19068                                                   CPP_CLOSE_PAREN)))
19069             decl = make_unbound_class_template (parser->scope,
19070                                                 name, NULL_TREE,
19071                                                 /*complain=*/tf_error);
19072           else
19073             decl = build_qualified_name (/*type=*/NULL_TREE,
19074                                          parser->scope, name,
19075                                          is_template);
19076         }
19077       parser->qualifying_scope = parser->scope;
19078       parser->object_scope = NULL_TREE;
19079     }
19080   else if (object_type)
19081     {
19082       tree object_decl = NULL_TREE;
19083       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19084          OBJECT_TYPE is not a class.  */
19085       if (CLASS_TYPE_P (object_type))
19086         /* If the OBJECT_TYPE is a template specialization, it may
19087            be instantiated during name lookup.  In that case, errors
19088            may be issued.  Even if we rollback the current tentative
19089            parse, those errors are valid.  */
19090         object_decl = lookup_member (object_type,
19091                                      name,
19092                                      /*protect=*/0,
19093                                      tag_type != none_type);
19094       /* Look it up in the enclosing context, too.  */
19095       decl = lookup_name_real (name, tag_type != none_type,
19096                                /*nonclass=*/0,
19097                                /*block_p=*/true, is_namespace, flags);
19098       parser->object_scope = object_type;
19099       parser->qualifying_scope = NULL_TREE;
19100       if (object_decl)
19101         decl = object_decl;
19102     }
19103   else
19104     {
19105       decl = lookup_name_real (name, tag_type != none_type,
19106                                /*nonclass=*/0,
19107                                /*block_p=*/true, is_namespace, flags);
19108       parser->qualifying_scope = NULL_TREE;
19109       parser->object_scope = NULL_TREE;
19110     }
19111
19112   /* If the lookup failed, let our caller know.  */
19113   if (!decl || decl == error_mark_node)
19114     return error_mark_node;
19115
19116   /* Pull out the template from an injected-class-name (or multiple).  */
19117   if (is_template)
19118     decl = maybe_get_template_decl_from_type_decl (decl);
19119
19120   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19121   if (TREE_CODE (decl) == TREE_LIST)
19122     {
19123       if (ambiguous_decls)
19124         *ambiguous_decls = decl;
19125       /* The error message we have to print is too complicated for
19126          cp_parser_error, so we incorporate its actions directly.  */
19127       if (!cp_parser_simulate_error (parser))
19128         {
19129           error_at (name_location, "reference to %qD is ambiguous",
19130                     name);
19131           print_candidates (decl);
19132         }
19133       return error_mark_node;
19134     }
19135
19136   gcc_assert (DECL_P (decl)
19137               || TREE_CODE (decl) == OVERLOAD
19138               || TREE_CODE (decl) == SCOPE_REF
19139               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19140               || BASELINK_P (decl));
19141
19142   /* If we have resolved the name of a member declaration, check to
19143      see if the declaration is accessible.  When the name resolves to
19144      set of overloaded functions, accessibility is checked when
19145      overload resolution is done.
19146
19147      During an explicit instantiation, access is not checked at all,
19148      as per [temp.explicit].  */
19149   if (DECL_P (decl))
19150     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19151
19152   return decl;
19153 }
19154
19155 /* Like cp_parser_lookup_name, but for use in the typical case where
19156    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19157    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19158
19159 static tree
19160 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19161 {
19162   return cp_parser_lookup_name (parser, name,
19163                                 none_type,
19164                                 /*is_template=*/false,
19165                                 /*is_namespace=*/false,
19166                                 /*check_dependency=*/true,
19167                                 /*ambiguous_decls=*/NULL,
19168                                 location);
19169 }
19170
19171 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19172    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19173    true, the DECL indicates the class being defined in a class-head,
19174    or declared in an elaborated-type-specifier.
19175
19176    Otherwise, return DECL.  */
19177
19178 static tree
19179 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19180 {
19181   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19182      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19183
19184        struct A {
19185          template <typename T> struct B;
19186        };
19187
19188        template <typename T> struct A::B {};
19189
19190      Similarly, in an elaborated-type-specifier:
19191
19192        namespace N { struct X{}; }
19193
19194        struct A {
19195          template <typename T> friend struct N::X;
19196        };
19197
19198      However, if the DECL refers to a class type, and we are in
19199      the scope of the class, then the name lookup automatically
19200      finds the TYPE_DECL created by build_self_reference rather
19201      than a TEMPLATE_DECL.  For example, in:
19202
19203        template <class T> struct S {
19204          S s;
19205        };
19206
19207      there is no need to handle such case.  */
19208
19209   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19210     return DECL_TEMPLATE_RESULT (decl);
19211
19212   return decl;
19213 }
19214
19215 /* If too many, or too few, template-parameter lists apply to the
19216    declarator, issue an error message.  Returns TRUE if all went well,
19217    and FALSE otherwise.  */
19218
19219 static bool
19220 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19221                                                 cp_declarator *declarator,
19222                                                 location_t declarator_location)
19223 {
19224   unsigned num_templates;
19225
19226   /* We haven't seen any classes that involve template parameters yet.  */
19227   num_templates = 0;
19228
19229   switch (declarator->kind)
19230     {
19231     case cdk_id:
19232       if (declarator->u.id.qualifying_scope)
19233         {
19234           tree scope;
19235
19236           scope = declarator->u.id.qualifying_scope;
19237
19238           while (scope && CLASS_TYPE_P (scope))
19239             {
19240               /* You're supposed to have one `template <...>'
19241                  for every template class, but you don't need one
19242                  for a full specialization.  For example:
19243
19244                  template <class T> struct S{};
19245                  template <> struct S<int> { void f(); };
19246                  void S<int>::f () {}
19247
19248                  is correct; there shouldn't be a `template <>' for
19249                  the definition of `S<int>::f'.  */
19250               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19251                 /* If SCOPE does not have template information of any
19252                    kind, then it is not a template, nor is it nested
19253                    within a template.  */
19254                 break;
19255               if (explicit_class_specialization_p (scope))
19256                 break;
19257               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19258                 ++num_templates;
19259
19260               scope = TYPE_CONTEXT (scope);
19261             }
19262         }
19263       else if (TREE_CODE (declarator->u.id.unqualified_name)
19264                == TEMPLATE_ID_EXPR)
19265         /* If the DECLARATOR has the form `X<y>' then it uses one
19266            additional level of template parameters.  */
19267         ++num_templates;
19268
19269       return cp_parser_check_template_parameters 
19270         (parser, num_templates, declarator_location, declarator);
19271
19272
19273     case cdk_function:
19274     case cdk_array:
19275     case cdk_pointer:
19276     case cdk_reference:
19277     case cdk_ptrmem:
19278       return (cp_parser_check_declarator_template_parameters
19279               (parser, declarator->declarator, declarator_location));
19280
19281     case cdk_error:
19282       return true;
19283
19284     default:
19285       gcc_unreachable ();
19286     }
19287   return false;
19288 }
19289
19290 /* NUM_TEMPLATES were used in the current declaration.  If that is
19291    invalid, return FALSE and issue an error messages.  Otherwise,
19292    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19293    declarator and we can print more accurate diagnostics.  */
19294
19295 static bool
19296 cp_parser_check_template_parameters (cp_parser* parser,
19297                                      unsigned num_templates,
19298                                      location_t location,
19299                                      cp_declarator *declarator)
19300 {
19301   /* If there are the same number of template classes and parameter
19302      lists, that's OK.  */
19303   if (parser->num_template_parameter_lists == num_templates)
19304     return true;
19305   /* If there are more, but only one more, then we are referring to a
19306      member template.  That's OK too.  */
19307   if (parser->num_template_parameter_lists == num_templates + 1)
19308     return true;
19309   /* If there are more template classes than parameter lists, we have
19310      something like:
19311
19312        template <class T> void S<T>::R<T>::f ();  */
19313   if (parser->num_template_parameter_lists < num_templates)
19314     {
19315       if (declarator && !current_function_decl)
19316         error_at (location, "specializing member %<%T::%E%> "
19317                   "requires %<template<>%> syntax", 
19318                   declarator->u.id.qualifying_scope,
19319                   declarator->u.id.unqualified_name);
19320       else if (declarator)
19321         error_at (location, "invalid declaration of %<%T::%E%>",
19322                   declarator->u.id.qualifying_scope,
19323                   declarator->u.id.unqualified_name);
19324       else 
19325         error_at (location, "too few template-parameter-lists");
19326       return false;
19327     }
19328   /* Otherwise, there are too many template parameter lists.  We have
19329      something like:
19330
19331      template <class T> template <class U> void S::f();  */
19332   error_at (location, "too many template-parameter-lists");
19333   return false;
19334 }
19335
19336 /* Parse an optional `::' token indicating that the following name is
19337    from the global namespace.  If so, PARSER->SCOPE is set to the
19338    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19339    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19340    Returns the new value of PARSER->SCOPE, if the `::' token is
19341    present, and NULL_TREE otherwise.  */
19342
19343 static tree
19344 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19345 {
19346   cp_token *token;
19347
19348   /* Peek at the next token.  */
19349   token = cp_lexer_peek_token (parser->lexer);
19350   /* If we're looking at a `::' token then we're starting from the
19351      global namespace, not our current location.  */
19352   if (token->type == CPP_SCOPE)
19353     {
19354       /* Consume the `::' token.  */
19355       cp_lexer_consume_token (parser->lexer);
19356       /* Set the SCOPE so that we know where to start the lookup.  */
19357       parser->scope = global_namespace;
19358       parser->qualifying_scope = global_namespace;
19359       parser->object_scope = NULL_TREE;
19360
19361       return parser->scope;
19362     }
19363   else if (!current_scope_valid_p)
19364     {
19365       parser->scope = NULL_TREE;
19366       parser->qualifying_scope = NULL_TREE;
19367       parser->object_scope = NULL_TREE;
19368     }
19369
19370   return NULL_TREE;
19371 }
19372
19373 /* Returns TRUE if the upcoming token sequence is the start of a
19374    constructor declarator.  If FRIEND_P is true, the declarator is
19375    preceded by the `friend' specifier.  */
19376
19377 static bool
19378 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19379 {
19380   bool constructor_p;
19381   tree nested_name_specifier;
19382   cp_token *next_token;
19383
19384   /* The common case is that this is not a constructor declarator, so
19385      try to avoid doing lots of work if at all possible.  It's not
19386      valid declare a constructor at function scope.  */
19387   if (parser->in_function_body)
19388     return false;
19389   /* And only certain tokens can begin a constructor declarator.  */
19390   next_token = cp_lexer_peek_token (parser->lexer);
19391   if (next_token->type != CPP_NAME
19392       && next_token->type != CPP_SCOPE
19393       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19394       && next_token->type != CPP_TEMPLATE_ID)
19395     return false;
19396
19397   /* Parse tentatively; we are going to roll back all of the tokens
19398      consumed here.  */
19399   cp_parser_parse_tentatively (parser);
19400   /* Assume that we are looking at a constructor declarator.  */
19401   constructor_p = true;
19402
19403   /* Look for the optional `::' operator.  */
19404   cp_parser_global_scope_opt (parser,
19405                               /*current_scope_valid_p=*/false);
19406   /* Look for the nested-name-specifier.  */
19407   nested_name_specifier
19408     = (cp_parser_nested_name_specifier_opt (parser,
19409                                             /*typename_keyword_p=*/false,
19410                                             /*check_dependency_p=*/false,
19411                                             /*type_p=*/false,
19412                                             /*is_declaration=*/false));
19413   /* Outside of a class-specifier, there must be a
19414      nested-name-specifier.  */
19415   if (!nested_name_specifier &&
19416       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19417        || friend_p))
19418     constructor_p = false;
19419   else if (nested_name_specifier == error_mark_node)
19420     constructor_p = false;
19421
19422   /* If we have a class scope, this is easy; DR 147 says that S::S always
19423      names the constructor, and no other qualified name could.  */
19424   if (constructor_p && nested_name_specifier
19425       && TYPE_P (nested_name_specifier))
19426     {
19427       tree id = cp_parser_unqualified_id (parser,
19428                                           /*template_keyword_p=*/false,
19429                                           /*check_dependency_p=*/false,
19430                                           /*declarator_p=*/true,
19431                                           /*optional_p=*/false);
19432       if (is_overloaded_fn (id))
19433         id = DECL_NAME (get_first_fn (id));
19434       if (!constructor_name_p (id, nested_name_specifier))
19435         constructor_p = false;
19436     }
19437   /* If we still think that this might be a constructor-declarator,
19438      look for a class-name.  */
19439   else if (constructor_p)
19440     {
19441       /* If we have:
19442
19443            template <typename T> struct S {
19444              S();
19445            };
19446
19447          we must recognize that the nested `S' names a class.  */
19448       tree type_decl;
19449       type_decl = cp_parser_class_name (parser,
19450                                         /*typename_keyword_p=*/false,
19451                                         /*template_keyword_p=*/false,
19452                                         none_type,
19453                                         /*check_dependency_p=*/false,
19454                                         /*class_head_p=*/false,
19455                                         /*is_declaration=*/false);
19456       /* If there was no class-name, then this is not a constructor.  */
19457       constructor_p = !cp_parser_error_occurred (parser);
19458
19459       /* If we're still considering a constructor, we have to see a `(',
19460          to begin the parameter-declaration-clause, followed by either a
19461          `)', an `...', or a decl-specifier.  We need to check for a
19462          type-specifier to avoid being fooled into thinking that:
19463
19464            S (f) (int);
19465
19466          is a constructor.  (It is actually a function named `f' that
19467          takes one parameter (of type `int') and returns a value of type
19468          `S'.  */
19469       if (constructor_p
19470           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19471         constructor_p = false;
19472
19473       if (constructor_p
19474           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19475           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19476           /* A parameter declaration begins with a decl-specifier,
19477              which is either the "attribute" keyword, a storage class
19478              specifier, or (usually) a type-specifier.  */
19479           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19480         {
19481           tree type;
19482           tree pushed_scope = NULL_TREE;
19483           unsigned saved_num_template_parameter_lists;
19484
19485           /* Names appearing in the type-specifier should be looked up
19486              in the scope of the class.  */
19487           if (current_class_type)
19488             type = NULL_TREE;
19489           else
19490             {
19491               type = TREE_TYPE (type_decl);
19492               if (TREE_CODE (type) == TYPENAME_TYPE)
19493                 {
19494                   type = resolve_typename_type (type,
19495                                                 /*only_current_p=*/false);
19496                   if (TREE_CODE (type) == TYPENAME_TYPE)
19497                     {
19498                       cp_parser_abort_tentative_parse (parser);
19499                       return false;
19500                     }
19501                 }
19502               pushed_scope = push_scope (type);
19503             }
19504
19505           /* Inside the constructor parameter list, surrounding
19506              template-parameter-lists do not apply.  */
19507           saved_num_template_parameter_lists
19508             = parser->num_template_parameter_lists;
19509           parser->num_template_parameter_lists = 0;
19510
19511           /* Look for the type-specifier.  */
19512           cp_parser_type_specifier (parser,
19513                                     CP_PARSER_FLAGS_NONE,
19514                                     /*decl_specs=*/NULL,
19515                                     /*is_declarator=*/true,
19516                                     /*declares_class_or_enum=*/NULL,
19517                                     /*is_cv_qualifier=*/NULL);
19518
19519           parser->num_template_parameter_lists
19520             = saved_num_template_parameter_lists;
19521
19522           /* Leave the scope of the class.  */
19523           if (pushed_scope)
19524             pop_scope (pushed_scope);
19525
19526           constructor_p = !cp_parser_error_occurred (parser);
19527         }
19528     }
19529
19530   /* We did not really want to consume any tokens.  */
19531   cp_parser_abort_tentative_parse (parser);
19532
19533   return constructor_p;
19534 }
19535
19536 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19537    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19538    they must be performed once we are in the scope of the function.
19539
19540    Returns the function defined.  */
19541
19542 static tree
19543 cp_parser_function_definition_from_specifiers_and_declarator
19544   (cp_parser* parser,
19545    cp_decl_specifier_seq *decl_specifiers,
19546    tree attributes,
19547    const cp_declarator *declarator)
19548 {
19549   tree fn;
19550   bool success_p;
19551
19552   /* Begin the function-definition.  */
19553   success_p = start_function (decl_specifiers, declarator, attributes);
19554
19555   /* The things we're about to see are not directly qualified by any
19556      template headers we've seen thus far.  */
19557   reset_specialization ();
19558
19559   /* If there were names looked up in the decl-specifier-seq that we
19560      did not check, check them now.  We must wait until we are in the
19561      scope of the function to perform the checks, since the function
19562      might be a friend.  */
19563   perform_deferred_access_checks ();
19564
19565   if (!success_p)
19566     {
19567       /* Skip the entire function.  */
19568       cp_parser_skip_to_end_of_block_or_statement (parser);
19569       fn = error_mark_node;
19570     }
19571   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19572     {
19573       /* Seen already, skip it.  An error message has already been output.  */
19574       cp_parser_skip_to_end_of_block_or_statement (parser);
19575       fn = current_function_decl;
19576       current_function_decl = NULL_TREE;
19577       /* If this is a function from a class, pop the nested class.  */
19578       if (current_class_name)
19579         pop_nested_class ();
19580     }
19581   else
19582     fn = cp_parser_function_definition_after_declarator (parser,
19583                                                          /*inline_p=*/false);
19584
19585   return fn;
19586 }
19587
19588 /* Parse the part of a function-definition that follows the
19589    declarator.  INLINE_P is TRUE iff this function is an inline
19590    function defined within a class-specifier.
19591
19592    Returns the function defined.  */
19593
19594 static tree
19595 cp_parser_function_definition_after_declarator (cp_parser* parser,
19596                                                 bool inline_p)
19597 {
19598   tree fn;
19599   bool ctor_initializer_p = false;
19600   bool saved_in_unbraced_linkage_specification_p;
19601   bool saved_in_function_body;
19602   unsigned saved_num_template_parameter_lists;
19603   cp_token *token;
19604
19605   saved_in_function_body = parser->in_function_body;
19606   parser->in_function_body = true;
19607   /* If the next token is `return', then the code may be trying to
19608      make use of the "named return value" extension that G++ used to
19609      support.  */
19610   token = cp_lexer_peek_token (parser->lexer);
19611   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19612     {
19613       /* Consume the `return' keyword.  */
19614       cp_lexer_consume_token (parser->lexer);
19615       /* Look for the identifier that indicates what value is to be
19616          returned.  */
19617       cp_parser_identifier (parser);
19618       /* Issue an error message.  */
19619       error_at (token->location,
19620                 "named return values are no longer supported");
19621       /* Skip tokens until we reach the start of the function body.  */
19622       while (true)
19623         {
19624           cp_token *token = cp_lexer_peek_token (parser->lexer);
19625           if (token->type == CPP_OPEN_BRACE
19626               || token->type == CPP_EOF
19627               || token->type == CPP_PRAGMA_EOL)
19628             break;
19629           cp_lexer_consume_token (parser->lexer);
19630         }
19631     }
19632   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19633      anything declared inside `f'.  */
19634   saved_in_unbraced_linkage_specification_p
19635     = parser->in_unbraced_linkage_specification_p;
19636   parser->in_unbraced_linkage_specification_p = false;
19637   /* Inside the function, surrounding template-parameter-lists do not
19638      apply.  */
19639   saved_num_template_parameter_lists
19640     = parser->num_template_parameter_lists;
19641   parser->num_template_parameter_lists = 0;
19642
19643   start_lambda_scope (current_function_decl);
19644
19645   /* If the next token is `try', then we are looking at a
19646      function-try-block.  */
19647   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19648     ctor_initializer_p = cp_parser_function_try_block (parser);
19649   /* A function-try-block includes the function-body, so we only do
19650      this next part if we're not processing a function-try-block.  */
19651   else
19652     ctor_initializer_p
19653       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19654
19655   finish_lambda_scope ();
19656
19657   /* Finish the function.  */
19658   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19659                         (inline_p ? 2 : 0));
19660   /* Generate code for it, if necessary.  */
19661   expand_or_defer_fn (fn);
19662   /* Restore the saved values.  */
19663   parser->in_unbraced_linkage_specification_p
19664     = saved_in_unbraced_linkage_specification_p;
19665   parser->num_template_parameter_lists
19666     = saved_num_template_parameter_lists;
19667   parser->in_function_body = saved_in_function_body;
19668
19669   return fn;
19670 }
19671
19672 /* Parse a template-declaration, assuming that the `export' (and
19673    `extern') keywords, if present, has already been scanned.  MEMBER_P
19674    is as for cp_parser_template_declaration.  */
19675
19676 static void
19677 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19678 {
19679   tree decl = NULL_TREE;
19680   VEC (deferred_access_check,gc) *checks;
19681   tree parameter_list;
19682   bool friend_p = false;
19683   bool need_lang_pop;
19684   cp_token *token;
19685
19686   /* Look for the `template' keyword.  */
19687   token = cp_lexer_peek_token (parser->lexer);
19688   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19689     return;
19690
19691   /* And the `<'.  */
19692   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19693     return;
19694   if (at_class_scope_p () && current_function_decl)
19695     {
19696       /* 14.5.2.2 [temp.mem]
19697
19698          A local class shall not have member templates.  */
19699       error_at (token->location,
19700                 "invalid declaration of member template in local class");
19701       cp_parser_skip_to_end_of_block_or_statement (parser);
19702       return;
19703     }
19704   /* [temp]
19705
19706      A template ... shall not have C linkage.  */
19707   if (current_lang_name == lang_name_c)
19708     {
19709       error_at (token->location, "template with C linkage");
19710       /* Give it C++ linkage to avoid confusing other parts of the
19711          front end.  */
19712       push_lang_context (lang_name_cplusplus);
19713       need_lang_pop = true;
19714     }
19715   else
19716     need_lang_pop = false;
19717
19718   /* We cannot perform access checks on the template parameter
19719      declarations until we know what is being declared, just as we
19720      cannot check the decl-specifier list.  */
19721   push_deferring_access_checks (dk_deferred);
19722
19723   /* If the next token is `>', then we have an invalid
19724      specialization.  Rather than complain about an invalid template
19725      parameter, issue an error message here.  */
19726   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19727     {
19728       cp_parser_error (parser, "invalid explicit specialization");
19729       begin_specialization ();
19730       parameter_list = NULL_TREE;
19731     }
19732   else
19733     /* Parse the template parameters.  */
19734     parameter_list = cp_parser_template_parameter_list (parser);
19735
19736   /* Get the deferred access checks from the parameter list.  These
19737      will be checked once we know what is being declared, as for a
19738      member template the checks must be performed in the scope of the
19739      class containing the member.  */
19740   checks = get_deferred_access_checks ();
19741
19742   /* Look for the `>'.  */
19743   cp_parser_skip_to_end_of_template_parameter_list (parser);
19744   /* We just processed one more parameter list.  */
19745   ++parser->num_template_parameter_lists;
19746   /* If the next token is `template', there are more template
19747      parameters.  */
19748   if (cp_lexer_next_token_is_keyword (parser->lexer,
19749                                       RID_TEMPLATE))
19750     cp_parser_template_declaration_after_export (parser, member_p);
19751   else
19752     {
19753       /* There are no access checks when parsing a template, as we do not
19754          know if a specialization will be a friend.  */
19755       push_deferring_access_checks (dk_no_check);
19756       token = cp_lexer_peek_token (parser->lexer);
19757       decl = cp_parser_single_declaration (parser,
19758                                            checks,
19759                                            member_p,
19760                                            /*explicit_specialization_p=*/false,
19761                                            &friend_p);
19762       pop_deferring_access_checks ();
19763
19764       /* If this is a member template declaration, let the front
19765          end know.  */
19766       if (member_p && !friend_p && decl)
19767         {
19768           if (TREE_CODE (decl) == TYPE_DECL)
19769             cp_parser_check_access_in_redeclaration (decl, token->location);
19770
19771           decl = finish_member_template_decl (decl);
19772         }
19773       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19774         make_friend_class (current_class_type, TREE_TYPE (decl),
19775                            /*complain=*/true);
19776     }
19777   /* We are done with the current parameter list.  */
19778   --parser->num_template_parameter_lists;
19779
19780   pop_deferring_access_checks ();
19781
19782   /* Finish up.  */
19783   finish_template_decl (parameter_list);
19784
19785   /* Register member declarations.  */
19786   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19787     finish_member_declaration (decl);
19788   /* For the erroneous case of a template with C linkage, we pushed an
19789      implicit C++ linkage scope; exit that scope now.  */
19790   if (need_lang_pop)
19791     pop_lang_context ();
19792   /* If DECL is a function template, we must return to parse it later.
19793      (Even though there is no definition, there might be default
19794      arguments that need handling.)  */
19795   if (member_p && decl
19796       && (TREE_CODE (decl) == FUNCTION_DECL
19797           || DECL_FUNCTION_TEMPLATE_P (decl)))
19798     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19799 }
19800
19801 /* Perform the deferred access checks from a template-parameter-list.
19802    CHECKS is a TREE_LIST of access checks, as returned by
19803    get_deferred_access_checks.  */
19804
19805 static void
19806 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19807 {
19808   ++processing_template_parmlist;
19809   perform_access_checks (checks);
19810   --processing_template_parmlist;
19811 }
19812
19813 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19814    `function-definition' sequence.  MEMBER_P is true, this declaration
19815    appears in a class scope.
19816
19817    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19818    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19819
19820 static tree
19821 cp_parser_single_declaration (cp_parser* parser,
19822                               VEC (deferred_access_check,gc)* checks,
19823                               bool member_p,
19824                               bool explicit_specialization_p,
19825                               bool* friend_p)
19826 {
19827   int declares_class_or_enum;
19828   tree decl = NULL_TREE;
19829   cp_decl_specifier_seq decl_specifiers;
19830   bool function_definition_p = false;
19831   cp_token *decl_spec_token_start;
19832
19833   /* This function is only used when processing a template
19834      declaration.  */
19835   gcc_assert (innermost_scope_kind () == sk_template_parms
19836               || innermost_scope_kind () == sk_template_spec);
19837
19838   /* Defer access checks until we know what is being declared.  */
19839   push_deferring_access_checks (dk_deferred);
19840
19841   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19842      alternative.  */
19843   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19844   cp_parser_decl_specifier_seq (parser,
19845                                 CP_PARSER_FLAGS_OPTIONAL,
19846                                 &decl_specifiers,
19847                                 &declares_class_or_enum);
19848   if (friend_p)
19849     *friend_p = cp_parser_friend_p (&decl_specifiers);
19850
19851   /* There are no template typedefs.  */
19852   if (decl_specifiers.specs[(int) ds_typedef])
19853     {
19854       error_at (decl_spec_token_start->location,
19855                 "template declaration of %<typedef%>");
19856       decl = error_mark_node;
19857     }
19858
19859   /* Gather up the access checks that occurred the
19860      decl-specifier-seq.  */
19861   stop_deferring_access_checks ();
19862
19863   /* Check for the declaration of a template class.  */
19864   if (declares_class_or_enum)
19865     {
19866       if (cp_parser_declares_only_class_p (parser))
19867         {
19868           decl = shadow_tag (&decl_specifiers);
19869
19870           /* In this case:
19871
19872                struct C {
19873                  friend template <typename T> struct A<T>::B;
19874                };
19875
19876              A<T>::B will be represented by a TYPENAME_TYPE, and
19877              therefore not recognized by shadow_tag.  */
19878           if (friend_p && *friend_p
19879               && !decl
19880               && decl_specifiers.type
19881               && TYPE_P (decl_specifiers.type))
19882             decl = decl_specifiers.type;
19883
19884           if (decl && decl != error_mark_node)
19885             decl = TYPE_NAME (decl);
19886           else
19887             decl = error_mark_node;
19888
19889           /* Perform access checks for template parameters.  */
19890           cp_parser_perform_template_parameter_access_checks (checks);
19891         }
19892     }
19893
19894   /* Complain about missing 'typename' or other invalid type names.  */
19895   if (!decl_specifiers.any_type_specifiers_p)
19896     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19897
19898   /* If it's not a template class, try for a template function.  If
19899      the next token is a `;', then this declaration does not declare
19900      anything.  But, if there were errors in the decl-specifiers, then
19901      the error might well have come from an attempted class-specifier.
19902      In that case, there's no need to warn about a missing declarator.  */
19903   if (!decl
19904       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19905           || decl_specifiers.type != error_mark_node))
19906     {
19907       decl = cp_parser_init_declarator (parser,
19908                                         &decl_specifiers,
19909                                         checks,
19910                                         /*function_definition_allowed_p=*/true,
19911                                         member_p,
19912                                         declares_class_or_enum,
19913                                         &function_definition_p);
19914
19915     /* 7.1.1-1 [dcl.stc]
19916
19917        A storage-class-specifier shall not be specified in an explicit
19918        specialization...  */
19919     if (decl
19920         && explicit_specialization_p
19921         && decl_specifiers.storage_class != sc_none)
19922       {
19923         error_at (decl_spec_token_start->location,
19924                   "explicit template specialization cannot have a storage class");
19925         decl = error_mark_node;
19926       }
19927     }
19928
19929   pop_deferring_access_checks ();
19930
19931   /* Clear any current qualification; whatever comes next is the start
19932      of something new.  */
19933   parser->scope = NULL_TREE;
19934   parser->qualifying_scope = NULL_TREE;
19935   parser->object_scope = NULL_TREE;
19936   /* Look for a trailing `;' after the declaration.  */
19937   if (!function_definition_p
19938       && (decl == error_mark_node
19939           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19940     cp_parser_skip_to_end_of_block_or_statement (parser);
19941
19942   return decl;
19943 }
19944
19945 /* Parse a cast-expression that is not the operand of a unary "&".  */
19946
19947 static tree
19948 cp_parser_simple_cast_expression (cp_parser *parser)
19949 {
19950   return cp_parser_cast_expression (parser, /*address_p=*/false,
19951                                     /*cast_p=*/false, NULL);
19952 }
19953
19954 /* Parse a functional cast to TYPE.  Returns an expression
19955    representing the cast.  */
19956
19957 static tree
19958 cp_parser_functional_cast (cp_parser* parser, tree type)
19959 {
19960   VEC(tree,gc) *vec;
19961   tree expression_list;
19962   tree cast;
19963   bool nonconst_p;
19964
19965   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19966     {
19967       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19968       expression_list = cp_parser_braced_list (parser, &nonconst_p);
19969       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19970       if (TREE_CODE (type) == TYPE_DECL)
19971         type = TREE_TYPE (type);
19972       return finish_compound_literal (type, expression_list);
19973     }
19974
19975
19976   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19977                                                  /*cast_p=*/true,
19978                                                  /*allow_expansion_p=*/true,
19979                                                  /*non_constant_p=*/NULL);
19980   if (vec == NULL)
19981     expression_list = error_mark_node;
19982   else
19983     {
19984       expression_list = build_tree_list_vec (vec);
19985       release_tree_vector (vec);
19986     }
19987
19988   cast = build_functional_cast (type, expression_list,
19989                                 tf_warning_or_error);
19990   /* [expr.const]/1: In an integral constant expression "only type
19991      conversions to integral or enumeration type can be used".  */
19992   if (TREE_CODE (type) == TYPE_DECL)
19993     type = TREE_TYPE (type);
19994   if (cast != error_mark_node
19995       && !cast_valid_in_integral_constant_expression_p (type)
19996       && cp_parser_non_integral_constant_expression (parser,
19997                                                      NIC_CONSTRUCTOR))
19998     return error_mark_node;
19999   return cast;
20000 }
20001
20002 /* Save the tokens that make up the body of a member function defined
20003    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20004    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20005    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20006    for the member function.  */
20007
20008 static tree
20009 cp_parser_save_member_function_body (cp_parser* parser,
20010                                      cp_decl_specifier_seq *decl_specifiers,
20011                                      cp_declarator *declarator,
20012                                      tree attributes)
20013 {
20014   cp_token *first;
20015   cp_token *last;
20016   tree fn;
20017
20018   /* Create the FUNCTION_DECL.  */
20019   fn = grokmethod (decl_specifiers, declarator, attributes);
20020   /* If something went badly wrong, bail out now.  */
20021   if (fn == error_mark_node)
20022     {
20023       /* If there's a function-body, skip it.  */
20024       if (cp_parser_token_starts_function_definition_p
20025           (cp_lexer_peek_token (parser->lexer)))
20026         cp_parser_skip_to_end_of_block_or_statement (parser);
20027       return error_mark_node;
20028     }
20029
20030   /* Remember it, if there default args to post process.  */
20031   cp_parser_save_default_args (parser, fn);
20032
20033   /* Save away the tokens that make up the body of the
20034      function.  */
20035   first = parser->lexer->next_token;
20036   /* We can have braced-init-list mem-initializers before the fn body.  */
20037   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20038     {
20039       cp_lexer_consume_token (parser->lexer);
20040       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20041              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20042         {
20043           /* cache_group will stop after an un-nested { } pair, too.  */
20044           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20045             break;
20046
20047           /* variadic mem-inits have ... after the ')'.  */
20048           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20049             cp_lexer_consume_token (parser->lexer);
20050         }
20051     }
20052   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20053   /* Handle function try blocks.  */
20054   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20055     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20056   last = parser->lexer->next_token;
20057
20058   /* Save away the inline definition; we will process it when the
20059      class is complete.  */
20060   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20061   DECL_PENDING_INLINE_P (fn) = 1;
20062
20063   /* We need to know that this was defined in the class, so that
20064      friend templates are handled correctly.  */
20065   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20066
20067   /* Add FN to the queue of functions to be parsed later.  */
20068   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20069
20070   return fn;
20071 }
20072
20073 /* Parse a template-argument-list, as well as the trailing ">" (but
20074    not the opening ">").  See cp_parser_template_argument_list for the
20075    return value.  */
20076
20077 static tree
20078 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20079 {
20080   tree arguments;
20081   tree saved_scope;
20082   tree saved_qualifying_scope;
20083   tree saved_object_scope;
20084   bool saved_greater_than_is_operator_p;
20085   int saved_unevaluated_operand;
20086   int saved_inhibit_evaluation_warnings;
20087
20088   /* [temp.names]
20089
20090      When parsing a template-id, the first non-nested `>' is taken as
20091      the end of the template-argument-list rather than a greater-than
20092      operator.  */
20093   saved_greater_than_is_operator_p
20094     = parser->greater_than_is_operator_p;
20095   parser->greater_than_is_operator_p = false;
20096   /* Parsing the argument list may modify SCOPE, so we save it
20097      here.  */
20098   saved_scope = parser->scope;
20099   saved_qualifying_scope = parser->qualifying_scope;
20100   saved_object_scope = parser->object_scope;
20101   /* We need to evaluate the template arguments, even though this
20102      template-id may be nested within a "sizeof".  */
20103   saved_unevaluated_operand = cp_unevaluated_operand;
20104   cp_unevaluated_operand = 0;
20105   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20106   c_inhibit_evaluation_warnings = 0;
20107   /* Parse the template-argument-list itself.  */
20108   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20109       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20110     arguments = NULL_TREE;
20111   else
20112     arguments = cp_parser_template_argument_list (parser);
20113   /* Look for the `>' that ends the template-argument-list. If we find
20114      a '>>' instead, it's probably just a typo.  */
20115   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20116     {
20117       if (cxx_dialect != cxx98)
20118         {
20119           /* In C++0x, a `>>' in a template argument list or cast
20120              expression is considered to be two separate `>'
20121              tokens. So, change the current token to a `>', but don't
20122              consume it: it will be consumed later when the outer
20123              template argument list (or cast expression) is parsed.
20124              Note that this replacement of `>' for `>>' is necessary
20125              even if we are parsing tentatively: in the tentative
20126              case, after calling
20127              cp_parser_enclosed_template_argument_list we will always
20128              throw away all of the template arguments and the first
20129              closing `>', either because the template argument list
20130              was erroneous or because we are replacing those tokens
20131              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20132              not have been thrown away) is needed either to close an
20133              outer template argument list or to complete a new-style
20134              cast.  */
20135           cp_token *token = cp_lexer_peek_token (parser->lexer);
20136           token->type = CPP_GREATER;
20137         }
20138       else if (!saved_greater_than_is_operator_p)
20139         {
20140           /* If we're in a nested template argument list, the '>>' has
20141             to be a typo for '> >'. We emit the error message, but we
20142             continue parsing and we push a '>' as next token, so that
20143             the argument list will be parsed correctly.  Note that the
20144             global source location is still on the token before the
20145             '>>', so we need to say explicitly where we want it.  */
20146           cp_token *token = cp_lexer_peek_token (parser->lexer);
20147           error_at (token->location, "%<>>%> should be %<> >%> "
20148                     "within a nested template argument list");
20149
20150           token->type = CPP_GREATER;
20151         }
20152       else
20153         {
20154           /* If this is not a nested template argument list, the '>>'
20155             is a typo for '>'. Emit an error message and continue.
20156             Same deal about the token location, but here we can get it
20157             right by consuming the '>>' before issuing the diagnostic.  */
20158           cp_token *token = cp_lexer_consume_token (parser->lexer);
20159           error_at (token->location,
20160                     "spurious %<>>%>, use %<>%> to terminate "
20161                     "a template argument list");
20162         }
20163     }
20164   else
20165     cp_parser_skip_to_end_of_template_parameter_list (parser);
20166   /* The `>' token might be a greater-than operator again now.  */
20167   parser->greater_than_is_operator_p
20168     = saved_greater_than_is_operator_p;
20169   /* Restore the SAVED_SCOPE.  */
20170   parser->scope = saved_scope;
20171   parser->qualifying_scope = saved_qualifying_scope;
20172   parser->object_scope = saved_object_scope;
20173   cp_unevaluated_operand = saved_unevaluated_operand;
20174   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20175
20176   return arguments;
20177 }
20178
20179 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20180    arguments, or the body of the function have not yet been parsed,
20181    parse them now.  */
20182
20183 static void
20184 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20185 {
20186   /* If this member is a template, get the underlying
20187      FUNCTION_DECL.  */
20188   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20189     member_function = DECL_TEMPLATE_RESULT (member_function);
20190
20191   /* There should not be any class definitions in progress at this
20192      point; the bodies of members are only parsed outside of all class
20193      definitions.  */
20194   gcc_assert (parser->num_classes_being_defined == 0);
20195   /* While we're parsing the member functions we might encounter more
20196      classes.  We want to handle them right away, but we don't want
20197      them getting mixed up with functions that are currently in the
20198      queue.  */
20199   push_unparsed_function_queues (parser);
20200
20201   /* Make sure that any template parameters are in scope.  */
20202   maybe_begin_member_template_processing (member_function);
20203
20204   /* If the body of the function has not yet been parsed, parse it
20205      now.  */
20206   if (DECL_PENDING_INLINE_P (member_function))
20207     {
20208       tree function_scope;
20209       cp_token_cache *tokens;
20210
20211       /* The function is no longer pending; we are processing it.  */
20212       tokens = DECL_PENDING_INLINE_INFO (member_function);
20213       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20214       DECL_PENDING_INLINE_P (member_function) = 0;
20215
20216       /* If this is a local class, enter the scope of the containing
20217          function.  */
20218       function_scope = current_function_decl;
20219       if (function_scope)
20220         push_function_context ();
20221
20222       /* Push the body of the function onto the lexer stack.  */
20223       cp_parser_push_lexer_for_tokens (parser, tokens);
20224
20225       /* Let the front end know that we going to be defining this
20226          function.  */
20227       start_preparsed_function (member_function, NULL_TREE,
20228                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20229
20230       /* Don't do access checking if it is a templated function.  */
20231       if (processing_template_decl)
20232         push_deferring_access_checks (dk_no_check);
20233
20234       /* Now, parse the body of the function.  */
20235       cp_parser_function_definition_after_declarator (parser,
20236                                                       /*inline_p=*/true);
20237
20238       if (processing_template_decl)
20239         pop_deferring_access_checks ();
20240
20241       /* Leave the scope of the containing function.  */
20242       if (function_scope)
20243         pop_function_context ();
20244       cp_parser_pop_lexer (parser);
20245     }
20246
20247   /* Remove any template parameters from the symbol table.  */
20248   maybe_end_member_template_processing ();
20249
20250   /* Restore the queue.  */
20251   pop_unparsed_function_queues (parser);
20252 }
20253
20254 /* If DECL contains any default args, remember it on the unparsed
20255    functions queue.  */
20256
20257 static void
20258 cp_parser_save_default_args (cp_parser* parser, tree decl)
20259 {
20260   tree probe;
20261
20262   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20263        probe;
20264        probe = TREE_CHAIN (probe))
20265     if (TREE_PURPOSE (probe))
20266       {
20267         cp_default_arg_entry *entry
20268           = VEC_safe_push (cp_default_arg_entry, gc,
20269                            unparsed_funs_with_default_args, NULL);
20270         entry->class_type = current_class_type;
20271         entry->decl = decl;
20272         break;
20273       }
20274 }
20275
20276 /* FN is a FUNCTION_DECL which may contains a parameter with an
20277    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20278    assumes that the current scope is the scope in which the default
20279    argument should be processed.  */
20280
20281 static void
20282 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20283 {
20284   bool saved_local_variables_forbidden_p;
20285   tree parm, parmdecl;
20286
20287   /* While we're parsing the default args, we might (due to the
20288      statement expression extension) encounter more classes.  We want
20289      to handle them right away, but we don't want them getting mixed
20290      up with default args that are currently in the queue.  */
20291   push_unparsed_function_queues (parser);
20292
20293   /* Local variable names (and the `this' keyword) may not appear
20294      in a default argument.  */
20295   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20296   parser->local_variables_forbidden_p = true;
20297
20298   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20299          parmdecl = DECL_ARGUMENTS (fn);
20300        parm && parm != void_list_node;
20301        parm = TREE_CHAIN (parm),
20302          parmdecl = DECL_CHAIN (parmdecl))
20303     {
20304       cp_token_cache *tokens;
20305       tree default_arg = TREE_PURPOSE (parm);
20306       tree parsed_arg;
20307       VEC(tree,gc) *insts;
20308       tree copy;
20309       unsigned ix;
20310
20311       if (!default_arg)
20312         continue;
20313
20314       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20315         /* This can happen for a friend declaration for a function
20316            already declared with default arguments.  */
20317         continue;
20318
20319        /* Push the saved tokens for the default argument onto the parser's
20320           lexer stack.  */
20321       tokens = DEFARG_TOKENS (default_arg);
20322       cp_parser_push_lexer_for_tokens (parser, tokens);
20323
20324       start_lambda_scope (parmdecl);
20325
20326       /* Parse the assignment-expression.  */
20327       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20328       if (parsed_arg == error_mark_node)
20329         {
20330           cp_parser_pop_lexer (parser);
20331           continue;
20332         }
20333
20334       if (!processing_template_decl)
20335         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20336
20337       TREE_PURPOSE (parm) = parsed_arg;
20338
20339       /* Update any instantiations we've already created.  */
20340       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20341            VEC_iterate (tree, insts, ix, copy); ix++)
20342         TREE_PURPOSE (copy) = parsed_arg;
20343
20344       finish_lambda_scope ();
20345
20346       /* If the token stream has not been completely used up, then
20347          there was extra junk after the end of the default
20348          argument.  */
20349       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20350         cp_parser_error (parser, "expected %<,%>");
20351
20352       /* Revert to the main lexer.  */
20353       cp_parser_pop_lexer (parser);
20354     }
20355
20356   /* Make sure no default arg is missing.  */
20357   check_default_args (fn);
20358
20359   /* Restore the state of local_variables_forbidden_p.  */
20360   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20361
20362   /* Restore the queue.  */
20363   pop_unparsed_function_queues (parser);
20364 }
20365
20366 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20367    either a TYPE or an expression, depending on the form of the
20368    input.  The KEYWORD indicates which kind of expression we have
20369    encountered.  */
20370
20371 static tree
20372 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20373 {
20374   tree expr = NULL_TREE;
20375   const char *saved_message;
20376   char *tmp;
20377   bool saved_integral_constant_expression_p;
20378   bool saved_non_integral_constant_expression_p;
20379   bool pack_expansion_p = false;
20380
20381   /* Types cannot be defined in a `sizeof' expression.  Save away the
20382      old message.  */
20383   saved_message = parser->type_definition_forbidden_message;
20384   /* And create the new one.  */
20385   tmp = concat ("types may not be defined in %<",
20386                 IDENTIFIER_POINTER (ridpointers[keyword]),
20387                 "%> expressions", NULL);
20388   parser->type_definition_forbidden_message = tmp;
20389
20390   /* The restrictions on constant-expressions do not apply inside
20391      sizeof expressions.  */
20392   saved_integral_constant_expression_p
20393     = parser->integral_constant_expression_p;
20394   saved_non_integral_constant_expression_p
20395     = parser->non_integral_constant_expression_p;
20396   parser->integral_constant_expression_p = false;
20397
20398   /* If it's a `...', then we are computing the length of a parameter
20399      pack.  */
20400   if (keyword == RID_SIZEOF
20401       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20402     {
20403       /* Consume the `...'.  */
20404       cp_lexer_consume_token (parser->lexer);
20405       maybe_warn_variadic_templates ();
20406
20407       /* Note that this is an expansion.  */
20408       pack_expansion_p = true;
20409     }
20410
20411   /* Do not actually evaluate the expression.  */
20412   ++cp_unevaluated_operand;
20413   ++c_inhibit_evaluation_warnings;
20414   /* If it's a `(', then we might be looking at the type-id
20415      construction.  */
20416   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20417     {
20418       tree type;
20419       bool saved_in_type_id_in_expr_p;
20420
20421       /* We can't be sure yet whether we're looking at a type-id or an
20422          expression.  */
20423       cp_parser_parse_tentatively (parser);
20424       /* Consume the `('.  */
20425       cp_lexer_consume_token (parser->lexer);
20426       /* Parse the type-id.  */
20427       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20428       parser->in_type_id_in_expr_p = true;
20429       type = cp_parser_type_id (parser);
20430       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20431       /* Now, look for the trailing `)'.  */
20432       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20433       /* If all went well, then we're done.  */
20434       if (cp_parser_parse_definitely (parser))
20435         {
20436           cp_decl_specifier_seq decl_specs;
20437
20438           /* Build a trivial decl-specifier-seq.  */
20439           clear_decl_specs (&decl_specs);
20440           decl_specs.type = type;
20441
20442           /* Call grokdeclarator to figure out what type this is.  */
20443           expr = grokdeclarator (NULL,
20444                                  &decl_specs,
20445                                  TYPENAME,
20446                                  /*initialized=*/0,
20447                                  /*attrlist=*/NULL);
20448         }
20449     }
20450
20451   /* If the type-id production did not work out, then we must be
20452      looking at the unary-expression production.  */
20453   if (!expr)
20454     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20455                                        /*cast_p=*/false, NULL);
20456
20457   if (pack_expansion_p)
20458     /* Build a pack expansion. */
20459     expr = make_pack_expansion (expr);
20460
20461   /* Go back to evaluating expressions.  */
20462   --cp_unevaluated_operand;
20463   --c_inhibit_evaluation_warnings;
20464
20465   /* Free the message we created.  */
20466   free (tmp);
20467   /* And restore the old one.  */
20468   parser->type_definition_forbidden_message = saved_message;
20469   parser->integral_constant_expression_p
20470     = saved_integral_constant_expression_p;
20471   parser->non_integral_constant_expression_p
20472     = saved_non_integral_constant_expression_p;
20473
20474   return expr;
20475 }
20476
20477 /* If the current declaration has no declarator, return true.  */
20478
20479 static bool
20480 cp_parser_declares_only_class_p (cp_parser *parser)
20481 {
20482   /* If the next token is a `;' or a `,' then there is no
20483      declarator.  */
20484   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20485           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20486 }
20487
20488 /* Update the DECL_SPECS to reflect the storage class indicated by
20489    KEYWORD.  */
20490
20491 static void
20492 cp_parser_set_storage_class (cp_parser *parser,
20493                              cp_decl_specifier_seq *decl_specs,
20494                              enum rid keyword,
20495                              location_t location)
20496 {
20497   cp_storage_class storage_class;
20498
20499   if (parser->in_unbraced_linkage_specification_p)
20500     {
20501       error_at (location, "invalid use of %qD in linkage specification",
20502                 ridpointers[keyword]);
20503       return;
20504     }
20505   else if (decl_specs->storage_class != sc_none)
20506     {
20507       decl_specs->conflicting_specifiers_p = true;
20508       return;
20509     }
20510
20511   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20512       && decl_specs->specs[(int) ds_thread])
20513     {
20514       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20515       decl_specs->specs[(int) ds_thread] = 0;
20516     }
20517
20518   switch (keyword)
20519     {
20520     case RID_AUTO:
20521       storage_class = sc_auto;
20522       break;
20523     case RID_REGISTER:
20524       storage_class = sc_register;
20525       break;
20526     case RID_STATIC:
20527       storage_class = sc_static;
20528       break;
20529     case RID_EXTERN:
20530       storage_class = sc_extern;
20531       break;
20532     case RID_MUTABLE:
20533       storage_class = sc_mutable;
20534       break;
20535     default:
20536       gcc_unreachable ();
20537     }
20538   decl_specs->storage_class = storage_class;
20539
20540   /* A storage class specifier cannot be applied alongside a typedef 
20541      specifier. If there is a typedef specifier present then set 
20542      conflicting_specifiers_p which will trigger an error later
20543      on in grokdeclarator. */
20544   if (decl_specs->specs[(int)ds_typedef])
20545     decl_specs->conflicting_specifiers_p = true;
20546 }
20547
20548 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20549    is true, the type is a user-defined type; otherwise it is a
20550    built-in type specified by a keyword.  */
20551
20552 static void
20553 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20554                               tree type_spec,
20555                               location_t location,
20556                               bool user_defined_p)
20557 {
20558   decl_specs->any_specifiers_p = true;
20559
20560   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20561      (with, for example, in "typedef int wchar_t;") we remember that
20562      this is what happened.  In system headers, we ignore these
20563      declarations so that G++ can work with system headers that are not
20564      C++-safe.  */
20565   if (decl_specs->specs[(int) ds_typedef]
20566       && !user_defined_p
20567       && (type_spec == boolean_type_node
20568           || type_spec == char16_type_node
20569           || type_spec == char32_type_node
20570           || type_spec == wchar_type_node)
20571       && (decl_specs->type
20572           || decl_specs->specs[(int) ds_long]
20573           || decl_specs->specs[(int) ds_short]
20574           || decl_specs->specs[(int) ds_unsigned]
20575           || decl_specs->specs[(int) ds_signed]))
20576     {
20577       decl_specs->redefined_builtin_type = type_spec;
20578       if (!decl_specs->type)
20579         {
20580           decl_specs->type = type_spec;
20581           decl_specs->user_defined_type_p = false;
20582           decl_specs->type_location = location;
20583         }
20584     }
20585   else if (decl_specs->type)
20586     decl_specs->multiple_types_p = true;
20587   else
20588     {
20589       decl_specs->type = type_spec;
20590       decl_specs->user_defined_type_p = user_defined_p;
20591       decl_specs->redefined_builtin_type = NULL_TREE;
20592       decl_specs->type_location = location;
20593     }
20594 }
20595
20596 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20597    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20598
20599 static bool
20600 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20601 {
20602   return decl_specifiers->specs[(int) ds_friend] != 0;
20603 }
20604
20605 /* Issue an error message indicating that TOKEN_DESC was expected.
20606    If KEYWORD is true, it indicated this function is called by
20607    cp_parser_require_keword and the required token can only be
20608    a indicated keyword. */
20609
20610 static void
20611 cp_parser_required_error (cp_parser *parser,
20612                           required_token token_desc,
20613                           bool keyword)
20614 {
20615   switch (token_desc)
20616     {
20617       case RT_NEW:
20618         cp_parser_error (parser, "expected %<new%>");
20619         return;
20620       case RT_DELETE:
20621         cp_parser_error (parser, "expected %<delete%>");
20622         return;
20623       case RT_RETURN:
20624         cp_parser_error (parser, "expected %<return%>");
20625         return;
20626       case RT_WHILE:
20627         cp_parser_error (parser, "expected %<while%>");
20628         return;
20629       case RT_EXTERN:
20630         cp_parser_error (parser, "expected %<extern%>");
20631         return;
20632       case RT_STATIC_ASSERT:
20633         cp_parser_error (parser, "expected %<static_assert%>");
20634         return;
20635       case RT_DECLTYPE:
20636         cp_parser_error (parser, "expected %<decltype%>");
20637         return;
20638       case RT_OPERATOR:
20639         cp_parser_error (parser, "expected %<operator%>");
20640         return;
20641       case RT_CLASS:
20642         cp_parser_error (parser, "expected %<class%>");
20643         return;
20644       case RT_TEMPLATE:
20645         cp_parser_error (parser, "expected %<template%>");
20646         return;
20647       case RT_NAMESPACE:
20648         cp_parser_error (parser, "expected %<namespace%>");
20649         return;
20650       case RT_USING:
20651         cp_parser_error (parser, "expected %<using%>");
20652         return;
20653       case RT_ASM:
20654         cp_parser_error (parser, "expected %<asm%>");
20655         return;
20656       case RT_TRY:
20657         cp_parser_error (parser, "expected %<try%>");
20658         return;
20659       case RT_CATCH:
20660         cp_parser_error (parser, "expected %<catch%>");
20661         return;
20662       case RT_THROW:
20663         cp_parser_error (parser, "expected %<throw%>");
20664         return;
20665       case RT_LABEL:
20666         cp_parser_error (parser, "expected %<__label__%>");
20667         return;
20668       case RT_AT_TRY:
20669         cp_parser_error (parser, "expected %<@try%>");
20670         return;
20671       case RT_AT_SYNCHRONIZED:
20672         cp_parser_error (parser, "expected %<@synchronized%>");
20673         return;
20674       case RT_AT_THROW:
20675         cp_parser_error (parser, "expected %<@throw%>");
20676         return;
20677       default:
20678         break;
20679     }
20680   if (!keyword)
20681     {
20682       switch (token_desc)
20683         {
20684           case RT_SEMICOLON:
20685             cp_parser_error (parser, "expected %<;%>");
20686             return;
20687           case RT_OPEN_PAREN:
20688             cp_parser_error (parser, "expected %<(%>");
20689             return;
20690           case RT_CLOSE_BRACE:
20691             cp_parser_error (parser, "expected %<}%>");
20692             return;
20693           case RT_OPEN_BRACE:
20694             cp_parser_error (parser, "expected %<{%>");
20695             return;
20696           case RT_CLOSE_SQUARE:
20697             cp_parser_error (parser, "expected %<]%>");
20698             return;
20699           case RT_OPEN_SQUARE:
20700             cp_parser_error (parser, "expected %<[%>");
20701             return;
20702           case RT_COMMA:
20703             cp_parser_error (parser, "expected %<,%>");
20704             return;
20705           case RT_SCOPE:
20706             cp_parser_error (parser, "expected %<::%>");
20707             return;
20708           case RT_LESS:
20709             cp_parser_error (parser, "expected %<<%>");
20710             return;
20711           case RT_GREATER:
20712             cp_parser_error (parser, "expected %<>%>");
20713             return;
20714           case RT_EQ:
20715             cp_parser_error (parser, "expected %<=%>");
20716             return;
20717           case RT_ELLIPSIS:
20718             cp_parser_error (parser, "expected %<...%>");
20719             return;
20720           case RT_MULT:
20721             cp_parser_error (parser, "expected %<*%>");
20722             return;
20723           case RT_COMPL:
20724             cp_parser_error (parser, "expected %<~%>");
20725             return;
20726           case RT_COLON:
20727             cp_parser_error (parser, "expected %<:%>");
20728             return;
20729           case RT_COLON_SCOPE:
20730             cp_parser_error (parser, "expected %<:%> or %<::%>");
20731             return;
20732           case RT_CLOSE_PAREN:
20733             cp_parser_error (parser, "expected %<)%>");
20734             return;
20735           case RT_COMMA_CLOSE_PAREN:
20736             cp_parser_error (parser, "expected %<,%> or %<)%>");
20737             return;
20738           case RT_PRAGMA_EOL:
20739             cp_parser_error (parser, "expected end of line");
20740             return;
20741           case RT_NAME:
20742             cp_parser_error (parser, "expected identifier");
20743             return;
20744           case RT_SELECT:
20745             cp_parser_error (parser, "expected selection-statement");
20746             return;
20747           case RT_INTERATION:
20748             cp_parser_error (parser, "expected iteration-statement");
20749             return;
20750           case RT_JUMP:
20751             cp_parser_error (parser, "expected jump-statement");
20752             return;
20753           case RT_CLASS_KEY:
20754             cp_parser_error (parser, "expected class-key");
20755             return;
20756           case RT_CLASS_TYPENAME_TEMPLATE:
20757             cp_parser_error (parser,
20758                  "expected %<class%>, %<typename%>, or %<template%>");
20759             return;
20760           default:
20761             gcc_unreachable ();
20762         }
20763     }
20764   else
20765     gcc_unreachable ();
20766 }
20767
20768
20769
20770 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20771    issue an error message indicating that TOKEN_DESC was expected.
20772
20773    Returns the token consumed, if the token had the appropriate type.
20774    Otherwise, returns NULL.  */
20775
20776 static cp_token *
20777 cp_parser_require (cp_parser* parser,
20778                    enum cpp_ttype type,
20779                    required_token token_desc)
20780 {
20781   if (cp_lexer_next_token_is (parser->lexer, type))
20782     return cp_lexer_consume_token (parser->lexer);
20783   else
20784     {
20785       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20786       if (!cp_parser_simulate_error (parser))
20787         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20788       return NULL;
20789     }
20790 }
20791
20792 /* An error message is produced if the next token is not '>'.
20793    All further tokens are skipped until the desired token is
20794    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20795
20796 static void
20797 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20798 {
20799   /* Current level of '< ... >'.  */
20800   unsigned level = 0;
20801   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20802   unsigned nesting_depth = 0;
20803
20804   /* Are we ready, yet?  If not, issue error message.  */
20805   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20806     return;
20807
20808   /* Skip tokens until the desired token is found.  */
20809   while (true)
20810     {
20811       /* Peek at the next token.  */
20812       switch (cp_lexer_peek_token (parser->lexer)->type)
20813         {
20814         case CPP_LESS:
20815           if (!nesting_depth)
20816             ++level;
20817           break;
20818
20819         case CPP_RSHIFT:
20820           if (cxx_dialect == cxx98)
20821             /* C++0x views the `>>' operator as two `>' tokens, but
20822                C++98 does not. */
20823             break;
20824           else if (!nesting_depth && level-- == 0)
20825             {
20826               /* We've hit a `>>' where the first `>' closes the
20827                  template argument list, and the second `>' is
20828                  spurious.  Just consume the `>>' and stop; we've
20829                  already produced at least one error.  */
20830               cp_lexer_consume_token (parser->lexer);
20831               return;
20832             }
20833           /* Fall through for C++0x, so we handle the second `>' in
20834              the `>>'.  */
20835
20836         case CPP_GREATER:
20837           if (!nesting_depth && level-- == 0)
20838             {
20839               /* We've reached the token we want, consume it and stop.  */
20840               cp_lexer_consume_token (parser->lexer);
20841               return;
20842             }
20843           break;
20844
20845         case CPP_OPEN_PAREN:
20846         case CPP_OPEN_SQUARE:
20847           ++nesting_depth;
20848           break;
20849
20850         case CPP_CLOSE_PAREN:
20851         case CPP_CLOSE_SQUARE:
20852           if (nesting_depth-- == 0)
20853             return;
20854           break;
20855
20856         case CPP_EOF:
20857         case CPP_PRAGMA_EOL:
20858         case CPP_SEMICOLON:
20859         case CPP_OPEN_BRACE:
20860         case CPP_CLOSE_BRACE:
20861           /* The '>' was probably forgotten, don't look further.  */
20862           return;
20863
20864         default:
20865           break;
20866         }
20867
20868       /* Consume this token.  */
20869       cp_lexer_consume_token (parser->lexer);
20870     }
20871 }
20872
20873 /* If the next token is the indicated keyword, consume it.  Otherwise,
20874    issue an error message indicating that TOKEN_DESC was expected.
20875
20876    Returns the token consumed, if the token had the appropriate type.
20877    Otherwise, returns NULL.  */
20878
20879 static cp_token *
20880 cp_parser_require_keyword (cp_parser* parser,
20881                            enum rid keyword,
20882                            required_token token_desc)
20883 {
20884   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20885
20886   if (token && token->keyword != keyword)
20887     {
20888       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20889       return NULL;
20890     }
20891
20892   return token;
20893 }
20894
20895 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20896    function-definition.  */
20897
20898 static bool
20899 cp_parser_token_starts_function_definition_p (cp_token* token)
20900 {
20901   return (/* An ordinary function-body begins with an `{'.  */
20902           token->type == CPP_OPEN_BRACE
20903           /* A ctor-initializer begins with a `:'.  */
20904           || token->type == CPP_COLON
20905           /* A function-try-block begins with `try'.  */
20906           || token->keyword == RID_TRY
20907           /* The named return value extension begins with `return'.  */
20908           || token->keyword == RID_RETURN);
20909 }
20910
20911 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20912    definition.  */
20913
20914 static bool
20915 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20916 {
20917   cp_token *token;
20918
20919   token = cp_lexer_peek_token (parser->lexer);
20920   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20921 }
20922
20923 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20924    C++0x) ending a template-argument.  */
20925
20926 static bool
20927 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20928 {
20929   cp_token *token;
20930
20931   token = cp_lexer_peek_token (parser->lexer);
20932   return (token->type == CPP_COMMA 
20933           || token->type == CPP_GREATER
20934           || token->type == CPP_ELLIPSIS
20935           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20936 }
20937
20938 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20939    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
20940
20941 static bool
20942 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20943                                                      size_t n)
20944 {
20945   cp_token *token;
20946
20947   token = cp_lexer_peek_nth_token (parser->lexer, n);
20948   if (token->type == CPP_LESS)
20949     return true;
20950   /* Check for the sequence `<::' in the original code. It would be lexed as
20951      `[:', where `[' is a digraph, and there is no whitespace before
20952      `:'.  */
20953   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20954     {
20955       cp_token *token2;
20956       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20957       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20958         return true;
20959     }
20960   return false;
20961 }
20962
20963 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20964    or none_type otherwise.  */
20965
20966 static enum tag_types
20967 cp_parser_token_is_class_key (cp_token* token)
20968 {
20969   switch (token->keyword)
20970     {
20971     case RID_CLASS:
20972       return class_type;
20973     case RID_STRUCT:
20974       return record_type;
20975     case RID_UNION:
20976       return union_type;
20977
20978     default:
20979       return none_type;
20980     }
20981 }
20982
20983 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
20984
20985 static void
20986 cp_parser_check_class_key (enum tag_types class_key, tree type)
20987 {
20988   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20989     permerror (input_location, "%qs tag used in naming %q#T",
20990             class_key == union_type ? "union"
20991              : class_key == record_type ? "struct" : "class",
20992              type);
20993 }
20994
20995 /* Issue an error message if DECL is redeclared with different
20996    access than its original declaration [class.access.spec/3].
20997    This applies to nested classes and nested class templates.
20998    [class.mem/1].  */
20999
21000 static void
21001 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21002 {
21003   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21004     return;
21005
21006   if ((TREE_PRIVATE (decl)
21007        != (current_access_specifier == access_private_node))
21008       || (TREE_PROTECTED (decl)
21009           != (current_access_specifier == access_protected_node)))
21010     error_at (location, "%qD redeclared with different access", decl);
21011 }
21012
21013 /* Look for the `template' keyword, as a syntactic disambiguator.
21014    Return TRUE iff it is present, in which case it will be
21015    consumed.  */
21016
21017 static bool
21018 cp_parser_optional_template_keyword (cp_parser *parser)
21019 {
21020   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21021     {
21022       /* The `template' keyword can only be used within templates;
21023          outside templates the parser can always figure out what is a
21024          template and what is not.  */
21025       if (!processing_template_decl)
21026         {
21027           cp_token *token = cp_lexer_peek_token (parser->lexer);
21028           error_at (token->location,
21029                     "%<template%> (as a disambiguator) is only allowed "
21030                     "within templates");
21031           /* If this part of the token stream is rescanned, the same
21032              error message would be generated.  So, we purge the token
21033              from the stream.  */
21034           cp_lexer_purge_token (parser->lexer);
21035           return false;
21036         }
21037       else
21038         {
21039           /* Consume the `template' keyword.  */
21040           cp_lexer_consume_token (parser->lexer);
21041           return true;
21042         }
21043     }
21044
21045   return false;
21046 }
21047
21048 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21049    set PARSER->SCOPE, and perform other related actions.  */
21050
21051 static void
21052 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21053 {
21054   int i;
21055   struct tree_check *check_value;
21056   deferred_access_check *chk;
21057   VEC (deferred_access_check,gc) *checks;
21058
21059   /* Get the stored value.  */
21060   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21061   /* Perform any access checks that were deferred.  */
21062   checks = check_value->checks;
21063   if (checks)
21064     {
21065       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21066         perform_or_defer_access_check (chk->binfo,
21067                                        chk->decl,
21068                                        chk->diag_decl);
21069     }
21070   /* Set the scope from the stored value.  */
21071   parser->scope = check_value->value;
21072   parser->qualifying_scope = check_value->qualifying_scope;
21073   parser->object_scope = NULL_TREE;
21074 }
21075
21076 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21077    encounter the end of a block before what we were looking for.  */
21078
21079 static bool
21080 cp_parser_cache_group (cp_parser *parser,
21081                        enum cpp_ttype end,
21082                        unsigned depth)
21083 {
21084   while (true)
21085     {
21086       cp_token *token = cp_lexer_peek_token (parser->lexer);
21087
21088       /* Abort a parenthesized expression if we encounter a semicolon.  */
21089       if ((end == CPP_CLOSE_PAREN || depth == 0)
21090           && token->type == CPP_SEMICOLON)
21091         return true;
21092       /* If we've reached the end of the file, stop.  */
21093       if (token->type == CPP_EOF
21094           || (end != CPP_PRAGMA_EOL
21095               && token->type == CPP_PRAGMA_EOL))
21096         return true;
21097       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21098         /* We've hit the end of an enclosing block, so there's been some
21099            kind of syntax error.  */
21100         return true;
21101
21102       /* Consume the token.  */
21103       cp_lexer_consume_token (parser->lexer);
21104       /* See if it starts a new group.  */
21105       if (token->type == CPP_OPEN_BRACE)
21106         {
21107           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21108           /* In theory this should probably check end == '}', but
21109              cp_parser_save_member_function_body needs it to exit
21110              after either '}' or ')' when called with ')'.  */
21111           if (depth == 0)
21112             return false;
21113         }
21114       else if (token->type == CPP_OPEN_PAREN)
21115         {
21116           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21117           if (depth == 0 && end == CPP_CLOSE_PAREN)
21118             return false;
21119         }
21120       else if (token->type == CPP_PRAGMA)
21121         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21122       else if (token->type == end)
21123         return false;
21124     }
21125 }
21126
21127 /* Begin parsing tentatively.  We always save tokens while parsing
21128    tentatively so that if the tentative parsing fails we can restore the
21129    tokens.  */
21130
21131 static void
21132 cp_parser_parse_tentatively (cp_parser* parser)
21133 {
21134   /* Enter a new parsing context.  */
21135   parser->context = cp_parser_context_new (parser->context);
21136   /* Begin saving tokens.  */
21137   cp_lexer_save_tokens (parser->lexer);
21138   /* In order to avoid repetitive access control error messages,
21139      access checks are queued up until we are no longer parsing
21140      tentatively.  */
21141   push_deferring_access_checks (dk_deferred);
21142 }
21143
21144 /* Commit to the currently active tentative parse.  */
21145
21146 static void
21147 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21148 {
21149   cp_parser_context *context;
21150   cp_lexer *lexer;
21151
21152   /* Mark all of the levels as committed.  */
21153   lexer = parser->lexer;
21154   for (context = parser->context; context->next; context = context->next)
21155     {
21156       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21157         break;
21158       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21159       while (!cp_lexer_saving_tokens (lexer))
21160         lexer = lexer->next;
21161       cp_lexer_commit_tokens (lexer);
21162     }
21163 }
21164
21165 /* Abort the currently active tentative parse.  All consumed tokens
21166    will be rolled back, and no diagnostics will be issued.  */
21167
21168 static void
21169 cp_parser_abort_tentative_parse (cp_parser* parser)
21170 {
21171   cp_parser_simulate_error (parser);
21172   /* Now, pretend that we want to see if the construct was
21173      successfully parsed.  */
21174   cp_parser_parse_definitely (parser);
21175 }
21176
21177 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21178    token stream.  Otherwise, commit to the tokens we have consumed.
21179    Returns true if no error occurred; false otherwise.  */
21180
21181 static bool
21182 cp_parser_parse_definitely (cp_parser* parser)
21183 {
21184   bool error_occurred;
21185   cp_parser_context *context;
21186
21187   /* Remember whether or not an error occurred, since we are about to
21188      destroy that information.  */
21189   error_occurred = cp_parser_error_occurred (parser);
21190   /* Remove the topmost context from the stack.  */
21191   context = parser->context;
21192   parser->context = context->next;
21193   /* If no parse errors occurred, commit to the tentative parse.  */
21194   if (!error_occurred)
21195     {
21196       /* Commit to the tokens read tentatively, unless that was
21197          already done.  */
21198       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21199         cp_lexer_commit_tokens (parser->lexer);
21200
21201       pop_to_parent_deferring_access_checks ();
21202     }
21203   /* Otherwise, if errors occurred, roll back our state so that things
21204      are just as they were before we began the tentative parse.  */
21205   else
21206     {
21207       cp_lexer_rollback_tokens (parser->lexer);
21208       pop_deferring_access_checks ();
21209     }
21210   /* Add the context to the front of the free list.  */
21211   context->next = cp_parser_context_free_list;
21212   cp_parser_context_free_list = context;
21213
21214   return !error_occurred;
21215 }
21216
21217 /* Returns true if we are parsing tentatively and are not committed to
21218    this tentative parse.  */
21219
21220 static bool
21221 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21222 {
21223   return (cp_parser_parsing_tentatively (parser)
21224           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21225 }
21226
21227 /* Returns nonzero iff an error has occurred during the most recent
21228    tentative parse.  */
21229
21230 static bool
21231 cp_parser_error_occurred (cp_parser* parser)
21232 {
21233   return (cp_parser_parsing_tentatively (parser)
21234           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21235 }
21236
21237 /* Returns nonzero if GNU extensions are allowed.  */
21238
21239 static bool
21240 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21241 {
21242   return parser->allow_gnu_extensions_p;
21243 }
21244 \f
21245 /* Objective-C++ Productions */
21246
21247
21248 /* Parse an Objective-C expression, which feeds into a primary-expression
21249    above.
21250
21251    objc-expression:
21252      objc-message-expression
21253      objc-string-literal
21254      objc-encode-expression
21255      objc-protocol-expression
21256      objc-selector-expression
21257
21258   Returns a tree representation of the expression.  */
21259
21260 static tree
21261 cp_parser_objc_expression (cp_parser* parser)
21262 {
21263   /* Try to figure out what kind of declaration is present.  */
21264   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21265
21266   switch (kwd->type)
21267     {
21268     case CPP_OPEN_SQUARE:
21269       return cp_parser_objc_message_expression (parser);
21270
21271     case CPP_OBJC_STRING:
21272       kwd = cp_lexer_consume_token (parser->lexer);
21273       return objc_build_string_object (kwd->u.value);
21274
21275     case CPP_KEYWORD:
21276       switch (kwd->keyword)
21277         {
21278         case RID_AT_ENCODE:
21279           return cp_parser_objc_encode_expression (parser);
21280
21281         case RID_AT_PROTOCOL:
21282           return cp_parser_objc_protocol_expression (parser);
21283
21284         case RID_AT_SELECTOR:
21285           return cp_parser_objc_selector_expression (parser);
21286
21287         default:
21288           break;
21289         }
21290     default:
21291       error_at (kwd->location,
21292                 "misplaced %<@%D%> Objective-C++ construct",
21293                 kwd->u.value);
21294       cp_parser_skip_to_end_of_block_or_statement (parser);
21295     }
21296
21297   return error_mark_node;
21298 }
21299
21300 /* Parse an Objective-C message expression.
21301
21302    objc-message-expression:
21303      [ objc-message-receiver objc-message-args ]
21304
21305    Returns a representation of an Objective-C message.  */
21306
21307 static tree
21308 cp_parser_objc_message_expression (cp_parser* parser)
21309 {
21310   tree receiver, messageargs;
21311
21312   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21313   receiver = cp_parser_objc_message_receiver (parser);
21314   messageargs = cp_parser_objc_message_args (parser);
21315   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21316
21317   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21318 }
21319
21320 /* Parse an objc-message-receiver.
21321
21322    objc-message-receiver:
21323      expression
21324      simple-type-specifier
21325
21326   Returns a representation of the type or expression.  */
21327
21328 static tree
21329 cp_parser_objc_message_receiver (cp_parser* parser)
21330 {
21331   tree rcv;
21332
21333   /* An Objective-C message receiver may be either (1) a type
21334      or (2) an expression.  */
21335   cp_parser_parse_tentatively (parser);
21336   rcv = cp_parser_expression (parser, false, NULL);
21337
21338   if (cp_parser_parse_definitely (parser))
21339     return rcv;
21340
21341   rcv = cp_parser_simple_type_specifier (parser,
21342                                          /*decl_specs=*/NULL,
21343                                          CP_PARSER_FLAGS_NONE);
21344
21345   return objc_get_class_reference (rcv);
21346 }
21347
21348 /* Parse the arguments and selectors comprising an Objective-C message.
21349
21350    objc-message-args:
21351      objc-selector
21352      objc-selector-args
21353      objc-selector-args , objc-comma-args
21354
21355    objc-selector-args:
21356      objc-selector [opt] : assignment-expression
21357      objc-selector-args objc-selector [opt] : assignment-expression
21358
21359    objc-comma-args:
21360      assignment-expression
21361      objc-comma-args , assignment-expression
21362
21363    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21364    selector arguments and TREE_VALUE containing a list of comma
21365    arguments.  */
21366
21367 static tree
21368 cp_parser_objc_message_args (cp_parser* parser)
21369 {
21370   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21371   bool maybe_unary_selector_p = true;
21372   cp_token *token = cp_lexer_peek_token (parser->lexer);
21373
21374   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21375     {
21376       tree selector = NULL_TREE, arg;
21377
21378       if (token->type != CPP_COLON)
21379         selector = cp_parser_objc_selector (parser);
21380
21381       /* Detect if we have a unary selector.  */
21382       if (maybe_unary_selector_p
21383           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21384         return build_tree_list (selector, NULL_TREE);
21385
21386       maybe_unary_selector_p = false;
21387       cp_parser_require (parser, CPP_COLON, RT_COLON);
21388       arg = cp_parser_assignment_expression (parser, false, NULL);
21389
21390       sel_args
21391         = chainon (sel_args,
21392                    build_tree_list (selector, arg));
21393
21394       token = cp_lexer_peek_token (parser->lexer);
21395     }
21396
21397   /* Handle non-selector arguments, if any. */
21398   while (token->type == CPP_COMMA)
21399     {
21400       tree arg;
21401
21402       cp_lexer_consume_token (parser->lexer);
21403       arg = cp_parser_assignment_expression (parser, false, NULL);
21404
21405       addl_args
21406         = chainon (addl_args,
21407                    build_tree_list (NULL_TREE, arg));
21408
21409       token = cp_lexer_peek_token (parser->lexer);
21410     }
21411
21412   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21413     {
21414       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21415       return build_tree_list (error_mark_node, error_mark_node);
21416     }
21417
21418   return build_tree_list (sel_args, addl_args);
21419 }
21420
21421 /* Parse an Objective-C encode expression.
21422
21423    objc-encode-expression:
21424      @encode objc-typename
21425
21426    Returns an encoded representation of the type argument.  */
21427
21428 static tree
21429 cp_parser_objc_encode_expression (cp_parser* parser)
21430 {
21431   tree type;
21432   cp_token *token;
21433
21434   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21435   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21436   token = cp_lexer_peek_token (parser->lexer);
21437   type = complete_type (cp_parser_type_id (parser));
21438   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21439
21440   if (!type)
21441     {
21442       error_at (token->location, 
21443                 "%<@encode%> must specify a type as an argument");
21444       return error_mark_node;
21445     }
21446
21447   /* This happens if we find @encode(T) (where T is a template
21448      typename or something dependent on a template typename) when
21449      parsing a template.  In that case, we can't compile it
21450      immediately, but we rather create an AT_ENCODE_EXPR which will
21451      need to be instantiated when the template is used.
21452   */
21453   if (dependent_type_p (type))
21454     {
21455       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21456       TREE_READONLY (value) = 1;
21457       return value;
21458     }
21459
21460   return objc_build_encode_expr (type);
21461 }
21462
21463 /* Parse an Objective-C @defs expression.  */
21464
21465 static tree
21466 cp_parser_objc_defs_expression (cp_parser *parser)
21467 {
21468   tree name;
21469
21470   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21471   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21472   name = cp_parser_identifier (parser);
21473   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21474
21475   return objc_get_class_ivars (name);
21476 }
21477
21478 /* Parse an Objective-C protocol expression.
21479
21480   objc-protocol-expression:
21481     @protocol ( identifier )
21482
21483   Returns a representation of the protocol expression.  */
21484
21485 static tree
21486 cp_parser_objc_protocol_expression (cp_parser* parser)
21487 {
21488   tree proto;
21489
21490   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21491   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21492   proto = cp_parser_identifier (parser);
21493   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21494
21495   return objc_build_protocol_expr (proto);
21496 }
21497
21498 /* Parse an Objective-C selector expression.
21499
21500    objc-selector-expression:
21501      @selector ( objc-method-signature )
21502
21503    objc-method-signature:
21504      objc-selector
21505      objc-selector-seq
21506
21507    objc-selector-seq:
21508      objc-selector :
21509      objc-selector-seq objc-selector :
21510
21511   Returns a representation of the method selector.  */
21512
21513 static tree
21514 cp_parser_objc_selector_expression (cp_parser* parser)
21515 {
21516   tree sel_seq = NULL_TREE;
21517   bool maybe_unary_selector_p = true;
21518   cp_token *token;
21519   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21520
21521   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21522   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21523   token = cp_lexer_peek_token (parser->lexer);
21524
21525   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21526          || token->type == CPP_SCOPE)
21527     {
21528       tree selector = NULL_TREE;
21529
21530       if (token->type != CPP_COLON
21531           || token->type == CPP_SCOPE)
21532         selector = cp_parser_objc_selector (parser);
21533
21534       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21535           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21536         {
21537           /* Detect if we have a unary selector.  */
21538           if (maybe_unary_selector_p)
21539             {
21540               sel_seq = selector;
21541               goto finish_selector;
21542             }
21543           else
21544             {
21545               cp_parser_error (parser, "expected %<:%>");
21546             }
21547         }
21548       maybe_unary_selector_p = false;
21549       token = cp_lexer_consume_token (parser->lexer);
21550
21551       if (token->type == CPP_SCOPE)
21552         {
21553           sel_seq
21554             = chainon (sel_seq,
21555                        build_tree_list (selector, NULL_TREE));
21556           sel_seq
21557             = chainon (sel_seq,
21558                        build_tree_list (NULL_TREE, NULL_TREE));
21559         }
21560       else
21561         sel_seq
21562           = chainon (sel_seq,
21563                      build_tree_list (selector, NULL_TREE));
21564
21565       token = cp_lexer_peek_token (parser->lexer);
21566     }
21567
21568  finish_selector:
21569   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21570
21571   return objc_build_selector_expr (loc, sel_seq);
21572 }
21573
21574 /* Parse a list of identifiers.
21575
21576    objc-identifier-list:
21577      identifier
21578      objc-identifier-list , identifier
21579
21580    Returns a TREE_LIST of identifier nodes.  */
21581
21582 static tree
21583 cp_parser_objc_identifier_list (cp_parser* parser)
21584 {
21585   tree identifier;
21586   tree list;
21587   cp_token *sep;
21588
21589   identifier = cp_parser_identifier (parser);
21590   if (identifier == error_mark_node)
21591     return error_mark_node;      
21592
21593   list = build_tree_list (NULL_TREE, identifier);
21594   sep = cp_lexer_peek_token (parser->lexer);
21595
21596   while (sep->type == CPP_COMMA)
21597     {
21598       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21599       identifier = cp_parser_identifier (parser);
21600       if (identifier == error_mark_node)
21601         return list;
21602
21603       list = chainon (list, build_tree_list (NULL_TREE,
21604                                              identifier));
21605       sep = cp_lexer_peek_token (parser->lexer);
21606     }
21607   
21608   return list;
21609 }
21610
21611 /* Parse an Objective-C alias declaration.
21612
21613    objc-alias-declaration:
21614      @compatibility_alias identifier identifier ;
21615
21616    This function registers the alias mapping with the Objective-C front end.
21617    It returns nothing.  */
21618
21619 static void
21620 cp_parser_objc_alias_declaration (cp_parser* parser)
21621 {
21622   tree alias, orig;
21623
21624   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21625   alias = cp_parser_identifier (parser);
21626   orig = cp_parser_identifier (parser);
21627   objc_declare_alias (alias, orig);
21628   cp_parser_consume_semicolon_at_end_of_statement (parser);
21629 }
21630
21631 /* Parse an Objective-C class forward-declaration.
21632
21633    objc-class-declaration:
21634      @class objc-identifier-list ;
21635
21636    The function registers the forward declarations with the Objective-C
21637    front end.  It returns nothing.  */
21638
21639 static void
21640 cp_parser_objc_class_declaration (cp_parser* parser)
21641 {
21642   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21643   objc_declare_class (cp_parser_objc_identifier_list (parser));
21644   cp_parser_consume_semicolon_at_end_of_statement (parser);
21645 }
21646
21647 /* Parse a list of Objective-C protocol references.
21648
21649    objc-protocol-refs-opt:
21650      objc-protocol-refs [opt]
21651
21652    objc-protocol-refs:
21653      < objc-identifier-list >
21654
21655    Returns a TREE_LIST of identifiers, if any.  */
21656
21657 static tree
21658 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21659 {
21660   tree protorefs = NULL_TREE;
21661
21662   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21663     {
21664       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21665       protorefs = cp_parser_objc_identifier_list (parser);
21666       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21667     }
21668
21669   return protorefs;
21670 }
21671
21672 /* Parse a Objective-C visibility specification.  */
21673
21674 static void
21675 cp_parser_objc_visibility_spec (cp_parser* parser)
21676 {
21677   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21678
21679   switch (vis->keyword)
21680     {
21681     case RID_AT_PRIVATE:
21682       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21683       break;
21684     case RID_AT_PROTECTED:
21685       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21686       break;
21687     case RID_AT_PUBLIC:
21688       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21689       break;
21690     case RID_AT_PACKAGE:
21691       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21692       break;
21693     default:
21694       return;
21695     }
21696
21697   /* Eat '@private'/'@protected'/'@public'.  */
21698   cp_lexer_consume_token (parser->lexer);
21699 }
21700
21701 /* Parse an Objective-C method type.  Return 'true' if it is a class
21702    (+) method, and 'false' if it is an instance (-) method.  */
21703
21704 static inline bool
21705 cp_parser_objc_method_type (cp_parser* parser)
21706 {
21707   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21708     return true;
21709   else
21710     return false;
21711 }
21712
21713 /* Parse an Objective-C protocol qualifier.  */
21714
21715 static tree
21716 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21717 {
21718   tree quals = NULL_TREE, node;
21719   cp_token *token = cp_lexer_peek_token (parser->lexer);
21720
21721   node = token->u.value;
21722
21723   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21724          && (node == ridpointers [(int) RID_IN]
21725              || node == ridpointers [(int) RID_OUT]
21726              || node == ridpointers [(int) RID_INOUT]
21727              || node == ridpointers [(int) RID_BYCOPY]
21728              || node == ridpointers [(int) RID_BYREF]
21729              || node == ridpointers [(int) RID_ONEWAY]))
21730     {
21731       quals = tree_cons (NULL_TREE, node, quals);
21732       cp_lexer_consume_token (parser->lexer);
21733       token = cp_lexer_peek_token (parser->lexer);
21734       node = token->u.value;
21735     }
21736
21737   return quals;
21738 }
21739
21740 /* Parse an Objective-C typename.  */
21741
21742 static tree
21743 cp_parser_objc_typename (cp_parser* parser)
21744 {
21745   tree type_name = NULL_TREE;
21746
21747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21748     {
21749       tree proto_quals, cp_type = NULL_TREE;
21750
21751       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21752       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21753
21754       /* An ObjC type name may consist of just protocol qualifiers, in which
21755          case the type shall default to 'id'.  */
21756       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21757         cp_type = cp_parser_type_id (parser);
21758
21759       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21760       type_name = build_tree_list (proto_quals, cp_type);
21761     }
21762
21763   return type_name;
21764 }
21765
21766 /* Check to see if TYPE refers to an Objective-C selector name.  */
21767
21768 static bool
21769 cp_parser_objc_selector_p (enum cpp_ttype type)
21770 {
21771   return (type == CPP_NAME || type == CPP_KEYWORD
21772           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21773           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21774           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21775           || type == CPP_XOR || type == CPP_XOR_EQ);
21776 }
21777
21778 /* Parse an Objective-C selector.  */
21779
21780 static tree
21781 cp_parser_objc_selector (cp_parser* parser)
21782 {
21783   cp_token *token = cp_lexer_consume_token (parser->lexer);
21784
21785   if (!cp_parser_objc_selector_p (token->type))
21786     {
21787       error_at (token->location, "invalid Objective-C++ selector name");
21788       return error_mark_node;
21789     }
21790
21791   /* C++ operator names are allowed to appear in ObjC selectors.  */
21792   switch (token->type)
21793     {
21794     case CPP_AND_AND: return get_identifier ("and");
21795     case CPP_AND_EQ: return get_identifier ("and_eq");
21796     case CPP_AND: return get_identifier ("bitand");
21797     case CPP_OR: return get_identifier ("bitor");
21798     case CPP_COMPL: return get_identifier ("compl");
21799     case CPP_NOT: return get_identifier ("not");
21800     case CPP_NOT_EQ: return get_identifier ("not_eq");
21801     case CPP_OR_OR: return get_identifier ("or");
21802     case CPP_OR_EQ: return get_identifier ("or_eq");
21803     case CPP_XOR: return get_identifier ("xor");
21804     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21805     default: return token->u.value;
21806     }
21807 }
21808
21809 /* Parse an Objective-C params list.  */
21810
21811 static tree
21812 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21813 {
21814   tree params = NULL_TREE;
21815   bool maybe_unary_selector_p = true;
21816   cp_token *token = cp_lexer_peek_token (parser->lexer);
21817
21818   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21819     {
21820       tree selector = NULL_TREE, type_name, identifier;
21821       tree parm_attr = NULL_TREE;
21822
21823       if (token->keyword == RID_ATTRIBUTE)
21824         break;
21825
21826       if (token->type != CPP_COLON)
21827         selector = cp_parser_objc_selector (parser);
21828
21829       /* Detect if we have a unary selector.  */
21830       if (maybe_unary_selector_p
21831           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21832         {
21833           params = selector; /* Might be followed by attributes.  */
21834           break;
21835         }
21836
21837       maybe_unary_selector_p = false;
21838       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21839         {
21840           /* Something went quite wrong.  There should be a colon
21841              here, but there is not.  Stop parsing parameters.  */
21842           break;
21843         }
21844       type_name = cp_parser_objc_typename (parser);
21845       /* New ObjC allows attributes on parameters too.  */
21846       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21847         parm_attr = cp_parser_attributes_opt (parser);
21848       identifier = cp_parser_identifier (parser);
21849
21850       params
21851         = chainon (params,
21852                    objc_build_keyword_decl (selector,
21853                                             type_name,
21854                                             identifier,
21855                                             parm_attr));
21856
21857       token = cp_lexer_peek_token (parser->lexer);
21858     }
21859
21860   if (params == NULL_TREE)
21861     {
21862       cp_parser_error (parser, "objective-c++ method declaration is expected");
21863       return error_mark_node;
21864     }
21865
21866   /* We allow tail attributes for the method.  */
21867   if (token->keyword == RID_ATTRIBUTE)
21868     {
21869       *attributes = cp_parser_attributes_opt (parser);
21870       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21871           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21872         return params;
21873       cp_parser_error (parser, 
21874                        "method attributes must be specified at the end");
21875       return error_mark_node;
21876     }
21877
21878   if (params == NULL_TREE)
21879     {
21880       cp_parser_error (parser, "objective-c++ method declaration is expected");
21881       return error_mark_node;
21882     }
21883   return params;
21884 }
21885
21886 /* Parse the non-keyword Objective-C params.  */
21887
21888 static tree
21889 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
21890                                        tree* attributes)
21891 {
21892   tree params = make_node (TREE_LIST);
21893   cp_token *token = cp_lexer_peek_token (parser->lexer);
21894   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21895
21896   while (token->type == CPP_COMMA)
21897     {
21898       cp_parameter_declarator *parmdecl;
21899       tree parm;
21900
21901       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21902       token = cp_lexer_peek_token (parser->lexer);
21903
21904       if (token->type == CPP_ELLIPSIS)
21905         {
21906           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
21907           *ellipsisp = true;
21908           token = cp_lexer_peek_token (parser->lexer);
21909           break;
21910         }
21911
21912       /* TODO: parse attributes for tail parameters.  */
21913       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21914       parm = grokdeclarator (parmdecl->declarator,
21915                              &parmdecl->decl_specifiers,
21916                              PARM, /*initialized=*/0,
21917                              /*attrlist=*/NULL);
21918
21919       chainon (params, build_tree_list (NULL_TREE, parm));
21920       token = cp_lexer_peek_token (parser->lexer);
21921     }
21922
21923   /* We allow tail attributes for the method.  */
21924   if (token->keyword == RID_ATTRIBUTE)
21925     {
21926       if (*attributes == NULL_TREE)
21927         {
21928           *attributes = cp_parser_attributes_opt (parser);
21929           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21930               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21931             return params;
21932         }
21933       else        
21934         /* We have an error, but parse the attributes, so that we can 
21935            carry on.  */
21936         *attributes = cp_parser_attributes_opt (parser);
21937
21938       cp_parser_error (parser, 
21939                        "method attributes must be specified at the end");
21940       return error_mark_node;
21941     }
21942
21943   return params;
21944 }
21945
21946 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
21947
21948 static void
21949 cp_parser_objc_interstitial_code (cp_parser* parser)
21950 {
21951   cp_token *token = cp_lexer_peek_token (parser->lexer);
21952
21953   /* If the next token is `extern' and the following token is a string
21954      literal, then we have a linkage specification.  */
21955   if (token->keyword == RID_EXTERN
21956       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21957     cp_parser_linkage_specification (parser);
21958   /* Handle #pragma, if any.  */
21959   else if (token->type == CPP_PRAGMA)
21960     cp_parser_pragma (parser, pragma_external);
21961   /* Allow stray semicolons.  */
21962   else if (token->type == CPP_SEMICOLON)
21963     cp_lexer_consume_token (parser->lexer);
21964   /* Mark methods as optional or required, when building protocols.  */
21965   else if (token->keyword == RID_AT_OPTIONAL)
21966     {
21967       cp_lexer_consume_token (parser->lexer);
21968       objc_set_method_opt (true);
21969     }
21970   else if (token->keyword == RID_AT_REQUIRED)
21971     {
21972       cp_lexer_consume_token (parser->lexer);
21973       objc_set_method_opt (false);
21974     }
21975   else if (token->keyword == RID_NAMESPACE)
21976     cp_parser_namespace_definition (parser);
21977   /* Other stray characters must generate errors.  */
21978   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21979     {
21980       cp_lexer_consume_token (parser->lexer);
21981       error ("stray `%s' between Objective-C++ methods",
21982              token->type == CPP_OPEN_BRACE ? "{" : "}");
21983     }
21984   /* Finally, try to parse a block-declaration, or a function-definition.  */
21985   else
21986     cp_parser_block_declaration (parser, /*statement_p=*/false);
21987 }
21988
21989 /* Parse a method signature.  */
21990
21991 static tree
21992 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21993 {
21994   tree rettype, kwdparms, optparms;
21995   bool ellipsis = false;
21996   bool is_class_method;
21997
21998   is_class_method = cp_parser_objc_method_type (parser);
21999   rettype = cp_parser_objc_typename (parser);
22000   *attributes = NULL_TREE;
22001   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22002   if (kwdparms == error_mark_node)
22003     return error_mark_node;
22004   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22005   if (optparms == error_mark_node)
22006     return error_mark_node;
22007
22008   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22009 }
22010
22011 static bool
22012 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22013 {
22014   tree tattr;  
22015   cp_lexer_save_tokens (parser->lexer);
22016   tattr = cp_parser_attributes_opt (parser);
22017   gcc_assert (tattr) ;
22018   
22019   /* If the attributes are followed by a method introducer, this is not allowed.
22020      Dump the attributes and flag the situation.  */
22021   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22022       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22023     return true;
22024
22025   /* Otherwise, the attributes introduce some interstitial code, possibly so
22026      rewind to allow that check.  */
22027   cp_lexer_rollback_tokens (parser->lexer);
22028   return false;  
22029 }
22030
22031 /* Parse an Objective-C method prototype list.  */
22032
22033 static void
22034 cp_parser_objc_method_prototype_list (cp_parser* parser)
22035 {
22036   cp_token *token = cp_lexer_peek_token (parser->lexer);
22037
22038   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22039     {
22040       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22041         {
22042           tree attributes, sig;
22043           bool is_class_method;
22044           if (token->type == CPP_PLUS)
22045             is_class_method = true;
22046           else
22047             is_class_method = false;
22048           sig = cp_parser_objc_method_signature (parser, &attributes);
22049           if (sig == error_mark_node)
22050             {
22051               cp_parser_skip_to_end_of_block_or_statement (parser);
22052               token = cp_lexer_peek_token (parser->lexer);
22053               continue;
22054             }
22055           objc_add_method_declaration (is_class_method, sig, attributes);
22056           cp_parser_consume_semicolon_at_end_of_statement (parser);
22057         }
22058       else if (token->keyword == RID_AT_PROPERTY)
22059         cp_parser_objc_at_property_declaration (parser);
22060       else if (token->keyword == RID_ATTRIBUTE 
22061                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22062         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22063                     OPT_Wattributes, 
22064                     "prefix attributes are ignored for methods");
22065       else
22066         /* Allow for interspersed non-ObjC++ code.  */
22067         cp_parser_objc_interstitial_code (parser);
22068
22069       token = cp_lexer_peek_token (parser->lexer);
22070     }
22071
22072   if (token->type != CPP_EOF)
22073     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22074   else
22075     cp_parser_error (parser, "expected %<@end%>");
22076
22077   objc_finish_interface ();
22078 }
22079
22080 /* Parse an Objective-C method definition list.  */
22081
22082 static void
22083 cp_parser_objc_method_definition_list (cp_parser* parser)
22084 {
22085   cp_token *token = cp_lexer_peek_token (parser->lexer);
22086
22087   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22088     {
22089       tree meth;
22090
22091       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22092         {
22093           cp_token *ptk;
22094           tree sig, attribute;
22095           bool is_class_method;
22096           if (token->type == CPP_PLUS)
22097             is_class_method = true;
22098           else
22099             is_class_method = false;
22100           push_deferring_access_checks (dk_deferred);
22101           sig = cp_parser_objc_method_signature (parser, &attribute);
22102           if (sig == error_mark_node)
22103             {
22104               cp_parser_skip_to_end_of_block_or_statement (parser);
22105               token = cp_lexer_peek_token (parser->lexer);
22106               continue;
22107             }
22108           objc_start_method_definition (is_class_method, sig, attribute);
22109
22110           /* For historical reasons, we accept an optional semicolon.  */
22111           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22112             cp_lexer_consume_token (parser->lexer);
22113
22114           ptk = cp_lexer_peek_token (parser->lexer);
22115           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22116                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22117             {
22118               perform_deferred_access_checks ();
22119               stop_deferring_access_checks ();
22120               meth = cp_parser_function_definition_after_declarator (parser,
22121                                                                      false);
22122               pop_deferring_access_checks ();
22123               objc_finish_method_definition (meth);
22124             }
22125         }
22126       /* The following case will be removed once @synthesize is
22127          completely implemented.  */
22128       else if (token->keyword == RID_AT_PROPERTY)
22129         cp_parser_objc_at_property_declaration (parser);
22130       else if (token->keyword == RID_AT_SYNTHESIZE)
22131         cp_parser_objc_at_synthesize_declaration (parser);
22132       else if (token->keyword == RID_AT_DYNAMIC)
22133         cp_parser_objc_at_dynamic_declaration (parser);
22134       else if (token->keyword == RID_ATTRIBUTE 
22135                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22136         warning_at (token->location, OPT_Wattributes,
22137                     "prefix attributes are ignored for methods");
22138       else
22139         /* Allow for interspersed non-ObjC++ code.  */
22140         cp_parser_objc_interstitial_code (parser);
22141
22142       token = cp_lexer_peek_token (parser->lexer);
22143     }
22144
22145   if (token->type != CPP_EOF)
22146     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22147   else
22148     cp_parser_error (parser, "expected %<@end%>");
22149
22150   objc_finish_implementation ();
22151 }
22152
22153 /* Parse Objective-C ivars.  */
22154
22155 static void
22156 cp_parser_objc_class_ivars (cp_parser* parser)
22157 {
22158   cp_token *token = cp_lexer_peek_token (parser->lexer);
22159
22160   if (token->type != CPP_OPEN_BRACE)
22161     return;     /* No ivars specified.  */
22162
22163   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22164   token = cp_lexer_peek_token (parser->lexer);
22165
22166   while (token->type != CPP_CLOSE_BRACE 
22167         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22168     {
22169       cp_decl_specifier_seq declspecs;
22170       int decl_class_or_enum_p;
22171       tree prefix_attributes;
22172
22173       cp_parser_objc_visibility_spec (parser);
22174
22175       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22176         break;
22177
22178       cp_parser_decl_specifier_seq (parser,
22179                                     CP_PARSER_FLAGS_OPTIONAL,
22180                                     &declspecs,
22181                                     &decl_class_or_enum_p);
22182
22183       /* auto, register, static, extern, mutable.  */
22184       if (declspecs.storage_class != sc_none)
22185         {
22186           cp_parser_error (parser, "invalid type for instance variable");         
22187           declspecs.storage_class = sc_none;
22188         }
22189
22190       /* __thread.  */
22191       if (declspecs.specs[(int) ds_thread])
22192         {
22193           cp_parser_error (parser, "invalid type for instance variable");
22194           declspecs.specs[(int) ds_thread] = 0;
22195         }
22196       
22197       /* typedef.  */
22198       if (declspecs.specs[(int) ds_typedef])
22199         {
22200           cp_parser_error (parser, "invalid type for instance variable");
22201           declspecs.specs[(int) ds_typedef] = 0;
22202         }
22203
22204       prefix_attributes = declspecs.attributes;
22205       declspecs.attributes = NULL_TREE;
22206
22207       /* Keep going until we hit the `;' at the end of the
22208          declaration.  */
22209       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22210         {
22211           tree width = NULL_TREE, attributes, first_attribute, decl;
22212           cp_declarator *declarator = NULL;
22213           int ctor_dtor_or_conv_p;
22214
22215           /* Check for a (possibly unnamed) bitfield declaration.  */
22216           token = cp_lexer_peek_token (parser->lexer);
22217           if (token->type == CPP_COLON)
22218             goto eat_colon;
22219
22220           if (token->type == CPP_NAME
22221               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22222                   == CPP_COLON))
22223             {
22224               /* Get the name of the bitfield.  */
22225               declarator = make_id_declarator (NULL_TREE,
22226                                                cp_parser_identifier (parser),
22227                                                sfk_none);
22228
22229              eat_colon:
22230               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22231               /* Get the width of the bitfield.  */
22232               width
22233                 = cp_parser_constant_expression (parser,
22234                                                  /*allow_non_constant=*/false,
22235                                                  NULL);
22236             }
22237           else
22238             {
22239               /* Parse the declarator.  */
22240               declarator
22241                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22242                                         &ctor_dtor_or_conv_p,
22243                                         /*parenthesized_p=*/NULL,
22244                                         /*member_p=*/false);
22245             }
22246
22247           /* Look for attributes that apply to the ivar.  */
22248           attributes = cp_parser_attributes_opt (parser);
22249           /* Remember which attributes are prefix attributes and
22250              which are not.  */
22251           first_attribute = attributes;
22252           /* Combine the attributes.  */
22253           attributes = chainon (prefix_attributes, attributes);
22254
22255           if (width)
22256               /* Create the bitfield declaration.  */
22257               decl = grokbitfield (declarator, &declspecs,
22258                                    width,
22259                                    attributes);
22260           else
22261             decl = grokfield (declarator, &declspecs,
22262                               NULL_TREE, /*init_const_expr_p=*/false,
22263                               NULL_TREE, attributes);
22264
22265           /* Add the instance variable.  */
22266           objc_add_instance_variable (decl);
22267
22268           /* Reset PREFIX_ATTRIBUTES.  */
22269           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22270             attributes = TREE_CHAIN (attributes);
22271           if (attributes)
22272             TREE_CHAIN (attributes) = NULL_TREE;
22273
22274           token = cp_lexer_peek_token (parser->lexer);
22275
22276           if (token->type == CPP_COMMA)
22277             {
22278               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22279               continue;
22280             }
22281           break;
22282         }
22283
22284       cp_parser_consume_semicolon_at_end_of_statement (parser);
22285       token = cp_lexer_peek_token (parser->lexer);
22286     }
22287
22288   if (token->keyword == RID_AT_END)
22289     cp_parser_error (parser, "expected %<}%>");
22290
22291   /* Do not consume the RID_AT_END, so it will be read again as terminating
22292      the @interface of @implementation.  */ 
22293   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22294     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22295     
22296   /* For historical reasons, we accept an optional semicolon.  */
22297   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22298     cp_lexer_consume_token (parser->lexer);
22299 }
22300
22301 /* Parse an Objective-C protocol declaration.  */
22302
22303 static void
22304 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22305 {
22306   tree proto, protorefs;
22307   cp_token *tok;
22308
22309   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22310   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22311     {
22312       tok = cp_lexer_peek_token (parser->lexer);
22313       error_at (tok->location, "identifier expected after %<@protocol%>");
22314       goto finish;
22315     }
22316
22317   /* See if we have a forward declaration or a definition.  */
22318   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22319
22320   /* Try a forward declaration first.  */
22321   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22322     {
22323       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22324      finish:
22325       cp_parser_consume_semicolon_at_end_of_statement (parser);
22326     }
22327
22328   /* Ok, we got a full-fledged definition (or at least should).  */
22329   else
22330     {
22331       proto = cp_parser_identifier (parser);
22332       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22333       objc_start_protocol (proto, protorefs, attributes);
22334       cp_parser_objc_method_prototype_list (parser);
22335     }
22336 }
22337
22338 /* Parse an Objective-C superclass or category.  */
22339
22340 static void
22341 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22342                                                           tree *categ)
22343 {
22344   cp_token *next = cp_lexer_peek_token (parser->lexer);
22345
22346   *super = *categ = NULL_TREE;
22347   if (next->type == CPP_COLON)
22348     {
22349       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22350       *super = cp_parser_identifier (parser);
22351     }
22352   else if (next->type == CPP_OPEN_PAREN)
22353     {
22354       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22355       *categ = cp_parser_identifier (parser);
22356       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22357     }
22358 }
22359
22360 /* Parse an Objective-C class interface.  */
22361
22362 static void
22363 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22364 {
22365   tree name, super, categ, protos;
22366
22367   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22368   name = cp_parser_identifier (parser);
22369   if (name == error_mark_node)
22370     {
22371       /* It's hard to recover because even if valid @interface stuff
22372          is to follow, we can't compile it (or validate it) if we
22373          don't even know which class it refers to.  Let's assume this
22374          was a stray '@interface' token in the stream and skip it.
22375       */
22376       return;
22377     }
22378   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22379   protos = cp_parser_objc_protocol_refs_opt (parser);
22380
22381   /* We have either a class or a category on our hands.  */
22382   if (categ)
22383     objc_start_category_interface (name, categ, protos, attributes);
22384   else
22385     {
22386       objc_start_class_interface (name, super, protos, attributes);
22387       /* Handle instance variable declarations, if any.  */
22388       cp_parser_objc_class_ivars (parser);
22389       objc_continue_interface ();
22390     }
22391
22392   cp_parser_objc_method_prototype_list (parser);
22393 }
22394
22395 /* Parse an Objective-C class implementation.  */
22396
22397 static void
22398 cp_parser_objc_class_implementation (cp_parser* parser)
22399 {
22400   tree name, super, categ;
22401
22402   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22403   name = cp_parser_identifier (parser);
22404   if (name == error_mark_node)
22405     {
22406       /* It's hard to recover because even if valid @implementation
22407          stuff is to follow, we can't compile it (or validate it) if
22408          we don't even know which class it refers to.  Let's assume
22409          this was a stray '@implementation' token in the stream and
22410          skip it.
22411       */
22412       return;
22413     }
22414   cp_parser_objc_superclass_or_category (parser, &super, &categ);
22415
22416   /* We have either a class or a category on our hands.  */
22417   if (categ)
22418     objc_start_category_implementation (name, categ);
22419   else
22420     {
22421       objc_start_class_implementation (name, super);
22422       /* Handle instance variable declarations, if any.  */
22423       cp_parser_objc_class_ivars (parser);
22424       objc_continue_implementation ();
22425     }
22426
22427   cp_parser_objc_method_definition_list (parser);
22428 }
22429
22430 /* Consume the @end token and finish off the implementation.  */
22431
22432 static void
22433 cp_parser_objc_end_implementation (cp_parser* parser)
22434 {
22435   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22436   objc_finish_implementation ();
22437 }
22438
22439 /* Parse an Objective-C declaration.  */
22440
22441 static void
22442 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22443 {
22444   /* Try to figure out what kind of declaration is present.  */
22445   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22446
22447   if (attributes)
22448     switch (kwd->keyword)
22449       {
22450         case RID_AT_ALIAS:
22451         case RID_AT_CLASS:
22452         case RID_AT_END:
22453           error_at (kwd->location, "attributes may not be specified before"
22454                     " the %<@%D%> Objective-C++ keyword",
22455                     kwd->u.value);
22456           attributes = NULL;
22457           break;
22458         case RID_AT_IMPLEMENTATION:
22459           warning_at (kwd->location, OPT_Wattributes,
22460                       "prefix attributes are ignored before %<@%D%>",
22461                       kwd->u.value);
22462           attributes = NULL;
22463         default:
22464           break;
22465       }
22466
22467   switch (kwd->keyword)
22468     {
22469     case RID_AT_ALIAS:
22470       cp_parser_objc_alias_declaration (parser);
22471       break;
22472     case RID_AT_CLASS:
22473       cp_parser_objc_class_declaration (parser);
22474       break;
22475     case RID_AT_PROTOCOL:
22476       cp_parser_objc_protocol_declaration (parser, attributes);
22477       break;
22478     case RID_AT_INTERFACE:
22479       cp_parser_objc_class_interface (parser, attributes);
22480       break;
22481     case RID_AT_IMPLEMENTATION:
22482       cp_parser_objc_class_implementation (parser);
22483       break;
22484     case RID_AT_END:
22485       cp_parser_objc_end_implementation (parser);
22486       break;
22487     default:
22488       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22489                 kwd->u.value);
22490       cp_parser_skip_to_end_of_block_or_statement (parser);
22491     }
22492 }
22493
22494 /* Parse an Objective-C try-catch-finally statement.
22495
22496    objc-try-catch-finally-stmt:
22497      @try compound-statement objc-catch-clause-seq [opt]
22498        objc-finally-clause [opt]
22499
22500    objc-catch-clause-seq:
22501      objc-catch-clause objc-catch-clause-seq [opt]
22502
22503    objc-catch-clause:
22504      @catch ( exception-declaration ) compound-statement
22505
22506    objc-finally-clause
22507      @finally compound-statement
22508
22509    Returns NULL_TREE.  */
22510
22511 static tree
22512 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22513   location_t location;
22514   tree stmt;
22515
22516   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22517   location = cp_lexer_peek_token (parser->lexer)->location;
22518   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22519      node, lest it get absorbed into the surrounding block.  */
22520   stmt = push_stmt_list ();
22521   cp_parser_compound_statement (parser, NULL, false);
22522   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22523
22524   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22525     {
22526       cp_parameter_declarator *parmdecl;
22527       tree parm;
22528
22529       cp_lexer_consume_token (parser->lexer);
22530       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22531       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22532       parm = grokdeclarator (parmdecl->declarator,
22533                              &parmdecl->decl_specifiers,
22534                              PARM, /*initialized=*/0,
22535                              /*attrlist=*/NULL);
22536       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22537       objc_begin_catch_clause (parm);
22538       cp_parser_compound_statement (parser, NULL, false);
22539       objc_finish_catch_clause ();
22540     }
22541
22542   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22543     {
22544       cp_lexer_consume_token (parser->lexer);
22545       location = cp_lexer_peek_token (parser->lexer)->location;
22546       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22547          node, lest it get absorbed into the surrounding block.  */
22548       stmt = push_stmt_list ();
22549       cp_parser_compound_statement (parser, NULL, false);
22550       objc_build_finally_clause (location, pop_stmt_list (stmt));
22551     }
22552
22553   return objc_finish_try_stmt ();
22554 }
22555
22556 /* Parse an Objective-C synchronized statement.
22557
22558    objc-synchronized-stmt:
22559      @synchronized ( expression ) compound-statement
22560
22561    Returns NULL_TREE.  */
22562
22563 static tree
22564 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22565   location_t location;
22566   tree lock, stmt;
22567
22568   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22569
22570   location = cp_lexer_peek_token (parser->lexer)->location;
22571   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22572   lock = cp_parser_expression (parser, false, NULL);
22573   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22574
22575   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22576      node, lest it get absorbed into the surrounding block.  */
22577   stmt = push_stmt_list ();
22578   cp_parser_compound_statement (parser, NULL, false);
22579
22580   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22581 }
22582
22583 /* Parse an Objective-C throw statement.
22584
22585    objc-throw-stmt:
22586      @throw assignment-expression [opt] ;
22587
22588    Returns a constructed '@throw' statement.  */
22589
22590 static tree
22591 cp_parser_objc_throw_statement (cp_parser *parser) {
22592   tree expr = NULL_TREE;
22593   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22594
22595   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22596
22597   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22598     expr = cp_parser_assignment_expression (parser, false, NULL);
22599
22600   cp_parser_consume_semicolon_at_end_of_statement (parser);
22601
22602   return objc_build_throw_stmt (loc, expr);
22603 }
22604
22605 /* Parse an Objective-C statement.  */
22606
22607 static tree
22608 cp_parser_objc_statement (cp_parser * parser) {
22609   /* Try to figure out what kind of declaration is present.  */
22610   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22611
22612   switch (kwd->keyword)
22613     {
22614     case RID_AT_TRY:
22615       return cp_parser_objc_try_catch_finally_statement (parser);
22616     case RID_AT_SYNCHRONIZED:
22617       return cp_parser_objc_synchronized_statement (parser);
22618     case RID_AT_THROW:
22619       return cp_parser_objc_throw_statement (parser);
22620     default:
22621       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22622                kwd->u.value);
22623       cp_parser_skip_to_end_of_block_or_statement (parser);
22624     }
22625
22626   return error_mark_node;
22627 }
22628
22629 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22630    look ahead to see if an objc keyword follows the attributes.  This
22631    is to detect the use of prefix attributes on ObjC @interface and 
22632    @protocol.  */
22633
22634 static bool
22635 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22636 {
22637   cp_lexer_save_tokens (parser->lexer);
22638   *attrib = cp_parser_attributes_opt (parser);
22639   gcc_assert (*attrib);
22640   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22641     {
22642       cp_lexer_commit_tokens (parser->lexer);
22643       return true;
22644     }
22645   cp_lexer_rollback_tokens (parser->lexer);
22646   return false;  
22647 }
22648
22649 /* This routine is a minimal replacement for
22650    c_parser_struct_declaration () used when parsing the list of
22651    types/names or ObjC++ properties.  For example, when parsing the
22652    code
22653
22654    @property (readonly) int a, b, c;
22655
22656    this function is responsible for parsing "int a, int b, int c" and
22657    returning the declarations as CHAIN of DECLs.
22658
22659    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22660    similar parsing.  */
22661 static tree
22662 cp_parser_objc_struct_declaration (cp_parser *parser)
22663 {
22664   tree decls = NULL_TREE;
22665   cp_decl_specifier_seq declspecs;
22666   int decl_class_or_enum_p;
22667   tree prefix_attributes;
22668
22669   cp_parser_decl_specifier_seq (parser,
22670                                 CP_PARSER_FLAGS_NONE,
22671                                 &declspecs,
22672                                 &decl_class_or_enum_p);
22673
22674   if (declspecs.type == error_mark_node)
22675     return error_mark_node;
22676
22677   /* auto, register, static, extern, mutable.  */
22678   if (declspecs.storage_class != sc_none)
22679     {
22680       cp_parser_error (parser, "invalid type for property");
22681       declspecs.storage_class = sc_none;
22682     }
22683   
22684   /* __thread.  */
22685   if (declspecs.specs[(int) ds_thread])
22686     {
22687       cp_parser_error (parser, "invalid type for property");
22688       declspecs.specs[(int) ds_thread] = 0;
22689     }
22690   
22691   /* typedef.  */
22692   if (declspecs.specs[(int) ds_typedef])
22693     {
22694       cp_parser_error (parser, "invalid type for property");
22695       declspecs.specs[(int) ds_typedef] = 0;
22696     }
22697
22698   prefix_attributes = declspecs.attributes;
22699   declspecs.attributes = NULL_TREE;
22700
22701   /* Keep going until we hit the `;' at the end of the declaration. */
22702   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22703     {
22704       tree attributes, first_attribute, decl;
22705       cp_declarator *declarator;
22706       cp_token *token;
22707
22708       /* Parse the declarator.  */
22709       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22710                                          NULL, NULL, false);
22711
22712       /* Look for attributes that apply to the ivar.  */
22713       attributes = cp_parser_attributes_opt (parser);
22714       /* Remember which attributes are prefix attributes and
22715          which are not.  */
22716       first_attribute = attributes;
22717       /* Combine the attributes.  */
22718       attributes = chainon (prefix_attributes, attributes);
22719       
22720       decl = grokfield (declarator, &declspecs,
22721                         NULL_TREE, /*init_const_expr_p=*/false,
22722                         NULL_TREE, attributes);
22723
22724       if (decl == error_mark_node || decl == NULL_TREE)
22725         return error_mark_node;
22726       
22727       /* Reset PREFIX_ATTRIBUTES.  */
22728       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22729         attributes = TREE_CHAIN (attributes);
22730       if (attributes)
22731         TREE_CHAIN (attributes) = NULL_TREE;
22732
22733       DECL_CHAIN (decl) = decls;
22734       decls = decl;
22735
22736       token = cp_lexer_peek_token (parser->lexer);
22737       if (token->type == CPP_COMMA)
22738         {
22739           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22740           continue;
22741         }
22742       else
22743         break;
22744     }
22745   return decls;
22746 }
22747
22748 /* Parse an Objective-C @property declaration.  The syntax is:
22749
22750    objc-property-declaration:
22751      '@property' objc-property-attributes[opt] struct-declaration ;
22752
22753    objc-property-attributes:
22754     '(' objc-property-attribute-list ')'
22755
22756    objc-property-attribute-list:
22757      objc-property-attribute
22758      objc-property-attribute-list, objc-property-attribute
22759
22760    objc-property-attribute
22761      'getter' = identifier
22762      'setter' = identifier
22763      'readonly'
22764      'readwrite'
22765      'assign'
22766      'retain'
22767      'copy'
22768      'nonatomic'
22769
22770   For example:
22771     @property NSString *name;
22772     @property (readonly) id object;
22773     @property (retain, nonatomic, getter=getTheName) id name;
22774     @property int a, b, c;
22775
22776    PS: This function is identical to
22777    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
22778 static void 
22779 cp_parser_objc_at_property_declaration (cp_parser *parser)
22780 {
22781   /* The following variables hold the attributes of the properties as
22782      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
22783      seen.  When we see an attribute, we set them to 'true' (if they
22784      are boolean properties) or to the identifier (if they have an
22785      argument, ie, for getter and setter).  Note that here we only
22786      parse the list of attributes, check the syntax and accumulate the
22787      attributes that we find.  objc_add_property_declaration() will
22788      then process the information.  */
22789   bool property_assign = false;
22790   bool property_copy = false;
22791   tree property_getter_ident = NULL_TREE;
22792   bool property_nonatomic = false;
22793   bool property_readonly = false;
22794   bool property_readwrite = false;
22795   bool property_retain = false;
22796   tree property_setter_ident = NULL_TREE;
22797
22798   /* 'properties' is the list of properties that we read.  Usually a
22799      single one, but maybe more (eg, in "@property int a, b, c;" there
22800      are three).  */
22801   tree properties;
22802   location_t loc;
22803
22804   loc = cp_lexer_peek_token (parser->lexer)->location;
22805
22806   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
22807
22808   /* Parse the optional attribute list...  */
22809   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22810     {
22811       /* Eat the '('.  */
22812       cp_lexer_consume_token (parser->lexer);
22813
22814       while (true)
22815         {
22816           bool syntax_error = false;
22817           cp_token *token = cp_lexer_peek_token (parser->lexer);
22818           enum rid keyword;
22819
22820           if (token->type != CPP_NAME)
22821             {
22822               cp_parser_error (parser, "expected identifier");
22823               break;
22824             }
22825           keyword = C_RID_CODE (token->u.value);
22826           cp_lexer_consume_token (parser->lexer);
22827           switch (keyword)
22828             {
22829             case RID_ASSIGN:    property_assign = true;    break;
22830             case RID_COPY:      property_copy = true;      break;
22831             case RID_NONATOMIC: property_nonatomic = true; break;
22832             case RID_READONLY:  property_readonly = true;  break;
22833             case RID_READWRITE: property_readwrite = true; break;
22834             case RID_RETAIN:    property_retain = true;    break;
22835
22836             case RID_GETTER:
22837             case RID_SETTER:
22838               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22839                 {
22840                   cp_parser_error (parser,
22841                                    "getter/setter/ivar attribute must be followed by %<=%>");
22842                   syntax_error = true;
22843                   break;
22844                 }
22845               cp_lexer_consume_token (parser->lexer); /* eat the = */
22846               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22847                 {
22848                   cp_parser_error (parser, "expected identifier");
22849                   syntax_error = true;
22850                   break;
22851                 }
22852               if (keyword == RID_SETTER)
22853                 {
22854                   if (property_setter_ident != NULL_TREE)
22855                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22856                   else
22857                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22858                   cp_lexer_consume_token (parser->lexer);
22859                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22860                     cp_parser_error (parser, "setter name must terminate with %<:%>");
22861                   else
22862                     cp_lexer_consume_token (parser->lexer);
22863                 }
22864               else
22865                 {
22866                   if (property_getter_ident != NULL_TREE)
22867                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22868                   else
22869                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22870                   cp_lexer_consume_token (parser->lexer);
22871                 }
22872               break;
22873             default:
22874               cp_parser_error (parser, "unknown property attribute");
22875               syntax_error = true;
22876               break;
22877             }
22878
22879           if (syntax_error)
22880             break;
22881           
22882           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22883             cp_lexer_consume_token (parser->lexer);
22884           else
22885             break;
22886         }
22887
22888       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22889         {
22890           cp_parser_skip_to_closing_parenthesis (parser,
22891                                                  /*recovering=*/true,
22892                                                  /*or_comma=*/false,
22893                                                  /*consume_paren=*/true);
22894         }
22895     }
22896
22897   /* ... and the property declaration(s).  */
22898   properties = cp_parser_objc_struct_declaration (parser);
22899
22900   if (properties == error_mark_node)
22901     {
22902       cp_parser_skip_to_end_of_statement (parser);
22903       /* If the next token is now a `;', consume it.  */
22904       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22905         cp_lexer_consume_token (parser->lexer);
22906       return;
22907     }
22908
22909   if (properties == NULL_TREE)
22910     cp_parser_error (parser, "expected identifier");
22911   else
22912     {
22913       /* Comma-separated properties are chained together in
22914          reverse order; add them one by one.  */
22915       properties = nreverse (properties);
22916       
22917       for (; properties; properties = TREE_CHAIN (properties))
22918         objc_add_property_declaration (loc, copy_node (properties),
22919                                        property_readonly, property_readwrite,
22920                                        property_assign, property_retain,
22921                                        property_copy, property_nonatomic,
22922                                        property_getter_ident, property_setter_ident);
22923     }
22924   
22925   cp_parser_consume_semicolon_at_end_of_statement (parser);
22926 }
22927
22928 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
22929
22930    objc-synthesize-declaration:
22931      @synthesize objc-synthesize-identifier-list ;
22932
22933    objc-synthesize-identifier-list:
22934      objc-synthesize-identifier
22935      objc-synthesize-identifier-list, objc-synthesize-identifier
22936
22937    objc-synthesize-identifier
22938      identifier
22939      identifier = identifier
22940
22941   For example:
22942     @synthesize MyProperty;
22943     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22944
22945   PS: This function is identical to c_parser_objc_at_synthesize_declaration
22946   for C.  Keep them in sync.
22947 */
22948 static void 
22949 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22950 {
22951   tree list = NULL_TREE;
22952   location_t loc;
22953   loc = cp_lexer_peek_token (parser->lexer)->location;
22954
22955   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
22956   while (true)
22957     {
22958       tree property, ivar;
22959       property = cp_parser_identifier (parser);
22960       if (property == error_mark_node)
22961         {
22962           cp_parser_consume_semicolon_at_end_of_statement (parser);
22963           return;
22964         }
22965       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22966         {
22967           cp_lexer_consume_token (parser->lexer);
22968           ivar = cp_parser_identifier (parser);
22969           if (ivar == error_mark_node)
22970             {
22971               cp_parser_consume_semicolon_at_end_of_statement (parser);
22972               return;
22973             }
22974         }
22975       else
22976         ivar = NULL_TREE;
22977       list = chainon (list, build_tree_list (ivar, property));
22978       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22979         cp_lexer_consume_token (parser->lexer);
22980       else
22981         break;
22982     }
22983   cp_parser_consume_semicolon_at_end_of_statement (parser);
22984   objc_add_synthesize_declaration (loc, list);
22985 }
22986
22987 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
22988
22989    objc-dynamic-declaration:
22990      @dynamic identifier-list ;
22991
22992    For example:
22993      @dynamic MyProperty;
22994      @dynamic MyProperty, AnotherProperty;
22995
22996   PS: This function is identical to c_parser_objc_at_dynamic_declaration
22997   for C.  Keep them in sync.
22998 */
22999 static void 
23000 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23001 {
23002   tree list = NULL_TREE;
23003   location_t loc;
23004   loc = cp_lexer_peek_token (parser->lexer)->location;
23005
23006   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23007   while (true)
23008     {
23009       tree property;
23010       property = cp_parser_identifier (parser);
23011       if (property == error_mark_node)
23012         {
23013           cp_parser_consume_semicolon_at_end_of_statement (parser);
23014           return;
23015         }
23016       list = chainon (list, build_tree_list (NULL, property));
23017       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23018         cp_lexer_consume_token (parser->lexer);
23019       else
23020         break;
23021     }
23022   cp_parser_consume_semicolon_at_end_of_statement (parser);
23023   objc_add_dynamic_declaration (loc, list);
23024 }
23025
23026 \f
23027 /* OpenMP 2.5 parsing routines.  */
23028
23029 /* Returns name of the next clause.
23030    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23031    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23032    returned and the token is consumed.  */
23033
23034 static pragma_omp_clause
23035 cp_parser_omp_clause_name (cp_parser *parser)
23036 {
23037   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23038
23039   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23040     result = PRAGMA_OMP_CLAUSE_IF;
23041   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23042     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23043   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23044     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23045   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23046     {
23047       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23048       const char *p = IDENTIFIER_POINTER (id);
23049
23050       switch (p[0])
23051         {
23052         case 'c':
23053           if (!strcmp ("collapse", p))
23054             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23055           else if (!strcmp ("copyin", p))
23056             result = PRAGMA_OMP_CLAUSE_COPYIN;
23057           else if (!strcmp ("copyprivate", p))
23058             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23059           break;
23060         case 'f':
23061           if (!strcmp ("firstprivate", p))
23062             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23063           break;
23064         case 'l':
23065           if (!strcmp ("lastprivate", p))
23066             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23067           break;
23068         case 'n':
23069           if (!strcmp ("nowait", p))
23070             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23071           else if (!strcmp ("num_threads", p))
23072             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23073           break;
23074         case 'o':
23075           if (!strcmp ("ordered", p))
23076             result = PRAGMA_OMP_CLAUSE_ORDERED;
23077           break;
23078         case 'r':
23079           if (!strcmp ("reduction", p))
23080             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23081           break;
23082         case 's':
23083           if (!strcmp ("schedule", p))
23084             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23085           else if (!strcmp ("shared", p))
23086             result = PRAGMA_OMP_CLAUSE_SHARED;
23087           break;
23088         case 'u':
23089           if (!strcmp ("untied", p))
23090             result = PRAGMA_OMP_CLAUSE_UNTIED;
23091           break;
23092         }
23093     }
23094
23095   if (result != PRAGMA_OMP_CLAUSE_NONE)
23096     cp_lexer_consume_token (parser->lexer);
23097
23098   return result;
23099 }
23100
23101 /* Validate that a clause of the given type does not already exist.  */
23102
23103 static void
23104 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23105                            const char *name, location_t location)
23106 {
23107   tree c;
23108
23109   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23110     if (OMP_CLAUSE_CODE (c) == code)
23111       {
23112         error_at (location, "too many %qs clauses", name);
23113         break;
23114       }
23115 }
23116
23117 /* OpenMP 2.5:
23118    variable-list:
23119      identifier
23120      variable-list , identifier
23121
23122    In addition, we match a closing parenthesis.  An opening parenthesis
23123    will have been consumed by the caller.
23124
23125    If KIND is nonzero, create the appropriate node and install the decl
23126    in OMP_CLAUSE_DECL and add the node to the head of the list.
23127
23128    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23129    return the list created.  */
23130
23131 static tree
23132 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23133                                 tree list)
23134 {
23135   cp_token *token;
23136   while (1)
23137     {
23138       tree name, decl;
23139
23140       token = cp_lexer_peek_token (parser->lexer);
23141       name = cp_parser_id_expression (parser, /*template_p=*/false,
23142                                       /*check_dependency_p=*/true,
23143                                       /*template_p=*/NULL,
23144                                       /*declarator_p=*/false,
23145                                       /*optional_p=*/false);
23146       if (name == error_mark_node)
23147         goto skip_comma;
23148
23149       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23150       if (decl == error_mark_node)
23151         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23152                                      token->location);
23153       else if (kind != 0)
23154         {
23155           tree u = build_omp_clause (token->location, kind);
23156           OMP_CLAUSE_DECL (u) = decl;
23157           OMP_CLAUSE_CHAIN (u) = list;
23158           list = u;
23159         }
23160       else
23161         list = tree_cons (decl, NULL_TREE, list);
23162
23163     get_comma:
23164       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23165         break;
23166       cp_lexer_consume_token (parser->lexer);
23167     }
23168
23169   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23170     {
23171       int ending;
23172
23173       /* Try to resync to an unnested comma.  Copied from
23174          cp_parser_parenthesized_expression_list.  */
23175     skip_comma:
23176       ending = cp_parser_skip_to_closing_parenthesis (parser,
23177                                                       /*recovering=*/true,
23178                                                       /*or_comma=*/true,
23179                                                       /*consume_paren=*/true);
23180       if (ending < 0)
23181         goto get_comma;
23182     }
23183
23184   return list;
23185 }
23186
23187 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23188    common case for omp clauses.  */
23189
23190 static tree
23191 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23192 {
23193   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23194     return cp_parser_omp_var_list_no_open (parser, kind, list);
23195   return list;
23196 }
23197
23198 /* OpenMP 3.0:
23199    collapse ( constant-expression ) */
23200
23201 static tree
23202 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23203 {
23204   tree c, num;
23205   location_t loc;
23206   HOST_WIDE_INT n;
23207
23208   loc = cp_lexer_peek_token (parser->lexer)->location;
23209   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23210     return list;
23211
23212   num = cp_parser_constant_expression (parser, false, NULL);
23213
23214   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23215     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23216                                            /*or_comma=*/false,
23217                                            /*consume_paren=*/true);
23218
23219   if (num == error_mark_node)
23220     return list;
23221   num = fold_non_dependent_expr (num);
23222   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23223       || !host_integerp (num, 0)
23224       || (n = tree_low_cst (num, 0)) <= 0
23225       || (int) n != n)
23226     {
23227       error_at (loc, "collapse argument needs positive constant integer expression");
23228       return list;
23229     }
23230
23231   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23232   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23233   OMP_CLAUSE_CHAIN (c) = list;
23234   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23235
23236   return c;
23237 }
23238
23239 /* OpenMP 2.5:
23240    default ( shared | none ) */
23241
23242 static tree
23243 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23244 {
23245   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23246   tree c;
23247
23248   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23249     return list;
23250   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23251     {
23252       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23253       const char *p = IDENTIFIER_POINTER (id);
23254
23255       switch (p[0])
23256         {
23257         case 'n':
23258           if (strcmp ("none", p) != 0)
23259             goto invalid_kind;
23260           kind = OMP_CLAUSE_DEFAULT_NONE;
23261           break;
23262
23263         case 's':
23264           if (strcmp ("shared", p) != 0)
23265             goto invalid_kind;
23266           kind = OMP_CLAUSE_DEFAULT_SHARED;
23267           break;
23268
23269         default:
23270           goto invalid_kind;
23271         }
23272
23273       cp_lexer_consume_token (parser->lexer);
23274     }
23275   else
23276     {
23277     invalid_kind:
23278       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23279     }
23280
23281   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23282     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23283                                            /*or_comma=*/false,
23284                                            /*consume_paren=*/true);
23285
23286   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23287     return list;
23288
23289   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23290   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23291   OMP_CLAUSE_CHAIN (c) = list;
23292   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23293
23294   return c;
23295 }
23296
23297 /* OpenMP 2.5:
23298    if ( expression ) */
23299
23300 static tree
23301 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23302 {
23303   tree t, c;
23304
23305   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23306     return list;
23307
23308   t = cp_parser_condition (parser);
23309
23310   if (t == error_mark_node
23311       || !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   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23317
23318   c = build_omp_clause (location, OMP_CLAUSE_IF);
23319   OMP_CLAUSE_IF_EXPR (c) = t;
23320   OMP_CLAUSE_CHAIN (c) = list;
23321
23322   return c;
23323 }
23324
23325 /* OpenMP 2.5:
23326    nowait */
23327
23328 static tree
23329 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23330                              tree list, location_t location)
23331 {
23332   tree c;
23333
23334   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23335
23336   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23337   OMP_CLAUSE_CHAIN (c) = list;
23338   return c;
23339 }
23340
23341 /* OpenMP 2.5:
23342    num_threads ( expression ) */
23343
23344 static tree
23345 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23346                                   location_t location)
23347 {
23348   tree t, c;
23349
23350   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23351     return list;
23352
23353   t = cp_parser_expression (parser, false, NULL);
23354
23355   if (t == error_mark_node
23356       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23357     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23358                                            /*or_comma=*/false,
23359                                            /*consume_paren=*/true);
23360
23361   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23362                              "num_threads", location);
23363
23364   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23365   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23366   OMP_CLAUSE_CHAIN (c) = list;
23367
23368   return c;
23369 }
23370
23371 /* OpenMP 2.5:
23372    ordered */
23373
23374 static tree
23375 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23376                               tree list, location_t location)
23377 {
23378   tree c;
23379
23380   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23381                              "ordered", location);
23382
23383   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23384   OMP_CLAUSE_CHAIN (c) = list;
23385   return c;
23386 }
23387
23388 /* OpenMP 2.5:
23389    reduction ( reduction-operator : variable-list )
23390
23391    reduction-operator:
23392      One of: + * - & ^ | && || */
23393
23394 static tree
23395 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23396 {
23397   enum tree_code code;
23398   tree nlist, c;
23399
23400   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23401     return list;
23402
23403   switch (cp_lexer_peek_token (parser->lexer)->type)
23404     {
23405     case CPP_PLUS:
23406       code = PLUS_EXPR;
23407       break;
23408     case CPP_MULT:
23409       code = MULT_EXPR;
23410       break;
23411     case CPP_MINUS:
23412       code = MINUS_EXPR;
23413       break;
23414     case CPP_AND:
23415       code = BIT_AND_EXPR;
23416       break;
23417     case CPP_XOR:
23418       code = BIT_XOR_EXPR;
23419       break;
23420     case CPP_OR:
23421       code = BIT_IOR_EXPR;
23422       break;
23423     case CPP_AND_AND:
23424       code = TRUTH_ANDIF_EXPR;
23425       break;
23426     case CPP_OR_OR:
23427       code = TRUTH_ORIF_EXPR;
23428       break;
23429     default:
23430       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23431                                "%<|%>, %<&&%>, or %<||%>");
23432     resync_fail:
23433       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23434                                              /*or_comma=*/false,
23435                                              /*consume_paren=*/true);
23436       return list;
23437     }
23438   cp_lexer_consume_token (parser->lexer);
23439
23440   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23441     goto resync_fail;
23442
23443   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23444   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23445     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23446
23447   return nlist;
23448 }
23449
23450 /* OpenMP 2.5:
23451    schedule ( schedule-kind )
23452    schedule ( schedule-kind , expression )
23453
23454    schedule-kind:
23455      static | dynamic | guided | runtime | auto  */
23456
23457 static tree
23458 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23459 {
23460   tree c, t;
23461
23462   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23463     return list;
23464
23465   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23466
23467   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23468     {
23469       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23470       const char *p = IDENTIFIER_POINTER (id);
23471
23472       switch (p[0])
23473         {
23474         case 'd':
23475           if (strcmp ("dynamic", p) != 0)
23476             goto invalid_kind;
23477           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23478           break;
23479
23480         case 'g':
23481           if (strcmp ("guided", p) != 0)
23482             goto invalid_kind;
23483           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23484           break;
23485
23486         case 'r':
23487           if (strcmp ("runtime", p) != 0)
23488             goto invalid_kind;
23489           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23490           break;
23491
23492         default:
23493           goto invalid_kind;
23494         }
23495     }
23496   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23497     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23498   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23499     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23500   else
23501     goto invalid_kind;
23502   cp_lexer_consume_token (parser->lexer);
23503
23504   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23505     {
23506       cp_token *token;
23507       cp_lexer_consume_token (parser->lexer);
23508
23509       token = cp_lexer_peek_token (parser->lexer);
23510       t = cp_parser_assignment_expression (parser, false, NULL);
23511
23512       if (t == error_mark_node)
23513         goto resync_fail;
23514       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23515         error_at (token->location, "schedule %<runtime%> does not take "
23516                   "a %<chunk_size%> parameter");
23517       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23518         error_at (token->location, "schedule %<auto%> does not take "
23519                   "a %<chunk_size%> parameter");
23520       else
23521         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23522
23523       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23524         goto resync_fail;
23525     }
23526   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23527     goto resync_fail;
23528
23529   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23530   OMP_CLAUSE_CHAIN (c) = list;
23531   return c;
23532
23533  invalid_kind:
23534   cp_parser_error (parser, "invalid schedule kind");
23535  resync_fail:
23536   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23537                                          /*or_comma=*/false,
23538                                          /*consume_paren=*/true);
23539   return list;
23540 }
23541
23542 /* OpenMP 3.0:
23543    untied */
23544
23545 static tree
23546 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23547                              tree list, location_t location)
23548 {
23549   tree c;
23550
23551   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23552
23553   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23554   OMP_CLAUSE_CHAIN (c) = list;
23555   return c;
23556 }
23557
23558 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23559    is a bitmask in MASK.  Return the list of clauses found; the result
23560    of clause default goes in *pdefault.  */
23561
23562 static tree
23563 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23564                            const char *where, cp_token *pragma_tok)
23565 {
23566   tree clauses = NULL;
23567   bool first = true;
23568   cp_token *token = NULL;
23569
23570   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23571     {
23572       pragma_omp_clause c_kind;
23573       const char *c_name;
23574       tree prev = clauses;
23575
23576       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23577         cp_lexer_consume_token (parser->lexer);
23578
23579       token = cp_lexer_peek_token (parser->lexer);
23580       c_kind = cp_parser_omp_clause_name (parser);
23581       first = false;
23582
23583       switch (c_kind)
23584         {
23585         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23586           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23587                                                    token->location);
23588           c_name = "collapse";
23589           break;
23590         case PRAGMA_OMP_CLAUSE_COPYIN:
23591           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23592           c_name = "copyin";
23593           break;
23594         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23595           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23596                                             clauses);
23597           c_name = "copyprivate";
23598           break;
23599         case PRAGMA_OMP_CLAUSE_DEFAULT:
23600           clauses = cp_parser_omp_clause_default (parser, clauses,
23601                                                   token->location);
23602           c_name = "default";
23603           break;
23604         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23605           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23606                                             clauses);
23607           c_name = "firstprivate";
23608           break;
23609         case PRAGMA_OMP_CLAUSE_IF:
23610           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23611           c_name = "if";
23612           break;
23613         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23614           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23615                                             clauses);
23616           c_name = "lastprivate";
23617           break;
23618         case PRAGMA_OMP_CLAUSE_NOWAIT:
23619           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23620           c_name = "nowait";
23621           break;
23622         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23623           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23624                                                       token->location);
23625           c_name = "num_threads";
23626           break;
23627         case PRAGMA_OMP_CLAUSE_ORDERED:
23628           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23629                                                   token->location);
23630           c_name = "ordered";
23631           break;
23632         case PRAGMA_OMP_CLAUSE_PRIVATE:
23633           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23634                                             clauses);
23635           c_name = "private";
23636           break;
23637         case PRAGMA_OMP_CLAUSE_REDUCTION:
23638           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23639           c_name = "reduction";
23640           break;
23641         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23642           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23643                                                    token->location);
23644           c_name = "schedule";
23645           break;
23646         case PRAGMA_OMP_CLAUSE_SHARED:
23647           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23648                                             clauses);
23649           c_name = "shared";
23650           break;
23651         case PRAGMA_OMP_CLAUSE_UNTIED:
23652           clauses = cp_parser_omp_clause_untied (parser, clauses,
23653                                                  token->location);
23654           c_name = "nowait";
23655           break;
23656         default:
23657           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23658           goto saw_error;
23659         }
23660
23661       if (((mask >> c_kind) & 1) == 0)
23662         {
23663           /* Remove the invalid clause(s) from the list to avoid
23664              confusing the rest of the compiler.  */
23665           clauses = prev;
23666           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23667         }
23668     }
23669  saw_error:
23670   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23671   return finish_omp_clauses (clauses);
23672 }
23673
23674 /* OpenMP 2.5:
23675    structured-block:
23676      statement
23677
23678    In practice, we're also interested in adding the statement to an
23679    outer node.  So it is convenient if we work around the fact that
23680    cp_parser_statement calls add_stmt.  */
23681
23682 static unsigned
23683 cp_parser_begin_omp_structured_block (cp_parser *parser)
23684 {
23685   unsigned save = parser->in_statement;
23686
23687   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23688      This preserves the "not within loop or switch" style error messages
23689      for nonsense cases like
23690         void foo() {
23691         #pragma omp single
23692           break;
23693         }
23694   */
23695   if (parser->in_statement)
23696     parser->in_statement = IN_OMP_BLOCK;
23697
23698   return save;
23699 }
23700
23701 static void
23702 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23703 {
23704   parser->in_statement = save;
23705 }
23706
23707 static tree
23708 cp_parser_omp_structured_block (cp_parser *parser)
23709 {
23710   tree stmt = begin_omp_structured_block ();
23711   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23712
23713   cp_parser_statement (parser, NULL_TREE, false, NULL);
23714
23715   cp_parser_end_omp_structured_block (parser, save);
23716   return finish_omp_structured_block (stmt);
23717 }
23718
23719 /* OpenMP 2.5:
23720    # pragma omp atomic new-line
23721      expression-stmt
23722
23723    expression-stmt:
23724      x binop= expr | x++ | ++x | x-- | --x
23725    binop:
23726      +, *, -, /, &, ^, |, <<, >>
23727
23728   where x is an lvalue expression with scalar type.  */
23729
23730 static void
23731 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23732 {
23733   tree lhs, rhs;
23734   enum tree_code code;
23735
23736   cp_parser_require_pragma_eol (parser, pragma_tok);
23737
23738   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23739                                     /*cast_p=*/false, NULL);
23740   switch (TREE_CODE (lhs))
23741     {
23742     case ERROR_MARK:
23743       goto saw_error;
23744
23745     case PREINCREMENT_EXPR:
23746     case POSTINCREMENT_EXPR:
23747       lhs = TREE_OPERAND (lhs, 0);
23748       code = PLUS_EXPR;
23749       rhs = integer_one_node;
23750       break;
23751
23752     case PREDECREMENT_EXPR:
23753     case POSTDECREMENT_EXPR:
23754       lhs = TREE_OPERAND (lhs, 0);
23755       code = MINUS_EXPR;
23756       rhs = integer_one_node;
23757       break;
23758
23759     case COMPOUND_EXPR:
23760       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23761          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23762          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23763          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23764          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23765                                              (TREE_OPERAND (lhs, 1), 0), 0)))
23766             == BOOLEAN_TYPE)
23767        /* Undo effects of boolean_increment for post {in,de}crement.  */
23768        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23769       /* FALLTHRU */
23770     case MODIFY_EXPR:
23771       if (TREE_CODE (lhs) == MODIFY_EXPR
23772          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23773        {
23774          /* Undo effects of boolean_increment.  */
23775          if (integer_onep (TREE_OPERAND (lhs, 1)))
23776            {
23777              /* This is pre or post increment.  */
23778              rhs = TREE_OPERAND (lhs, 1);
23779              lhs = TREE_OPERAND (lhs, 0);
23780              code = NOP_EXPR;
23781              break;
23782            }
23783        }
23784       /* FALLTHRU */
23785     default:
23786       switch (cp_lexer_peek_token (parser->lexer)->type)
23787         {
23788         case CPP_MULT_EQ:
23789           code = MULT_EXPR;
23790           break;
23791         case CPP_DIV_EQ:
23792           code = TRUNC_DIV_EXPR;
23793           break;
23794         case CPP_PLUS_EQ:
23795           code = PLUS_EXPR;
23796           break;
23797         case CPP_MINUS_EQ:
23798           code = MINUS_EXPR;
23799           break;
23800         case CPP_LSHIFT_EQ:
23801           code = LSHIFT_EXPR;
23802           break;
23803         case CPP_RSHIFT_EQ:
23804           code = RSHIFT_EXPR;
23805           break;
23806         case CPP_AND_EQ:
23807           code = BIT_AND_EXPR;
23808           break;
23809         case CPP_OR_EQ:
23810           code = BIT_IOR_EXPR;
23811           break;
23812         case CPP_XOR_EQ:
23813           code = BIT_XOR_EXPR;
23814           break;
23815         default:
23816           cp_parser_error (parser,
23817                            "invalid operator for %<#pragma omp atomic%>");
23818           goto saw_error;
23819         }
23820       cp_lexer_consume_token (parser->lexer);
23821
23822       rhs = cp_parser_expression (parser, false, NULL);
23823       if (rhs == error_mark_node)
23824         goto saw_error;
23825       break;
23826     }
23827   finish_omp_atomic (code, lhs, rhs);
23828   cp_parser_consume_semicolon_at_end_of_statement (parser);
23829   return;
23830
23831  saw_error:
23832   cp_parser_skip_to_end_of_block_or_statement (parser);
23833 }
23834
23835
23836 /* OpenMP 2.5:
23837    # pragma omp barrier new-line  */
23838
23839 static void
23840 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23841 {
23842   cp_parser_require_pragma_eol (parser, pragma_tok);
23843   finish_omp_barrier ();
23844 }
23845
23846 /* OpenMP 2.5:
23847    # pragma omp critical [(name)] new-line
23848      structured-block  */
23849
23850 static tree
23851 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23852 {
23853   tree stmt, name = NULL;
23854
23855   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23856     {
23857       cp_lexer_consume_token (parser->lexer);
23858
23859       name = cp_parser_identifier (parser);
23860
23861       if (name == error_mark_node
23862           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23863         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23864                                                /*or_comma=*/false,
23865                                                /*consume_paren=*/true);
23866       if (name == error_mark_node)
23867         name = NULL;
23868     }
23869   cp_parser_require_pragma_eol (parser, pragma_tok);
23870
23871   stmt = cp_parser_omp_structured_block (parser);
23872   return c_finish_omp_critical (input_location, stmt, name);
23873 }
23874
23875 /* OpenMP 2.5:
23876    # pragma omp flush flush-vars[opt] new-line
23877
23878    flush-vars:
23879      ( variable-list ) */
23880
23881 static void
23882 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23883 {
23884   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23885     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23886   cp_parser_require_pragma_eol (parser, pragma_tok);
23887
23888   finish_omp_flush ();
23889 }
23890
23891 /* Helper function, to parse omp for increment expression.  */
23892
23893 static tree
23894 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23895 {
23896   tree cond = cp_parser_binary_expression (parser, false, true,
23897                                            PREC_NOT_OPERATOR, NULL);
23898   bool overloaded_p;
23899
23900   if (cond == error_mark_node
23901       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23902     {
23903       cp_parser_skip_to_end_of_statement (parser);
23904       return error_mark_node;
23905     }
23906
23907   switch (TREE_CODE (cond))
23908     {
23909     case GT_EXPR:
23910     case GE_EXPR:
23911     case LT_EXPR:
23912     case LE_EXPR:
23913       break;
23914     default:
23915       return error_mark_node;
23916     }
23917
23918   /* If decl is an iterator, preserve LHS and RHS of the relational
23919      expr until finish_omp_for.  */
23920   if (decl
23921       && (type_dependent_expression_p (decl)
23922           || CLASS_TYPE_P (TREE_TYPE (decl))))
23923     return cond;
23924
23925   return build_x_binary_op (TREE_CODE (cond),
23926                             TREE_OPERAND (cond, 0), ERROR_MARK,
23927                             TREE_OPERAND (cond, 1), ERROR_MARK,
23928                             &overloaded_p, tf_warning_or_error);
23929 }
23930
23931 /* Helper function, to parse omp for increment expression.  */
23932
23933 static tree
23934 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23935 {
23936   cp_token *token = cp_lexer_peek_token (parser->lexer);
23937   enum tree_code op;
23938   tree lhs, rhs;
23939   cp_id_kind idk;
23940   bool decl_first;
23941
23942   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23943     {
23944       op = (token->type == CPP_PLUS_PLUS
23945             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23946       cp_lexer_consume_token (parser->lexer);
23947       lhs = cp_parser_cast_expression (parser, false, false, NULL);
23948       if (lhs != decl)
23949         return error_mark_node;
23950       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23951     }
23952
23953   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23954   if (lhs != decl)
23955     return error_mark_node;
23956
23957   token = cp_lexer_peek_token (parser->lexer);
23958   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23959     {
23960       op = (token->type == CPP_PLUS_PLUS
23961             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23962       cp_lexer_consume_token (parser->lexer);
23963       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23964     }
23965
23966   op = cp_parser_assignment_operator_opt (parser);
23967   if (op == ERROR_MARK)
23968     return error_mark_node;
23969
23970   if (op != NOP_EXPR)
23971     {
23972       rhs = cp_parser_assignment_expression (parser, false, NULL);
23973       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23974       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23975     }
23976
23977   lhs = cp_parser_binary_expression (parser, false, false,
23978                                      PREC_ADDITIVE_EXPRESSION, NULL);
23979   token = cp_lexer_peek_token (parser->lexer);
23980   decl_first = lhs == decl;
23981   if (decl_first)
23982     lhs = NULL_TREE;
23983   if (token->type != CPP_PLUS
23984       && token->type != CPP_MINUS)
23985     return error_mark_node;
23986
23987   do
23988     {
23989       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23990       cp_lexer_consume_token (parser->lexer);
23991       rhs = cp_parser_binary_expression (parser, false, false,
23992                                          PREC_ADDITIVE_EXPRESSION, NULL);
23993       token = cp_lexer_peek_token (parser->lexer);
23994       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23995         {
23996           if (lhs == NULL_TREE)
23997             {
23998               if (op == PLUS_EXPR)
23999                 lhs = rhs;
24000               else
24001                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24002             }
24003           else
24004             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24005                                      NULL, tf_warning_or_error);
24006         }
24007     }
24008   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24009
24010   if (!decl_first)
24011     {
24012       if (rhs != decl || op == MINUS_EXPR)
24013         return error_mark_node;
24014       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24015     }
24016   else
24017     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24018
24019   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24020 }
24021
24022 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24023
24024 static tree
24025 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24026 {
24027   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24028   tree real_decl, initv, condv, incrv, declv;
24029   tree this_pre_body, cl;
24030   location_t loc_first;
24031   bool collapse_err = false;
24032   int i, collapse = 1, nbraces = 0;
24033   VEC(tree,gc) *for_block = make_tree_vector ();
24034
24035   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24036     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24037       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24038
24039   gcc_assert (collapse >= 1);
24040
24041   declv = make_tree_vec (collapse);
24042   initv = make_tree_vec (collapse);
24043   condv = make_tree_vec (collapse);
24044   incrv = make_tree_vec (collapse);
24045
24046   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24047
24048   for (i = 0; i < collapse; i++)
24049     {
24050       int bracecount = 0;
24051       bool add_private_clause = false;
24052       location_t loc;
24053
24054       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24055         {
24056           cp_parser_error (parser, "for statement expected");
24057           return NULL;
24058         }
24059       loc = cp_lexer_consume_token (parser->lexer)->location;
24060
24061       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24062         return NULL;
24063
24064       init = decl = real_decl = NULL;
24065       this_pre_body = push_stmt_list ();
24066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24067         {
24068           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24069
24070              init-expr:
24071                        var = lb
24072                        integer-type var = lb
24073                        random-access-iterator-type var = lb
24074                        pointer-type var = lb
24075           */
24076           cp_decl_specifier_seq type_specifiers;
24077
24078           /* First, try to parse as an initialized declaration.  See
24079              cp_parser_condition, from whence the bulk of this is copied.  */
24080
24081           cp_parser_parse_tentatively (parser);
24082           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24083                                         /*is_trailing_return=*/false,
24084                                         &type_specifiers);
24085           if (cp_parser_parse_definitely (parser))
24086             {
24087               /* If parsing a type specifier seq succeeded, then this
24088                  MUST be a initialized declaration.  */
24089               tree asm_specification, attributes;
24090               cp_declarator *declarator;
24091
24092               declarator = cp_parser_declarator (parser,
24093                                                  CP_PARSER_DECLARATOR_NAMED,
24094                                                  /*ctor_dtor_or_conv_p=*/NULL,
24095                                                  /*parenthesized_p=*/NULL,
24096                                                  /*member_p=*/false);
24097               attributes = cp_parser_attributes_opt (parser);
24098               asm_specification = cp_parser_asm_specification_opt (parser);
24099
24100               if (declarator == cp_error_declarator) 
24101                 cp_parser_skip_to_end_of_statement (parser);
24102
24103               else 
24104                 {
24105                   tree pushed_scope, auto_node;
24106
24107                   decl = start_decl (declarator, &type_specifiers,
24108                                      SD_INITIALIZED, attributes,
24109                                      /*prefix_attributes=*/NULL_TREE,
24110                                      &pushed_scope);
24111
24112                   auto_node = type_uses_auto (TREE_TYPE (decl));
24113                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24114                     {
24115                       if (cp_lexer_next_token_is (parser->lexer, 
24116                                                   CPP_OPEN_PAREN))
24117                         error ("parenthesized initialization is not allowed in "
24118                                "OpenMP %<for%> loop");
24119                       else
24120                         /* Trigger an error.  */
24121                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24122
24123                       init = error_mark_node;
24124                       cp_parser_skip_to_end_of_statement (parser);
24125                     }
24126                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24127                            || type_dependent_expression_p (decl)
24128                            || auto_node)
24129                     {
24130                       bool is_direct_init, is_non_constant_init;
24131
24132                       init = cp_parser_initializer (parser,
24133                                                     &is_direct_init,
24134                                                     &is_non_constant_init);
24135
24136                       if (auto_node && describable_type (init))
24137                         {
24138                           TREE_TYPE (decl)
24139                             = do_auto_deduction (TREE_TYPE (decl), init,
24140                                                  auto_node);
24141
24142                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24143                               && !type_dependent_expression_p (decl))
24144                             goto non_class;
24145                         }
24146                       
24147                       cp_finish_decl (decl, init, !is_non_constant_init,
24148                                       asm_specification,
24149                                       LOOKUP_ONLYCONVERTING);
24150                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24151                         {
24152                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24153                           init = NULL_TREE;
24154                         }
24155                       else
24156                         init = pop_stmt_list (this_pre_body);
24157                       this_pre_body = NULL_TREE;
24158                     }
24159                   else
24160                     {
24161                       /* Consume '='.  */
24162                       cp_lexer_consume_token (parser->lexer);
24163                       init = cp_parser_assignment_expression (parser, false, NULL);
24164
24165                     non_class:
24166                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24167                         init = error_mark_node;
24168                       else
24169                         cp_finish_decl (decl, NULL_TREE,
24170                                         /*init_const_expr_p=*/false,
24171                                         asm_specification,
24172                                         LOOKUP_ONLYCONVERTING);
24173                     }
24174
24175                   if (pushed_scope)
24176                     pop_scope (pushed_scope);
24177                 }
24178             }
24179           else 
24180             {
24181               cp_id_kind idk;
24182               /* If parsing a type specifier sequence failed, then
24183                  this MUST be a simple expression.  */
24184               cp_parser_parse_tentatively (parser);
24185               decl = cp_parser_primary_expression (parser, false, false,
24186                                                    false, &idk);
24187               if (!cp_parser_error_occurred (parser)
24188                   && decl
24189                   && DECL_P (decl)
24190                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24191                 {
24192                   tree rhs;
24193
24194                   cp_parser_parse_definitely (parser);
24195                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24196                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24197                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24198                                                          rhs,
24199                                                          tf_warning_or_error));
24200                   add_private_clause = true;
24201                 }
24202               else
24203                 {
24204                   decl = NULL;
24205                   cp_parser_abort_tentative_parse (parser);
24206                   init = cp_parser_expression (parser, false, NULL);
24207                   if (init)
24208                     {
24209                       if (TREE_CODE (init) == MODIFY_EXPR
24210                           || TREE_CODE (init) == MODOP_EXPR)
24211                         real_decl = TREE_OPERAND (init, 0);
24212                     }
24213                 }
24214             }
24215         }
24216       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24217       if (this_pre_body)
24218         {
24219           this_pre_body = pop_stmt_list (this_pre_body);
24220           if (pre_body)
24221             {
24222               tree t = pre_body;
24223               pre_body = push_stmt_list ();
24224               add_stmt (t);
24225               add_stmt (this_pre_body);
24226               pre_body = pop_stmt_list (pre_body);
24227             }
24228           else
24229             pre_body = this_pre_body;
24230         }
24231
24232       if (decl)
24233         real_decl = decl;
24234       if (par_clauses != NULL && real_decl != NULL_TREE)
24235         {
24236           tree *c;
24237           for (c = par_clauses; *c ; )
24238             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24239                 && OMP_CLAUSE_DECL (*c) == real_decl)
24240               {
24241                 error_at (loc, "iteration variable %qD"
24242                           " should not be firstprivate", real_decl);
24243                 *c = OMP_CLAUSE_CHAIN (*c);
24244               }
24245             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24246                      && OMP_CLAUSE_DECL (*c) == real_decl)
24247               {
24248                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24249                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24250                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24251                 OMP_CLAUSE_DECL (l) = real_decl;
24252                 OMP_CLAUSE_CHAIN (l) = clauses;
24253                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24254                 clauses = l;
24255                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24256                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24257                 add_private_clause = false;
24258               }
24259             else
24260               {
24261                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24262                     && OMP_CLAUSE_DECL (*c) == real_decl)
24263                   add_private_clause = false;
24264                 c = &OMP_CLAUSE_CHAIN (*c);
24265               }
24266         }
24267
24268       if (add_private_clause)
24269         {
24270           tree c;
24271           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24272             {
24273               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24274                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24275                   && OMP_CLAUSE_DECL (c) == decl)
24276                 break;
24277               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24278                        && OMP_CLAUSE_DECL (c) == decl)
24279                 error_at (loc, "iteration variable %qD "
24280                           "should not be firstprivate",
24281                           decl);
24282               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24283                        && OMP_CLAUSE_DECL (c) == decl)
24284                 error_at (loc, "iteration variable %qD should not be reduction",
24285                           decl);
24286             }
24287           if (c == NULL)
24288             {
24289               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24290               OMP_CLAUSE_DECL (c) = decl;
24291               c = finish_omp_clauses (c);
24292               if (c)
24293                 {
24294                   OMP_CLAUSE_CHAIN (c) = clauses;
24295                   clauses = c;
24296                 }
24297             }
24298         }
24299
24300       cond = NULL;
24301       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24302         cond = cp_parser_omp_for_cond (parser, decl);
24303       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24304
24305       incr = NULL;
24306       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24307         {
24308           /* If decl is an iterator, preserve the operator on decl
24309              until finish_omp_for.  */
24310           if (decl
24311               && (type_dependent_expression_p (decl)
24312                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24313             incr = cp_parser_omp_for_incr (parser, decl);
24314           else
24315             incr = cp_parser_expression (parser, false, NULL);
24316         }
24317
24318       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24319         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24320                                                /*or_comma=*/false,
24321                                                /*consume_paren=*/true);
24322
24323       TREE_VEC_ELT (declv, i) = decl;
24324       TREE_VEC_ELT (initv, i) = init;
24325       TREE_VEC_ELT (condv, i) = cond;
24326       TREE_VEC_ELT (incrv, i) = incr;
24327
24328       if (i == collapse - 1)
24329         break;
24330
24331       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24332          in between the collapsed for loops to be still considered perfectly
24333          nested.  Hopefully the final version clarifies this.
24334          For now handle (multiple) {'s and empty statements.  */
24335       cp_parser_parse_tentatively (parser);
24336       do
24337         {
24338           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24339             break;
24340           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24341             {
24342               cp_lexer_consume_token (parser->lexer);
24343               bracecount++;
24344             }
24345           else if (bracecount
24346                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24347             cp_lexer_consume_token (parser->lexer);
24348           else
24349             {
24350               loc = cp_lexer_peek_token (parser->lexer)->location;
24351               error_at (loc, "not enough collapsed for loops");
24352               collapse_err = true;
24353               cp_parser_abort_tentative_parse (parser);
24354               declv = NULL_TREE;
24355               break;
24356             }
24357         }
24358       while (1);
24359
24360       if (declv)
24361         {
24362           cp_parser_parse_definitely (parser);
24363           nbraces += bracecount;
24364         }
24365     }
24366
24367   /* Note that we saved the original contents of this flag when we entered
24368      the structured block, and so we don't need to re-save it here.  */
24369   parser->in_statement = IN_OMP_FOR;
24370
24371   /* Note that the grammar doesn't call for a structured block here,
24372      though the loop as a whole is a structured block.  */
24373   body = push_stmt_list ();
24374   cp_parser_statement (parser, NULL_TREE, false, NULL);
24375   body = pop_stmt_list (body);
24376
24377   if (declv == NULL_TREE)
24378     ret = NULL_TREE;
24379   else
24380     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24381                           pre_body, clauses);
24382
24383   while (nbraces)
24384     {
24385       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24386         {
24387           cp_lexer_consume_token (parser->lexer);
24388           nbraces--;
24389         }
24390       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24391         cp_lexer_consume_token (parser->lexer);
24392       else
24393         {
24394           if (!collapse_err)
24395             {
24396               error_at (cp_lexer_peek_token (parser->lexer)->location,
24397                         "collapsed loops not perfectly nested");
24398             }
24399           collapse_err = true;
24400           cp_parser_statement_seq_opt (parser, NULL);
24401           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24402             break;
24403         }
24404     }
24405
24406   while (!VEC_empty (tree, for_block))
24407     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24408   release_tree_vector (for_block);
24409
24410   return ret;
24411 }
24412
24413 /* OpenMP 2.5:
24414    #pragma omp for for-clause[optseq] new-line
24415      for-loop  */
24416
24417 #define OMP_FOR_CLAUSE_MASK                             \
24418         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24419         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24420         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24421         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24422         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24423         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24424         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24425         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24426
24427 static tree
24428 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24429 {
24430   tree clauses, sb, ret;
24431   unsigned int save;
24432
24433   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24434                                        "#pragma omp for", pragma_tok);
24435
24436   sb = begin_omp_structured_block ();
24437   save = cp_parser_begin_omp_structured_block (parser);
24438
24439   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24440
24441   cp_parser_end_omp_structured_block (parser, save);
24442   add_stmt (finish_omp_structured_block (sb));
24443
24444   return ret;
24445 }
24446
24447 /* OpenMP 2.5:
24448    # pragma omp master new-line
24449      structured-block  */
24450
24451 static tree
24452 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24453 {
24454   cp_parser_require_pragma_eol (parser, pragma_tok);
24455   return c_finish_omp_master (input_location,
24456                               cp_parser_omp_structured_block (parser));
24457 }
24458
24459 /* OpenMP 2.5:
24460    # pragma omp ordered new-line
24461      structured-block  */
24462
24463 static tree
24464 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24465 {
24466   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24467   cp_parser_require_pragma_eol (parser, pragma_tok);
24468   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24469 }
24470
24471 /* OpenMP 2.5:
24472
24473    section-scope:
24474      { section-sequence }
24475
24476    section-sequence:
24477      section-directive[opt] structured-block
24478      section-sequence section-directive structured-block  */
24479
24480 static tree
24481 cp_parser_omp_sections_scope (cp_parser *parser)
24482 {
24483   tree stmt, substmt;
24484   bool error_suppress = false;
24485   cp_token *tok;
24486
24487   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24488     return NULL_TREE;
24489
24490   stmt = push_stmt_list ();
24491
24492   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24493     {
24494       unsigned save;
24495
24496       substmt = begin_omp_structured_block ();
24497       save = cp_parser_begin_omp_structured_block (parser);
24498
24499       while (1)
24500         {
24501           cp_parser_statement (parser, NULL_TREE, false, NULL);
24502
24503           tok = cp_lexer_peek_token (parser->lexer);
24504           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24505             break;
24506           if (tok->type == CPP_CLOSE_BRACE)
24507             break;
24508           if (tok->type == CPP_EOF)
24509             break;
24510         }
24511
24512       cp_parser_end_omp_structured_block (parser, save);
24513       substmt = finish_omp_structured_block (substmt);
24514       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24515       add_stmt (substmt);
24516     }
24517
24518   while (1)
24519     {
24520       tok = cp_lexer_peek_token (parser->lexer);
24521       if (tok->type == CPP_CLOSE_BRACE)
24522         break;
24523       if (tok->type == CPP_EOF)
24524         break;
24525
24526       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24527         {
24528           cp_lexer_consume_token (parser->lexer);
24529           cp_parser_require_pragma_eol (parser, tok);
24530           error_suppress = false;
24531         }
24532       else if (!error_suppress)
24533         {
24534           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24535           error_suppress = true;
24536         }
24537
24538       substmt = cp_parser_omp_structured_block (parser);
24539       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24540       add_stmt (substmt);
24541     }
24542   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24543
24544   substmt = pop_stmt_list (stmt);
24545
24546   stmt = make_node (OMP_SECTIONS);
24547   TREE_TYPE (stmt) = void_type_node;
24548   OMP_SECTIONS_BODY (stmt) = substmt;
24549
24550   add_stmt (stmt);
24551   return stmt;
24552 }
24553
24554 /* OpenMP 2.5:
24555    # pragma omp sections sections-clause[optseq] newline
24556      sections-scope  */
24557
24558 #define OMP_SECTIONS_CLAUSE_MASK                        \
24559         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24560         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24561         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24562         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24563         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24564
24565 static tree
24566 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24567 {
24568   tree clauses, ret;
24569
24570   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24571                                        "#pragma omp sections", pragma_tok);
24572
24573   ret = cp_parser_omp_sections_scope (parser);
24574   if (ret)
24575     OMP_SECTIONS_CLAUSES (ret) = clauses;
24576
24577   return ret;
24578 }
24579
24580 /* OpenMP 2.5:
24581    # pragma parallel parallel-clause new-line
24582    # pragma parallel for parallel-for-clause new-line
24583    # pragma parallel sections parallel-sections-clause new-line  */
24584
24585 #define OMP_PARALLEL_CLAUSE_MASK                        \
24586         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24587         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24588         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24589         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24590         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24591         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24592         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24593         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24594
24595 static tree
24596 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24597 {
24598   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24599   const char *p_name = "#pragma omp parallel";
24600   tree stmt, clauses, par_clause, ws_clause, block;
24601   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24602   unsigned int save;
24603   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24604
24605   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24606     {
24607       cp_lexer_consume_token (parser->lexer);
24608       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24609       p_name = "#pragma omp parallel for";
24610       mask |= OMP_FOR_CLAUSE_MASK;
24611       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24612     }
24613   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24614     {
24615       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24616       const char *p = IDENTIFIER_POINTER (id);
24617       if (strcmp (p, "sections") == 0)
24618         {
24619           cp_lexer_consume_token (parser->lexer);
24620           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24621           p_name = "#pragma omp parallel sections";
24622           mask |= OMP_SECTIONS_CLAUSE_MASK;
24623           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24624         }
24625     }
24626
24627   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24628   block = begin_omp_parallel ();
24629   save = cp_parser_begin_omp_structured_block (parser);
24630
24631   switch (p_kind)
24632     {
24633     case PRAGMA_OMP_PARALLEL:
24634       cp_parser_statement (parser, NULL_TREE, false, NULL);
24635       par_clause = clauses;
24636       break;
24637
24638     case PRAGMA_OMP_PARALLEL_FOR:
24639       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24640       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24641       break;
24642
24643     case PRAGMA_OMP_PARALLEL_SECTIONS:
24644       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24645       stmt = cp_parser_omp_sections_scope (parser);
24646       if (stmt)
24647         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24648       break;
24649
24650     default:
24651       gcc_unreachable ();
24652     }
24653
24654   cp_parser_end_omp_structured_block (parser, save);
24655   stmt = finish_omp_parallel (par_clause, block);
24656   if (p_kind != PRAGMA_OMP_PARALLEL)
24657     OMP_PARALLEL_COMBINED (stmt) = 1;
24658   return stmt;
24659 }
24660
24661 /* OpenMP 2.5:
24662    # pragma omp single single-clause[optseq] new-line
24663      structured-block  */
24664
24665 #define OMP_SINGLE_CLAUSE_MASK                          \
24666         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24667         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24668         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24669         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24670
24671 static tree
24672 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24673 {
24674   tree stmt = make_node (OMP_SINGLE);
24675   TREE_TYPE (stmt) = void_type_node;
24676
24677   OMP_SINGLE_CLAUSES (stmt)
24678     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24679                                  "#pragma omp single", pragma_tok);
24680   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24681
24682   return add_stmt (stmt);
24683 }
24684
24685 /* OpenMP 3.0:
24686    # pragma omp task task-clause[optseq] new-line
24687      structured-block  */
24688
24689 #define OMP_TASK_CLAUSE_MASK                            \
24690         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24691         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24692         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24693         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24694         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24695         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24696
24697 static tree
24698 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24699 {
24700   tree clauses, block;
24701   unsigned int save;
24702
24703   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24704                                        "#pragma omp task", pragma_tok);
24705   block = begin_omp_task ();
24706   save = cp_parser_begin_omp_structured_block (parser);
24707   cp_parser_statement (parser, NULL_TREE, false, NULL);
24708   cp_parser_end_omp_structured_block (parser, save);
24709   return finish_omp_task (clauses, block);
24710 }
24711
24712 /* OpenMP 3.0:
24713    # pragma omp taskwait new-line  */
24714
24715 static void
24716 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24717 {
24718   cp_parser_require_pragma_eol (parser, pragma_tok);
24719   finish_omp_taskwait ();
24720 }
24721
24722 /* OpenMP 2.5:
24723    # pragma omp threadprivate (variable-list) */
24724
24725 static void
24726 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24727 {
24728   tree vars;
24729
24730   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24731   cp_parser_require_pragma_eol (parser, pragma_tok);
24732
24733   finish_omp_threadprivate (vars);
24734 }
24735
24736 /* Main entry point to OpenMP statement pragmas.  */
24737
24738 static void
24739 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24740 {
24741   tree stmt;
24742
24743   switch (pragma_tok->pragma_kind)
24744     {
24745     case PRAGMA_OMP_ATOMIC:
24746       cp_parser_omp_atomic (parser, pragma_tok);
24747       return;
24748     case PRAGMA_OMP_CRITICAL:
24749       stmt = cp_parser_omp_critical (parser, pragma_tok);
24750       break;
24751     case PRAGMA_OMP_FOR:
24752       stmt = cp_parser_omp_for (parser, pragma_tok);
24753       break;
24754     case PRAGMA_OMP_MASTER:
24755       stmt = cp_parser_omp_master (parser, pragma_tok);
24756       break;
24757     case PRAGMA_OMP_ORDERED:
24758       stmt = cp_parser_omp_ordered (parser, pragma_tok);
24759       break;
24760     case PRAGMA_OMP_PARALLEL:
24761       stmt = cp_parser_omp_parallel (parser, pragma_tok);
24762       break;
24763     case PRAGMA_OMP_SECTIONS:
24764       stmt = cp_parser_omp_sections (parser, pragma_tok);
24765       break;
24766     case PRAGMA_OMP_SINGLE:
24767       stmt = cp_parser_omp_single (parser, pragma_tok);
24768       break;
24769     case PRAGMA_OMP_TASK:
24770       stmt = cp_parser_omp_task (parser, pragma_tok);
24771       break;
24772     default:
24773       gcc_unreachable ();
24774     }
24775
24776   if (stmt)
24777     SET_EXPR_LOCATION (stmt, pragma_tok->location);
24778 }
24779 \f
24780 /* The parser.  */
24781
24782 static GTY (()) cp_parser *the_parser;
24783
24784 \f
24785 /* Special handling for the first token or line in the file.  The first
24786    thing in the file might be #pragma GCC pch_preprocess, which loads a
24787    PCH file, which is a GC collection point.  So we need to handle this
24788    first pragma without benefit of an existing lexer structure.
24789
24790    Always returns one token to the caller in *FIRST_TOKEN.  This is
24791    either the true first token of the file, or the first token after
24792    the initial pragma.  */
24793
24794 static void
24795 cp_parser_initial_pragma (cp_token *first_token)
24796 {
24797   tree name = NULL;
24798
24799   cp_lexer_get_preprocessor_token (NULL, first_token);
24800   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24801     return;
24802
24803   cp_lexer_get_preprocessor_token (NULL, first_token);
24804   if (first_token->type == CPP_STRING)
24805     {
24806       name = first_token->u.value;
24807
24808       cp_lexer_get_preprocessor_token (NULL, first_token);
24809       if (first_token->type != CPP_PRAGMA_EOL)
24810         error_at (first_token->location,
24811                   "junk at end of %<#pragma GCC pch_preprocess%>");
24812     }
24813   else
24814     error_at (first_token->location, "expected string literal");
24815
24816   /* Skip to the end of the pragma.  */
24817   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24818     cp_lexer_get_preprocessor_token (NULL, first_token);
24819
24820   /* Now actually load the PCH file.  */
24821   if (name)
24822     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24823
24824   /* Read one more token to return to our caller.  We have to do this
24825      after reading the PCH file in, since its pointers have to be
24826      live.  */
24827   cp_lexer_get_preprocessor_token (NULL, first_token);
24828 }
24829
24830 /* Normal parsing of a pragma token.  Here we can (and must) use the
24831    regular lexer.  */
24832
24833 static bool
24834 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24835 {
24836   cp_token *pragma_tok;
24837   unsigned int id;
24838
24839   pragma_tok = cp_lexer_consume_token (parser->lexer);
24840   gcc_assert (pragma_tok->type == CPP_PRAGMA);
24841   parser->lexer->in_pragma = true;
24842
24843   id = pragma_tok->pragma_kind;
24844   switch (id)
24845     {
24846     case PRAGMA_GCC_PCH_PREPROCESS:
24847       error_at (pragma_tok->location,
24848                 "%<#pragma GCC pch_preprocess%> must be first");
24849       break;
24850
24851     case PRAGMA_OMP_BARRIER:
24852       switch (context)
24853         {
24854         case pragma_compound:
24855           cp_parser_omp_barrier (parser, pragma_tok);
24856           return false;
24857         case pragma_stmt:
24858           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24859                     "used in compound statements");
24860           break;
24861         default:
24862           goto bad_stmt;
24863         }
24864       break;
24865
24866     case PRAGMA_OMP_FLUSH:
24867       switch (context)
24868         {
24869         case pragma_compound:
24870           cp_parser_omp_flush (parser, pragma_tok);
24871           return false;
24872         case pragma_stmt:
24873           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24874                     "used in compound statements");
24875           break;
24876         default:
24877           goto bad_stmt;
24878         }
24879       break;
24880
24881     case PRAGMA_OMP_TASKWAIT:
24882       switch (context)
24883         {
24884         case pragma_compound:
24885           cp_parser_omp_taskwait (parser, pragma_tok);
24886           return false;
24887         case pragma_stmt:
24888           error_at (pragma_tok->location,
24889                     "%<#pragma omp taskwait%> may only be "
24890                     "used in compound statements");
24891           break;
24892         default:
24893           goto bad_stmt;
24894         }
24895       break;
24896
24897     case PRAGMA_OMP_THREADPRIVATE:
24898       cp_parser_omp_threadprivate (parser, pragma_tok);
24899       return false;
24900
24901     case PRAGMA_OMP_ATOMIC:
24902     case PRAGMA_OMP_CRITICAL:
24903     case PRAGMA_OMP_FOR:
24904     case PRAGMA_OMP_MASTER:
24905     case PRAGMA_OMP_ORDERED:
24906     case PRAGMA_OMP_PARALLEL:
24907     case PRAGMA_OMP_SECTIONS:
24908     case PRAGMA_OMP_SINGLE:
24909     case PRAGMA_OMP_TASK:
24910       if (context == pragma_external)
24911         goto bad_stmt;
24912       cp_parser_omp_construct (parser, pragma_tok);
24913       return true;
24914
24915     case PRAGMA_OMP_SECTION:
24916       error_at (pragma_tok->location, 
24917                 "%<#pragma omp section%> may only be used in "
24918                 "%<#pragma omp sections%> construct");
24919       break;
24920
24921     default:
24922       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24923       c_invoke_pragma_handler (id);
24924       break;
24925
24926     bad_stmt:
24927       cp_parser_error (parser, "expected declaration specifiers");
24928       break;
24929     }
24930
24931   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24932   return false;
24933 }
24934
24935 /* The interface the pragma parsers have to the lexer.  */
24936
24937 enum cpp_ttype
24938 pragma_lex (tree *value)
24939 {
24940   cp_token *tok;
24941   enum cpp_ttype ret;
24942
24943   tok = cp_lexer_peek_token (the_parser->lexer);
24944
24945   ret = tok->type;
24946   *value = tok->u.value;
24947
24948   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24949     ret = CPP_EOF;
24950   else if (ret == CPP_STRING)
24951     *value = cp_parser_string_literal (the_parser, false, false);
24952   else
24953     {
24954       cp_lexer_consume_token (the_parser->lexer);
24955       if (ret == CPP_KEYWORD)
24956         ret = CPP_NAME;
24957     }
24958
24959   return ret;
24960 }
24961
24962 \f
24963 /* External interface.  */
24964
24965 /* Parse one entire translation unit.  */
24966
24967 void
24968 c_parse_file (void)
24969 {
24970   static bool already_called = false;
24971
24972   if (already_called)
24973     {
24974       sorry ("inter-module optimizations not implemented for C++");
24975       return;
24976     }
24977   already_called = true;
24978
24979   the_parser = cp_parser_new ();
24980   push_deferring_access_checks (flag_access_control
24981                                 ? dk_no_deferred : dk_no_check);
24982   cp_parser_translation_unit (the_parser);
24983   the_parser = NULL;
24984 }
24985
24986 #include "gt-cp-parser.h"