]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/gcc/cp/parser.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  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 2, 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 COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 /* APPLE LOCAL C* language */
42 #include "tree-iterator.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 /* A token's value and its associated deferred access checks and
51    qualifying scope.  */
52
53 struct tree_check GTY(())
54 {
55   /* The value associated with the token.  */
56   tree value;
57   /* The checks that have been associated with value.  */
58   VEC (deferred_access_check, gc)* checks;
59   /* The token's qualifying scope (used when it is a
60      CPP_NESTED_NAME_SPECIFIER).  */
61   tree qualifying_scope;
62 };
63
64 /* A C++ token.  */
65
66 typedef struct cp_token GTY (())
67 {
68   /* The kind of token.  */
69   ENUM_BITFIELD (cpp_ttype) type : 8;
70   /* If this token is a keyword, this value indicates which keyword.
71      Otherwise, this value is RID_MAX.  */
72   ENUM_BITFIELD (rid) keyword : 8;
73   /* Token flags.  */
74   unsigned char flags;
75   /* Identifier for the pragma.  */
76   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
77   /* True if this token is from a system header.  */
78   BOOL_BITFIELD in_system_header : 1;
79   /* True if this token is from a context where it is implicitly extern "C" */
80   BOOL_BITFIELD implicit_extern_c : 1;
81   /* True for a CPP_NAME token that is not a keyword (i.e., for which
82      KEYWORD is RID_MAX) iff this name was looked up and found to be
83      ambiguous.  An error has already been reported.  */
84   BOOL_BITFIELD ambiguous_p : 1;
85   /* The input file stack index at which this token was found.  */
86   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
87   /* The value associated with this token, if any.  */
88   union cp_token_value {
89     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
90     struct tree_check* GTY((tag ("1"))) tree_check_value;
91     /* Use for all other tokens.  */
92     tree GTY((tag ("0"))) value;
93   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
94   /* The location at which this token was found.  */
95   location_t location;
96 } cp_token;
97
98 /* We use a stack of token pointer for saving token sets.  */
99 typedef struct cp_token *cp_token_position;
100 DEF_VEC_P (cp_token_position);
101 DEF_VEC_ALLOC_P (cp_token_position,heap);
102
103 static const cp_token eof_token =
104 {
105   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
106 #if USE_MAPPED_LOCATION
107   0
108 #else
109   {0, 0}
110 #endif
111 };
112
113 /* The cp_lexer structure represents the C++ lexer.  It is responsible
114    for managing the token stream from the preprocessor and supplying
115    it to the parser.  Tokens are never added to the cp_lexer after
116    it is created.  */
117
118 typedef struct cp_lexer GTY (())
119 {
120   /* The memory allocated for the buffer.  NULL if this lexer does not
121      own the token buffer.  */
122   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
123   /* If the lexer owns the buffer, this is the number of tokens in the
124      buffer.  */
125   size_t buffer_length;
126
127   /* A pointer just past the last available token.  The tokens
128      in this lexer are [buffer, last_token).  */
129   cp_token_position GTY ((skip)) last_token;
130
131   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
132      no more available tokens.  */
133   cp_token_position GTY ((skip)) next_token;
134
135   /* A stack indicating positions at which cp_lexer_save_tokens was
136      called.  The top entry is the most recent position at which we
137      began saving tokens.  If the stack is non-empty, we are saving
138      tokens.  */
139   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
140
141   /* The next lexer in a linked list of lexers.  */
142   struct cp_lexer *next;
143
144   /* True if we should output debugging information.  */
145   bool debugging_p;
146
147   /* True if we're in the context of parsing a pragma, and should not
148      increment past the end-of-line marker.  */
149   bool in_pragma;
150 } cp_lexer;
151
152 /* cp_token_cache is a range of tokens.  There is no need to represent
153    allocate heap memory for it, since tokens are never removed from the
154    lexer's array.  There is also no need for the GC to walk through
155    a cp_token_cache, since everything in here is referenced through
156    a lexer.  */
157
158 typedef struct cp_token_cache GTY(())
159 {
160   /* The beginning of the token range.  */
161   cp_token * GTY((skip)) first;
162
163   /* Points immediately after the last token in the range.  */
164   cp_token * GTY ((skip)) last;
165 } cp_token_cache;
166
167 /* Prototypes.  */
168
169 static cp_lexer *cp_lexer_new_main
170   (void);
171 static cp_lexer *cp_lexer_new_from_tokens
172   (cp_token_cache *tokens);
173 static void cp_lexer_destroy
174   (cp_lexer *);
175 static int cp_lexer_saving_tokens
176   (const cp_lexer *);
177 static cp_token_position cp_lexer_token_position
178   (cp_lexer *, bool);
179 static cp_token *cp_lexer_token_at
180   (cp_lexer *, cp_token_position);
181 static void cp_lexer_get_preprocessor_token
182   (cp_lexer *, cp_token *);
183 static inline cp_token *cp_lexer_peek_token
184   (cp_lexer *);
185 static cp_token *cp_lexer_peek_nth_token
186   (cp_lexer *, size_t);
187 static inline bool cp_lexer_next_token_is
188   (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_not
190   (cp_lexer *, enum cpp_ttype);
191 static bool cp_lexer_next_token_is_keyword
192   (cp_lexer *, enum rid);
193 static cp_token *cp_lexer_consume_token
194   (cp_lexer *);
195 static void cp_lexer_purge_token
196   (cp_lexer *);
197 static void cp_lexer_purge_tokens_after
198   (cp_lexer *, cp_token_position);
199 static void cp_lexer_save_tokens
200   (cp_lexer *);
201 static void cp_lexer_commit_tokens
202   (cp_lexer *);
203 static void cp_lexer_rollback_tokens
204   (cp_lexer *);
205 #ifdef ENABLE_CHECKING
206 static void cp_lexer_print_token
207   (FILE *, cp_token *);
208 static inline bool cp_lexer_debugging_p
209   (cp_lexer *);
210 static void cp_lexer_start_debugging
211   (cp_lexer *) ATTRIBUTE_UNUSED;
212 static void cp_lexer_stop_debugging
213   (cp_lexer *) ATTRIBUTE_UNUSED;
214 #else
215 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
216    about passing NULL to functions that require non-NULL arguments
217    (fputs, fprintf).  It will never be used, so all we need is a value
218    of the right type that's guaranteed not to be NULL.  */
219 #define cp_lexer_debug_stream stdout
220 #define cp_lexer_print_token(str, tok) (void) 0
221 #define cp_lexer_debugging_p(lexer) 0
222 #endif /* ENABLE_CHECKING */
223
224 static cp_token_cache *cp_token_cache_new
225   (cp_token *, cp_token *);
226
227 static void cp_parser_initial_pragma
228   (cp_token *);
229
230 /* Manifest constants.  */
231 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
232 #define CP_SAVED_TOKEN_STACK 5
233
234 /* A token type for keywords, as opposed to ordinary identifiers.  */
235 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
236
237 /* A token type for template-ids.  If a template-id is processed while
238    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
239    the value of the CPP_TEMPLATE_ID is whatever was returned by
240    cp_parser_template_id.  */
241 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
242
243 /* A token type for nested-name-specifiers.  If a
244    nested-name-specifier is processed while parsing tentatively, it is
245    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
246    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
247    cp_parser_nested_name_specifier_opt.  */
248 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
249
250 /* A token type for tokens that are not tokens at all; these are used
251    to represent slots in the array where there used to be a token
252    that has now been deleted.  */
253 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
254
255 /* The number of token types, including C++-specific ones.  */
256 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
257
258 /* Variables.  */
259
260 #ifdef ENABLE_CHECKING
261 /* The stream to which debugging output should be written.  */
262 static FILE *cp_lexer_debug_stream;
263 #endif /* ENABLE_CHECKING */
264
265 /* Create a new main C++ lexer, the lexer that gets tokens from the
266    preprocessor.  */
267
268 static cp_lexer *
269 cp_lexer_new_main (void)
270 {
271   cp_token first_token;
272   cp_lexer *lexer;
273   cp_token *pos;
274   size_t alloc;
275   size_t space;
276   cp_token *buffer;
277
278   /* It's possible that parsing the first pragma will load a PCH file,
279      which is a GC collection point.  So we have to do that before
280      allocating any memory.  */
281   cp_parser_initial_pragma (&first_token);
282
283   /* Tell c_lex_with_flags not to merge string constants.  */
284   c_lex_return_raw_strings = true;
285
286   c_common_no_more_pch ();
287
288   /* Allocate the memory.  */
289   lexer = GGC_CNEW (cp_lexer);
290
291 #ifdef ENABLE_CHECKING
292   /* Initially we are not debugging.  */
293   lexer->debugging_p = false;
294 #endif /* ENABLE_CHECKING */
295   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
296                                    CP_SAVED_TOKEN_STACK);
297
298   /* Create the buffer.  */
299   alloc = CP_LEXER_BUFFER_SIZE;
300   buffer = GGC_NEWVEC (cp_token, alloc);
301
302   /* Put the first token in the buffer.  */
303   space = alloc;
304   pos = buffer;
305   *pos = first_token;
306
307   /* Get the remaining tokens from the preprocessor.  */
308   while (pos->type != CPP_EOF)
309     {
310       pos++;
311       if (!--space)
312         {
313           space = alloc;
314           alloc *= 2;
315           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
316           pos = buffer + space;
317         }
318       cp_lexer_get_preprocessor_token (lexer, pos);
319     }
320   lexer->buffer = buffer;
321   lexer->buffer_length = alloc - space;
322   lexer->last_token = pos;
323   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
324
325   /* Subsequent preprocessor diagnostics should use compiler
326      diagnostic functions to get the compiler source location.  */
327   cpp_get_options (parse_in)->client_diagnostic = true;
328   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
329
330   gcc_assert (lexer->next_token->type != CPP_PURGED);
331   return lexer;
332 }
333
334 /* Create a new lexer whose token stream is primed with the tokens in
335    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
336
337 static cp_lexer *
338 cp_lexer_new_from_tokens (cp_token_cache *cache)
339 {
340   cp_token *first = cache->first;
341   cp_token *last = cache->last;
342   cp_lexer *lexer = GGC_CNEW (cp_lexer);
343
344   /* We do not own the buffer.  */
345   lexer->buffer = NULL;
346   lexer->buffer_length = 0;
347   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
348   lexer->last_token = last;
349
350   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
351                                    CP_SAVED_TOKEN_STACK);
352
353 #ifdef ENABLE_CHECKING
354   /* Initially we are not debugging.  */
355   lexer->debugging_p = false;
356 #endif
357
358   gcc_assert (lexer->next_token->type != CPP_PURGED);
359   return lexer;
360 }
361
362 /* Frees all resources associated with LEXER.  */
363
364 static void
365 cp_lexer_destroy (cp_lexer *lexer)
366 {
367   if (lexer->buffer)
368     ggc_free (lexer->buffer);
369   VEC_free (cp_token_position, heap, lexer->saved_tokens);
370   ggc_free (lexer);
371 }
372
373 /* Returns nonzero if debugging information should be output.  */
374
375 #ifdef ENABLE_CHECKING
376
377 static inline bool
378 cp_lexer_debugging_p (cp_lexer *lexer)
379 {
380   return lexer->debugging_p;
381 }
382
383 #endif /* ENABLE_CHECKING */
384
385 static inline cp_token_position
386 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
387 {
388   gcc_assert (!previous_p || lexer->next_token != &eof_token);
389
390   return lexer->next_token - previous_p;
391 }
392
393 static inline cp_token *
394 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
395 {
396   return pos;
397 }
398
399 /* nonzero if we are presently saving tokens.  */
400
401 static inline int
402 cp_lexer_saving_tokens (const cp_lexer* lexer)
403 {
404   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 }
406
407 /* Store the next token from the preprocessor in *TOKEN.  Return true
408    if we reach EOF.  */
409
410 static void
411 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
412                                  cp_token *token)
413 {
414   static int is_extern_c = 0;
415
416    /* Get a new token from the preprocessor.  */
417   token->type
418     = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
419   token->input_file_stack_index = input_file_stack_tick;
420   token->keyword = RID_MAX;
421   token->pragma_kind = PRAGMA_NONE;
422   token->in_system_header = in_system_header;
423
424   /* On some systems, some header files are surrounded by an
425      implicit extern "C" block.  Set a flag in the token if it
426      comes from such a header.  */
427   is_extern_c += pending_lang_change;
428   pending_lang_change = 0;
429   token->implicit_extern_c = is_extern_c > 0;
430
431   /* Check to see if this token is a keyword.  */
432   if (token->type == CPP_NAME)
433     {
434       if (C_IS_RESERVED_WORD (token->u.value))
435         {
436           /* Mark this token as a keyword.  */
437           token->type = CPP_KEYWORD;
438           /* Record which keyword.  */
439           token->keyword = C_RID_CODE (token->u.value);
440           /* Update the value.  Some keywords are mapped to particular
441              entities, rather than simply having the value of the
442              corresponding IDENTIFIER_NODE.  For example, `__const' is
443              mapped to `const'.  */
444           token->u.value = ridpointers[token->keyword];
445         }
446       else
447         {
448           token->ambiguous_p = false;
449           token->keyword = RID_MAX;
450         }
451     }
452   /* Handle Objective-C++ keywords.  */
453   else if (token->type == CPP_AT_NAME)
454     {
455       token->type = CPP_KEYWORD;
456       switch (C_RID_CODE (token->u.value))
457         {
458         /* Map 'class' to '@class', 'private' to '@private', etc.  */
459         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463         case RID_THROW: token->keyword = RID_AT_THROW; break;
464         case RID_TRY: token->keyword = RID_AT_TRY; break;
465         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466         default: token->keyword = C_RID_CODE (token->u.value);
467         }
468     }
469   else if (token->type == CPP_PRAGMA)
470     {
471       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
472       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473       token->u.value = NULL_TREE;
474     }
475 }
476
477 /* Update the globals input_location and in_system_header and the
478    input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485       in_system_header = token->in_system_header;
486       restore_input_file_stack (token->input_file_stack_index);
487     }
488 }
489
490 /* Return a pointer to the next token in the token stream, but do not
491    consume it.  */
492
493 static inline cp_token *
494 cp_lexer_peek_token (cp_lexer *lexer)
495 {
496   if (cp_lexer_debugging_p (lexer))
497     {
498       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
499       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
500       putc ('\n', cp_lexer_debug_stream);
501     }
502   return lexer->next_token;
503 }
504
505 /* Return true if the next token has the indicated TYPE.  */
506
507 static inline bool
508 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
509 {
510   return cp_lexer_peek_token (lexer)->type == type;
511 }
512
513 /* Return true if the next token does not have the indicated TYPE.  */
514
515 static inline bool
516 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
517 {
518   return !cp_lexer_next_token_is (lexer, type);
519 }
520
521 /* Return true if the next token is the indicated KEYWORD.  */
522
523 static inline bool
524 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
525 {
526   return cp_lexer_peek_token (lexer)->keyword == keyword;
527 }
528
529 /* Return true if the next token is a keyword for a decl-specifier.  */
530
531 static bool
532 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
533 {
534   cp_token *token;
535
536   token = cp_lexer_peek_token (lexer);
537   switch (token->keyword) 
538     {
539       /* Storage classes.  */
540     case RID_AUTO:
541     case RID_REGISTER:
542     case RID_STATIC:
543     case RID_EXTERN:
544     case RID_MUTABLE:
545     case RID_THREAD:
546       /* Elaborated type specifiers.  */
547     case RID_ENUM:
548     case RID_CLASS:
549     case RID_STRUCT:
550     case RID_UNION:
551     case RID_TYPENAME:
552       /* Simple type specifiers.  */
553     case RID_CHAR:
554     case RID_WCHAR:
555     case RID_BOOL:
556     case RID_SHORT:
557     case RID_INT:
558     case RID_LONG:
559     case RID_SIGNED:
560     case RID_UNSIGNED:
561     case RID_FLOAT:
562     case RID_DOUBLE:
563     case RID_VOID:
564       /* GNU extensions.  */ 
565     case RID_ATTRIBUTE:
566     case RID_TYPEOF:
567       return true;
568
569     default:
570       return false;
571     }
572 }
573
574 /* Return a pointer to the Nth token in the token stream.  If N is 1,
575    then this is precisely equivalent to cp_lexer_peek_token (except
576    that it is not inline).  One would like to disallow that case, but
577    there is one case (cp_parser_nth_token_starts_template_id) where
578    the caller passes a variable for N and it might be 1.  */
579
580 static cp_token *
581 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
582 {
583   cp_token *token;
584
585   /* N is 1-based, not zero-based.  */
586   gcc_assert (n > 0);
587
588   if (cp_lexer_debugging_p (lexer))
589     fprintf (cp_lexer_debug_stream,
590              "cp_lexer: peeking ahead %ld at token: ", (long)n);
591
592   --n;
593   token = lexer->next_token;
594   gcc_assert (!n || token != &eof_token);
595   while (n != 0)
596     {
597       ++token;
598       if (token == lexer->last_token)
599         {
600           token = (cp_token *)&eof_token;
601           break;
602         }
603
604       if (token->type != CPP_PURGED)
605         --n;
606     }
607
608   if (cp_lexer_debugging_p (lexer))
609     {
610       cp_lexer_print_token (cp_lexer_debug_stream, token);
611       putc ('\n', cp_lexer_debug_stream);
612     }
613
614   return token;
615 }
616
617 /* Return the next token, and advance the lexer's next_token pointer
618    to point to the next non-purged token.  */
619
620 static cp_token *
621 cp_lexer_consume_token (cp_lexer* lexer)
622 {
623   cp_token *token = lexer->next_token;
624
625   gcc_assert (token != &eof_token);
626   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
627
628   do
629     {
630       lexer->next_token++;
631       if (lexer->next_token == lexer->last_token)
632         {
633           lexer->next_token = (cp_token *)&eof_token;
634           break;
635         }
636
637     }
638   while (lexer->next_token->type == CPP_PURGED);
639
640   cp_lexer_set_source_position_from_token (token);
641
642   /* Provide debugging output.  */
643   if (cp_lexer_debugging_p (lexer))
644     {
645       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
646       cp_lexer_print_token (cp_lexer_debug_stream, token);
647       putc ('\n', cp_lexer_debug_stream);
648     }
649
650   return token;
651 }
652
653 /* Permanently remove the next token from the token stream, and
654    advance the next_token pointer to refer to the next non-purged
655    token.  */
656
657 static void
658 cp_lexer_purge_token (cp_lexer *lexer)
659 {
660   cp_token *tok = lexer->next_token;
661
662   gcc_assert (tok != &eof_token);
663   tok->type = CPP_PURGED;
664   tok->location = UNKNOWN_LOCATION;
665   tok->u.value = NULL_TREE;
666   tok->keyword = RID_MAX;
667
668   do
669     {
670       tok++;
671       if (tok == lexer->last_token)
672         {
673           tok = (cp_token *)&eof_token;
674           break;
675         }
676     }
677   while (tok->type == CPP_PURGED);
678   lexer->next_token = tok;
679 }
680
681 /* Permanently remove all tokens after TOK, up to, but not
682    including, the token that will be returned next by
683    cp_lexer_peek_token.  */
684
685 static void
686 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
687 {
688   cp_token *peek = lexer->next_token;
689
690   if (peek == &eof_token)
691     peek = lexer->last_token;
692
693   gcc_assert (tok < peek);
694
695   for ( tok += 1; tok != peek; tok += 1)
696     {
697       tok->type = CPP_PURGED;
698       tok->location = UNKNOWN_LOCATION;
699       tok->u.value = NULL_TREE;
700       tok->keyword = RID_MAX;
701     }
702 }
703
704 /* Begin saving tokens.  All tokens consumed after this point will be
705    preserved.  */
706
707 static void
708 cp_lexer_save_tokens (cp_lexer* lexer)
709 {
710   /* Provide debugging output.  */
711   if (cp_lexer_debugging_p (lexer))
712     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
713
714   VEC_safe_push (cp_token_position, heap,
715                  lexer->saved_tokens, lexer->next_token);
716 }
717
718 /* Commit to the portion of the token stream most recently saved.  */
719
720 static void
721 cp_lexer_commit_tokens (cp_lexer* lexer)
722 {
723   /* Provide debugging output.  */
724   if (cp_lexer_debugging_p (lexer))
725     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
726
727   VEC_pop (cp_token_position, lexer->saved_tokens);
728 }
729
730 /* Return all tokens saved since the last call to cp_lexer_save_tokens
731    to the token stream.  Stop saving tokens.  */
732
733 static void
734 cp_lexer_rollback_tokens (cp_lexer* lexer)
735 {
736   /* Provide debugging output.  */
737   if (cp_lexer_debugging_p (lexer))
738     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
739
740   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Print a representation of the TOKEN on the STREAM.  */
744
745 #ifdef ENABLE_CHECKING
746
747 static void
748 cp_lexer_print_token (FILE * stream, cp_token *token)
749 {
750   /* We don't use cpp_type2name here because the parser defines
751      a few tokens of its own.  */
752   static const char *const token_names[] = {
753     /* cpplib-defined token types */
754 #define OP(e, s) #e,
755 #define TK(e, s) #e,
756     TTYPE_TABLE
757 #undef OP
758 #undef TK
759     /* C++ parser token types - see "Manifest constants", above.  */
760     "KEYWORD",
761     "TEMPLATE_ID",
762     "NESTED_NAME_SPECIFIER",
763     "PURGED"
764   };
765
766   /* If we have a name for the token, print it out.  Otherwise, we
767      simply give the numeric code.  */
768   gcc_assert (token->type < ARRAY_SIZE(token_names));
769   fputs (token_names[token->type], stream);
770
771   /* For some tokens, print the associated data.  */
772   switch (token->type)
773     {
774     case CPP_KEYWORD:
775       /* Some keywords have a value that is not an IDENTIFIER_NODE.
776          For example, `struct' is mapped to an INTEGER_CST.  */
777       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
778         break;
779       /* else fall through */
780     case CPP_NAME:
781       fputs (IDENTIFIER_POINTER (token->u.value), stream);
782       break;
783
784     case CPP_STRING:
785     case CPP_WSTRING:
786       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
787       break;
788
789     default:
790       break;
791     }
792 }
793
794 /* Start emitting debugging information.  */
795
796 static void
797 cp_lexer_start_debugging (cp_lexer* lexer)
798 {
799   lexer->debugging_p = true;
800 }
801
802 /* Stop emitting debugging information.  */
803
804 static void
805 cp_lexer_stop_debugging (cp_lexer* lexer)
806 {
807   lexer->debugging_p = false;
808 }
809
810 #endif /* ENABLE_CHECKING */
811
812 /* Create a new cp_token_cache, representing a range of tokens.  */
813
814 static cp_token_cache *
815 cp_token_cache_new (cp_token *first, cp_token *last)
816 {
817   cp_token_cache *cache = GGC_NEW (cp_token_cache);
818   cache->first = first;
819   cache->last = last;
820   return cache;
821 }
822
823 \f
824 /* Decl-specifiers.  */
825
826 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
827
828 static void
829 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
830 {
831   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
832 }
833
834 /* Declarators.  */
835
836 /* Nothing other than the parser should be creating declarators;
837    declarators are a semi-syntactic representation of C++ entities.
838    Other parts of the front end that need to create entities (like
839    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
840
841 static cp_declarator *make_call_declarator
842   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
843 static cp_declarator *make_array_declarator
844   (cp_declarator *, tree);
845 static cp_declarator *make_pointer_declarator
846   (cp_cv_quals, cp_declarator *);
847 static cp_declarator *make_reference_declarator
848   (cp_cv_quals, cp_declarator *);
849 static cp_parameter_declarator *make_parameter_declarator
850   (cp_decl_specifier_seq *, cp_declarator *, tree);
851 static cp_declarator *make_ptrmem_declarator
852   (cp_cv_quals, tree, cp_declarator *);
853
854 /* An erroneous declarator.  */
855 static cp_declarator *cp_error_declarator;
856
857 /* The obstack on which declarators and related data structures are
858    allocated.  */
859 static struct obstack declarator_obstack;
860
861 /* Alloc BYTES from the declarator memory pool.  */
862
863 static inline void *
864 alloc_declarator (size_t bytes)
865 {
866   return obstack_alloc (&declarator_obstack, bytes);
867 }
868
869 /* Allocate a declarator of the indicated KIND.  Clear fields that are
870    common to all declarators.  */
871
872 static cp_declarator *
873 make_declarator (cp_declarator_kind kind)
874 {
875   cp_declarator *declarator;
876
877   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
878   declarator->kind = kind;
879   declarator->attributes = NULL_TREE;
880   declarator->declarator = NULL;
881
882   return declarator;
883 }
884
885 /* Make a declarator for a generalized identifier.  If
886    QUALIFYING_SCOPE is non-NULL, the identifier is
887    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
888    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
889    is, if any.   */
890
891 static cp_declarator *
892 make_id_declarator (tree qualifying_scope, tree unqualified_name,
893                     special_function_kind sfk)
894 {
895   cp_declarator *declarator;
896
897   /* It is valid to write:
898
899        class C { void f(); };
900        typedef C D;
901        void D::f();
902
903      The standard is not clear about whether `typedef const C D' is
904      legal; as of 2002-09-15 the committee is considering that
905      question.  EDG 3.0 allows that syntax.  Therefore, we do as
906      well.  */
907   if (qualifying_scope && TYPE_P (qualifying_scope))
908     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
909
910   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
911               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
912               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
913
914   declarator = make_declarator (cdk_id);
915   declarator->u.id.qualifying_scope = qualifying_scope;
916   declarator->u.id.unqualified_name = unqualified_name;
917   declarator->u.id.sfk = sfk;
918
919   return declarator;
920 }
921
922 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
923    of modifiers such as const or volatile to apply to the pointer
924    type, represented as identifiers.  */
925
926 cp_declarator *
927 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
928 {
929   cp_declarator *declarator;
930
931   declarator = make_declarator (cdk_pointer);
932   declarator->declarator = target;
933   declarator->u.pointer.qualifiers = cv_qualifiers;
934   declarator->u.pointer.class_type = NULL_TREE;
935
936   return declarator;
937 }
938
939 /* Like make_pointer_declarator -- but for references.  */
940
941 cp_declarator *
942 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_reference);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950
951   return declarator;
952 }
953
954 /* Like make_pointer_declarator -- but for a pointer to a non-static
955    member of CLASS_TYPE.  */
956
957 cp_declarator *
958 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
959                         cp_declarator *pointee)
960 {
961   cp_declarator *declarator;
962
963   declarator = make_declarator (cdk_ptrmem);
964   declarator->declarator = pointee;
965   declarator->u.pointer.qualifiers = cv_qualifiers;
966   declarator->u.pointer.class_type = class_type;
967
968   return declarator;
969 }
970
971 /* Make a declarator for the function given by TARGET, with the
972    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
973    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
974    indicates what exceptions can be thrown.  */
975
976 cp_declarator *
977 make_call_declarator (cp_declarator *target,
978                       cp_parameter_declarator *parms,
979                       cp_cv_quals cv_qualifiers,
980                       tree exception_specification)
981 {
982   cp_declarator *declarator;
983
984   declarator = make_declarator (cdk_function);
985   declarator->declarator = target;
986   declarator->u.function.parameters = parms;
987   declarator->u.function.qualifiers = cv_qualifiers;
988   declarator->u.function.exception_specification = exception_specification;
989
990   return declarator;
991 }
992
993 /* Make a declarator for an array of BOUNDS elements, each of which is
994    defined by ELEMENT.  */
995
996 cp_declarator *
997 make_array_declarator (cp_declarator *element, tree bounds)
998 {
999   cp_declarator *declarator;
1000
1001   declarator = make_declarator (cdk_array);
1002   declarator->declarator = element;
1003   declarator->u.array.bounds = bounds;
1004
1005   return declarator;
1006 }
1007
1008 cp_parameter_declarator *no_parameters;
1009
1010 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1011    DECLARATOR and DEFAULT_ARGUMENT.  */
1012
1013 cp_parameter_declarator *
1014 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1015                            cp_declarator *declarator,
1016                            tree default_argument)
1017 {
1018   cp_parameter_declarator *parameter;
1019
1020   parameter = ((cp_parameter_declarator *)
1021                alloc_declarator (sizeof (cp_parameter_declarator)));
1022   parameter->next = NULL;
1023   if (decl_specifiers)
1024     parameter->decl_specifiers = *decl_specifiers;
1025   else
1026     clear_decl_specs (&parameter->decl_specifiers);
1027   parameter->declarator = declarator;
1028   parameter->default_argument = default_argument;
1029   parameter->ellipsis_p = false;
1030
1031   return parameter;
1032 }
1033
1034 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1035
1036 static bool
1037 function_declarator_p (const cp_declarator *declarator)
1038 {
1039   while (declarator)
1040     {
1041       if (declarator->kind == cdk_function
1042           && declarator->declarator->kind == cdk_id)
1043         return true;
1044       if (declarator->kind == cdk_id
1045           || declarator->kind == cdk_error)
1046         return false;
1047       declarator = declarator->declarator;
1048     }
1049   return false;
1050 }
1051  
1052 /* The parser.  */
1053
1054 /* Overview
1055    --------
1056
1057    A cp_parser parses the token stream as specified by the C++
1058    grammar.  Its job is purely parsing, not semantic analysis.  For
1059    example, the parser breaks the token stream into declarators,
1060    expressions, statements, and other similar syntactic constructs.
1061    It does not check that the types of the expressions on either side
1062    of an assignment-statement are compatible, or that a function is
1063    not declared with a parameter of type `void'.
1064
1065    The parser invokes routines elsewhere in the compiler to perform
1066    semantic analysis and to build up the abstract syntax tree for the
1067    code processed.
1068
1069    The parser (and the template instantiation code, which is, in a
1070    way, a close relative of parsing) are the only parts of the
1071    compiler that should be calling push_scope and pop_scope, or
1072    related functions.  The parser (and template instantiation code)
1073    keeps track of what scope is presently active; everything else
1074    should simply honor that.  (The code that generates static
1075    initializers may also need to set the scope, in order to check
1076    access control correctly when emitting the initializers.)
1077
1078    Methodology
1079    -----------
1080
1081    The parser is of the standard recursive-descent variety.  Upcoming
1082    tokens in the token stream are examined in order to determine which
1083    production to use when parsing a non-terminal.  Some C++ constructs
1084    require arbitrary look ahead to disambiguate.  For example, it is
1085    impossible, in the general case, to tell whether a statement is an
1086    expression or declaration without scanning the entire statement.
1087    Therefore, the parser is capable of "parsing tentatively."  When the
1088    parser is not sure what construct comes next, it enters this mode.
1089    Then, while we attempt to parse the construct, the parser queues up
1090    error messages, rather than issuing them immediately, and saves the
1091    tokens it consumes.  If the construct is parsed successfully, the
1092    parser "commits", i.e., it issues any queued error messages and
1093    the tokens that were being preserved are permanently discarded.
1094    If, however, the construct is not parsed successfully, the parser
1095    rolls back its state completely so that it can resume parsing using
1096    a different alternative.
1097
1098    Future Improvements
1099    -------------------
1100
1101    The performance of the parser could probably be improved substantially.
1102    We could often eliminate the need to parse tentatively by looking ahead
1103    a little bit.  In some places, this approach might not entirely eliminate
1104    the need to parse tentatively, but it might still speed up the average
1105    case.  */
1106
1107 /* Flags that are passed to some parsing functions.  These values can
1108    be bitwise-ored together.  */
1109
1110 typedef enum cp_parser_flags
1111 {
1112   /* No flags.  */
1113   CP_PARSER_FLAGS_NONE = 0x0,
1114   /* The construct is optional.  If it is not present, then no error
1115      should be issued.  */
1116   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1117   /* When parsing a type-specifier, do not allow user-defined types.  */
1118   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1119 } cp_parser_flags;
1120
1121 /* The different kinds of declarators we want to parse.  */
1122
1123 typedef enum cp_parser_declarator_kind
1124 {
1125   /* APPLE LOCAL begin blocks 6339747 */
1126   /* We want a block declarator.  */
1127   CP_PARSER_DECLARATOR_BLOCK,
1128   /* APPLE LOCAL end blocks 6339747 */
1129   /* We want an abstract declarator.  */
1130   CP_PARSER_DECLARATOR_ABSTRACT,
1131   /* We want a named declarator.  */
1132   CP_PARSER_DECLARATOR_NAMED,
1133   /* We don't mind, but the name must be an unqualified-id.  */
1134   CP_PARSER_DECLARATOR_EITHER
1135 } cp_parser_declarator_kind;
1136
1137 /* The precedence values used to parse binary expressions.  The minimum value
1138    of PREC must be 1, because zero is reserved to quickly discriminate
1139    binary operators from other tokens.  */
1140
1141 enum cp_parser_prec
1142 {
1143   PREC_NOT_OPERATOR,
1144   PREC_LOGICAL_OR_EXPRESSION,
1145   PREC_LOGICAL_AND_EXPRESSION,
1146   PREC_INCLUSIVE_OR_EXPRESSION,
1147   PREC_EXCLUSIVE_OR_EXPRESSION,
1148   PREC_AND_EXPRESSION,
1149   PREC_EQUALITY_EXPRESSION,
1150   PREC_RELATIONAL_EXPRESSION,
1151   PREC_SHIFT_EXPRESSION,
1152   PREC_ADDITIVE_EXPRESSION,
1153   PREC_MULTIPLICATIVE_EXPRESSION,
1154   PREC_PM_EXPRESSION,
1155   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1156 };
1157
1158 /* A mapping from a token type to a corresponding tree node type, with a
1159    precedence value.  */
1160
1161 typedef struct cp_parser_binary_operations_map_node
1162 {
1163   /* The token type.  */
1164   enum cpp_ttype token_type;
1165   /* The corresponding tree code.  */
1166   enum tree_code tree_type;
1167   /* The precedence of this operator.  */
1168   enum cp_parser_prec prec;
1169 } cp_parser_binary_operations_map_node;
1170
1171 /* The status of a tentative parse.  */
1172
1173 typedef enum cp_parser_status_kind
1174 {
1175   /* No errors have occurred.  */
1176   CP_PARSER_STATUS_KIND_NO_ERROR,
1177   /* An error has occurred.  */
1178   CP_PARSER_STATUS_KIND_ERROR,
1179   /* We are committed to this tentative parse, whether or not an error
1180      has occurred.  */
1181   CP_PARSER_STATUS_KIND_COMMITTED
1182 } cp_parser_status_kind;
1183
1184 typedef struct cp_parser_expression_stack_entry
1185 {
1186   /* Left hand side of the binary operation we are currently
1187      parsing.  */
1188   tree lhs;
1189   /* Original tree code for left hand side, if it was a binary
1190      expression itself (used for -Wparentheses).  */
1191   enum tree_code lhs_type;
1192   /* Tree code for the binary operation we are parsing.  */
1193   enum tree_code tree_type;
1194   /* Precedence of the binary operation we are parsing.  */
1195   int prec;
1196 } cp_parser_expression_stack_entry;
1197
1198 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1199    entries because precedence levels on the stack are monotonically
1200    increasing.  */
1201 typedef struct cp_parser_expression_stack_entry
1202   cp_parser_expression_stack[NUM_PREC_VALUES];
1203
1204 /* Context that is saved and restored when parsing tentatively.  */
1205 typedef struct cp_parser_context GTY (())
1206 {
1207   /* If this is a tentative parsing context, the status of the
1208      tentative parse.  */
1209   enum cp_parser_status_kind status;
1210   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1211      that are looked up in this context must be looked up both in the
1212      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1213      the context of the containing expression.  */
1214   tree object_type;
1215
1216   /* The next parsing context in the stack.  */
1217   struct cp_parser_context *next;
1218 } cp_parser_context;
1219
1220 /* Prototypes.  */
1221
1222 /* Constructors and destructors.  */
1223
1224 static cp_parser_context *cp_parser_context_new
1225   (cp_parser_context *);
1226
1227 /* Class variables.  */
1228
1229 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1230
1231 /* The operator-precedence table used by cp_parser_binary_expression.
1232    Transformed into an associative array (binops_by_token) by
1233    cp_parser_new.  */
1234
1235 static const cp_parser_binary_operations_map_node binops[] = {
1236   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1237   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1238
1239   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1240   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1241   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1242
1243   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1244   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1245
1246   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1247   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1248
1249   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1250   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1251   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1252   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1253
1254   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1255   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1256
1257   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1258
1259   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1260
1261   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1262
1263   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1264
1265   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1266 };
1267
1268 /* The same as binops, but initialized by cp_parser_new so that
1269    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1270    for speed.  */
1271 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1272
1273 /* Constructors and destructors.  */
1274
1275 /* Construct a new context.  The context below this one on the stack
1276    is given by NEXT.  */
1277
1278 static cp_parser_context *
1279 cp_parser_context_new (cp_parser_context* next)
1280 {
1281   cp_parser_context *context;
1282
1283   /* Allocate the storage.  */
1284   if (cp_parser_context_free_list != NULL)
1285     {
1286       /* Pull the first entry from the free list.  */
1287       context = cp_parser_context_free_list;
1288       cp_parser_context_free_list = context->next;
1289       memset (context, 0, sizeof (*context));
1290     }
1291   else
1292     context = GGC_CNEW (cp_parser_context);
1293
1294   /* No errors have occurred yet in this context.  */
1295   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1296   /* If this is not the bottomost context, copy information that we
1297      need from the previous context.  */
1298   if (next)
1299     {
1300       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1301          expression, then we are parsing one in this context, too.  */
1302       context->object_type = next->object_type;
1303       /* Thread the stack.  */
1304       context->next = next;
1305     }
1306
1307   return context;
1308 }
1309
1310 /* The cp_parser structure represents the C++ parser.  */
1311
1312 typedef struct cp_parser GTY(())
1313 {
1314   /* The lexer from which we are obtaining tokens.  */
1315   cp_lexer *lexer;
1316
1317   /* The scope in which names should be looked up.  If NULL_TREE, then
1318      we look up names in the scope that is currently open in the
1319      source program.  If non-NULL, this is either a TYPE or
1320      NAMESPACE_DECL for the scope in which we should look.  It can
1321      also be ERROR_MARK, when we've parsed a bogus scope.
1322
1323      This value is not cleared automatically after a name is looked
1324      up, so we must be careful to clear it before starting a new look
1325      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1326      will look up `Z' in the scope of `X', rather than the current
1327      scope.)  Unfortunately, it is difficult to tell when name lookup
1328      is complete, because we sometimes peek at a token, look it up,
1329      and then decide not to consume it.   */
1330   tree scope;
1331
1332   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1333      last lookup took place.  OBJECT_SCOPE is used if an expression
1334      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1335      respectively.  QUALIFYING_SCOPE is used for an expression of the
1336      form "X::Y"; it refers to X.  */
1337   tree object_scope;
1338   tree qualifying_scope;
1339
1340   /* A stack of parsing contexts.  All but the bottom entry on the
1341      stack will be tentative contexts.
1342
1343      We parse tentatively in order to determine which construct is in
1344      use in some situations.  For example, in order to determine
1345      whether a statement is an expression-statement or a
1346      declaration-statement we parse it tentatively as a
1347      declaration-statement.  If that fails, we then reparse the same
1348      token stream as an expression-statement.  */
1349   cp_parser_context *context;
1350
1351   /* True if we are parsing GNU C++.  If this flag is not set, then
1352      GNU extensions are not recognized.  */
1353   bool allow_gnu_extensions_p;
1354
1355   /* TRUE if the `>' token should be interpreted as the greater-than
1356      operator.  FALSE if it is the end of a template-id or
1357      template-parameter-list.  */
1358   bool greater_than_is_operator_p;
1359
1360   /* TRUE if default arguments are allowed within a parameter list
1361      that starts at this point. FALSE if only a gnu extension makes
1362      them permissible.  */
1363   bool default_arg_ok_p;
1364
1365   /* TRUE if we are parsing an integral constant-expression.  See
1366      [expr.const] for a precise definition.  */
1367   bool integral_constant_expression_p;
1368
1369   /* TRUE if we are parsing an integral constant-expression -- but a
1370      non-constant expression should be permitted as well.  This flag
1371      is used when parsing an array bound so that GNU variable-length
1372      arrays are tolerated.  */
1373   bool allow_non_integral_constant_expression_p;
1374
1375   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1376      been seen that makes the expression non-constant.  */
1377   bool non_integral_constant_expression_p;
1378
1379   /* TRUE if local variable names and `this' are forbidden in the
1380      current context.  */
1381   bool local_variables_forbidden_p;
1382
1383   /* TRUE if the declaration we are parsing is part of a
1384      linkage-specification of the form `extern string-literal
1385      declaration'.  */
1386   bool in_unbraced_linkage_specification_p;
1387
1388   /* TRUE if we are presently parsing a declarator, after the
1389      direct-declarator.  */
1390   bool in_declarator_p;
1391
1392   /* TRUE if we are presently parsing a template-argument-list.  */
1393   bool in_template_argument_list_p;
1394
1395   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1396      to IN_OMP_BLOCK if parsing OpenMP structured block and
1397      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1398      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1399      iteration-statement, OpenMP block or loop within that switch.  */
1400 #define IN_SWITCH_STMT          1
1401 #define IN_ITERATION_STMT       2
1402 #define IN_OMP_BLOCK            4
1403 #define IN_OMP_FOR              8
1404   unsigned char in_statement;
1405
1406   /* TRUE if we are presently parsing the body of a switch statement.
1407      Note that this doesn't quite overlap with in_statement above.
1408      The difference relates to giving the right sets of error messages:
1409      "case not in switch" vs "break statement used with OpenMP...".  */
1410   bool in_switch_statement_p;
1411
1412   /* TRUE if we are parsing a type-id in an expression context.  In
1413      such a situation, both "type (expr)" and "type (type)" are valid
1414      alternatives.  */
1415   bool in_type_id_in_expr_p;
1416
1417   /* TRUE if we are currently in a header file where declarations are
1418      implicitly extern "C".  */
1419   bool implicit_extern_c;
1420
1421   /* TRUE if strings in expressions should be translated to the execution
1422      character set.  */
1423   bool translate_strings_p;
1424
1425   /* TRUE if we are presently parsing the body of a function, but not
1426      a local class.  */
1427   bool in_function_body;
1428
1429   /* If non-NULL, then we are parsing a construct where new type
1430      definitions are not permitted.  The string stored here will be
1431      issued as an error message if a type is defined.  */
1432   const char *type_definition_forbidden_message;
1433
1434   /* A list of lists. The outer list is a stack, used for member
1435      functions of local classes. At each level there are two sub-list,
1436      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1437      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1438      TREE_VALUE's. The functions are chained in reverse declaration
1439      order.
1440
1441      The TREE_PURPOSE sublist contains those functions with default
1442      arguments that need post processing, and the TREE_VALUE sublist
1443      contains those functions with definitions that need post
1444      processing.
1445
1446      These lists can only be processed once the outermost class being
1447      defined is complete.  */
1448   tree unparsed_functions_queues;
1449
1450   /* The number of classes whose definitions are currently in
1451      progress.  */
1452   unsigned num_classes_being_defined;
1453
1454   /* The number of template parameter lists that apply directly to the
1455      current declaration.  */
1456   unsigned num_template_parameter_lists;
1457 } cp_parser;
1458
1459 /* Prototypes.  */
1460
1461 /* Constructors and destructors.  */
1462
1463 static cp_parser *cp_parser_new
1464   (void);
1465
1466 /* Routines to parse various constructs.
1467
1468    Those that return `tree' will return the error_mark_node (rather
1469    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1470    Sometimes, they will return an ordinary node if error-recovery was
1471    attempted, even though a parse error occurred.  So, to check
1472    whether or not a parse error occurred, you should always use
1473    cp_parser_error_occurred.  If the construct is optional (indicated
1474    either by an `_opt' in the name of the function that does the
1475    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1476    the construct is not present.  */
1477
1478 /* Lexical conventions [gram.lex]  */
1479
1480 static tree cp_parser_identifier
1481   (cp_parser *);
1482 static tree cp_parser_string_literal
1483   (cp_parser *, bool, bool);
1484
1485 /* Basic concepts [gram.basic]  */
1486
1487 static bool cp_parser_translation_unit
1488   (cp_parser *);
1489
1490 /* Expressions [gram.expr]  */
1491
1492 static tree cp_parser_primary_expression
1493   (cp_parser *, bool, bool, bool, cp_id_kind *);
1494 static tree cp_parser_id_expression
1495   (cp_parser *, bool, bool, bool *, bool, bool);
1496 static tree cp_parser_unqualified_id
1497   (cp_parser *, bool, bool, bool, bool);
1498 static tree cp_parser_nested_name_specifier_opt
1499   (cp_parser *, bool, bool, bool, bool);
1500 static tree cp_parser_nested_name_specifier
1501   (cp_parser *, bool, bool, bool, bool);
1502 static tree cp_parser_class_or_namespace_name
1503   (cp_parser *, bool, bool, bool, bool, bool);
1504 static tree cp_parser_postfix_expression
1505   (cp_parser *, bool, bool);
1506 static tree cp_parser_postfix_open_square_expression
1507   (cp_parser *, tree, bool);
1508 static tree cp_parser_postfix_dot_deref_expression
1509   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1510 static tree cp_parser_parenthesized_expression_list
1511   (cp_parser *, bool, bool, bool *);
1512 static void cp_parser_pseudo_destructor_name
1513   (cp_parser *, tree *, tree *);
1514 static tree cp_parser_unary_expression
1515   (cp_parser *, bool, bool);
1516 static enum tree_code cp_parser_unary_operator
1517   (cp_token *);
1518 static tree cp_parser_new_expression
1519   (cp_parser *);
1520 static tree cp_parser_new_placement
1521   (cp_parser *);
1522 static tree cp_parser_new_type_id
1523   (cp_parser *, tree *);
1524 static cp_declarator *cp_parser_new_declarator_opt
1525   (cp_parser *);
1526 static cp_declarator *cp_parser_direct_new_declarator
1527   (cp_parser *);
1528 static tree cp_parser_new_initializer
1529   (cp_parser *);
1530 static tree cp_parser_delete_expression
1531   (cp_parser *);
1532 static tree cp_parser_cast_expression
1533   (cp_parser *, bool, bool);
1534 static tree cp_parser_binary_expression
1535   (cp_parser *, bool);
1536 static tree cp_parser_question_colon_clause
1537   (cp_parser *, tree);
1538 static tree cp_parser_assignment_expression
1539   (cp_parser *, bool);
1540 static enum tree_code cp_parser_assignment_operator_opt
1541   (cp_parser *);
1542 static tree cp_parser_expression
1543   (cp_parser *, bool);
1544 static tree cp_parser_constant_expression
1545   (cp_parser *, bool, bool *);
1546 static tree cp_parser_builtin_offsetof
1547   (cp_parser *);
1548 /* APPLE LOCAL begin blocks 6040305 (ca) */
1549 static tree cp_parser_block_literal_expr (cp_parser *);
1550 /* APPLE LOCAL end blocks 6040305 (ca) */
1551 /* APPLE LOCAL begin C* language */
1552 static void objc_foreach_stmt 
1553   (cp_parser *, tree);
1554 /* APPLE LOCAL end C* language */
1555 /* APPLE LOCAL begin C* property (Radar 4436866) */
1556 static void objc_cp_parser_at_property
1557   (cp_parser *);
1558 static void objc_cp_parse_property_decl
1559   (cp_parser *);
1560 /* APPLE LOCAL end C* property (Radar 4436866) */
1561 /* APPLE LOCAL begin radar 4548636 */
1562 static bool objc_attr_follwed_by_at_keyword
1563   (cp_parser *);
1564 /* APPLE LOCAL end radar 4548636 */
1565
1566 /* Statements [gram.stmt.stmt]  */
1567
1568 static void cp_parser_statement
1569   (cp_parser *, tree, bool, bool *);
1570 static void cp_parser_label_for_labeled_statement
1571   (cp_parser *);
1572 static tree cp_parser_expression_statement
1573   (cp_parser *, tree);
1574 static tree cp_parser_compound_statement
1575   /* APPLE LOCAL radar 5982990 */
1576   (cp_parser *, tree, bool, bool);
1577 static void cp_parser_statement_seq_opt
1578   (cp_parser *, tree);
1579 static tree cp_parser_selection_statement
1580   (cp_parser *, bool *);
1581 static tree cp_parser_condition
1582   (cp_parser *);
1583 static tree cp_parser_iteration_statement
1584   (cp_parser *);
1585 static void cp_parser_for_init_statement
1586   (cp_parser *);
1587 static tree cp_parser_jump_statement
1588   (cp_parser *);
1589 static void cp_parser_declaration_statement
1590   (cp_parser *);
1591
1592 static tree cp_parser_implicitly_scoped_statement
1593   (cp_parser *, bool *);
1594 static void cp_parser_already_scoped_statement
1595   (cp_parser *);
1596
1597 /* Declarations [gram.dcl.dcl] */
1598
1599 static void cp_parser_declaration_seq_opt
1600   (cp_parser *);
1601 static void cp_parser_declaration
1602   (cp_parser *);
1603 static void cp_parser_block_declaration
1604   (cp_parser *, bool);
1605 static void cp_parser_simple_declaration
1606   (cp_parser *, bool);
1607 static void cp_parser_decl_specifier_seq
1608   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1609 static tree cp_parser_storage_class_specifier_opt
1610   (cp_parser *);
1611 static tree cp_parser_function_specifier_opt
1612   (cp_parser *, cp_decl_specifier_seq *);
1613 static tree cp_parser_type_specifier
1614   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1615    int *, bool *);
1616 static tree cp_parser_simple_type_specifier
1617   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1618 static tree cp_parser_type_name
1619   (cp_parser *);
1620 static tree cp_parser_elaborated_type_specifier
1621   (cp_parser *, bool, bool);
1622 static tree cp_parser_enum_specifier
1623   (cp_parser *);
1624 static void cp_parser_enumerator_list
1625   (cp_parser *, tree);
1626 static void cp_parser_enumerator_definition
1627   (cp_parser *, tree);
1628 static tree cp_parser_namespace_name
1629   (cp_parser *);
1630 static void cp_parser_namespace_definition
1631   (cp_parser *);
1632 static void cp_parser_namespace_body
1633   (cp_parser *);
1634 static tree cp_parser_qualified_namespace_specifier
1635   (cp_parser *);
1636 static void cp_parser_namespace_alias_definition
1637   (cp_parser *);
1638 static bool cp_parser_using_declaration
1639   (cp_parser *, bool);
1640 static void cp_parser_using_directive
1641   (cp_parser *);
1642 static void cp_parser_asm_definition
1643   (cp_parser *);
1644 static void cp_parser_linkage_specification
1645   (cp_parser *);
1646
1647 /* Declarators [gram.dcl.decl] */
1648
1649 static tree cp_parser_init_declarator
1650   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1651 static cp_declarator *cp_parser_declarator
1652   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1653 static cp_declarator *cp_parser_direct_declarator
1654   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1655 static enum tree_code cp_parser_ptr_operator
1656   (cp_parser *, tree *, cp_cv_quals *);
1657 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1658   (cp_parser *);
1659 static tree cp_parser_declarator_id
1660   (cp_parser *, bool);
1661 static tree cp_parser_type_id
1662   (cp_parser *);
1663 static void cp_parser_type_specifier_seq
1664   (cp_parser *, bool, cp_decl_specifier_seq *);
1665 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1666   (cp_parser *);
1667 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1668   (cp_parser *, bool *);
1669 static cp_parameter_declarator *cp_parser_parameter_declaration
1670   (cp_parser *, bool, bool *);
1671 static void cp_parser_function_body
1672   (cp_parser *);
1673 static tree cp_parser_initializer
1674   (cp_parser *, bool *, bool *);
1675 static tree cp_parser_initializer_clause
1676   (cp_parser *, bool *);
1677 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1678   (cp_parser *, bool *);
1679
1680 static bool cp_parser_ctor_initializer_opt_and_function_body
1681   (cp_parser *);
1682
1683 /* Classes [gram.class] */
1684
1685 static tree cp_parser_class_name
1686   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1687 static tree cp_parser_class_specifier
1688   (cp_parser *);
1689 static tree cp_parser_class_head
1690   (cp_parser *, bool *, tree *, tree *);
1691 static enum tag_types cp_parser_class_key
1692   (cp_parser *);
1693 static void cp_parser_member_specification_opt
1694   (cp_parser *);
1695 static void cp_parser_member_declaration
1696   (cp_parser *);
1697 static tree cp_parser_pure_specifier
1698   (cp_parser *);
1699 static tree cp_parser_constant_initializer
1700   (cp_parser *);
1701
1702 /* Derived classes [gram.class.derived] */
1703
1704 static tree cp_parser_base_clause
1705   (cp_parser *);
1706 static tree cp_parser_base_specifier
1707   (cp_parser *);
1708
1709 /* Special member functions [gram.special] */
1710
1711 static tree cp_parser_conversion_function_id
1712   (cp_parser *);
1713 static tree cp_parser_conversion_type_id
1714   (cp_parser *);
1715 static cp_declarator *cp_parser_conversion_declarator_opt
1716   (cp_parser *);
1717 static bool cp_parser_ctor_initializer_opt
1718   (cp_parser *);
1719 static void cp_parser_mem_initializer_list
1720   (cp_parser *);
1721 static tree cp_parser_mem_initializer
1722   (cp_parser *);
1723 static tree cp_parser_mem_initializer_id
1724   (cp_parser *);
1725
1726 /* Overloading [gram.over] */
1727
1728 static tree cp_parser_operator_function_id
1729   (cp_parser *);
1730 static tree cp_parser_operator
1731   (cp_parser *);
1732
1733 /* Templates [gram.temp] */
1734
1735 static void cp_parser_template_declaration
1736   (cp_parser *, bool);
1737 static tree cp_parser_template_parameter_list
1738   (cp_parser *);
1739 static tree cp_parser_template_parameter
1740   (cp_parser *, bool *);
1741 static tree cp_parser_type_parameter
1742   (cp_parser *);
1743 static tree cp_parser_template_id
1744   (cp_parser *, bool, bool, bool);
1745 static tree cp_parser_template_name
1746   (cp_parser *, bool, bool, bool, bool *);
1747 static tree cp_parser_template_argument_list
1748   (cp_parser *);
1749 static tree cp_parser_template_argument
1750   (cp_parser *);
1751 static void cp_parser_explicit_instantiation
1752   (cp_parser *);
1753 static void cp_parser_explicit_specialization
1754   (cp_parser *);
1755
1756 /* Exception handling [gram.exception] */
1757
1758 static tree cp_parser_try_block
1759   (cp_parser *);
1760 static bool cp_parser_function_try_block
1761   (cp_parser *);
1762 static void cp_parser_handler_seq
1763   (cp_parser *);
1764 static void cp_parser_handler
1765   (cp_parser *);
1766 static tree cp_parser_exception_declaration
1767   (cp_parser *);
1768 static tree cp_parser_throw_expression
1769   (cp_parser *);
1770 static tree cp_parser_exception_specification_opt
1771   (cp_parser *);
1772 static tree cp_parser_type_id_list
1773   (cp_parser *);
1774
1775 /* GNU Extensions */
1776
1777 static tree cp_parser_asm_specification_opt
1778   (cp_parser *);
1779 static tree cp_parser_asm_operand_list
1780   (cp_parser *);
1781 static tree cp_parser_asm_clobber_list
1782   (cp_parser *);
1783 static tree cp_parser_attributes_opt
1784   (cp_parser *);
1785 static tree cp_parser_attribute_list
1786   (cp_parser *);
1787 static bool cp_parser_extension_opt
1788   (cp_parser *, int *);
1789 static void cp_parser_label_declaration
1790   (cp_parser *);
1791
1792 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1793 static bool cp_parser_pragma
1794   (cp_parser *, enum pragma_context);
1795
1796 /* Objective-C++ Productions */
1797
1798 static tree cp_parser_objc_message_receiver
1799   (cp_parser *);
1800 static tree cp_parser_objc_message_args
1801   (cp_parser *);
1802 static tree cp_parser_objc_message_expression
1803   (cp_parser *);
1804 /* APPLE LOCAL begin radar 5277239 */
1805 static tree cp_parser_objc_reference_expression
1806   (cp_parser *, tree);
1807 /* APPLE LOCAL end radar 5277239 */
1808 static tree cp_parser_objc_encode_expression
1809   (cp_parser *);
1810 static tree cp_parser_objc_defs_expression
1811   (cp_parser *);
1812 static tree cp_parser_objc_protocol_expression
1813   (cp_parser *);
1814 static tree cp_parser_objc_selector_expression
1815   (cp_parser *);
1816 static tree cp_parser_objc_expression
1817   (cp_parser *);
1818 static bool cp_parser_objc_selector_p
1819   (enum cpp_ttype);
1820 static tree cp_parser_objc_selector
1821   (cp_parser *);
1822 /* APPLE LOCAL begin radar 3803157 - objc attribute */
1823 static void cp_parser_objc_maybe_attributes 
1824 (cp_parser *, tree *);
1825 static tree cp_parser_objc_identifier_list
1826 (cp_parser *);
1827 /* APPLE LOCAL end radar 3803157 - objc attribute */
1828 static tree cp_parser_objc_protocol_refs_opt
1829   (cp_parser *);
1830 static void cp_parser_objc_declaration
1831   (cp_parser *);
1832 static tree cp_parser_objc_statement
1833   (cp_parser *);
1834
1835 /* Utility Routines */
1836
1837 static tree cp_parser_lookup_name
1838   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1839 static tree cp_parser_lookup_name_simple
1840   (cp_parser *, tree);
1841 static tree cp_parser_maybe_treat_template_as_class
1842   (tree, bool);
1843 static bool cp_parser_check_declarator_template_parameters
1844   (cp_parser *, cp_declarator *);
1845 static bool cp_parser_check_template_parameters
1846   (cp_parser *, unsigned);
1847 static tree cp_parser_simple_cast_expression
1848   (cp_parser *);
1849 static tree cp_parser_global_scope_opt
1850   (cp_parser *, bool);
1851 static bool cp_parser_constructor_declarator_p
1852   (cp_parser *, bool);
1853 static tree cp_parser_function_definition_from_specifiers_and_declarator
1854   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1855 static tree cp_parser_function_definition_after_declarator
1856   (cp_parser *, bool);
1857 static void cp_parser_template_declaration_after_export
1858   (cp_parser *, bool);
1859 static void cp_parser_perform_template_parameter_access_checks
1860   (VEC (deferred_access_check,gc)*);
1861 static tree cp_parser_single_declaration
1862   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1863 static tree cp_parser_functional_cast
1864   (cp_parser *, tree);
1865 static tree cp_parser_save_member_function_body
1866   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1867 static tree cp_parser_enclosed_template_argument_list
1868   (cp_parser *);
1869 static void cp_parser_save_default_args
1870   (cp_parser *, tree);
1871 static void cp_parser_late_parsing_for_member
1872   (cp_parser *, tree);
1873 static void cp_parser_late_parsing_default_args
1874   (cp_parser *, tree);
1875 static tree cp_parser_sizeof_operand
1876   (cp_parser *, enum rid);
1877 static bool cp_parser_declares_only_class_p
1878   (cp_parser *);
1879 static void cp_parser_set_storage_class
1880   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1881 static void cp_parser_set_decl_spec_type
1882   (cp_decl_specifier_seq *, tree, bool);
1883 static bool cp_parser_friend_p
1884   (const cp_decl_specifier_seq *);
1885 static cp_token *cp_parser_require
1886   (cp_parser *, enum cpp_ttype, const char *);
1887 static cp_token *cp_parser_require_keyword
1888   (cp_parser *, enum rid, const char *);
1889 static bool cp_parser_token_starts_function_definition_p
1890   (cp_token *);
1891 static bool cp_parser_next_token_starts_class_definition_p
1892   (cp_parser *);
1893 static bool cp_parser_next_token_ends_template_argument_p
1894   (cp_parser *);
1895 static bool cp_parser_nth_token_starts_template_argument_list_p
1896   (cp_parser *, size_t);
1897 static enum tag_types cp_parser_token_is_class_key
1898   (cp_token *);
1899 static void cp_parser_check_class_key
1900   (enum tag_types, tree type);
1901 static void cp_parser_check_access_in_redeclaration
1902   (tree type);
1903 static bool cp_parser_optional_template_keyword
1904   (cp_parser *);
1905 static void cp_parser_pre_parsed_nested_name_specifier
1906   (cp_parser *);
1907 static void cp_parser_cache_group
1908   (cp_parser *, enum cpp_ttype, unsigned);
1909 static void cp_parser_parse_tentatively
1910   (cp_parser *);
1911 static void cp_parser_commit_to_tentative_parse
1912   (cp_parser *);
1913 static void cp_parser_abort_tentative_parse
1914   (cp_parser *);
1915 static bool cp_parser_parse_definitely
1916   (cp_parser *);
1917 static inline bool cp_parser_parsing_tentatively
1918   (cp_parser *);
1919 static bool cp_parser_uncommitted_to_tentative_parse_p
1920   (cp_parser *);
1921 static void cp_parser_error
1922   (cp_parser *, const char *);
1923 static void cp_parser_name_lookup_error
1924   (cp_parser *, tree, tree, const char *);
1925 static bool cp_parser_simulate_error
1926   (cp_parser *);
1927 static bool cp_parser_check_type_definition
1928   (cp_parser *);
1929 static void cp_parser_check_for_definition_in_return_type
1930   (cp_declarator *, tree);
1931 static void cp_parser_check_for_invalid_template_id
1932   (cp_parser *, tree);
1933 static bool cp_parser_non_integral_constant_expression
1934   (cp_parser *, const char *);
1935 static void cp_parser_diagnose_invalid_type_name
1936   (cp_parser *, tree, tree);
1937 static bool cp_parser_parse_and_diagnose_invalid_type_name
1938   (cp_parser *);
1939 static int cp_parser_skip_to_closing_parenthesis
1940   (cp_parser *, bool, bool, bool);
1941 static void cp_parser_skip_to_end_of_statement
1942   (cp_parser *);
1943 static void cp_parser_consume_semicolon_at_end_of_statement
1944   (cp_parser *);
1945 static void cp_parser_skip_to_end_of_block_or_statement
1946   (cp_parser *);
1947 static void cp_parser_skip_to_closing_brace
1948   (cp_parser *);
1949 static void cp_parser_skip_to_end_of_template_parameter_list
1950   (cp_parser *);
1951 static void cp_parser_skip_to_pragma_eol
1952   (cp_parser*, cp_token *);
1953 static bool cp_parser_error_occurred
1954   (cp_parser *);
1955 static bool cp_parser_allow_gnu_extensions_p
1956   (cp_parser *);
1957 static bool cp_parser_is_string_literal
1958   (cp_token *);
1959 static bool cp_parser_is_keyword
1960   (cp_token *, enum rid);
1961 static tree cp_parser_make_typename_type
1962   (cp_parser *, tree, tree);
1963
1964 /* Returns nonzero if we are parsing tentatively.  */
1965
1966 static inline bool
1967 cp_parser_parsing_tentatively (cp_parser* parser)
1968 {
1969   return parser->context->next != NULL;
1970 }
1971
1972 /* Returns nonzero if TOKEN is a string literal.  */
1973
1974 static bool
1975 cp_parser_is_string_literal (cp_token* token)
1976 {
1977   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1978 }
1979
1980 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1981
1982 static bool
1983 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1984 {
1985   return token->keyword == keyword;
1986 }
1987
1988 /* If not parsing tentatively, issue a diagnostic of the form
1989       FILE:LINE: MESSAGE before TOKEN
1990    where TOKEN is the next token in the input stream.  MESSAGE
1991    (specified by the caller) is usually of the form "expected
1992    OTHER-TOKEN".  */
1993
1994 static void
1995 cp_parser_error (cp_parser* parser, const char* message)
1996 {
1997   if (!cp_parser_simulate_error (parser))
1998     {
1999       cp_token *token = cp_lexer_peek_token (parser->lexer);
2000       /* This diagnostic makes more sense if it is tagged to the line
2001          of the token we just peeked at.  */
2002       cp_lexer_set_source_position_from_token (token);
2003
2004       if (token->type == CPP_PRAGMA)
2005         {
2006           error ("%<#pragma%> is not allowed here");
2007           cp_parser_skip_to_pragma_eol (parser, token);
2008           return;
2009         }
2010
2011       c_parse_error (message,
2012                      /* Because c_parser_error does not understand
2013                         CPP_KEYWORD, keywords are treated like
2014                         identifiers.  */
2015                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2016                      token->u.value);
2017     }
2018 }
2019
2020 /* Issue an error about name-lookup failing.  NAME is the
2021    IDENTIFIER_NODE DECL is the result of
2022    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2023    the thing that we hoped to find.  */
2024
2025 static void
2026 cp_parser_name_lookup_error (cp_parser* parser,
2027                              tree name,
2028                              tree decl,
2029                              const char* desired)
2030 {
2031   /* If name lookup completely failed, tell the user that NAME was not
2032      declared.  */
2033   if (decl == error_mark_node)
2034     {
2035       if (parser->scope && parser->scope != global_namespace)
2036         error ("%<%D::%D%> has not been declared",
2037                parser->scope, name);
2038       else if (parser->scope == global_namespace)
2039         error ("%<::%D%> has not been declared", name);
2040       else if (parser->object_scope
2041                && !CLASS_TYPE_P (parser->object_scope))
2042         error ("request for member %qD in non-class type %qT",
2043                name, parser->object_scope);
2044       else if (parser->object_scope)
2045         error ("%<%T::%D%> has not been declared",
2046                parser->object_scope, name);
2047       else
2048         error ("%qD has not been declared", name);
2049     }
2050   else if (parser->scope && parser->scope != global_namespace)
2051     error ("%<%D::%D%> %s", parser->scope, name, desired);
2052   else if (parser->scope == global_namespace)
2053     error ("%<::%D%> %s", name, desired);
2054   else
2055     error ("%qD %s", name, desired);
2056 }
2057
2058 /* If we are parsing tentatively, remember that an error has occurred
2059    during this tentative parse.  Returns true if the error was
2060    simulated; false if a message should be issued by the caller.  */
2061
2062 static bool
2063 cp_parser_simulate_error (cp_parser* parser)
2064 {
2065   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2066     {
2067       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2068       return true;
2069     }
2070   return false;
2071 }
2072
2073 /* Check for repeated decl-specifiers.  */
2074
2075 static void
2076 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2077 {
2078   cp_decl_spec ds;
2079
2080   for (ds = ds_first; ds != ds_last; ++ds)
2081     {
2082       unsigned count = decl_specs->specs[(int)ds];
2083       if (count < 2)
2084         continue;
2085       /* The "long" specifier is a special case because of "long long".  */
2086       if (ds == ds_long)
2087         {
2088           if (count > 2)
2089             error ("%<long long long%> is too long for GCC");
2090           else if (pedantic && !in_system_header && warn_long_long)
2091             pedwarn ("ISO C++ does not support %<long long%>");
2092         }
2093       else if (count > 1)
2094         {
2095           static const char *const decl_spec_names[] = {
2096             "signed",
2097             "unsigned",
2098             "short",
2099             "long",
2100             "const",
2101             "volatile",
2102             "restrict",
2103             "inline",
2104             "virtual",
2105             "explicit",
2106             "friend",
2107             "typedef",
2108             "__complex",
2109             "__thread"
2110           };
2111           error ("duplicate %qs", decl_spec_names[(int)ds]);
2112         }
2113     }
2114 }
2115
2116 /* This function is called when a type is defined.  If type
2117    definitions are forbidden at this point, an error message is
2118    issued.  */
2119
2120 static bool
2121 cp_parser_check_type_definition (cp_parser* parser)
2122 {
2123   /* If types are forbidden here, issue a message.  */
2124   if (parser->type_definition_forbidden_message)
2125     {
2126       /* Use `%s' to print the string in case there are any escape
2127          characters in the message.  */
2128       error ("%s", parser->type_definition_forbidden_message);
2129       return false;
2130     }
2131   return true;
2132 }
2133
2134 /* This function is called when the DECLARATOR is processed.  The TYPE
2135    was a type defined in the decl-specifiers.  If it is invalid to
2136    define a type in the decl-specifiers for DECLARATOR, an error is
2137    issued.  */
2138
2139 static void
2140 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2141                                                tree type)
2142 {
2143   /* [dcl.fct] forbids type definitions in return types.
2144      Unfortunately, it's not easy to know whether or not we are
2145      processing a return type until after the fact.  */
2146   while (declarator
2147          && (declarator->kind == cdk_pointer
2148              || declarator->kind == cdk_reference
2149              || declarator->kind == cdk_ptrmem))
2150     declarator = declarator->declarator;
2151   if (declarator
2152       && declarator->kind == cdk_function)
2153     {
2154       error ("new types may not be defined in a return type");
2155       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2156               type);
2157     }
2158 }
2159
2160 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2161    "<" in any valid C++ program.  If the next token is indeed "<",
2162    issue a message warning the user about what appears to be an
2163    invalid attempt to form a template-id.  */
2164
2165 static void
2166 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2167                                          tree type)
2168 {
2169   cp_token_position start = 0;
2170
2171   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2172     {
2173       if (TYPE_P (type))
2174         error ("%qT is not a template", type);
2175       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2176         error ("%qE is not a template", type);
2177       else
2178         error ("invalid template-id");
2179       /* Remember the location of the invalid "<".  */
2180       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2181         start = cp_lexer_token_position (parser->lexer, true);
2182       /* Consume the "<".  */
2183       cp_lexer_consume_token (parser->lexer);
2184       /* Parse the template arguments.  */
2185       cp_parser_enclosed_template_argument_list (parser);
2186       /* Permanently remove the invalid template arguments so that
2187          this error message is not issued again.  */
2188       if (start)
2189         cp_lexer_purge_tokens_after (parser->lexer, start);
2190     }
2191 }
2192
2193 /* If parsing an integral constant-expression, issue an error message
2194    about the fact that THING appeared and return true.  Otherwise,
2195    return false.  In either case, set
2196    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2197
2198 static bool
2199 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2200                                             const char *thing)
2201 {
2202   parser->non_integral_constant_expression_p = true;
2203   if (parser->integral_constant_expression_p)
2204     {
2205       if (!parser->allow_non_integral_constant_expression_p)
2206         {
2207           error ("%s cannot appear in a constant-expression", thing);
2208           return true;
2209         }
2210     }
2211   return false;
2212 }
2213
2214 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2215    qualifying scope (or NULL, if none) for ID.  This function commits
2216    to the current active tentative parse, if any.  (Otherwise, the
2217    problematic construct might be encountered again later, resulting
2218    in duplicate error messages.)  */
2219
2220 static void
2221 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2222 {
2223   tree decl, old_scope;
2224   /* Try to lookup the identifier.  */
2225   old_scope = parser->scope;
2226   parser->scope = scope;
2227   decl = cp_parser_lookup_name_simple (parser, id);
2228   parser->scope = old_scope;
2229   /* If the lookup found a template-name, it means that the user forgot
2230   to specify an argument list. Emit a useful error message.  */
2231   if (TREE_CODE (decl) == TEMPLATE_DECL)
2232     error ("invalid use of template-name %qE without an argument list", decl);
2233   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2234     error ("invalid use of destructor %qD as a type", id);
2235   else if (TREE_CODE (decl) == TYPE_DECL)
2236     /* Something like 'unsigned A a;'  */
2237     error ("invalid combination of multiple type-specifiers");
2238   else if (!parser->scope)
2239     {
2240       /* Issue an error message.  */
2241       error ("%qE does not name a type", id);
2242       /* If we're in a template class, it's possible that the user was
2243          referring to a type from a base class.  For example:
2244
2245            template <typename T> struct A { typedef T X; };
2246            template <typename T> struct B : public A<T> { X x; };
2247
2248          The user should have said "typename A<T>::X".  */
2249       if (processing_template_decl && current_class_type
2250           && TYPE_BINFO (current_class_type))
2251         {
2252           tree b;
2253
2254           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2255                b;
2256                b = TREE_CHAIN (b))
2257             {
2258               tree base_type = BINFO_TYPE (b);
2259               if (CLASS_TYPE_P (base_type)
2260                   && dependent_type_p (base_type))
2261                 {
2262                   tree field;
2263                   /* Go from a particular instantiation of the
2264                      template (which will have an empty TYPE_FIELDs),
2265                      to the main version.  */
2266                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2267                   for (field = TYPE_FIELDS (base_type);
2268                        field;
2269                        field = TREE_CHAIN (field))
2270                     if (TREE_CODE (field) == TYPE_DECL
2271                         && DECL_NAME (field) == id)
2272                       {
2273                         inform ("(perhaps %<typename %T::%E%> was intended)",
2274                                 BINFO_TYPE (b), id);
2275                         break;
2276                       }
2277                   if (field)
2278                     break;
2279                 }
2280             }
2281         }
2282     }
2283   /* Here we diagnose qualified-ids where the scope is actually correct,
2284      but the identifier does not resolve to a valid type name.  */
2285   else if (parser->scope != error_mark_node)
2286     {
2287       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2288         error ("%qE in namespace %qE does not name a type",
2289                id, parser->scope);
2290       else if (TYPE_P (parser->scope))
2291         error ("%qE in class %qT does not name a type", id, parser->scope);
2292       else
2293         gcc_unreachable ();
2294     }
2295   cp_parser_commit_to_tentative_parse (parser);
2296 }
2297
2298 /* Check for a common situation where a type-name should be present,
2299    but is not, and issue a sensible error message.  Returns true if an
2300    invalid type-name was detected.
2301
2302    The situation handled by this function are variable declarations of the
2303    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2304    Usually, `ID' should name a type, but if we got here it means that it
2305    does not. We try to emit the best possible error message depending on
2306    how exactly the id-expression looks like.  */
2307
2308 static bool
2309 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2310 {
2311   tree id;
2312
2313   cp_parser_parse_tentatively (parser);
2314   id = cp_parser_id_expression (parser,
2315                                 /*template_keyword_p=*/false,
2316                                 /*check_dependency_p=*/true,
2317                                 /*template_p=*/NULL,
2318                                 /*declarator_p=*/true,
2319                                 /*optional_p=*/false);
2320   /* After the id-expression, there should be a plain identifier,
2321      otherwise this is not a simple variable declaration. Also, if
2322      the scope is dependent, we cannot do much.  */
2323   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2324       || (parser->scope && TYPE_P (parser->scope)
2325           && dependent_type_p (parser->scope))
2326       || TREE_CODE (id) == TYPE_DECL)
2327     {
2328       cp_parser_abort_tentative_parse (parser);
2329       return false;
2330     }
2331   if (!cp_parser_parse_definitely (parser))
2332     return false;
2333
2334   /* Emit a diagnostic for the invalid type.  */
2335   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2336   /* Skip to the end of the declaration; there's no point in
2337      trying to process it.  */
2338   cp_parser_skip_to_end_of_block_or_statement (parser);
2339   return true;
2340 }
2341
2342 /* Consume tokens up to, and including, the next non-nested closing `)'.
2343    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2344    are doing error recovery. Returns -1 if OR_COMMA is true and we
2345    found an unnested comma.  */
2346
2347 static int
2348 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2349                                        bool recovering,
2350                                        bool or_comma,
2351                                        bool consume_paren)
2352 {
2353   unsigned paren_depth = 0;
2354   unsigned brace_depth = 0;
2355
2356   if (recovering && !or_comma
2357       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2358     return 0;
2359
2360   while (true)
2361     {
2362       cp_token * token = cp_lexer_peek_token (parser->lexer);
2363
2364       switch (token->type)
2365         {
2366         case CPP_EOF:
2367         case CPP_PRAGMA_EOL:
2368           /* If we've run out of tokens, then there is no closing `)'.  */
2369           return 0;
2370
2371         case CPP_SEMICOLON:
2372           /* This matches the processing in skip_to_end_of_statement.  */
2373           if (!brace_depth)
2374             return 0;
2375           break;
2376
2377         case CPP_OPEN_BRACE:
2378           ++brace_depth;
2379           break;
2380         case CPP_CLOSE_BRACE:
2381           if (!brace_depth--)
2382             return 0;
2383           break;
2384
2385         case CPP_COMMA:
2386           if (recovering && or_comma && !brace_depth && !paren_depth)
2387             return -1;
2388           break;
2389
2390         case CPP_OPEN_PAREN:
2391           if (!brace_depth)
2392             ++paren_depth;
2393           break;
2394
2395         case CPP_CLOSE_PAREN:
2396           if (!brace_depth && !paren_depth--)
2397             {
2398               if (consume_paren)
2399                 cp_lexer_consume_token (parser->lexer);
2400               return 1;
2401             }
2402           break;
2403
2404         default:
2405           break;
2406         }
2407
2408       /* Consume the token.  */
2409       cp_lexer_consume_token (parser->lexer);
2410     }
2411 }
2412
2413 /* Consume tokens until we reach the end of the current statement.
2414    Normally, that will be just before consuming a `;'.  However, if a
2415    non-nested `}' comes first, then we stop before consuming that.  */
2416
2417 static void
2418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2419 {
2420   unsigned nesting_depth = 0;
2421
2422   while (true)
2423     {
2424       cp_token *token = cp_lexer_peek_token (parser->lexer);
2425
2426       switch (token->type)
2427         {
2428         case CPP_EOF:
2429         case CPP_PRAGMA_EOL:
2430           /* If we've run out of tokens, stop.  */
2431           return;
2432
2433         case CPP_SEMICOLON:
2434           /* If the next token is a `;', we have reached the end of the
2435              statement.  */
2436           if (!nesting_depth)
2437             return;
2438           break;
2439
2440         case CPP_CLOSE_BRACE:
2441           /* If this is a non-nested '}', stop before consuming it.
2442              That way, when confronted with something like:
2443
2444                { 3 + }
2445
2446              we stop before consuming the closing '}', even though we
2447              have not yet reached a `;'.  */
2448           if (nesting_depth == 0)
2449             return;
2450
2451           /* If it is the closing '}' for a block that we have
2452              scanned, stop -- but only after consuming the token.
2453              That way given:
2454
2455                 void f g () { ... }
2456                 typedef int I;
2457
2458              we will stop after the body of the erroneously declared
2459              function, but before consuming the following `typedef'
2460              declaration.  */
2461           if (--nesting_depth == 0)
2462             {
2463               cp_lexer_consume_token (parser->lexer);
2464               return;
2465             }
2466
2467         case CPP_OPEN_BRACE:
2468           ++nesting_depth;
2469           break;
2470
2471         default:
2472           break;
2473         }
2474
2475       /* Consume the token.  */
2476       cp_lexer_consume_token (parser->lexer);
2477     }
2478 }
2479
2480 /* APPLE LOCAL begin radar 5277239 */
2481 /* This routine checks that type_decl is a class or class object followed by a '.'
2482  which is an alternative syntax to class-method messaging [class-name class-method]
2483  */
2484
2485 static bool
2486 cp_objc_property_reference_prefix (cp_parser *parser, tree type)
2487 {
2488   return c_dialect_objc () && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
2489   && (objc_is_id (type) || objc_is_class_name (type));
2490 }
2491 /* APPLE LOCAL end radar 5277239 */
2492 /* APPLE LOCAL begin C* property (Radar 4436866, 4591909) */
2493 /* This routine parses the propery declarations. */
2494
2495 static void
2496 objc_cp_parse_property_decl (cp_parser *parser)
2497 {
2498   int declares_class_or_enum;
2499   cp_decl_specifier_seq declspecs;
2500   
2501   cp_parser_decl_specifier_seq (parser,
2502                                                   CP_PARSER_FLAGS_NONE,
2503                                                   &declspecs,
2504                                                   &declares_class_or_enum);
2505   /* Keep going until we hit the `;' at the end of the declaration. */
2506   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
2507     {
2508       tree property;
2509       cp_token *token;
2510       cp_declarator *declarator
2511       = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
2512                                                 NULL, NULL, false);
2513       property = grokdeclarator (declarator, &declspecs, NORMAL,0, NULL);
2514       /* Revover from any kind of error in property declaration. */
2515       if (property == error_mark_node || property == NULL_TREE)
2516         return;
2517       /* Add to property list. */
2518       objc_add_property_variable (copy_node (property));
2519       if (token->type == CPP_COMMA)
2520       {
2521         cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
2522         continue;
2523       }
2524       else if (token->type == CPP_EOF)
2525         return;
2526     }
2527     cp_lexer_consume_token (parser->lexer);  /* Eat ';'.  */
2528 }
2529
2530 /* This function parses a @property declaration inside an objective class
2531    or its implementation. */
2532
2533 static void 
2534 objc_cp_parser_at_property (cp_parser *parser)
2535 {
2536   cp_token *token;
2537
2538   objc_set_property_attr (0, NULL_TREE);
2539   /* Consume @property */
2540   cp_lexer_consume_token (parser->lexer);
2541   token = cp_lexer_peek_token (parser->lexer);
2542   if (token->type == CPP_OPEN_PAREN)
2543     {
2544       cp_lexer_consume_token (parser->lexer);
2545       while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF)
2546         {
2547            tree node;
2548            /* property has attribute list. */
2549            /* Consume '(' */
2550            node = cp_parser_identifier (parser);
2551            if (node == ridpointers [(int) RID_READONLY])
2552             {
2553               /* Do the readyonly thing. */
2554               objc_set_property_attr (1, NULL_TREE);
2555             }   
2556           else if (node == ridpointers [(int) RID_GETTER]
2557                    || node == ridpointers [(int) RID_SETTER])
2558             {
2559               /* Do the getter/setter attribute. */
2560               token = cp_lexer_consume_token (parser->lexer);
2561               if (token->type == CPP_EQ)
2562                 {
2563                   /* APPLE LOCAL radar 4675792 */
2564                   tree attr_ident = cp_parser_objc_selector (parser);
2565                   int num;
2566                   if (node == ridpointers [(int) RID_GETTER])
2567                     num = 2;
2568                   else
2569                     {
2570                       num = 3;
2571                       /* Consume the ':' which must always follow the setter name. */
2572                       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
2573                           cp_lexer_consume_token (parser->lexer);
2574                     }
2575                   objc_set_property_attr (num, attr_ident);
2576                 }
2577               else
2578                 {
2579                   error ("getter/setter attribute must be followed by '='");
2580                   break;
2581                 }
2582             }
2583           /* APPLE LOCAL begin radar 4947014 - objc atomic property */
2584           else if (node == ridpointers [(int) RID_NONATOMIC])
2585             {
2586               objc_set_property_attr (13, NULL_TREE);
2587             }
2588           /* APPLE LOCAL end radar 4947014 - objc atomic property */
2589           else
2590             {
2591               error ("unknown property attribute");
2592               break;
2593             }
2594           /* APPLE LOCAL begin radar 6302949 */
2595           if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
2596               && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
2597               && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
2598             warning (0, "property attributes must be separated by a comma");
2599           /* APPLE LOCAL end radar 6302949 */
2600           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
2601             cp_lexer_consume_token (parser->lexer);
2602           token = cp_lexer_peek_token (parser->lexer);    
2603         }
2604         if (token->type != CPP_CLOSE_PAREN)
2605           {
2606             error ("syntax error in @property's attribute declaration");
2607           }
2608         /* Consume ')' */
2609         cp_lexer_consume_token (parser->lexer);
2610     }
2611     objc_cp_parse_property_decl (parser);
2612 }
2613 /* APPLE LOCAL end C* property (Radar 4436866, 4591909) */
2614
2615 /* This function is called at the end of a statement or declaration.
2616    If the next token is a semicolon, it is consumed; otherwise, error
2617    recovery is attempted.  */
2618
2619 static void
2620 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2621 {
2622   /* Look for the trailing `;'.  */
2623   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2624     {
2625       /* If there is additional (erroneous) input, skip to the end of
2626          the statement.  */
2627       cp_parser_skip_to_end_of_statement (parser);
2628       /* If the next token is now a `;', consume it.  */
2629       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2630         cp_lexer_consume_token (parser->lexer);
2631     }
2632 }
2633
2634 /* Skip tokens until we have consumed an entire block, or until we
2635    have consumed a non-nested `;'.  */
2636
2637 static void
2638 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2639 {
2640   int nesting_depth = 0;
2641
2642   while (nesting_depth >= 0)
2643     {
2644       cp_token *token = cp_lexer_peek_token (parser->lexer);
2645
2646       switch (token->type)
2647         {
2648         case CPP_EOF:
2649         case CPP_PRAGMA_EOL:
2650           /* If we've run out of tokens, stop.  */
2651           return;
2652
2653         case CPP_SEMICOLON:
2654           /* Stop if this is an unnested ';'. */
2655           if (!nesting_depth)
2656             nesting_depth = -1;
2657           break;
2658
2659         case CPP_CLOSE_BRACE:
2660           /* Stop if this is an unnested '}', or closes the outermost
2661              nesting level.  */
2662           nesting_depth--;
2663           if (!nesting_depth)
2664             nesting_depth = -1;
2665           break;
2666
2667         case CPP_OPEN_BRACE:
2668           /* Nest. */
2669           nesting_depth++;
2670           break;
2671
2672         default:
2673           break;
2674         }
2675
2676       /* Consume the token.  */
2677       cp_lexer_consume_token (parser->lexer);
2678     }
2679 }
2680
2681 /* Skip tokens until a non-nested closing curly brace is the next
2682    token.  */
2683
2684 static void
2685 cp_parser_skip_to_closing_brace (cp_parser *parser)
2686 {
2687   unsigned nesting_depth = 0;
2688
2689   while (true)
2690     {
2691       cp_token *token = cp_lexer_peek_token (parser->lexer);
2692
2693       switch (token->type)
2694         {
2695         case CPP_EOF:
2696         case CPP_PRAGMA_EOL:
2697           /* If we've run out of tokens, stop.  */
2698           return;
2699
2700         case CPP_CLOSE_BRACE:
2701           /* If the next token is a non-nested `}', then we have reached
2702              the end of the current block.  */
2703           if (nesting_depth-- == 0)
2704             return;
2705           break;
2706
2707         case CPP_OPEN_BRACE:
2708           /* If it the next token is a `{', then we are entering a new
2709              block.  Consume the entire block.  */
2710           ++nesting_depth;
2711           break;
2712
2713         default:
2714           break;
2715         }
2716
2717       /* Consume the token.  */
2718       cp_lexer_consume_token (parser->lexer);
2719     }
2720 }
2721
2722 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2723    parameter is the PRAGMA token, allowing us to purge the entire pragma
2724    sequence.  */
2725
2726 static void
2727 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2728 {
2729   cp_token *token;
2730
2731   parser->lexer->in_pragma = false;
2732
2733   do
2734     token = cp_lexer_consume_token (parser->lexer);
2735   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2736
2737   /* Ensure that the pragma is not parsed again.  */
2738   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2739 }
2740
2741 /* Require pragma end of line, resyncing with it as necessary.  The
2742    arguments are as for cp_parser_skip_to_pragma_eol.  */
2743
2744 static void
2745 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2746 {
2747   parser->lexer->in_pragma = false;
2748   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2749     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2750 }
2751
2752 /* This is a simple wrapper around make_typename_type. When the id is
2753    an unresolved identifier node, we can provide a superior diagnostic
2754    using cp_parser_diagnose_invalid_type_name.  */
2755
2756 static tree
2757 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2758 {
2759   tree result;
2760   if (TREE_CODE (id) == IDENTIFIER_NODE)
2761     {
2762       result = make_typename_type (scope, id, typename_type,
2763                                    /*complain=*/tf_none);
2764       if (result == error_mark_node)
2765         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2766       return result;
2767     }
2768   return make_typename_type (scope, id, typename_type, tf_error);
2769 }
2770
2771
2772 /* Create a new C++ parser.  */
2773
2774 static cp_parser *
2775 cp_parser_new (void)
2776 {
2777   cp_parser *parser;
2778   cp_lexer *lexer;
2779   unsigned i;
2780
2781   /* cp_lexer_new_main is called before calling ggc_alloc because
2782      cp_lexer_new_main might load a PCH file.  */
2783   lexer = cp_lexer_new_main ();
2784
2785   /* Initialize the binops_by_token so that we can get the tree
2786      directly from the token.  */
2787   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2788     binops_by_token[binops[i].token_type] = binops[i];
2789
2790   parser = GGC_CNEW (cp_parser);
2791   parser->lexer = lexer;
2792   parser->context = cp_parser_context_new (NULL);
2793
2794   /* For now, we always accept GNU extensions.  */
2795   parser->allow_gnu_extensions_p = 1;
2796
2797   /* The `>' token is a greater-than operator, not the end of a
2798      template-id.  */
2799   parser->greater_than_is_operator_p = true;
2800
2801   parser->default_arg_ok_p = true;
2802
2803   /* We are not parsing a constant-expression.  */
2804   parser->integral_constant_expression_p = false;
2805   parser->allow_non_integral_constant_expression_p = false;
2806   parser->non_integral_constant_expression_p = false;
2807
2808   /* Local variable names are not forbidden.  */
2809   parser->local_variables_forbidden_p = false;
2810
2811   /* We are not processing an `extern "C"' declaration.  */
2812   parser->in_unbraced_linkage_specification_p = false;
2813
2814   /* We are not processing a declarator.  */
2815   parser->in_declarator_p = false;
2816
2817   /* We are not processing a template-argument-list.  */
2818   parser->in_template_argument_list_p = false;
2819
2820   /* We are not in an iteration statement.  */
2821   parser->in_statement = 0;
2822
2823   /* We are not in a switch statement.  */
2824   parser->in_switch_statement_p = false;
2825
2826   /* We are not parsing a type-id inside an expression.  */
2827   parser->in_type_id_in_expr_p = false;
2828
2829   /* Declarations aren't implicitly extern "C".  */
2830   parser->implicit_extern_c = false;
2831
2832   /* String literals should be translated to the execution character set.  */
2833   parser->translate_strings_p = true;
2834
2835   /* We are not parsing a function body.  */
2836   parser->in_function_body = false;
2837
2838   /* The unparsed function queue is empty.  */
2839   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2840
2841   /* There are no classes being defined.  */
2842   parser->num_classes_being_defined = 0;
2843
2844   /* No template parameters apply.  */
2845   parser->num_template_parameter_lists = 0;
2846
2847   return parser;
2848 }
2849
2850 /* Create a cp_lexer structure which will emit the tokens in CACHE
2851    and push it onto the parser's lexer stack.  This is used for delayed
2852    parsing of in-class method bodies and default arguments, and should
2853    not be confused with tentative parsing.  */
2854 static void
2855 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2856 {
2857   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2858   lexer->next = parser->lexer;
2859   parser->lexer = lexer;
2860
2861   /* Move the current source position to that of the first token in the
2862      new lexer.  */
2863   cp_lexer_set_source_position_from_token (lexer->next_token);
2864 }
2865
2866 /* Pop the top lexer off the parser stack.  This is never used for the
2867    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2868 static void
2869 cp_parser_pop_lexer (cp_parser *parser)
2870 {
2871   cp_lexer *lexer = parser->lexer;
2872   parser->lexer = lexer->next;
2873   cp_lexer_destroy (lexer);
2874
2875   /* Put the current source position back where it was before this
2876      lexer was pushed.  */
2877   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2878 }
2879
2880 /* Lexical conventions [gram.lex]  */
2881
2882 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2883    identifier.  */
2884
2885 static tree
2886 cp_parser_identifier (cp_parser* parser)
2887 {
2888   cp_token *token;
2889
2890   /* Look for the identifier.  */
2891   token = cp_parser_require (parser, CPP_NAME, "identifier");
2892   /* Return the value.  */
2893   return token ? token->u.value : error_mark_node;
2894 }
2895
2896 /* Parse a sequence of adjacent string constants.  Returns a
2897    TREE_STRING representing the combined, nul-terminated string
2898    constant.  If TRANSLATE is true, translate the string to the
2899    execution character set.  If WIDE_OK is true, a wide string is
2900    invalid here.
2901
2902    C++98 [lex.string] says that if a narrow string literal token is
2903    adjacent to a wide string literal token, the behavior is undefined.
2904    However, C99 6.4.5p4 says that this results in a wide string literal.
2905    We follow C99 here, for consistency with the C front end.
2906
2907    This code is largely lifted from lex_string() in c-lex.c.
2908
2909    FUTURE: ObjC++ will need to handle @-strings here.  */
2910 static tree
2911 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2912 {
2913   tree value;
2914   bool wide = false;
2915   size_t count;
2916   struct obstack str_ob;
2917   cpp_string str, istr, *strs;
2918   cp_token *tok;
2919
2920   tok = cp_lexer_peek_token (parser->lexer);
2921   if (!cp_parser_is_string_literal (tok))
2922     {
2923       cp_parser_error (parser, "expected string-literal");
2924       return error_mark_node;
2925     }
2926
2927   /* Try to avoid the overhead of creating and destroying an obstack
2928      for the common case of just one string.  */
2929   if (!cp_parser_is_string_literal
2930       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2931     {
2932       cp_lexer_consume_token (parser->lexer);
2933
2934       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2935       str.len = TREE_STRING_LENGTH (tok->u.value);
2936       count = 1;
2937       if (tok->type == CPP_WSTRING)
2938         wide = true;
2939
2940       strs = &str;
2941     }
2942   else
2943     {
2944       gcc_obstack_init (&str_ob);
2945       count = 0;
2946
2947       do
2948         {
2949           cp_lexer_consume_token (parser->lexer);
2950           count++;
2951           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2952           str.len = TREE_STRING_LENGTH (tok->u.value);
2953           if (tok->type == CPP_WSTRING)
2954             wide = true;
2955
2956           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2957
2958           tok = cp_lexer_peek_token (parser->lexer);
2959         }
2960       while (cp_parser_is_string_literal (tok));
2961
2962       strs = (cpp_string *) obstack_finish (&str_ob);
2963     }
2964
2965   if (wide && !wide_ok)
2966     {
2967       cp_parser_error (parser, "a wide string is invalid in this context");
2968       wide = false;
2969     }
2970
2971   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2972       (parse_in, strs, count, &istr, wide))
2973     {
2974       value = build_string (istr.len, (char *)istr.text);
2975       free ((void *)istr.text);
2976
2977       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2978       value = fix_string_type (value);
2979     }
2980   else
2981     /* cpp_interpret_string has issued an error.  */
2982     value = error_mark_node;
2983
2984   if (count > 1)
2985     obstack_free (&str_ob, 0);
2986
2987   return value;
2988 }
2989
2990
2991 /* Basic concepts [gram.basic]  */
2992
2993 /* Parse a translation-unit.
2994
2995    translation-unit:
2996      declaration-seq [opt]
2997
2998    Returns TRUE if all went well.  */
2999
3000 static bool
3001 cp_parser_translation_unit (cp_parser* parser)
3002 {
3003   /* The address of the first non-permanent object on the declarator
3004      obstack.  */
3005   static void *declarator_obstack_base;
3006
3007   bool success;
3008
3009   /* Create the declarator obstack, if necessary.  */
3010   if (!cp_error_declarator)
3011     {
3012       gcc_obstack_init (&declarator_obstack);
3013       /* Create the error declarator.  */
3014       cp_error_declarator = make_declarator (cdk_error);
3015       /* Create the empty parameter list.  */
3016       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3017       /* Remember where the base of the declarator obstack lies.  */
3018       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3019     }
3020
3021   cp_parser_declaration_seq_opt (parser);
3022
3023   /* If there are no tokens left then all went well.  */
3024   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3025     {
3026       /* Get rid of the token array; we don't need it any more.  */
3027       cp_lexer_destroy (parser->lexer);
3028       parser->lexer = NULL;
3029
3030       /* This file might have been a context that's implicitly extern
3031          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3032       if (parser->implicit_extern_c)
3033         {
3034           pop_lang_context ();
3035           parser->implicit_extern_c = false;
3036         }
3037
3038       /* Finish up.  */
3039       finish_translation_unit ();
3040
3041       success = true;
3042     }
3043   else
3044     {
3045       cp_parser_error (parser, "expected declaration");
3046       success = false;
3047     }
3048
3049   /* Make sure the declarator obstack was fully cleaned up.  */
3050   gcc_assert (obstack_next_free (&declarator_obstack)
3051               == declarator_obstack_base);
3052
3053   /* All went well.  */
3054   return success;
3055 }
3056
3057 /* Expressions [gram.expr] */
3058
3059 /* Parse a primary-expression.
3060
3061    primary-expression:
3062      literal
3063      this
3064      ( expression )
3065      id-expression
3066
3067    GNU Extensions:
3068
3069    primary-expression:
3070      ( compound-statement )
3071      __builtin_va_arg ( assignment-expression , type-id )
3072      __builtin_offsetof ( type-id , offsetof-expression )
3073    APPLE LOCAL blocks 6040305 (cf)
3074    block-literal-expr
3075
3076    Objective-C++ Extension:
3077
3078    primary-expression:
3079      objc-expression
3080
3081    literal:
3082      __null
3083
3084    ADDRESS_P is true iff this expression was immediately preceded by
3085    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3086    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3087    true iff this expression is a template argument.
3088
3089    Returns a representation of the expression.  Upon return, *IDK
3090    indicates what kind of id-expression (if any) was present.  */
3091
3092 static tree
3093 cp_parser_primary_expression (cp_parser *parser,
3094                               bool address_p,
3095                               bool cast_p,
3096                               bool template_arg_p,
3097                               cp_id_kind *idk)
3098 {
3099   cp_token *token;
3100
3101   /* Assume the primary expression is not an id-expression.  */
3102   *idk = CP_ID_KIND_NONE;
3103
3104   /* Peek at the next token.  */
3105   token = cp_lexer_peek_token (parser->lexer);
3106   switch (token->type)
3107     {
3108       /* APPLE LOCAL begin blocks 6040305 (cf) */
3109     case CPP_XOR:
3110       if (flag_blocks)
3111         {
3112           tree expr = cp_parser_block_literal_expr (parser);
3113           return expr;
3114         }
3115       cp_parser_error (parser, "expected primary-expression");
3116       return error_mark_node;
3117       /* APPLE LOCAL end blocks 6040305 (cf) */
3118       /* literal:
3119            integer-literal
3120            character-literal
3121            floating-literal
3122            string-literal
3123            boolean-literal  */
3124     case CPP_CHAR:
3125     case CPP_WCHAR:
3126     case CPP_NUMBER:
3127       token = cp_lexer_consume_token (parser->lexer);
3128       /* Floating-point literals are only allowed in an integral
3129          constant expression if they are cast to an integral or
3130          enumeration type.  */
3131       if (TREE_CODE (token->u.value) == REAL_CST
3132           && parser->integral_constant_expression_p
3133           && pedantic)
3134         {
3135           /* CAST_P will be set even in invalid code like "int(2.7 +
3136              ...)".   Therefore, we have to check that the next token
3137              is sure to end the cast.  */
3138           if (cast_p)
3139             {
3140               cp_token *next_token;
3141
3142               next_token = cp_lexer_peek_token (parser->lexer);
3143               if (/* The comma at the end of an
3144                      enumerator-definition.  */
3145                   next_token->type != CPP_COMMA
3146                   /* The curly brace at the end of an enum-specifier.  */
3147                   && next_token->type != CPP_CLOSE_BRACE
3148                   /* The end of a statement.  */
3149                   && next_token->type != CPP_SEMICOLON
3150                   /* The end of the cast-expression.  */
3151                   && next_token->type != CPP_CLOSE_PAREN
3152                   /* The end of an array bound.  */
3153                   && next_token->type != CPP_CLOSE_SQUARE
3154                   /* The closing ">" in a template-argument-list.  */
3155                   && (next_token->type != CPP_GREATER
3156                       || parser->greater_than_is_operator_p))
3157                 cast_p = false;
3158             }
3159
3160           /* If we are within a cast, then the constraint that the
3161              cast is to an integral or enumeration type will be
3162              checked at that point.  If we are not within a cast, then
3163              this code is invalid.  */
3164           if (!cast_p)
3165             cp_parser_non_integral_constant_expression
3166               (parser, "floating-point literal");
3167         }
3168       return token->u.value;
3169
3170     case CPP_STRING:
3171     case CPP_WSTRING:
3172       /* ??? Should wide strings be allowed when parser->translate_strings_p
3173          is false (i.e. in attributes)?  If not, we can kill the third
3174          argument to cp_parser_string_literal.  */
3175       return cp_parser_string_literal (parser,
3176                                        parser->translate_strings_p,
3177                                        true);
3178
3179     case CPP_OPEN_PAREN:
3180       {
3181         tree expr;
3182         bool saved_greater_than_is_operator_p;
3183
3184         /* Consume the `('.  */
3185         cp_lexer_consume_token (parser->lexer);
3186         /* Within a parenthesized expression, a `>' token is always
3187            the greater-than operator.  */
3188         saved_greater_than_is_operator_p
3189           = parser->greater_than_is_operator_p;
3190         parser->greater_than_is_operator_p = true;
3191         /* If we see `( { ' then we are looking at the beginning of
3192            a GNU statement-expression.  */
3193         if (cp_parser_allow_gnu_extensions_p (parser)
3194             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3195           {
3196             /* Statement-expressions are not allowed by the standard.  */
3197             if (pedantic)
3198               pedwarn ("ISO C++ forbids braced-groups within expressions");
3199
3200             /* And they're not allowed outside of a function-body; you
3201                cannot, for example, write:
3202
3203                  int i = ({ int j = 3; j + 1; });
3204
3205                at class or namespace scope.  */
3206             if (!parser->in_function_body)
3207               error ("statement-expressions are allowed only inside functions");
3208             /* Start the statement-expression.  */
3209             expr = begin_stmt_expr ();
3210             /* Parse the compound-statement.  */
3211             /* APPLE LOCAL radar 5982990 */
3212             cp_parser_compound_statement (parser, expr, false, false);
3213             /* Finish up.  */
3214             expr = finish_stmt_expr (expr, false);
3215           }
3216         else
3217           {
3218             /* Parse the parenthesized expression.  */
3219             expr = cp_parser_expression (parser, cast_p);
3220             /* Let the front end know that this expression was
3221                enclosed in parentheses. This matters in case, for
3222                example, the expression is of the form `A::B', since
3223                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3224                not.  */
3225             finish_parenthesized_expr (expr);
3226           }
3227         /* The `>' token might be the end of a template-id or
3228            template-parameter-list now.  */
3229         parser->greater_than_is_operator_p
3230           = saved_greater_than_is_operator_p;
3231         /* Consume the `)'.  */
3232         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3233           cp_parser_skip_to_end_of_statement (parser);
3234
3235         return expr;
3236       }
3237
3238     case CPP_KEYWORD:
3239       switch (token->keyword)
3240         {
3241           /* These two are the boolean literals.  */
3242         case RID_TRUE:
3243           cp_lexer_consume_token (parser->lexer);
3244           return boolean_true_node;
3245         case RID_FALSE:
3246           cp_lexer_consume_token (parser->lexer);
3247           return boolean_false_node;
3248
3249           /* The `__null' literal.  */
3250         case RID_NULL:
3251           cp_lexer_consume_token (parser->lexer);
3252           return null_node;
3253
3254           /* Recognize the `this' keyword.  */
3255         case RID_THIS:
3256           cp_lexer_consume_token (parser->lexer);
3257           if (parser->local_variables_forbidden_p)
3258             {
3259               error ("%<this%> may not be used in this context");
3260               return error_mark_node;
3261             }
3262           /* Pointers cannot appear in constant-expressions.  */
3263           if (cp_parser_non_integral_constant_expression (parser,
3264                                                           "`this'"))
3265             return error_mark_node;
3266           return finish_this_expr ();
3267
3268           /* The `operator' keyword can be the beginning of an
3269              id-expression.  */
3270         case RID_OPERATOR:
3271           goto id_expression;
3272
3273         case RID_FUNCTION_NAME:
3274         case RID_PRETTY_FUNCTION_NAME:
3275         case RID_C99_FUNCTION_NAME:
3276           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3277              __func__ are the names of variables -- but they are
3278              treated specially.  Therefore, they are handled here,
3279              rather than relying on the generic id-expression logic
3280              below.  Grammatically, these names are id-expressions.
3281
3282              Consume the token.  */
3283           token = cp_lexer_consume_token (parser->lexer);
3284           /* Look up the name.  */
3285           return finish_fname (token->u.value);
3286
3287         case RID_VA_ARG:
3288           {
3289             tree expression;
3290             tree type;
3291
3292             /* The `__builtin_va_arg' construct is used to handle
3293                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3294             cp_lexer_consume_token (parser->lexer);
3295             /* Look for the opening `('.  */
3296             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3297             /* Now, parse the assignment-expression.  */
3298             expression = cp_parser_assignment_expression (parser,
3299                                                           /*cast_p=*/false);
3300             /* Look for the `,'.  */
3301             cp_parser_require (parser, CPP_COMMA, "`,'");
3302             /* Parse the type-id.  */
3303             type = cp_parser_type_id (parser);
3304             /* Look for the closing `)'.  */
3305             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3306             /* Using `va_arg' in a constant-expression is not
3307                allowed.  */
3308             if (cp_parser_non_integral_constant_expression (parser,
3309                                                             "`va_arg'"))
3310               return error_mark_node;
3311             return build_x_va_arg (expression, type);
3312           }
3313
3314         case RID_OFFSETOF:
3315           return cp_parser_builtin_offsetof (parser);
3316
3317           /* Objective-C++ expressions.  */
3318         case RID_AT_ENCODE:
3319         case RID_AT_PROTOCOL:
3320         case RID_AT_SELECTOR:
3321           return cp_parser_objc_expression (parser);
3322
3323         default:
3324           cp_parser_error (parser, "expected primary-expression");
3325           return error_mark_node;
3326         }
3327
3328       /* An id-expression can start with either an identifier, a
3329          `::' as the beginning of a qualified-id, or the "operator"
3330          keyword.  */
3331     case CPP_NAME:
3332     case CPP_SCOPE:
3333     case CPP_TEMPLATE_ID:
3334     case CPP_NESTED_NAME_SPECIFIER:
3335       {
3336         tree id_expression;
3337         tree decl;
3338         const char *error_msg;
3339         bool template_p;
3340         bool done;
3341
3342       id_expression:
3343         /* Parse the id-expression.  */
3344         id_expression
3345           = cp_parser_id_expression (parser,
3346                                      /*template_keyword_p=*/false,
3347                                      /*check_dependency_p=*/true,
3348                                      &template_p,
3349                                      /*declarator_p=*/false,
3350                                      /*optional_p=*/false);
3351         if (id_expression == error_mark_node)
3352           return error_mark_node;
3353         token = cp_lexer_peek_token (parser->lexer);
3354         done = (token->type != CPP_OPEN_SQUARE
3355                 && token->type != CPP_OPEN_PAREN
3356                 && token->type != CPP_DOT
3357                 && token->type != CPP_DEREF
3358                 && token->type != CPP_PLUS_PLUS
3359                 && token->type != CPP_MINUS_MINUS);
3360         /* If we have a template-id, then no further lookup is
3361            required.  If the template-id was for a template-class, we
3362            will sometimes have a TYPE_DECL at this point.  */
3363         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3364                  || TREE_CODE (id_expression) == TYPE_DECL)
3365           decl = id_expression;
3366         /* Look up the name.  */
3367         else
3368           {
3369             tree ambiguous_decls;
3370
3371             decl = cp_parser_lookup_name (parser, id_expression,
3372                                           none_type,
3373                                           template_p,
3374                                           /*is_namespace=*/false,
3375                                           /*check_dependency=*/true,
3376                                           &ambiguous_decls);
3377             /* If the lookup was ambiguous, an error will already have
3378                been issued.  */
3379             if (ambiguous_decls)
3380               return error_mark_node;
3381
3382             /* APPLE LOCAL begin radar 5277239 */
3383             if (TREE_CODE (decl) == TYPE_DECL 
3384                 && cp_objc_property_reference_prefix (parser, TREE_TYPE (decl)))
3385               return cp_parser_objc_reference_expression (parser, decl);
3386             /* APPLE LOCAL end radar 5277239 */
3387             /* In Objective-C++, an instance variable (ivar) may be preferred
3388                to whatever cp_parser_lookup_name() found.  */
3389             decl = objc_lookup_ivar (decl, id_expression);
3390
3391             /* If name lookup gives us a SCOPE_REF, then the
3392                qualifying scope was dependent.  */
3393             if (TREE_CODE (decl) == SCOPE_REF)
3394               {
3395                 /* At this point, we do not know if DECL is a valid
3396                    integral constant expression.  We assume that it is
3397                    in fact such an expression, so that code like:
3398
3399                       template <int N> struct A {
3400                         int a[B<N>::i];
3401                       };
3402                      
3403                    is accepted.  At template-instantiation time, we
3404                    will check that B<N>::i is actually a constant.  */
3405                 return decl;
3406               }
3407             /* Check to see if DECL is a local variable in a context
3408                where that is forbidden.  */
3409             if (parser->local_variables_forbidden_p
3410                 && local_variable_p (decl))
3411               {
3412                 /* It might be that we only found DECL because we are
3413                    trying to be generous with pre-ISO scoping rules.
3414                    For example, consider:
3415
3416                      int i;
3417                      void g() {
3418                        for (int i = 0; i < 10; ++i) {}
3419                        extern void f(int j = i);
3420                      }
3421
3422                    Here, name look up will originally find the out
3423                    of scope `i'.  We need to issue a warning message,
3424                    but then use the global `i'.  */
3425                 decl = check_for_out_of_scope_variable (decl);
3426                 if (local_variable_p (decl))
3427                   {
3428                     error ("local variable %qD may not appear in this context",
3429                            decl);
3430                     return error_mark_node;
3431                   }
3432               }
3433           }
3434
3435         decl = (finish_id_expression
3436                 (id_expression, decl, parser->scope,
3437                  idk,
3438                  parser->integral_constant_expression_p,
3439                  parser->allow_non_integral_constant_expression_p,
3440                  &parser->non_integral_constant_expression_p,
3441                  template_p, done, address_p,
3442                  template_arg_p,
3443                  &error_msg));
3444         if (error_msg)
3445           cp_parser_error (parser, error_msg);
3446         return decl;
3447       }
3448
3449       /* Anything else is an error.  */
3450     default:
3451       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3452       if (c_dialect_objc ()
3453           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3454         return cp_parser_objc_expression (parser);
3455
3456       cp_parser_error (parser, "expected primary-expression");
3457       return error_mark_node;
3458     }
3459 }
3460
3461 /* Parse an id-expression.
3462
3463    id-expression:
3464      unqualified-id
3465      qualified-id
3466
3467    qualified-id:
3468      :: [opt] nested-name-specifier template [opt] unqualified-id
3469      :: identifier
3470      :: operator-function-id
3471      :: template-id
3472
3473    Return a representation of the unqualified portion of the
3474    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3475    a `::' or nested-name-specifier.
3476
3477    Often, if the id-expression was a qualified-id, the caller will
3478    want to make a SCOPE_REF to represent the qualified-id.  This
3479    function does not do this in order to avoid wastefully creating
3480    SCOPE_REFs when they are not required.
3481
3482    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3483    `template' keyword.
3484
3485    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3486    uninstantiated templates.
3487
3488    If *TEMPLATE_P is non-NULL, it is set to true iff the
3489    `template' keyword is used to explicitly indicate that the entity
3490    named is a template.
3491
3492    If DECLARATOR_P is true, the id-expression is appearing as part of
3493    a declarator, rather than as part of an expression.  */
3494
3495 static tree
3496 cp_parser_id_expression (cp_parser *parser,
3497                          bool template_keyword_p,
3498                          bool check_dependency_p,
3499                          bool *template_p,
3500                          bool declarator_p,
3501                          bool optional_p)
3502 {
3503   bool global_scope_p;
3504   bool nested_name_specifier_p;
3505
3506   /* Assume the `template' keyword was not used.  */
3507   if (template_p)
3508     *template_p = template_keyword_p;
3509
3510   /* Look for the optional `::' operator.  */
3511   global_scope_p
3512     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3513        != NULL_TREE);
3514   /* Look for the optional nested-name-specifier.  */
3515   nested_name_specifier_p
3516     = (cp_parser_nested_name_specifier_opt (parser,
3517                                             /*typename_keyword_p=*/false,
3518                                             check_dependency_p,
3519                                             /*type_p=*/false,
3520                                             declarator_p)
3521        != NULL_TREE);
3522   /* If there is a nested-name-specifier, then we are looking at
3523      the first qualified-id production.  */
3524   if (nested_name_specifier_p)
3525     {
3526       tree saved_scope;
3527       tree saved_object_scope;
3528       tree saved_qualifying_scope;
3529       tree unqualified_id;
3530       bool is_template;
3531
3532       /* See if the next token is the `template' keyword.  */
3533       if (!template_p)
3534         template_p = &is_template;
3535       *template_p = cp_parser_optional_template_keyword (parser);
3536       /* Name lookup we do during the processing of the
3537          unqualified-id might obliterate SCOPE.  */
3538       saved_scope = parser->scope;
3539       saved_object_scope = parser->object_scope;
3540       saved_qualifying_scope = parser->qualifying_scope;
3541       /* Process the final unqualified-id.  */
3542       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3543                                                  check_dependency_p,
3544                                                  declarator_p,
3545                                                  /*optional_p=*/false);
3546       /* Restore the SAVED_SCOPE for our caller.  */
3547       parser->scope = saved_scope;
3548       parser->object_scope = saved_object_scope;
3549       parser->qualifying_scope = saved_qualifying_scope;
3550
3551       return unqualified_id;
3552     }
3553   /* Otherwise, if we are in global scope, then we are looking at one
3554      of the other qualified-id productions.  */
3555   else if (global_scope_p)
3556     {
3557       cp_token *token;
3558       tree id;
3559
3560       /* Peek at the next token.  */
3561       token = cp_lexer_peek_token (parser->lexer);
3562
3563       /* If it's an identifier, and the next token is not a "<", then
3564          we can avoid the template-id case.  This is an optimization
3565          for this common case.  */
3566       if (token->type == CPP_NAME
3567           && !cp_parser_nth_token_starts_template_argument_list_p
3568                (parser, 2))
3569         return cp_parser_identifier (parser);
3570
3571       cp_parser_parse_tentatively (parser);
3572       /* Try a template-id.  */
3573       id = cp_parser_template_id (parser,
3574                                   /*template_keyword_p=*/false,
3575                                   /*check_dependency_p=*/true,
3576                                   declarator_p);
3577       /* If that worked, we're done.  */
3578       if (cp_parser_parse_definitely (parser))
3579         return id;
3580
3581       /* Peek at the next token.  (Changes in the token buffer may
3582          have invalidated the pointer obtained above.)  */
3583       token = cp_lexer_peek_token (parser->lexer);
3584
3585       switch (token->type)
3586         {
3587         case CPP_NAME:
3588           return cp_parser_identifier (parser);
3589
3590         case CPP_KEYWORD:
3591           if (token->keyword == RID_OPERATOR)
3592             return cp_parser_operator_function_id (parser);
3593           /* Fall through.  */
3594
3595         default:
3596           cp_parser_error (parser, "expected id-expression");
3597           return error_mark_node;
3598         }
3599     }
3600   else
3601     return cp_parser_unqualified_id (parser, template_keyword_p,
3602                                      /*check_dependency_p=*/true,
3603                                      declarator_p,
3604                                      optional_p);
3605 }
3606
3607 /* Parse an unqualified-id.
3608
3609    unqualified-id:
3610      identifier
3611      operator-function-id
3612      conversion-function-id
3613      ~ class-name
3614      template-id
3615
3616    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3617    keyword, in a construct like `A::template ...'.
3618
3619    Returns a representation of unqualified-id.  For the `identifier'
3620    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3621    production a BIT_NOT_EXPR is returned; the operand of the
3622    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3623    other productions, see the documentation accompanying the
3624    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3625    names are looked up in uninstantiated templates.  If DECLARATOR_P
3626    is true, the unqualified-id is appearing as part of a declarator,
3627    rather than as part of an expression.  */
3628
3629 static tree
3630 cp_parser_unqualified_id (cp_parser* parser,
3631                           bool template_keyword_p,
3632                           bool check_dependency_p,
3633                           bool declarator_p,
3634                           bool optional_p)
3635 {
3636   cp_token *token;
3637
3638   /* Peek at the next token.  */
3639   token = cp_lexer_peek_token (parser->lexer);
3640
3641   switch (token->type)
3642     {
3643     case CPP_NAME:
3644       {
3645         tree id;
3646
3647         /* We don't know yet whether or not this will be a
3648            template-id.  */
3649         cp_parser_parse_tentatively (parser);
3650         /* Try a template-id.  */
3651         id = cp_parser_template_id (parser, template_keyword_p,
3652                                     check_dependency_p,
3653                                     declarator_p);
3654         /* If it worked, we're done.  */
3655         if (cp_parser_parse_definitely (parser))
3656           return id;
3657         /* Otherwise, it's an ordinary identifier.  */
3658         return cp_parser_identifier (parser);
3659       }
3660
3661     case CPP_TEMPLATE_ID:
3662       return cp_parser_template_id (parser, template_keyword_p,
3663                                     check_dependency_p,
3664                                     declarator_p);
3665
3666     case CPP_COMPL:
3667       {
3668         tree type_decl;
3669         tree qualifying_scope;
3670         tree object_scope;
3671         tree scope;
3672         bool done;
3673
3674         /* Consume the `~' token.  */
3675         cp_lexer_consume_token (parser->lexer);
3676         /* Parse the class-name.  The standard, as written, seems to
3677            say that:
3678
3679              template <typename T> struct S { ~S (); };
3680              template <typename T> S<T>::~S() {}
3681
3682            is invalid, since `~' must be followed by a class-name, but
3683            `S<T>' is dependent, and so not known to be a class.
3684            That's not right; we need to look in uninstantiated
3685            templates.  A further complication arises from:
3686
3687              template <typename T> void f(T t) {
3688                t.T::~T();
3689              }
3690
3691            Here, it is not possible to look up `T' in the scope of `T'
3692            itself.  We must look in both the current scope, and the
3693            scope of the containing complete expression.
3694
3695            Yet another issue is:
3696
3697              struct S {
3698                int S;
3699                ~S();
3700              };
3701
3702              S::~S() {}
3703
3704            The standard does not seem to say that the `S' in `~S'
3705            should refer to the type `S' and not the data member
3706            `S::S'.  */
3707
3708         /* DR 244 says that we look up the name after the "~" in the
3709            same scope as we looked up the qualifying name.  That idea
3710            isn't fully worked out; it's more complicated than that.  */
3711         scope = parser->scope;
3712         object_scope = parser->object_scope;
3713         qualifying_scope = parser->qualifying_scope;
3714
3715         /* Check for invalid scopes.  */
3716         if (scope == error_mark_node)
3717           {
3718             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3719               cp_lexer_consume_token (parser->lexer);
3720             return error_mark_node;
3721           }
3722         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3723           {
3724             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3725               error ("scope %qT before %<~%> is not a class-name", scope);
3726             cp_parser_simulate_error (parser);
3727             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3728               cp_lexer_consume_token (parser->lexer);
3729             return error_mark_node;
3730           }
3731         gcc_assert (!scope || TYPE_P (scope));
3732
3733         /* If the name is of the form "X::~X" it's OK.  */
3734         token = cp_lexer_peek_token (parser->lexer);
3735         if (scope
3736             && token->type == CPP_NAME
3737             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3738                 == CPP_OPEN_PAREN)
3739             && constructor_name_p (token->u.value, scope))
3740           {
3741             cp_lexer_consume_token (parser->lexer);
3742             return build_nt (BIT_NOT_EXPR, scope);
3743           }
3744
3745         /* If there was an explicit qualification (S::~T), first look
3746            in the scope given by the qualification (i.e., S).  */
3747         done = false;
3748         type_decl = NULL_TREE;
3749         if (scope)
3750           {
3751             cp_parser_parse_tentatively (parser);
3752             type_decl = cp_parser_class_name (parser,
3753                                               /*typename_keyword_p=*/false,
3754                                               /*template_keyword_p=*/false,
3755                                               none_type,
3756                                               /*check_dependency=*/false,
3757                                               /*class_head_p=*/false,
3758                                               declarator_p);
3759             if (cp_parser_parse_definitely (parser))
3760               done = true;
3761           }
3762         /* In "N::S::~S", look in "N" as well.  */
3763         if (!done && scope && qualifying_scope)
3764           {
3765             cp_parser_parse_tentatively (parser);
3766             parser->scope = qualifying_scope;
3767             parser->object_scope = NULL_TREE;
3768             parser->qualifying_scope = NULL_TREE;
3769             type_decl
3770               = cp_parser_class_name (parser,
3771                                       /*typename_keyword_p=*/false,
3772                                       /*template_keyword_p=*/false,
3773                                       none_type,
3774                                       /*check_dependency=*/false,
3775                                       /*class_head_p=*/false,
3776                                       declarator_p);
3777             if (cp_parser_parse_definitely (parser))
3778               done = true;
3779           }
3780         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3781         else if (!done && object_scope)
3782           {
3783             cp_parser_parse_tentatively (parser);
3784             parser->scope = object_scope;
3785             parser->object_scope = NULL_TREE;
3786             parser->qualifying_scope = NULL_TREE;
3787             type_decl
3788               = cp_parser_class_name (parser,
3789                                       /*typename_keyword_p=*/false,
3790                                       /*template_keyword_p=*/false,
3791                                       none_type,
3792                                       /*check_dependency=*/false,
3793                                       /*class_head_p=*/false,
3794                                       declarator_p);
3795             if (cp_parser_parse_definitely (parser))
3796               done = true;
3797           }
3798         /* Look in the surrounding context.  */
3799         if (!done)
3800           {
3801             parser->scope = NULL_TREE;
3802             parser->object_scope = NULL_TREE;
3803             parser->qualifying_scope = NULL_TREE;
3804             type_decl
3805               = cp_parser_class_name (parser,
3806                                       /*typename_keyword_p=*/false,
3807                                       /*template_keyword_p=*/false,
3808                                       none_type,
3809                                       /*check_dependency=*/false,
3810                                       /*class_head_p=*/false,
3811                                       declarator_p);
3812           }
3813         /* If an error occurred, assume that the name of the
3814            destructor is the same as the name of the qualifying
3815            class.  That allows us to keep parsing after running
3816            into ill-formed destructor names.  */
3817         if (type_decl == error_mark_node && scope)
3818           return build_nt (BIT_NOT_EXPR, scope);
3819         else if (type_decl == error_mark_node)
3820           return error_mark_node;
3821
3822         /* Check that destructor name and scope match.  */
3823         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3824           {
3825             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3826               error ("declaration of %<~%T%> as member of %qT",
3827                      type_decl, scope);
3828             cp_parser_simulate_error (parser);
3829             return error_mark_node;
3830           }
3831
3832         /* [class.dtor]
3833
3834            A typedef-name that names a class shall not be used as the
3835            identifier in the declarator for a destructor declaration.  */
3836         if (declarator_p
3837             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3838             && !DECL_SELF_REFERENCE_P (type_decl)
3839             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3840           error ("typedef-name %qD used as destructor declarator",
3841                  type_decl);
3842
3843         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3844       }
3845
3846     case CPP_KEYWORD:
3847       if (token->keyword == RID_OPERATOR)
3848         {
3849           tree id;
3850
3851           /* This could be a template-id, so we try that first.  */
3852           cp_parser_parse_tentatively (parser);
3853           /* Try a template-id.  */
3854           id = cp_parser_template_id (parser, template_keyword_p,
3855                                       /*check_dependency_p=*/true,
3856                                       declarator_p);
3857           /* If that worked, we're done.  */
3858           if (cp_parser_parse_definitely (parser))
3859             return id;
3860           /* We still don't know whether we're looking at an
3861              operator-function-id or a conversion-function-id.  */
3862           cp_parser_parse_tentatively (parser);
3863           /* Try an operator-function-id.  */
3864           id = cp_parser_operator_function_id (parser);
3865           /* If that didn't work, try a conversion-function-id.  */
3866           if (!cp_parser_parse_definitely (parser))
3867             id = cp_parser_conversion_function_id (parser);
3868
3869           return id;
3870         }
3871       /* Fall through.  */
3872
3873     default:
3874       if (optional_p)
3875         return NULL_TREE;
3876       cp_parser_error (parser, "expected unqualified-id");
3877       return error_mark_node;
3878     }
3879 }
3880
3881 /* Parse an (optional) nested-name-specifier.
3882
3883    nested-name-specifier:
3884      class-or-namespace-name :: nested-name-specifier [opt]
3885      class-or-namespace-name :: template nested-name-specifier [opt]
3886
3887    PARSER->SCOPE should be set appropriately before this function is
3888    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3889    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3890    in name lookups.
3891
3892    Sets PARSER->SCOPE to the class (TYPE) or namespace
3893    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3894    it unchanged if there is no nested-name-specifier.  Returns the new
3895    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3896
3897    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3898    part of a declaration and/or decl-specifier.  */
3899
3900 static tree
3901 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3902                                      bool typename_keyword_p,
3903                                      bool check_dependency_p,
3904                                      bool type_p,
3905                                      bool is_declaration)
3906 {
3907   bool success = false;
3908   cp_token_position start = 0;
3909   cp_token *token;
3910
3911   /* Remember where the nested-name-specifier starts.  */
3912   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3913     {
3914       start = cp_lexer_token_position (parser->lexer, false);
3915       push_deferring_access_checks (dk_deferred);
3916     }
3917
3918   while (true)
3919     {
3920       tree new_scope;
3921       tree old_scope;
3922       tree saved_qualifying_scope;
3923       bool template_keyword_p;
3924
3925       /* Spot cases that cannot be the beginning of a
3926          nested-name-specifier.  */
3927       token = cp_lexer_peek_token (parser->lexer);
3928
3929       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3930          the already parsed nested-name-specifier.  */
3931       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3932         {
3933           /* Grab the nested-name-specifier and continue the loop.  */
3934           cp_parser_pre_parsed_nested_name_specifier (parser);
3935           /* If we originally encountered this nested-name-specifier
3936              with IS_DECLARATION set to false, we will not have
3937              resolved TYPENAME_TYPEs, so we must do so here.  */
3938           if (is_declaration
3939               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3940             {
3941               new_scope = resolve_typename_type (parser->scope,
3942                                                  /*only_current_p=*/false);
3943               if (new_scope != error_mark_node)
3944                 parser->scope = new_scope;
3945             }
3946           success = true;
3947           continue;
3948         }
3949
3950       /* Spot cases that cannot be the beginning of a
3951          nested-name-specifier.  On the second and subsequent times
3952          through the loop, we look for the `template' keyword.  */
3953       if (success && token->keyword == RID_TEMPLATE)
3954         ;
3955       /* A template-id can start a nested-name-specifier.  */
3956       else if (token->type == CPP_TEMPLATE_ID)
3957         ;
3958       else
3959         {
3960           /* If the next token is not an identifier, then it is
3961              definitely not a class-or-namespace-name.  */
3962           if (token->type != CPP_NAME)
3963             break;
3964           /* If the following token is neither a `<' (to begin a
3965              template-id), nor a `::', then we are not looking at a
3966              nested-name-specifier.  */
3967           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3968           if (token->type != CPP_SCOPE
3969               && !cp_parser_nth_token_starts_template_argument_list_p
3970                   (parser, 2))
3971             break;
3972         }
3973
3974       /* The nested-name-specifier is optional, so we parse
3975          tentatively.  */
3976       cp_parser_parse_tentatively (parser);
3977
3978       /* Look for the optional `template' keyword, if this isn't the
3979          first time through the loop.  */
3980       if (success)
3981         template_keyword_p = cp_parser_optional_template_keyword (parser);
3982       else
3983         template_keyword_p = false;
3984
3985       /* Save the old scope since the name lookup we are about to do
3986          might destroy it.  */
3987       old_scope = parser->scope;
3988       saved_qualifying_scope = parser->qualifying_scope;
3989       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3990          look up names in "X<T>::I" in order to determine that "Y" is
3991          a template.  So, if we have a typename at this point, we make
3992          an effort to look through it.  */
3993       if (is_declaration
3994           && !typename_keyword_p
3995           && parser->scope
3996           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3997         parser->scope = resolve_typename_type (parser->scope,
3998                                                /*only_current_p=*/false);
3999       /* Parse the qualifying entity.  */
4000       new_scope
4001         = cp_parser_class_or_namespace_name (parser,
4002                                              typename_keyword_p,
4003                                              template_keyword_p,
4004                                              check_dependency_p,
4005                                              type_p,
4006                                              is_declaration);
4007       /* Look for the `::' token.  */
4008       cp_parser_require (parser, CPP_SCOPE, "`::'");
4009
4010       /* If we found what we wanted, we keep going; otherwise, we're
4011          done.  */
4012       if (!cp_parser_parse_definitely (parser))
4013         {
4014           bool error_p = false;
4015
4016           /* Restore the OLD_SCOPE since it was valid before the
4017              failed attempt at finding the last
4018              class-or-namespace-name.  */
4019           parser->scope = old_scope;
4020           parser->qualifying_scope = saved_qualifying_scope;
4021           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4022             break;
4023           /* If the next token is an identifier, and the one after
4024              that is a `::', then any valid interpretation would have
4025              found a class-or-namespace-name.  */
4026           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4027                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4028                      == CPP_SCOPE)
4029                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4030                      != CPP_COMPL))
4031             {
4032               token = cp_lexer_consume_token (parser->lexer);
4033               if (!error_p)
4034                 {
4035                   if (!token->ambiguous_p)
4036                     {
4037                       tree decl;
4038                       tree ambiguous_decls;
4039
4040                       decl = cp_parser_lookup_name (parser, token->u.value,
4041                                                     none_type,
4042                                                     /*is_template=*/false,
4043                                                     /*is_namespace=*/false,
4044                                                     /*check_dependency=*/true,
4045                                                     &ambiguous_decls);
4046                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4047                         error ("%qD used without template parameters", decl);
4048                       else if (ambiguous_decls)
4049                         {
4050                           error ("reference to %qD is ambiguous",
4051                                  token->u.value);
4052                           print_candidates (ambiguous_decls);
4053                           decl = error_mark_node;
4054                         }
4055                       else
4056                         cp_parser_name_lookup_error
4057                           (parser, token->u.value, decl,
4058                            "is not a class or namespace");
4059                     }
4060                   parser->scope = error_mark_node;
4061                   error_p = true;
4062                   /* Treat this as a successful nested-name-specifier
4063                      due to:
4064
4065                      [basic.lookup.qual]
4066
4067                      If the name found is not a class-name (clause
4068                      _class_) or namespace-name (_namespace.def_), the
4069                      program is ill-formed.  */
4070                   success = true;
4071                 }
4072               cp_lexer_consume_token (parser->lexer);
4073             }
4074           break;
4075         }
4076       /* We've found one valid nested-name-specifier.  */
4077       success = true;
4078       /* Name lookup always gives us a DECL.  */
4079       if (TREE_CODE (new_scope) == TYPE_DECL)
4080         new_scope = TREE_TYPE (new_scope);
4081       /* Uses of "template" must be followed by actual templates.  */
4082       if (template_keyword_p
4083           && !(CLASS_TYPE_P (new_scope)
4084                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4085                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4086                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4087           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4088                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4089                    == TEMPLATE_ID_EXPR)))
4090         pedwarn (TYPE_P (new_scope)
4091                  ? "%qT is not a template"
4092                  : "%qD is not a template",
4093                  new_scope);
4094       /* If it is a class scope, try to complete it; we are about to
4095          be looking up names inside the class.  */
4096       if (TYPE_P (new_scope)
4097           /* Since checking types for dependency can be expensive,
4098              avoid doing it if the type is already complete.  */
4099           && !COMPLETE_TYPE_P (new_scope)
4100           /* Do not try to complete dependent types.  */
4101           && !dependent_type_p (new_scope))
4102         new_scope = complete_type (new_scope);
4103       /* Make sure we look in the right scope the next time through
4104          the loop.  */
4105       parser->scope = new_scope;
4106     }
4107
4108   /* If parsing tentatively, replace the sequence of tokens that makes
4109      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4110      token.  That way, should we re-parse the token stream, we will
4111      not have to repeat the effort required to do the parse, nor will
4112      we issue duplicate error messages.  */
4113   if (success && start)
4114     {
4115       cp_token *token;
4116
4117       token = cp_lexer_token_at (parser->lexer, start);
4118       /* Reset the contents of the START token.  */
4119       token->type = CPP_NESTED_NAME_SPECIFIER;
4120       /* Retrieve any deferred checks.  Do not pop this access checks yet
4121          so the memory will not be reclaimed during token replacing below.  */
4122       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4123       token->u.tree_check_value->value = parser->scope;
4124       token->u.tree_check_value->checks = get_deferred_access_checks ();
4125       token->u.tree_check_value->qualifying_scope =
4126         parser->qualifying_scope;
4127       token->keyword = RID_MAX;
4128
4129       /* Purge all subsequent tokens.  */
4130       cp_lexer_purge_tokens_after (parser->lexer, start);
4131     }
4132
4133   if (start)
4134     pop_to_parent_deferring_access_checks ();
4135
4136   return success ? parser->scope : NULL_TREE;
4137 }
4138
4139 /* Parse a nested-name-specifier.  See
4140    cp_parser_nested_name_specifier_opt for details.  This function
4141    behaves identically, except that it will an issue an error if no
4142    nested-name-specifier is present.  */
4143
4144 static tree
4145 cp_parser_nested_name_specifier (cp_parser *parser,
4146                                  bool typename_keyword_p,
4147                                  bool check_dependency_p,
4148                                  bool type_p,
4149                                  bool is_declaration)
4150 {
4151   tree scope;
4152
4153   /* Look for the nested-name-specifier.  */
4154   scope = cp_parser_nested_name_specifier_opt (parser,
4155                                                typename_keyword_p,
4156                                                check_dependency_p,
4157                                                type_p,
4158                                                is_declaration);
4159   /* If it was not present, issue an error message.  */
4160   if (!scope)
4161     {
4162       cp_parser_error (parser, "expected nested-name-specifier");
4163       parser->scope = NULL_TREE;
4164     }
4165
4166   return scope;
4167 }
4168
4169 /* Parse a class-or-namespace-name.
4170
4171    class-or-namespace-name:
4172      class-name
4173      namespace-name
4174
4175    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4176    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4177    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4178    TYPE_P is TRUE iff the next name should be taken as a class-name,
4179    even the same name is declared to be another entity in the same
4180    scope.
4181
4182    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4183    specified by the class-or-namespace-name.  If neither is found the
4184    ERROR_MARK_NODE is returned.  */
4185
4186 static tree
4187 cp_parser_class_or_namespace_name (cp_parser *parser,
4188                                    bool typename_keyword_p,
4189                                    bool template_keyword_p,
4190                                    bool check_dependency_p,
4191                                    bool type_p,
4192                                    bool is_declaration)
4193 {
4194   tree saved_scope;
4195   tree saved_qualifying_scope;
4196   tree saved_object_scope;
4197   tree scope;
4198   bool only_class_p;
4199
4200   /* Before we try to parse the class-name, we must save away the
4201      current PARSER->SCOPE since cp_parser_class_name will destroy
4202      it.  */
4203   saved_scope = parser->scope;
4204   saved_qualifying_scope = parser->qualifying_scope;
4205   saved_object_scope = parser->object_scope;
4206   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4207      there is no need to look for a namespace-name.  */
4208   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4209   if (!only_class_p)
4210     cp_parser_parse_tentatively (parser);
4211   scope = cp_parser_class_name (parser,
4212                                 typename_keyword_p,
4213                                 template_keyword_p,
4214                                 type_p ? class_type : none_type,
4215                                 check_dependency_p,
4216                                 /*class_head_p=*/false,
4217                                 is_declaration);
4218   /* If that didn't work, try for a namespace-name.  */
4219   if (!only_class_p && !cp_parser_parse_definitely (parser))
4220     {
4221       /* Restore the saved scope.  */
4222       parser->scope = saved_scope;
4223       parser->qualifying_scope = saved_qualifying_scope;
4224       parser->object_scope = saved_object_scope;
4225       /* If we are not looking at an identifier followed by the scope
4226          resolution operator, then this is not part of a
4227          nested-name-specifier.  (Note that this function is only used
4228          to parse the components of a nested-name-specifier.)  */
4229       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4230           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4231         return error_mark_node;
4232       scope = cp_parser_namespace_name (parser);
4233     }
4234
4235   return scope;
4236 }
4237
4238 /* Parse a postfix-expression.
4239
4240    postfix-expression:
4241      primary-expression
4242      postfix-expression [ expression ]
4243      postfix-expression ( expression-list [opt] )
4244      simple-type-specifier ( expression-list [opt] )
4245      typename :: [opt] nested-name-specifier identifier
4246        ( expression-list [opt] )
4247      typename :: [opt] nested-name-specifier template [opt] template-id
4248        ( expression-list [opt] )
4249      postfix-expression . template [opt] id-expression
4250      postfix-expression -> template [opt] id-expression
4251      postfix-expression . pseudo-destructor-name
4252      postfix-expression -> pseudo-destructor-name
4253      postfix-expression ++
4254      postfix-expression --
4255      dynamic_cast < type-id > ( expression )
4256      static_cast < type-id > ( expression )
4257      reinterpret_cast < type-id > ( expression )
4258      const_cast < type-id > ( expression )
4259      typeid ( expression )
4260      typeid ( type-id )
4261
4262    GNU Extension:
4263
4264    postfix-expression:
4265      ( type-id ) { initializer-list , [opt] }
4266
4267    This extension is a GNU version of the C99 compound-literal
4268    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4269    but they are essentially the same concept.)
4270
4271    If ADDRESS_P is true, the postfix expression is the operand of the
4272    `&' operator.  CAST_P is true if this expression is the target of a
4273    cast.
4274
4275    Returns a representation of the expression.  */
4276
4277 static tree
4278 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4279 {
4280   cp_token *token;
4281   enum rid keyword;
4282   cp_id_kind idk = CP_ID_KIND_NONE;
4283   tree postfix_expression = NULL_TREE;
4284
4285   /* Peek at the next token.  */
4286   token = cp_lexer_peek_token (parser->lexer);
4287   /* Some of the productions are determined by keywords.  */
4288   keyword = token->keyword;
4289   switch (keyword)
4290     {
4291     case RID_DYNCAST:
4292     case RID_STATCAST:
4293     case RID_REINTCAST:
4294     case RID_CONSTCAST:
4295       {
4296         tree type;
4297         tree expression;
4298         const char *saved_message;
4299
4300         /* All of these can be handled in the same way from the point
4301            of view of parsing.  Begin by consuming the token
4302            identifying the cast.  */
4303         cp_lexer_consume_token (parser->lexer);
4304
4305         /* New types cannot be defined in the cast.  */
4306         saved_message = parser->type_definition_forbidden_message;
4307         parser->type_definition_forbidden_message
4308           = "types may not be defined in casts";
4309
4310         /* Look for the opening `<'.  */
4311         cp_parser_require (parser, CPP_LESS, "`<'");
4312         /* Parse the type to which we are casting.  */
4313         type = cp_parser_type_id (parser);
4314         /* Look for the closing `>'.  */
4315         cp_parser_require (parser, CPP_GREATER, "`>'");
4316         /* Restore the old message.  */
4317         parser->type_definition_forbidden_message = saved_message;
4318
4319         /* And the expression which is being cast.  */
4320         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4321         expression = cp_parser_expression (parser, /*cast_p=*/true);
4322         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4323
4324         /* Only type conversions to integral or enumeration types
4325            can be used in constant-expressions.  */
4326         if (!cast_valid_in_integral_constant_expression_p (type)
4327             && (cp_parser_non_integral_constant_expression
4328                 (parser,
4329                  "a cast to a type other than an integral or "
4330                  "enumeration type")))
4331           return error_mark_node;
4332
4333         switch (keyword)
4334           {
4335           case RID_DYNCAST:
4336             postfix_expression
4337               = build_dynamic_cast (type, expression);
4338             break;
4339           case RID_STATCAST:
4340             postfix_expression
4341               = build_static_cast (type, expression);
4342             break;
4343           case RID_REINTCAST:
4344             postfix_expression
4345               = build_reinterpret_cast (type, expression);
4346             break;
4347           case RID_CONSTCAST:
4348             postfix_expression
4349               = build_const_cast (type, expression);
4350             break;
4351           default:
4352             gcc_unreachable ();
4353           }
4354       }
4355       break;
4356
4357     case RID_TYPEID:
4358       {
4359         tree type;
4360         const char *saved_message;
4361         bool saved_in_type_id_in_expr_p;
4362
4363         /* Consume the `typeid' token.  */
4364         cp_lexer_consume_token (parser->lexer);
4365         /* Look for the `(' token.  */
4366         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4367         /* Types cannot be defined in a `typeid' expression.  */
4368         saved_message = parser->type_definition_forbidden_message;
4369         parser->type_definition_forbidden_message
4370           = "types may not be defined in a `typeid\' expression";
4371         /* We can't be sure yet whether we're looking at a type-id or an
4372            expression.  */
4373         cp_parser_parse_tentatively (parser);
4374         /* Try a type-id first.  */
4375         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4376         parser->in_type_id_in_expr_p = true;
4377         type = cp_parser_type_id (parser);
4378         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4379         /* Look for the `)' token.  Otherwise, we can't be sure that
4380            we're not looking at an expression: consider `typeid (int
4381            (3))', for example.  */
4382         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4383         /* If all went well, simply lookup the type-id.  */
4384         if (cp_parser_parse_definitely (parser))
4385           postfix_expression = get_typeid (type);
4386         /* Otherwise, fall back to the expression variant.  */
4387         else
4388           {
4389             tree expression;
4390
4391             /* Look for an expression.  */
4392             expression = cp_parser_expression (parser, /*cast_p=*/false);
4393             /* Compute its typeid.  */
4394             postfix_expression = build_typeid (expression);
4395             /* Look for the `)' token.  */
4396             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4397           }
4398         /* Restore the saved message.  */
4399         parser->type_definition_forbidden_message = saved_message;
4400         /* `typeid' may not appear in an integral constant expression.  */
4401         if (cp_parser_non_integral_constant_expression(parser,
4402                                                        "`typeid' operator"))
4403           return error_mark_node;
4404       }
4405       break;
4406
4407     case RID_TYPENAME:
4408       {
4409         tree type;
4410         /* The syntax permitted here is the same permitted for an
4411            elaborated-type-specifier.  */
4412         type = cp_parser_elaborated_type_specifier (parser,
4413                                                     /*is_friend=*/false,
4414                                                     /*is_declaration=*/false);
4415         postfix_expression = cp_parser_functional_cast (parser, type);
4416       }
4417       break;
4418
4419     default:
4420       {
4421         tree type;
4422
4423         /* If the next thing is a simple-type-specifier, we may be
4424            looking at a functional cast.  We could also be looking at
4425            an id-expression.  So, we try the functional cast, and if
4426            that doesn't work we fall back to the primary-expression.  */
4427         cp_parser_parse_tentatively (parser);
4428         /* Look for the simple-type-specifier.  */
4429         type = cp_parser_simple_type_specifier (parser,
4430                                                 /*decl_specs=*/NULL,
4431                                                 CP_PARSER_FLAGS_NONE);
4432         /* Parse the cast itself.  */
4433         if (!cp_parser_error_occurred (parser))
4434           postfix_expression
4435             = cp_parser_functional_cast (parser, type);
4436         /* If that worked, we're done.  */
4437         if (cp_parser_parse_definitely (parser))
4438           break;
4439
4440         /* If the functional-cast didn't work out, try a
4441            compound-literal.  */
4442         if (cp_parser_allow_gnu_extensions_p (parser)
4443             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4444           {
4445             VEC(constructor_elt,gc) *initializer_list = NULL;
4446             bool saved_in_type_id_in_expr_p;
4447
4448             cp_parser_parse_tentatively (parser);
4449             /* Consume the `('.  */
4450             cp_lexer_consume_token (parser->lexer);
4451             /* Parse the type.  */
4452             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4453             parser->in_type_id_in_expr_p = true;
4454             type = cp_parser_type_id (parser);
4455             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4456             /* Look for the `)'.  */
4457             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4458             /* Look for the `{'.  */
4459             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4460             /* If things aren't going well, there's no need to
4461                keep going.  */
4462             if (!cp_parser_error_occurred (parser))
4463               {
4464                 bool non_constant_p;
4465                 /* Parse the initializer-list.  */
4466                 initializer_list
4467                   = cp_parser_initializer_list (parser, &non_constant_p);
4468                 /* Allow a trailing `,'.  */
4469                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4470                   cp_lexer_consume_token (parser->lexer);
4471                 /* Look for the final `}'.  */
4472                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4473               }
4474             /* If that worked, we're definitely looking at a
4475                compound-literal expression.  */
4476             if (cp_parser_parse_definitely (parser))
4477               {
4478                 /* Warn the user that a compound literal is not
4479                    allowed in standard C++.  */
4480                 if (pedantic)
4481                   pedwarn ("ISO C++ forbids compound-literals");
4482                 /* For simplicitly, we disallow compound literals in
4483                    constant-expressions for simpliicitly.  We could
4484                    allow compound literals of integer type, whose
4485                    initializer was a constant, in constant
4486                    expressions.  Permitting that usage, as a further
4487                    extension, would not change the meaning of any
4488                    currently accepted programs.  (Of course, as
4489                    compound literals are not part of ISO C++, the
4490                    standard has nothing to say.)  */
4491                 if (cp_parser_non_integral_constant_expression 
4492                     (parser, "non-constant compound literals"))
4493                   {
4494                     postfix_expression = error_mark_node;
4495                     break;
4496                   }
4497                 /* Form the representation of the compound-literal.  */
4498                 postfix_expression
4499                   = finish_compound_literal (type, initializer_list);
4500                 break;
4501               }
4502           }
4503
4504         /* It must be a primary-expression.  */
4505         postfix_expression
4506           = cp_parser_primary_expression (parser, address_p, cast_p,
4507                                           /*template_arg_p=*/false,
4508                                           &idk);
4509       }
4510       break;
4511     }
4512
4513   /* Keep looping until the postfix-expression is complete.  */
4514   while (true)
4515     {
4516       if (idk == CP_ID_KIND_UNQUALIFIED
4517           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4518           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4519         /* It is not a Koenig lookup function call.  */
4520         postfix_expression
4521           = unqualified_name_lookup_error (postfix_expression);
4522
4523       /* Peek at the next token.  */
4524       token = cp_lexer_peek_token (parser->lexer);
4525
4526       switch (token->type)
4527         {
4528         case CPP_OPEN_SQUARE:
4529           postfix_expression
4530             = cp_parser_postfix_open_square_expression (parser,
4531                                                         postfix_expression,
4532                                                         false);
4533           idk = CP_ID_KIND_NONE;
4534           break;
4535
4536         case CPP_OPEN_PAREN:
4537           /* postfix-expression ( expression-list [opt] ) */
4538           {
4539             bool koenig_p;
4540             bool is_builtin_constant_p;
4541             bool saved_integral_constant_expression_p = false;
4542             bool saved_non_integral_constant_expression_p = false;
4543             tree args;
4544
4545             is_builtin_constant_p
4546               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4547             if (is_builtin_constant_p)
4548               {
4549                 /* The whole point of __builtin_constant_p is to allow
4550                    non-constant expressions to appear as arguments.  */
4551                 saved_integral_constant_expression_p
4552                   = parser->integral_constant_expression_p;
4553                 saved_non_integral_constant_expression_p
4554                   = parser->non_integral_constant_expression_p;
4555                 parser->integral_constant_expression_p = false;
4556               }
4557             args = (cp_parser_parenthesized_expression_list
4558                     (parser, /*is_attribute_list=*/false,
4559                      /*cast_p=*/false,
4560                      /*non_constant_p=*/NULL));
4561             if (is_builtin_constant_p)
4562               {
4563                 parser->integral_constant_expression_p
4564                   = saved_integral_constant_expression_p;
4565                 parser->non_integral_constant_expression_p
4566                   = saved_non_integral_constant_expression_p;
4567               }
4568
4569             if (args == error_mark_node)
4570               {
4571                 postfix_expression = error_mark_node;
4572                 break;
4573               }
4574
4575             /* Function calls are not permitted in
4576                constant-expressions.  */
4577             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4578                 && cp_parser_non_integral_constant_expression (parser,
4579                                                                "a function call"))
4580               {
4581                 postfix_expression = error_mark_node;
4582                 break;
4583               }
4584
4585             koenig_p = false;
4586             if (idk == CP_ID_KIND_UNQUALIFIED)
4587               {
4588                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4589                   {
4590                     if (args)
4591                       {
4592                         koenig_p = true;
4593                         postfix_expression
4594                           = perform_koenig_lookup (postfix_expression, args);
4595                       }
4596                     else
4597                       postfix_expression
4598                         = unqualified_fn_lookup_error (postfix_expression);
4599                   }
4600                 /* We do not perform argument-dependent lookup if
4601                    normal lookup finds a non-function, in accordance
4602                    with the expected resolution of DR 218.  */
4603                 else if (args && is_overloaded_fn (postfix_expression))
4604                   {
4605                     tree fn = get_first_fn (postfix_expression);
4606
4607                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4608                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4609
4610                     /* Only do argument dependent lookup if regular
4611                        lookup does not find a set of member functions.
4612                        [basic.lookup.koenig]/2a  */
4613                     if (!DECL_FUNCTION_MEMBER_P (fn))
4614                       {
4615                         koenig_p = true;
4616                         postfix_expression
4617                           = perform_koenig_lookup (postfix_expression, args);
4618                       }
4619                   }
4620               }
4621
4622             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4623               {
4624                 tree instance = TREE_OPERAND (postfix_expression, 0);
4625                 tree fn = TREE_OPERAND (postfix_expression, 1);
4626
4627                 if (processing_template_decl
4628                     && (type_dependent_expression_p (instance)
4629                         || (!BASELINK_P (fn)
4630                             && TREE_CODE (fn) != FIELD_DECL)
4631                         || type_dependent_expression_p (fn)
4632                         || any_type_dependent_arguments_p (args)))
4633                   {
4634                     postfix_expression
4635                       = build_min_nt (CALL_EXPR, postfix_expression,
4636                                       args, NULL_TREE);
4637                     break;
4638                   }
4639
4640                 if (BASELINK_P (fn))
4641                   postfix_expression
4642                     = (build_new_method_call
4643                        (instance, fn, args, NULL_TREE,
4644                         (idk == CP_ID_KIND_QUALIFIED
4645                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4646                         /*fn_p=*/NULL));
4647                 else
4648                   postfix_expression
4649                     = finish_call_expr (postfix_expression, args,
4650                                         /*disallow_virtual=*/false,
4651                                         /*koenig_p=*/false);
4652               }
4653             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4654                      || TREE_CODE (postfix_expression) == MEMBER_REF
4655                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4656               postfix_expression = (build_offset_ref_call_from_tree
4657                                     (postfix_expression, args));
4658             else if (idk == CP_ID_KIND_QUALIFIED)
4659               /* A call to a static class member, or a namespace-scope
4660                  function.  */
4661               postfix_expression
4662                 = finish_call_expr (postfix_expression, args,
4663                                     /*disallow_virtual=*/true,
4664                                     koenig_p);
4665             else
4666               /* All other function calls.  */
4667               postfix_expression
4668                 = finish_call_expr (postfix_expression, args,
4669                                     /*disallow_virtual=*/false,
4670                                     koenig_p);
4671
4672             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4673             idk = CP_ID_KIND_NONE;
4674           }
4675           break;
4676
4677         case CPP_DOT:
4678         case CPP_DEREF:
4679           /* postfix-expression . template [opt] id-expression
4680              postfix-expression . pseudo-destructor-name
4681              postfix-expression -> template [opt] id-expression
4682              postfix-expression -> pseudo-destructor-name */
4683
4684           /* Consume the `.' or `->' operator.  */
4685           cp_lexer_consume_token (parser->lexer);
4686
4687           postfix_expression
4688             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4689                                                       postfix_expression,
4690                                                       false, &idk);
4691           break;
4692
4693         case CPP_PLUS_PLUS:
4694           /* postfix-expression ++  */
4695           /* Consume the `++' token.  */
4696           cp_lexer_consume_token (parser->lexer);
4697           /* Generate a representation for the complete expression.  */
4698           postfix_expression
4699             = finish_increment_expr (postfix_expression,
4700                                      POSTINCREMENT_EXPR);
4701           /* Increments may not appear in constant-expressions.  */
4702           if (cp_parser_non_integral_constant_expression (parser,
4703                                                           "an increment"))
4704             postfix_expression = error_mark_node;
4705           idk = CP_ID_KIND_NONE;
4706           break;
4707
4708         case CPP_MINUS_MINUS:
4709           /* postfix-expression -- */
4710           /* Consume the `--' token.  */
4711           cp_lexer_consume_token (parser->lexer);
4712           /* Generate a representation for the complete expression.  */
4713           postfix_expression
4714             = finish_increment_expr (postfix_expression,
4715                                      POSTDECREMENT_EXPR);
4716           /* Decrements may not appear in constant-expressions.  */
4717           if (cp_parser_non_integral_constant_expression (parser,
4718                                                           "a decrement"))
4719             postfix_expression = error_mark_node;
4720           idk = CP_ID_KIND_NONE;
4721           break;
4722
4723         default:
4724           return postfix_expression;
4725         }
4726     }
4727
4728   /* We should never get here.  */
4729   gcc_unreachable ();
4730   return error_mark_node;
4731 }
4732
4733 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4734    by cp_parser_builtin_offsetof.  We're looking for
4735
4736      postfix-expression [ expression ]
4737
4738    FOR_OFFSETOF is set if we're being called in that context, which
4739    changes how we deal with integer constant expressions.  */
4740
4741 static tree
4742 cp_parser_postfix_open_square_expression (cp_parser *parser,
4743                                           tree postfix_expression,
4744                                           bool for_offsetof)
4745 {
4746   tree index;
4747
4748   /* Consume the `[' token.  */
4749   cp_lexer_consume_token (parser->lexer);
4750
4751   /* Parse the index expression.  */
4752   /* ??? For offsetof, there is a question of what to allow here.  If
4753      offsetof is not being used in an integral constant expression context,
4754      then we *could* get the right answer by computing the value at runtime.
4755      If we are in an integral constant expression context, then we might
4756      could accept any constant expression; hard to say without analysis.
4757      Rather than open the barn door too wide right away, allow only integer
4758      constant expressions here.  */
4759   if (for_offsetof)
4760     index = cp_parser_constant_expression (parser, false, NULL);
4761   else
4762     index = cp_parser_expression (parser, /*cast_p=*/false);
4763
4764   /* Look for the closing `]'.  */
4765   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4766
4767   /* Build the ARRAY_REF.  */
4768   postfix_expression = grok_array_decl (postfix_expression, index);
4769
4770   /* When not doing offsetof, array references are not permitted in
4771      constant-expressions.  */
4772   if (!for_offsetof
4773       && (cp_parser_non_integral_constant_expression
4774           (parser, "an array reference")))
4775     postfix_expression = error_mark_node;
4776
4777   return postfix_expression;
4778 }
4779
4780 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4781    by cp_parser_builtin_offsetof.  We're looking for
4782
4783      postfix-expression . template [opt] id-expression
4784      postfix-expression . pseudo-destructor-name
4785      postfix-expression -> template [opt] id-expression
4786      postfix-expression -> pseudo-destructor-name
4787
4788    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4789    limits what of the above we'll actually accept, but nevermind.
4790    TOKEN_TYPE is the "." or "->" token, which will already have been
4791    removed from the stream.  */
4792
4793 static tree
4794 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4795                                         enum cpp_ttype token_type,
4796                                         tree postfix_expression,
4797                                         bool for_offsetof, cp_id_kind *idk)
4798 {
4799   tree name;
4800   bool dependent_p;
4801   bool pseudo_destructor_p;
4802   tree scope = NULL_TREE;
4803
4804   /* If this is a `->' operator, dereference the pointer.  */
4805   if (token_type == CPP_DEREF)
4806     postfix_expression = build_x_arrow (postfix_expression);
4807   /* Check to see whether or not the expression is type-dependent.  */
4808   dependent_p = type_dependent_expression_p (postfix_expression);
4809   /* The identifier following the `->' or `.' is not qualified.  */
4810   parser->scope = NULL_TREE;
4811   parser->qualifying_scope = NULL_TREE;
4812   parser->object_scope = NULL_TREE;
4813   *idk = CP_ID_KIND_NONE;
4814   /* Enter the scope corresponding to the type of the object
4815      given by the POSTFIX_EXPRESSION.  */
4816   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4817     {
4818       scope = TREE_TYPE (postfix_expression);
4819       /* According to the standard, no expression should ever have
4820          reference type.  Unfortunately, we do not currently match
4821          the standard in this respect in that our internal representation
4822          of an expression may have reference type even when the standard
4823          says it does not.  Therefore, we have to manually obtain the
4824          underlying type here.  */
4825       scope = non_reference (scope);
4826       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4827       if (scope == unknown_type_node)
4828         {
4829           error ("%qE does not have class type", postfix_expression);
4830           scope = NULL_TREE;
4831         }
4832       else
4833         scope = complete_type_or_else (scope, NULL_TREE);
4834       /* Let the name lookup machinery know that we are processing a
4835          class member access expression.  */
4836       parser->context->object_type = scope;
4837       /* If something went wrong, we want to be able to discern that case,
4838          as opposed to the case where there was no SCOPE due to the type
4839          of expression being dependent.  */
4840       if (!scope)
4841         scope = error_mark_node;
4842       /* If the SCOPE was erroneous, make the various semantic analysis
4843          functions exit quickly -- and without issuing additional error
4844          messages.  */
4845       if (scope == error_mark_node)
4846         postfix_expression = error_mark_node;
4847     }
4848
4849   /* Assume this expression is not a pseudo-destructor access.  */
4850   pseudo_destructor_p = false;
4851
4852   /* If the SCOPE is a scalar type, then, if this is a valid program,
4853      we must be looking at a pseudo-destructor-name.  */
4854   if (scope && SCALAR_TYPE_P (scope))
4855     {
4856       tree s;
4857       tree type;
4858
4859       cp_parser_parse_tentatively (parser);
4860       /* Parse the pseudo-destructor-name.  */
4861       s = NULL_TREE;
4862       cp_parser_pseudo_destructor_name (parser, &s, &type);
4863       if (cp_parser_parse_definitely (parser))
4864         {
4865           pseudo_destructor_p = true;
4866           postfix_expression
4867             = finish_pseudo_destructor_expr (postfix_expression,
4868                                              s, TREE_TYPE (type));
4869         }
4870     }
4871
4872   if (!pseudo_destructor_p)
4873     {
4874       /* If the SCOPE is not a scalar type, we are looking at an
4875          ordinary class member access expression, rather than a
4876          pseudo-destructor-name.  */
4877       bool template_p;
4878       /* Parse the id-expression.  */
4879       name = (cp_parser_id_expression
4880               (parser,
4881                cp_parser_optional_template_keyword (parser),
4882                /*check_dependency_p=*/true,
4883                &template_p,
4884                /*declarator_p=*/false,
4885                /*optional_p=*/false));
4886       /* In general, build a SCOPE_REF if the member name is qualified.
4887          However, if the name was not dependent and has already been
4888          resolved; there is no need to build the SCOPE_REF.  For example;
4889
4890              struct X { void f(); };
4891              template <typename T> void f(T* t) { t->X::f(); }
4892
4893          Even though "t" is dependent, "X::f" is not and has been resolved
4894          to a BASELINK; there is no need to include scope information.  */
4895
4896       /* But we do need to remember that there was an explicit scope for
4897          virtual function calls.  */
4898       if (parser->scope)
4899         *idk = CP_ID_KIND_QUALIFIED;
4900
4901       /* If the name is a template-id that names a type, we will get a
4902          TYPE_DECL here.  That is invalid code.  */
4903       if (TREE_CODE (name) == TYPE_DECL)
4904         {
4905           error ("invalid use of %qD", name);
4906           postfix_expression = error_mark_node;
4907         }
4908       else
4909         {
4910           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4911             {
4912               name = build_qualified_name (/*type=*/NULL_TREE,
4913                                            parser->scope,
4914                                            name,
4915                                            template_p);
4916               parser->scope = NULL_TREE;
4917               parser->qualifying_scope = NULL_TREE;
4918               parser->object_scope = NULL_TREE;
4919             }
4920           if (scope && name && BASELINK_P (name))
4921             adjust_result_of_qualified_name_lookup
4922               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4923           postfix_expression
4924             = finish_class_member_access_expr (postfix_expression, name,
4925                                                template_p);
4926         }
4927     }
4928
4929   /* We no longer need to look up names in the scope of the object on
4930      the left-hand side of the `.' or `->' operator.  */
4931   parser->context->object_type = NULL_TREE;
4932
4933   /* Outside of offsetof, these operators may not appear in
4934      constant-expressions.  */
4935   if (!for_offsetof
4936       && (cp_parser_non_integral_constant_expression
4937           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4938     postfix_expression = error_mark_node;
4939
4940   return postfix_expression;
4941 }
4942
4943 /* Parse a parenthesized expression-list.
4944
4945    expression-list:
4946      assignment-expression
4947      expression-list, assignment-expression
4948
4949    attribute-list:
4950      expression-list
4951      identifier
4952      identifier, expression-list
4953
4954    CAST_P is true if this expression is the target of a cast.
4955
4956    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4957    representation of an assignment-expression.  Note that a TREE_LIST
4958    is returned even if there is only a single expression in the list.
4959    error_mark_node is returned if the ( and or ) are
4960    missing. NULL_TREE is returned on no expressions. The parentheses
4961    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4962    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4963    indicates whether or not all of the expressions in the list were
4964    constant.  */
4965
4966 static tree
4967 cp_parser_parenthesized_expression_list (cp_parser* parser,
4968                                          bool is_attribute_list,
4969                                          bool cast_p,
4970                                          bool *non_constant_p)
4971 {
4972   tree expression_list = NULL_TREE;
4973   bool fold_expr_p = is_attribute_list;
4974   tree identifier = NULL_TREE;
4975
4976   /* Assume all the expressions will be constant.  */
4977   if (non_constant_p)
4978     *non_constant_p = false;
4979
4980   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4981     return error_mark_node;
4982
4983   /* Consume expressions until there are no more.  */
4984   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4985     while (true)
4986       {
4987         tree expr;
4988
4989         /* At the beginning of attribute lists, check to see if the
4990            next token is an identifier.  */
4991         if (is_attribute_list
4992             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4993           {
4994             cp_token *token;
4995
4996             /* Consume the identifier.  */
4997             token = cp_lexer_consume_token (parser->lexer);
4998             /* Save the identifier.  */
4999             identifier = token->u.value;
5000           }
5001         else
5002           {
5003             /* Parse the next assignment-expression.  */
5004             if (non_constant_p)
5005               {
5006                 bool expr_non_constant_p;
5007                 expr = (cp_parser_constant_expression
5008                         (parser, /*allow_non_constant_p=*/true,
5009                          &expr_non_constant_p));
5010                 if (expr_non_constant_p)
5011                   *non_constant_p = true;
5012               }
5013             else
5014               expr = cp_parser_assignment_expression (parser, cast_p);
5015
5016             if (fold_expr_p)
5017               expr = fold_non_dependent_expr (expr);
5018
5019              /* Add it to the list.  We add error_mark_node
5020                 expressions to the list, so that we can still tell if
5021                 the correct form for a parenthesized expression-list
5022                 is found. That gives better errors.  */
5023             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5024
5025             if (expr == error_mark_node)
5026               goto skip_comma;
5027           }
5028
5029         /* After the first item, attribute lists look the same as
5030            expression lists.  */
5031         is_attribute_list = false;
5032
5033       get_comma:;
5034         /* If the next token isn't a `,', then we are done.  */
5035         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5036           break;
5037
5038         /* Otherwise, consume the `,' and keep going.  */
5039         cp_lexer_consume_token (parser->lexer);
5040       }
5041
5042   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5043     {
5044       int ending;
5045
5046     skip_comma:;
5047       /* We try and resync to an unnested comma, as that will give the
5048          user better diagnostics.  */
5049       ending = cp_parser_skip_to_closing_parenthesis (parser,
5050                                                       /*recovering=*/true,
5051                                                       /*or_comma=*/true,
5052                                                       /*consume_paren=*/true);
5053       if (ending < 0)
5054         goto get_comma;
5055       if (!ending)
5056         return error_mark_node;
5057     }
5058
5059   /* We built up the list in reverse order so we must reverse it now.  */
5060   expression_list = nreverse (expression_list);
5061   if (identifier)
5062     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5063
5064   return expression_list;
5065 }
5066
5067 /* Parse a pseudo-destructor-name.
5068
5069    pseudo-destructor-name:
5070      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5071      :: [opt] nested-name-specifier template template-id :: ~ type-name
5072      :: [opt] nested-name-specifier [opt] ~ type-name
5073
5074    If either of the first two productions is used, sets *SCOPE to the
5075    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5076    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5077    or ERROR_MARK_NODE if the parse fails.  */
5078
5079 static void
5080 cp_parser_pseudo_destructor_name (cp_parser* parser,
5081                                   tree* scope,
5082                                   tree* type)
5083 {
5084   bool nested_name_specifier_p;
5085
5086   /* Assume that things will not work out.  */
5087   *type = error_mark_node;
5088
5089   /* Look for the optional `::' operator.  */
5090   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5091   /* Look for the optional nested-name-specifier.  */
5092   nested_name_specifier_p
5093     = (cp_parser_nested_name_specifier_opt (parser,
5094                                             /*typename_keyword_p=*/false,
5095                                             /*check_dependency_p=*/true,
5096                                             /*type_p=*/false,
5097                                             /*is_declaration=*/true)
5098        != NULL_TREE);
5099   /* Now, if we saw a nested-name-specifier, we might be doing the
5100      second production.  */
5101   if (nested_name_specifier_p
5102       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5103     {
5104       /* Consume the `template' keyword.  */
5105       cp_lexer_consume_token (parser->lexer);
5106       /* Parse the template-id.  */
5107       cp_parser_template_id (parser,
5108                              /*template_keyword_p=*/true,
5109                              /*check_dependency_p=*/false,
5110                              /*is_declaration=*/true);
5111       /* Look for the `::' token.  */
5112       cp_parser_require (parser, CPP_SCOPE, "`::'");
5113     }
5114   /* If the next token is not a `~', then there might be some
5115      additional qualification.  */
5116   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5117     {
5118       /* Look for the type-name.  */
5119       *scope = TREE_TYPE (cp_parser_type_name (parser));
5120
5121       if (*scope == error_mark_node)
5122         return;
5123
5124       /* If we don't have ::~, then something has gone wrong.  Since
5125          the only caller of this function is looking for something
5126          after `.' or `->' after a scalar type, most likely the
5127          program is trying to get a member of a non-aggregate
5128          type.  */
5129       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5130           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5131         {
5132           cp_parser_error (parser, "request for member of non-aggregate type");
5133           return;
5134         }
5135
5136       /* Look for the `::' token.  */
5137       cp_parser_require (parser, CPP_SCOPE, "`::'");
5138     }
5139   else
5140     *scope = NULL_TREE;
5141
5142   /* Look for the `~'.  */
5143   cp_parser_require (parser, CPP_COMPL, "`~'");
5144   /* Look for the type-name again.  We are not responsible for
5145      checking that it matches the first type-name.  */
5146   *type = cp_parser_type_name (parser);
5147 }
5148
5149 /* Parse a unary-expression.
5150
5151    unary-expression:
5152      postfix-expression
5153      ++ cast-expression
5154      -- cast-expression
5155      unary-operator cast-expression
5156      sizeof unary-expression
5157      sizeof ( type-id )
5158      new-expression
5159      delete-expression
5160
5161    GNU Extensions:
5162
5163    unary-expression:
5164      __extension__ cast-expression
5165      __alignof__ unary-expression
5166      __alignof__ ( type-id )
5167      __real__ cast-expression
5168      __imag__ cast-expression
5169      && identifier
5170
5171    ADDRESS_P is true iff the unary-expression is appearing as the
5172    operand of the `&' operator.   CAST_P is true if this expression is
5173    the target of a cast.
5174
5175    Returns a representation of the expression.  */
5176
5177 static tree
5178 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5179 {
5180   cp_token *token;
5181   enum tree_code unary_operator;
5182
5183   /* Peek at the next token.  */
5184   token = cp_lexer_peek_token (parser->lexer);
5185   /* Some keywords give away the kind of expression.  */
5186   if (token->type == CPP_KEYWORD)
5187     {
5188       enum rid keyword = token->keyword;
5189
5190       switch (keyword)
5191         {
5192         case RID_ALIGNOF:
5193         case RID_SIZEOF:
5194           {
5195             tree operand;
5196             enum tree_code op;
5197
5198             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5199             /* Consume the token.  */
5200             cp_lexer_consume_token (parser->lexer);
5201             /* Parse the operand.  */
5202             operand = cp_parser_sizeof_operand (parser, keyword);
5203
5204             if (TYPE_P (operand))
5205               return cxx_sizeof_or_alignof_type (operand, op, true);
5206             else
5207               return cxx_sizeof_or_alignof_expr (operand, op);
5208           }
5209
5210         case RID_NEW:
5211           return cp_parser_new_expression (parser);
5212
5213         case RID_DELETE:
5214           return cp_parser_delete_expression (parser);
5215
5216         case RID_EXTENSION:
5217           {
5218             /* The saved value of the PEDANTIC flag.  */
5219             int saved_pedantic;
5220             tree expr;
5221
5222             /* Save away the PEDANTIC flag.  */
5223             cp_parser_extension_opt (parser, &saved_pedantic);
5224             /* Parse the cast-expression.  */
5225             expr = cp_parser_simple_cast_expression (parser);
5226             /* Restore the PEDANTIC flag.  */
5227             pedantic = saved_pedantic;
5228
5229             return expr;
5230           }
5231
5232         case RID_REALPART:
5233         case RID_IMAGPART:
5234           {
5235             tree expression;
5236
5237             /* Consume the `__real__' or `__imag__' token.  */
5238             cp_lexer_consume_token (parser->lexer);
5239             /* Parse the cast-expression.  */
5240             expression = cp_parser_simple_cast_expression (parser);
5241             /* Create the complete representation.  */
5242             return build_x_unary_op ((keyword == RID_REALPART
5243                                       ? REALPART_EXPR : IMAGPART_EXPR),
5244                                      expression);
5245           }
5246           break;
5247
5248         default:
5249           break;
5250         }
5251     }
5252
5253   /* Look for the `:: new' and `:: delete', which also signal the
5254      beginning of a new-expression, or delete-expression,
5255      respectively.  If the next token is `::', then it might be one of
5256      these.  */
5257   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5258     {
5259       enum rid keyword;
5260
5261       /* See if the token after the `::' is one of the keywords in
5262          which we're interested.  */
5263       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5264       /* If it's `new', we have a new-expression.  */
5265       if (keyword == RID_NEW)
5266         return cp_parser_new_expression (parser);
5267       /* Similarly, for `delete'.  */
5268       else if (keyword == RID_DELETE)
5269         return cp_parser_delete_expression (parser);
5270     }
5271
5272   /* Look for a unary operator.  */
5273   unary_operator = cp_parser_unary_operator (token);
5274   /* The `++' and `--' operators can be handled similarly, even though
5275      they are not technically unary-operators in the grammar.  */
5276   if (unary_operator == ERROR_MARK)
5277     {
5278       if (token->type == CPP_PLUS_PLUS)
5279         unary_operator = PREINCREMENT_EXPR;
5280       else if (token->type == CPP_MINUS_MINUS)
5281         unary_operator = PREDECREMENT_EXPR;
5282       /* Handle the GNU address-of-label extension.  */
5283       else if (cp_parser_allow_gnu_extensions_p (parser)
5284                && token->type == CPP_AND_AND)
5285         {
5286           tree identifier;
5287
5288           /* Consume the '&&' token.  */
5289           cp_lexer_consume_token (parser->lexer);
5290           /* Look for the identifier.  */
5291           identifier = cp_parser_identifier (parser);
5292           /* Create an expression representing the address.  */
5293           return finish_label_address_expr (identifier);
5294         }
5295     }
5296   if (unary_operator != ERROR_MARK)
5297     {
5298       tree cast_expression;
5299       tree expression = error_mark_node;
5300       const char *non_constant_p = NULL;
5301
5302       /* Consume the operator token.  */
5303       token = cp_lexer_consume_token (parser->lexer);
5304       /* Parse the cast-expression.  */
5305       cast_expression
5306         = cp_parser_cast_expression (parser,
5307                                      unary_operator == ADDR_EXPR,
5308                                      /*cast_p=*/false);
5309       /* Now, build an appropriate representation.  */
5310       switch (unary_operator)
5311         {
5312         case INDIRECT_REF:
5313           non_constant_p = "`*'";
5314           expression = build_x_indirect_ref (cast_expression, "unary *");
5315           break;
5316
5317         case ADDR_EXPR:
5318           non_constant_p = "`&'";
5319           /* Fall through.  */
5320         case BIT_NOT_EXPR:
5321           expression = build_x_unary_op (unary_operator, cast_expression);
5322           break;
5323
5324         case PREINCREMENT_EXPR:
5325         case PREDECREMENT_EXPR:
5326           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5327                             ? "`++'" : "`--'");
5328           /* Fall through.  */
5329         case UNARY_PLUS_EXPR:
5330         case NEGATE_EXPR:
5331         case TRUTH_NOT_EXPR:
5332           expression = finish_unary_op_expr (unary_operator, cast_expression);
5333           break;
5334
5335         default:
5336           gcc_unreachable ();
5337         }
5338
5339       if (non_constant_p
5340           && cp_parser_non_integral_constant_expression (parser,
5341                                                          non_constant_p))
5342         expression = error_mark_node;
5343
5344       return expression;
5345     }
5346
5347   return cp_parser_postfix_expression (parser, address_p, cast_p);
5348 }
5349
5350 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5351    unary-operator, the corresponding tree code is returned.  */
5352
5353 static enum tree_code
5354 cp_parser_unary_operator (cp_token* token)
5355 {
5356   switch (token->type)
5357     {
5358     case CPP_MULT:
5359       return INDIRECT_REF;
5360
5361     case CPP_AND:
5362       return ADDR_EXPR;
5363
5364     case CPP_PLUS:
5365       return UNARY_PLUS_EXPR;
5366
5367     case CPP_MINUS:
5368       return NEGATE_EXPR;
5369
5370     case CPP_NOT:
5371       return TRUTH_NOT_EXPR;
5372
5373     case CPP_COMPL:
5374       return BIT_NOT_EXPR;
5375
5376     default:
5377       return ERROR_MARK;
5378     }
5379 }
5380
5381 /* Parse a new-expression.
5382
5383    new-expression:
5384      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5385      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5386
5387    Returns a representation of the expression.  */
5388
5389 static tree
5390 cp_parser_new_expression (cp_parser* parser)
5391 {
5392   bool global_scope_p;
5393   tree placement;
5394   tree type;
5395   tree initializer;
5396   tree nelts;
5397
5398   /* Look for the optional `::' operator.  */
5399   global_scope_p
5400     = (cp_parser_global_scope_opt (parser,
5401                                    /*current_scope_valid_p=*/false)
5402        != NULL_TREE);
5403   /* Look for the `new' operator.  */
5404   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5405   /* There's no easy way to tell a new-placement from the
5406      `( type-id )' construct.  */
5407   cp_parser_parse_tentatively (parser);
5408   /* Look for a new-placement.  */
5409   placement = cp_parser_new_placement (parser);
5410   /* If that didn't work out, there's no new-placement.  */
5411   if (!cp_parser_parse_definitely (parser))
5412     placement = NULL_TREE;
5413
5414   /* If the next token is a `(', then we have a parenthesized
5415      type-id.  */
5416   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5417     {
5418       /* Consume the `('.  */
5419       cp_lexer_consume_token (parser->lexer);
5420       /* Parse the type-id.  */
5421       type = cp_parser_type_id (parser);
5422       /* Look for the closing `)'.  */
5423       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5424       /* There should not be a direct-new-declarator in this production,
5425          but GCC used to allowed this, so we check and emit a sensible error
5426          message for this case.  */
5427       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5428         {
5429           error ("array bound forbidden after parenthesized type-id");
5430           inform ("try removing the parentheses around the type-id");
5431           cp_parser_direct_new_declarator (parser);
5432         }
5433       nelts = NULL_TREE;
5434     }
5435   /* Otherwise, there must be a new-type-id.  */
5436   else
5437     type = cp_parser_new_type_id (parser, &nelts);
5438
5439   /* If the next token is a `(', then we have a new-initializer.  */
5440   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5441     initializer = cp_parser_new_initializer (parser);
5442   else
5443     initializer = NULL_TREE;
5444
5445   /* A new-expression may not appear in an integral constant
5446      expression.  */
5447   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5448     return error_mark_node;
5449
5450   /* Create a representation of the new-expression.  */
5451   return build_new (placement, type, nelts, initializer, global_scope_p);
5452 }
5453
5454 /* Parse a new-placement.
5455
5456    new-placement:
5457      ( expression-list )
5458
5459    Returns the same representation as for an expression-list.  */
5460
5461 static tree
5462 cp_parser_new_placement (cp_parser* parser)
5463 {
5464   tree expression_list;
5465
5466   /* Parse the expression-list.  */
5467   expression_list = (cp_parser_parenthesized_expression_list
5468                      (parser, false, /*cast_p=*/false,
5469                       /*non_constant_p=*/NULL));
5470
5471   return expression_list;
5472 }
5473
5474 /* Parse a new-type-id.
5475
5476    new-type-id:
5477      type-specifier-seq new-declarator [opt]
5478
5479    Returns the TYPE allocated.  If the new-type-id indicates an array
5480    type, *NELTS is set to the number of elements in the last array
5481    bound; the TYPE will not include the last array bound.  */
5482
5483 static tree
5484 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5485 {
5486   cp_decl_specifier_seq type_specifier_seq;
5487   cp_declarator *new_declarator;
5488   cp_declarator *declarator;
5489   cp_declarator *outer_declarator;
5490   const char *saved_message;
5491   tree type;
5492
5493   /* The type-specifier sequence must not contain type definitions.
5494      (It cannot contain declarations of new types either, but if they
5495      are not definitions we will catch that because they are not
5496      complete.)  */
5497   saved_message = parser->type_definition_forbidden_message;
5498   parser->type_definition_forbidden_message
5499     = "types may not be defined in a new-type-id";
5500   /* Parse the type-specifier-seq.  */
5501   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5502                                 &type_specifier_seq);
5503   /* Restore the old message.  */
5504   parser->type_definition_forbidden_message = saved_message;
5505   /* Parse the new-declarator.  */
5506   new_declarator = cp_parser_new_declarator_opt (parser);
5507
5508   /* Determine the number of elements in the last array dimension, if
5509      any.  */
5510   *nelts = NULL_TREE;
5511   /* Skip down to the last array dimension.  */
5512   declarator = new_declarator;
5513   outer_declarator = NULL;
5514   while (declarator && (declarator->kind == cdk_pointer
5515                         || declarator->kind == cdk_ptrmem))
5516     {
5517       outer_declarator = declarator;
5518       declarator = declarator->declarator;
5519     }
5520   while (declarator
5521          && declarator->kind == cdk_array
5522          && declarator->declarator
5523          && declarator->declarator->kind == cdk_array)
5524     {
5525       outer_declarator = declarator;
5526       declarator = declarator->declarator;
5527     }
5528
5529   if (declarator && declarator->kind == cdk_array)
5530     {
5531       *nelts = declarator->u.array.bounds;
5532       if (*nelts == error_mark_node)
5533         *nelts = integer_one_node;
5534
5535       if (outer_declarator)
5536         outer_declarator->declarator = declarator->declarator;
5537       else
5538         new_declarator = NULL;
5539     }
5540
5541   type = groktypename (&type_specifier_seq, new_declarator);
5542   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5543     {
5544       *nelts = array_type_nelts_top (type);
5545       type = TREE_TYPE (type);
5546     }
5547   return type;
5548 }
5549
5550 /* Parse an (optional) new-declarator.
5551
5552    new-declarator:
5553      ptr-operator new-declarator [opt]
5554      direct-new-declarator
5555
5556    Returns the declarator.  */
5557
5558 static cp_declarator *
5559 cp_parser_new_declarator_opt (cp_parser* parser)
5560 {
5561   enum tree_code code;
5562   tree type;
5563   cp_cv_quals cv_quals;
5564
5565   /* We don't know if there's a ptr-operator next, or not.  */
5566   cp_parser_parse_tentatively (parser);
5567   /* Look for a ptr-operator.  */
5568   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5569   /* If that worked, look for more new-declarators.  */
5570   if (cp_parser_parse_definitely (parser))
5571     {
5572       cp_declarator *declarator;
5573
5574       /* Parse another optional declarator.  */
5575       declarator = cp_parser_new_declarator_opt (parser);
5576
5577       /* Create the representation of the declarator.  */
5578       if (type)
5579         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5580       else if (code == INDIRECT_REF)
5581         declarator = make_pointer_declarator (cv_quals, declarator);
5582       else
5583         declarator = make_reference_declarator (cv_quals, declarator);
5584
5585       return declarator;
5586     }
5587
5588   /* If the next token is a `[', there is a direct-new-declarator.  */
5589   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5590     return cp_parser_direct_new_declarator (parser);
5591
5592   return NULL;
5593 }
5594
5595 /* Parse a direct-new-declarator.
5596
5597    direct-new-declarator:
5598      [ expression ]
5599      direct-new-declarator [constant-expression]
5600
5601    */
5602
5603 static cp_declarator *
5604 cp_parser_direct_new_declarator (cp_parser* parser)
5605 {
5606   cp_declarator *declarator = NULL;
5607
5608   while (true)
5609     {
5610       tree expression;
5611
5612       /* Look for the opening `['.  */
5613       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5614       /* The first expression is not required to be constant.  */
5615       if (!declarator)
5616         {
5617           expression = cp_parser_expression (parser, /*cast_p=*/false);
5618           /* The standard requires that the expression have integral
5619              type.  DR 74 adds enumeration types.  We believe that the
5620              real intent is that these expressions be handled like the
5621              expression in a `switch' condition, which also allows
5622              classes with a single conversion to integral or
5623              enumeration type.  */
5624           if (!processing_template_decl)
5625             {
5626               expression
5627                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5628                                               expression,
5629                                               /*complain=*/true);
5630               if (!expression)
5631                 {
5632                   error ("expression in new-declarator must have integral "
5633                          "or enumeration type");
5634                   expression = error_mark_node;
5635                 }
5636             }
5637         }
5638       /* But all the other expressions must be.  */
5639       else
5640         expression
5641           = cp_parser_constant_expression (parser,
5642                                            /*allow_non_constant=*/false,
5643                                            NULL);
5644       /* Look for the closing `]'.  */
5645       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5646
5647       /* Add this bound to the declarator.  */
5648       declarator = make_array_declarator (declarator, expression);
5649
5650       /* If the next token is not a `[', then there are no more
5651          bounds.  */
5652       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5653         break;
5654     }
5655
5656   return declarator;
5657 }
5658
5659 /* Parse a new-initializer.
5660
5661    new-initializer:
5662      ( expression-list [opt] )
5663
5664    Returns a representation of the expression-list.  If there is no
5665    expression-list, VOID_ZERO_NODE is returned.  */
5666
5667 static tree
5668 cp_parser_new_initializer (cp_parser* parser)
5669 {
5670   tree expression_list;
5671
5672   expression_list = (cp_parser_parenthesized_expression_list
5673                      (parser, false, /*cast_p=*/false,
5674                       /*non_constant_p=*/NULL));
5675   if (!expression_list)
5676     expression_list = void_zero_node;
5677
5678   return expression_list;
5679 }
5680
5681 /* Parse a delete-expression.
5682
5683    delete-expression:
5684      :: [opt] delete cast-expression
5685      :: [opt] delete [ ] cast-expression
5686
5687    Returns a representation of the expression.  */
5688
5689 static tree
5690 cp_parser_delete_expression (cp_parser* parser)
5691 {
5692   bool global_scope_p;
5693   bool array_p;
5694   tree expression;
5695
5696   /* Look for the optional `::' operator.  */
5697   global_scope_p
5698     = (cp_parser_global_scope_opt (parser,
5699                                    /*current_scope_valid_p=*/false)
5700        != NULL_TREE);
5701   /* Look for the `delete' keyword.  */
5702   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5703   /* See if the array syntax is in use.  */
5704   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5705     {
5706       /* Consume the `[' token.  */
5707       cp_lexer_consume_token (parser->lexer);
5708       /* Look for the `]' token.  */
5709       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5710       /* Remember that this is the `[]' construct.  */
5711       array_p = true;
5712     }
5713   else
5714     array_p = false;
5715
5716   /* Parse the cast-expression.  */
5717   expression = cp_parser_simple_cast_expression (parser);
5718
5719   /* A delete-expression may not appear in an integral constant
5720      expression.  */
5721   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5722     return error_mark_node;
5723
5724   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5725 }
5726
5727 /* Parse a cast-expression.
5728
5729    cast-expression:
5730      unary-expression
5731      ( type-id ) cast-expression
5732
5733    ADDRESS_P is true iff the unary-expression is appearing as the
5734    operand of the `&' operator.   CAST_P is true if this expression is
5735    the target of a cast.
5736
5737    Returns a representation of the expression.  */
5738
5739 static tree
5740 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5741 {
5742   /* If it's a `(', then we might be looking at a cast.  */
5743   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5744     {
5745       tree type = NULL_TREE;
5746       tree expr = NULL_TREE;
5747       bool compound_literal_p;
5748       const char *saved_message;
5749
5750       /* There's no way to know yet whether or not this is a cast.
5751          For example, `(int (3))' is a unary-expression, while `(int)
5752          3' is a cast.  So, we resort to parsing tentatively.  */
5753       cp_parser_parse_tentatively (parser);
5754       /* Types may not be defined in a cast.  */
5755       saved_message = parser->type_definition_forbidden_message;
5756       parser->type_definition_forbidden_message
5757         = "types may not be defined in casts";
5758       /* Consume the `('.  */
5759       cp_lexer_consume_token (parser->lexer);
5760       /* A very tricky bit is that `(struct S) { 3 }' is a
5761          compound-literal (which we permit in C++ as an extension).
5762          But, that construct is not a cast-expression -- it is a
5763          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5764          is legal; if the compound-literal were a cast-expression,
5765          you'd need an extra set of parentheses.)  But, if we parse
5766          the type-id, and it happens to be a class-specifier, then we
5767          will commit to the parse at that point, because we cannot
5768          undo the action that is done when creating a new class.  So,
5769          then we cannot back up and do a postfix-expression.
5770
5771          Therefore, we scan ahead to the closing `)', and check to see
5772          if the token after the `)' is a `{'.  If so, we are not
5773          looking at a cast-expression.
5774
5775          Save tokens so that we can put them back.  */
5776       cp_lexer_save_tokens (parser->lexer);
5777       /* Skip tokens until the next token is a closing parenthesis.
5778          If we find the closing `)', and the next token is a `{', then
5779          we are looking at a compound-literal.  */
5780       compound_literal_p
5781         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5782                                                   /*consume_paren=*/true)
5783            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5784       /* Roll back the tokens we skipped.  */
5785       cp_lexer_rollback_tokens (parser->lexer);
5786       /* If we were looking at a compound-literal, simulate an error
5787          so that the call to cp_parser_parse_definitely below will
5788          fail.  */
5789       if (compound_literal_p)
5790         cp_parser_simulate_error (parser);
5791       else
5792         {
5793           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5794           parser->in_type_id_in_expr_p = true;
5795           /* Look for the type-id.  */
5796           type = cp_parser_type_id (parser);
5797           /* Look for the closing `)'.  */
5798           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5799           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5800         }
5801
5802       /* Restore the saved message.  */
5803       parser->type_definition_forbidden_message = saved_message;
5804
5805       /* If ok so far, parse the dependent expression. We cannot be
5806          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5807          ctor of T, but looks like a cast to function returning T
5808          without a dependent expression.  */
5809       if (!cp_parser_error_occurred (parser))
5810         expr = cp_parser_cast_expression (parser,
5811                                           /*address_p=*/false,
5812                                           /*cast_p=*/true);
5813
5814       if (cp_parser_parse_definitely (parser))
5815         {
5816           /* Warn about old-style casts, if so requested.  */
5817           if (warn_old_style_cast
5818               && !in_system_header
5819               && !VOID_TYPE_P (type)
5820               && current_lang_name != lang_name_c)
5821             warning (OPT_Wold_style_cast, "use of old-style cast");
5822
5823           /* Only type conversions to integral or enumeration types
5824              can be used in constant-expressions.  */
5825           if (!cast_valid_in_integral_constant_expression_p (type)
5826               && (cp_parser_non_integral_constant_expression
5827                   (parser,
5828                    "a cast to a type other than an integral or "
5829                    "enumeration type")))
5830             return error_mark_node;
5831
5832           /* Perform the cast.  */
5833           expr = build_c_cast (type, expr);
5834           return expr;
5835         }
5836     }
5837
5838   /* If we get here, then it's not a cast, so it must be a
5839      unary-expression.  */
5840   return cp_parser_unary_expression (parser, address_p, cast_p);
5841 }
5842
5843 /* Parse a binary expression of the general form:
5844
5845    pm-expression:
5846      cast-expression
5847      pm-expression .* cast-expression
5848      pm-expression ->* cast-expression
5849
5850    multiplicative-expression:
5851      pm-expression
5852      multiplicative-expression * pm-expression
5853      multiplicative-expression / pm-expression
5854      multiplicative-expression % pm-expression
5855
5856    additive-expression:
5857      multiplicative-expression
5858      additive-expression + multiplicative-expression
5859      additive-expression - multiplicative-expression
5860
5861    shift-expression:
5862      additive-expression
5863      shift-expression << additive-expression
5864      shift-expression >> additive-expression
5865
5866    relational-expression:
5867      shift-expression
5868      relational-expression < shift-expression
5869      relational-expression > shift-expression
5870      relational-expression <= shift-expression
5871      relational-expression >= shift-expression
5872
5873   GNU Extension:
5874
5875    relational-expression:
5876      relational-expression <? shift-expression
5877      relational-expression >? shift-expression
5878
5879    equality-expression:
5880      relational-expression
5881      equality-expression == relational-expression
5882      equality-expression != relational-expression
5883
5884    and-expression:
5885      equality-expression
5886      and-expression & equality-expression
5887
5888    exclusive-or-expression:
5889      and-expression
5890      exclusive-or-expression ^ and-expression
5891
5892    inclusive-or-expression:
5893      exclusive-or-expression
5894      inclusive-or-expression | exclusive-or-expression
5895
5896    logical-and-expression:
5897      inclusive-or-expression
5898      logical-and-expression && inclusive-or-expression
5899
5900    logical-or-expression:
5901      logical-and-expression
5902      logical-or-expression || logical-and-expression
5903
5904    All these are implemented with a single function like:
5905
5906    binary-expression:
5907      simple-cast-expression
5908      binary-expression <token> binary-expression
5909
5910    CAST_P is true if this expression is the target of a cast.
5911
5912    The binops_by_token map is used to get the tree codes for each <token> type.
5913    binary-expressions are associated according to a precedence table.  */
5914
5915 #define TOKEN_PRECEDENCE(token) \
5916   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5917    ? PREC_NOT_OPERATOR \
5918    : binops_by_token[token->type].prec)
5919
5920 static tree
5921 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5922 {
5923   cp_parser_expression_stack stack;
5924   cp_parser_expression_stack_entry *sp = &stack[0];
5925   tree lhs, rhs;
5926   cp_token *token;
5927   enum tree_code tree_type, lhs_type, rhs_type;
5928   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5929   bool overloaded_p;
5930
5931   /* Parse the first expression.  */
5932   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5933   lhs_type = ERROR_MARK;
5934
5935   for (;;)
5936     {
5937       /* Get an operator token.  */
5938       token = cp_lexer_peek_token (parser->lexer);
5939
5940       new_prec = TOKEN_PRECEDENCE (token);
5941
5942       /* Popping an entry off the stack means we completed a subexpression:
5943          - either we found a token which is not an operator (`>' where it is not
5944            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5945            will happen repeatedly;
5946          - or, we found an operator which has lower priority.  This is the case
5947            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5948            parsing `3 * 4'.  */
5949       if (new_prec <= prec)
5950         {
5951           if (sp == stack)
5952             break;
5953           else
5954             goto pop;
5955         }
5956
5957      get_rhs:
5958       tree_type = binops_by_token[token->type].tree_type;
5959
5960       /* We used the operator token.  */
5961       cp_lexer_consume_token (parser->lexer);
5962
5963       /* Extract another operand.  It may be the RHS of this expression
5964          or the LHS of a new, higher priority expression.  */
5965       rhs = cp_parser_simple_cast_expression (parser);
5966       rhs_type = ERROR_MARK;
5967
5968       /* Get another operator token.  Look up its precedence to avoid
5969          building a useless (immediately popped) stack entry for common
5970          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5971       token = cp_lexer_peek_token (parser->lexer);
5972       lookahead_prec = TOKEN_PRECEDENCE (token);
5973       if (lookahead_prec > new_prec)
5974         {
5975           /* ... and prepare to parse the RHS of the new, higher priority
5976              expression.  Since precedence levels on the stack are
5977              monotonically increasing, we do not have to care about
5978              stack overflows.  */
5979           sp->prec = prec;
5980           sp->tree_type = tree_type;
5981           sp->lhs = lhs;
5982           sp->lhs_type = lhs_type;
5983           sp++;
5984           lhs = rhs;
5985           lhs_type = rhs_type;
5986           prec = new_prec;
5987           new_prec = lookahead_prec;
5988           goto get_rhs;
5989
5990          pop:
5991           /* If the stack is not empty, we have parsed into LHS the right side
5992              (`4' in the example above) of an expression we had suspended.
5993              We can use the information on the stack to recover the LHS (`3')
5994              from the stack together with the tree code (`MULT_EXPR'), and
5995              the precedence of the higher level subexpression
5996              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5997              which will be used to actually build the additive expression.  */
5998           --sp;
5999           prec = sp->prec;
6000           tree_type = sp->tree_type;
6001           rhs = lhs;
6002           rhs_type = lhs_type;
6003           lhs = sp->lhs;
6004           lhs_type = sp->lhs_type;
6005         }
6006
6007       overloaded_p = false;
6008       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6009                                &overloaded_p);
6010       lhs_type = tree_type;
6011
6012       /* If the binary operator required the use of an overloaded operator,
6013          then this expression cannot be an integral constant-expression.
6014          An overloaded operator can be used even if both operands are
6015          otherwise permissible in an integral constant-expression if at
6016          least one of the operands is of enumeration type.  */
6017
6018       if (overloaded_p
6019           && (cp_parser_non_integral_constant_expression
6020               (parser, "calls to overloaded operators")))
6021         return error_mark_node;
6022     }
6023
6024   return lhs;
6025 }
6026
6027
6028 /* Parse the `? expression : assignment-expression' part of a
6029    conditional-expression.  The LOGICAL_OR_EXPR is the
6030    logical-or-expression that started the conditional-expression.
6031    Returns a representation of the entire conditional-expression.
6032
6033    This routine is used by cp_parser_assignment_expression.
6034
6035      ? expression : assignment-expression
6036
6037    GNU Extensions:
6038
6039      ? : assignment-expression */
6040
6041 static tree
6042 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6043 {
6044   tree expr;
6045   tree assignment_expr;
6046
6047   /* Consume the `?' token.  */
6048   cp_lexer_consume_token (parser->lexer);
6049   if (cp_parser_allow_gnu_extensions_p (parser)
6050       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6051     /* Implicit true clause.  */
6052     expr = NULL_TREE;
6053   else
6054     /* Parse the expression.  */
6055     expr = cp_parser_expression (parser, /*cast_p=*/false);
6056
6057   /* The next token should be a `:'.  */
6058   cp_parser_require (parser, CPP_COLON, "`:'");
6059   /* Parse the assignment-expression.  */
6060   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6061
6062   /* Build the conditional-expression.  */
6063   return build_x_conditional_expr (logical_or_expr,
6064                                    expr,
6065                                    assignment_expr);
6066 }
6067
6068 /* Parse an assignment-expression.
6069
6070    assignment-expression:
6071      conditional-expression
6072      logical-or-expression assignment-operator assignment_expression
6073      throw-expression
6074
6075    CAST_P is true if this expression is the target of a cast.
6076
6077    Returns a representation for the expression.  */
6078
6079 static tree
6080 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6081 {
6082   tree expr;
6083
6084   /* If the next token is the `throw' keyword, then we're looking at
6085      a throw-expression.  */
6086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6087     expr = cp_parser_throw_expression (parser);
6088   /* Otherwise, it must be that we are looking at a
6089      logical-or-expression.  */
6090   else
6091     {
6092       /* Parse the binary expressions (logical-or-expression).  */
6093       expr = cp_parser_binary_expression (parser, cast_p);
6094       /* If the next token is a `?' then we're actually looking at a
6095          conditional-expression.  */
6096       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6097         return cp_parser_question_colon_clause (parser, expr);
6098       else
6099         {
6100           enum tree_code assignment_operator;
6101
6102           /* If it's an assignment-operator, we're using the second
6103              production.  */
6104           assignment_operator
6105             = cp_parser_assignment_operator_opt (parser);
6106           if (assignment_operator != ERROR_MARK)
6107             {
6108               tree rhs;
6109
6110               /* Parse the right-hand side of the assignment.  */
6111               rhs = cp_parser_assignment_expression (parser, cast_p);
6112               /* An assignment may not appear in a
6113                  constant-expression.  */
6114               if (cp_parser_non_integral_constant_expression (parser,
6115                                                               "an assignment"))
6116                 return error_mark_node;
6117               /* Build the assignment expression.  */
6118               expr = build_x_modify_expr (expr,
6119                                           assignment_operator,
6120                                           rhs);
6121             }
6122         }
6123     }
6124
6125   return expr;
6126 }
6127
6128 /* Parse an (optional) assignment-operator.
6129
6130    assignment-operator: one of
6131      = *= /= %= += -= >>= <<= &= ^= |=
6132
6133    GNU Extension:
6134
6135    assignment-operator: one of
6136      <?= >?=
6137
6138    If the next token is an assignment operator, the corresponding tree
6139    code is returned, and the token is consumed.  For example, for
6140    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6141    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6142    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6143    operator, ERROR_MARK is returned.  */
6144
6145 static enum tree_code
6146 cp_parser_assignment_operator_opt (cp_parser* parser)
6147 {
6148   enum tree_code op;
6149   cp_token *token;
6150
6151   /* Peek at the next toen.  */
6152   token = cp_lexer_peek_token (parser->lexer);
6153
6154   switch (token->type)
6155     {
6156     case CPP_EQ:
6157       op = NOP_EXPR;
6158       break;
6159
6160     case CPP_MULT_EQ:
6161       op = MULT_EXPR;
6162       break;
6163
6164     case CPP_DIV_EQ:
6165       op = TRUNC_DIV_EXPR;
6166       break;
6167
6168     case CPP_MOD_EQ:
6169       op = TRUNC_MOD_EXPR;
6170       break;
6171
6172     case CPP_PLUS_EQ:
6173       op = PLUS_EXPR;
6174       break;
6175
6176     case CPP_MINUS_EQ:
6177       op = MINUS_EXPR;
6178       break;
6179
6180     case CPP_RSHIFT_EQ:
6181       op = RSHIFT_EXPR;
6182       break;
6183
6184     case CPP_LSHIFT_EQ:
6185       op = LSHIFT_EXPR;
6186       break;
6187
6188     case CPP_AND_EQ:
6189       op = BIT_AND_EXPR;
6190       break;
6191
6192     case CPP_XOR_EQ:
6193       op = BIT_XOR_EXPR;
6194       break;
6195
6196     case CPP_OR_EQ:
6197       op = BIT_IOR_EXPR;
6198       break;
6199
6200     default:
6201       /* Nothing else is an assignment operator.  */
6202       op = ERROR_MARK;
6203     }
6204
6205   /* If it was an assignment operator, consume it.  */
6206   if (op != ERROR_MARK)
6207     cp_lexer_consume_token (parser->lexer);
6208
6209   return op;
6210 }
6211
6212 /* Parse an expression.
6213
6214    expression:
6215      assignment-expression
6216      expression , assignment-expression
6217
6218    CAST_P is true if this expression is the target of a cast.
6219
6220    Returns a representation of the expression.  */
6221
6222 static tree
6223 cp_parser_expression (cp_parser* parser, bool cast_p)
6224 {
6225   tree expression = NULL_TREE;
6226
6227   while (true)
6228     {
6229       tree assignment_expression;
6230
6231       /* Parse the next assignment-expression.  */
6232       assignment_expression
6233         = cp_parser_assignment_expression (parser, cast_p);
6234       /* If this is the first assignment-expression, we can just
6235          save it away.  */
6236       if (!expression)
6237         expression = assignment_expression;
6238       else
6239         expression = build_x_compound_expr (expression,
6240                                             assignment_expression);
6241       /* If the next token is not a comma, then we are done with the
6242          expression.  */
6243       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6244         break;
6245       /* Consume the `,'.  */
6246       cp_lexer_consume_token (parser->lexer);
6247       /* A comma operator cannot appear in a constant-expression.  */
6248       if (cp_parser_non_integral_constant_expression (parser,
6249                                                       "a comma operator"))
6250         expression = error_mark_node;
6251     }
6252
6253   return expression;
6254 }
6255
6256 /* Parse a constant-expression.
6257
6258    constant-expression:
6259      conditional-expression
6260
6261   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6262   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6263   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6264   is false, NON_CONSTANT_P should be NULL.  */
6265
6266 static tree
6267 cp_parser_constant_expression (cp_parser* parser,
6268                                bool allow_non_constant_p,
6269                                bool *non_constant_p)
6270 {
6271   bool saved_integral_constant_expression_p;
6272   bool saved_allow_non_integral_constant_expression_p;
6273   bool saved_non_integral_constant_expression_p;
6274   tree expression;
6275
6276   /* It might seem that we could simply parse the
6277      conditional-expression, and then check to see if it were
6278      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6279      one that the compiler can figure out is constant, possibly after
6280      doing some simplifications or optimizations.  The standard has a
6281      precise definition of constant-expression, and we must honor
6282      that, even though it is somewhat more restrictive.
6283
6284      For example:
6285
6286        int i[(2, 3)];
6287
6288      is not a legal declaration, because `(2, 3)' is not a
6289      constant-expression.  The `,' operator is forbidden in a
6290      constant-expression.  However, GCC's constant-folding machinery
6291      will fold this operation to an INTEGER_CST for `3'.  */
6292
6293   /* Save the old settings.  */
6294   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6295   saved_allow_non_integral_constant_expression_p
6296     = parser->allow_non_integral_constant_expression_p;
6297   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6298   /* We are now parsing a constant-expression.  */
6299   parser->integral_constant_expression_p = true;
6300   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6301   parser->non_integral_constant_expression_p = false;
6302   /* Although the grammar says "conditional-expression", we parse an
6303      "assignment-expression", which also permits "throw-expression"
6304      and the use of assignment operators.  In the case that
6305      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6306      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6307      actually essential that we look for an assignment-expression.
6308      For example, cp_parser_initializer_clauses uses this function to
6309      determine whether a particular assignment-expression is in fact
6310      constant.  */
6311   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6312   /* Restore the old settings.  */
6313   parser->integral_constant_expression_p
6314     = saved_integral_constant_expression_p;
6315   parser->allow_non_integral_constant_expression_p
6316     = saved_allow_non_integral_constant_expression_p;
6317   if (allow_non_constant_p)
6318     *non_constant_p = parser->non_integral_constant_expression_p;
6319   else if (parser->non_integral_constant_expression_p)
6320     expression = error_mark_node;
6321   parser->non_integral_constant_expression_p
6322     = saved_non_integral_constant_expression_p;
6323
6324   return expression;
6325 }
6326
6327 /* Parse __builtin_offsetof.
6328
6329    offsetof-expression:
6330      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6331
6332    offsetof-member-designator:
6333      id-expression
6334      | offsetof-member-designator "." id-expression
6335      | offsetof-member-designator "[" expression "]"  */
6336
6337 static tree
6338 cp_parser_builtin_offsetof (cp_parser *parser)
6339 {
6340   int save_ice_p, save_non_ice_p;
6341   tree type, expr;
6342   cp_id_kind dummy;
6343
6344   /* We're about to accept non-integral-constant things, but will
6345      definitely yield an integral constant expression.  Save and
6346      restore these values around our local parsing.  */
6347   save_ice_p = parser->integral_constant_expression_p;
6348   save_non_ice_p = parser->non_integral_constant_expression_p;
6349
6350   /* Consume the "__builtin_offsetof" token.  */
6351   cp_lexer_consume_token (parser->lexer);
6352   /* Consume the opening `('.  */
6353   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6354   /* Parse the type-id.  */
6355   type = cp_parser_type_id (parser);
6356   /* Look for the `,'.  */
6357   cp_parser_require (parser, CPP_COMMA, "`,'");
6358
6359   /* Build the (type *)null that begins the traditional offsetof macro.  */
6360   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6361
6362   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6363   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6364                                                  true, &dummy);
6365   while (true)
6366     {
6367       cp_token *token = cp_lexer_peek_token (parser->lexer);
6368       switch (token->type)
6369         {
6370         case CPP_OPEN_SQUARE:
6371           /* offsetof-member-designator "[" expression "]" */
6372           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6373           break;
6374
6375         case CPP_DOT:
6376           /* offsetof-member-designator "." identifier */
6377           cp_lexer_consume_token (parser->lexer);
6378           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6379                                                          true, &dummy);
6380           break;
6381
6382         case CPP_CLOSE_PAREN:
6383           /* Consume the ")" token.  */
6384           cp_lexer_consume_token (parser->lexer);
6385           goto success;
6386
6387         default:
6388           /* Error.  We know the following require will fail, but
6389              that gives the proper error message.  */
6390           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6391           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6392           expr = error_mark_node;
6393           goto failure;
6394         }
6395     }
6396
6397  success:
6398   /* If we're processing a template, we can't finish the semantics yet.
6399      Otherwise we can fold the entire expression now.  */
6400   if (processing_template_decl)
6401     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6402   else
6403     expr = finish_offsetof (expr);
6404
6405  failure:
6406   parser->integral_constant_expression_p = save_ice_p;
6407   parser->non_integral_constant_expression_p = save_non_ice_p;
6408
6409   return expr;
6410 }
6411
6412 /* Statements [gram.stmt.stmt]  */
6413
6414 /* Parse a statement.
6415
6416    statement:
6417      labeled-statement
6418      expression-statement
6419      compound-statement
6420      selection-statement
6421      iteration-statement
6422      jump-statement
6423      declaration-statement
6424      try-block
6425
6426   IN_COMPOUND is true when the statement is nested inside a
6427   cp_parser_compound_statement; this matters for certain pragmas.
6428
6429   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6430   is a (possibly labeled) if statement which is not enclosed in braces
6431   and has an else clause.  This is used to implement -Wparentheses.  */
6432
6433 static void
6434 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6435                      bool in_compound, bool *if_p)
6436 {
6437   tree statement;
6438   cp_token *token;
6439   location_t statement_location;
6440
6441  restart:
6442   if (if_p != NULL)
6443     *if_p = false;
6444   /* There is no statement yet.  */
6445   statement = NULL_TREE;
6446   /* Peek at the next token.  */
6447   token = cp_lexer_peek_token (parser->lexer);
6448   /* Remember the location of the first token in the statement.  */
6449   statement_location = token->location;
6450   /* If this is a keyword, then that will often determine what kind of
6451      statement we have.  */
6452   if (token->type == CPP_KEYWORD)
6453     {
6454       enum rid keyword = token->keyword;
6455
6456       switch (keyword)
6457         {
6458         case RID_CASE:
6459         case RID_DEFAULT:
6460           /* Looks like a labeled-statement with a case label.
6461              Parse the label, and then use tail recursion to parse
6462              the statement.  */
6463           cp_parser_label_for_labeled_statement (parser);
6464           goto restart;
6465
6466         case RID_IF:
6467         case RID_SWITCH:
6468           statement = cp_parser_selection_statement (parser, if_p);
6469           break;
6470
6471         case RID_WHILE:
6472         case RID_DO:
6473         case RID_FOR:
6474           statement = cp_parser_iteration_statement (parser);
6475           break;
6476
6477         case RID_BREAK:
6478         case RID_CONTINUE:
6479         case RID_RETURN:
6480         case RID_GOTO:
6481           statement = cp_parser_jump_statement (parser);
6482           break;
6483
6484           /* Objective-C++ exception-handling constructs.  */
6485         case RID_AT_TRY:
6486         case RID_AT_CATCH:
6487         case RID_AT_FINALLY:
6488         case RID_AT_SYNCHRONIZED:
6489         case RID_AT_THROW:
6490           statement = cp_parser_objc_statement (parser);
6491           break;
6492
6493         case RID_TRY:
6494           statement = cp_parser_try_block (parser);
6495           break;
6496
6497         default:
6498           /* It might be a keyword like `int' that can start a
6499              declaration-statement.  */
6500           break;
6501         }
6502     }
6503   else if (token->type == CPP_NAME)
6504     {
6505       /* If the next token is a `:', then we are looking at a
6506          labeled-statement.  */
6507       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6508       if (token->type == CPP_COLON)
6509         {
6510           /* Looks like a labeled-statement with an ordinary label.
6511              Parse the label, and then use tail recursion to parse
6512              the statement.  */
6513           cp_parser_label_for_labeled_statement (parser);
6514           goto restart;
6515         }
6516     }
6517   /* Anything that starts with a `{' must be a compound-statement.  */
6518   else if (token->type == CPP_OPEN_BRACE)
6519     /* APPLE LOCAL radar 5982990 */
6520     statement = cp_parser_compound_statement (parser, NULL, false, false);
6521   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6522      a statement all its own.  */
6523   else if (token->type == CPP_PRAGMA)
6524     {
6525       /* Only certain OpenMP pragmas are attached to statements, and thus
6526          are considered statements themselves.  All others are not.  In
6527          the context of a compound, accept the pragma as a "statement" and
6528          return so that we can check for a close brace.  Otherwise we
6529          require a real statement and must go back and read one.  */
6530       if (in_compound)
6531         cp_parser_pragma (parser, pragma_compound);
6532       else if (!cp_parser_pragma (parser, pragma_stmt))
6533         goto restart;
6534       return;
6535     }
6536   else if (token->type == CPP_EOF)
6537     {
6538       cp_parser_error (parser, "expected statement");
6539       return;
6540     }
6541
6542   /* Everything else must be a declaration-statement or an
6543      expression-statement.  Try for the declaration-statement
6544      first, unless we are looking at a `;', in which case we know that
6545      we have an expression-statement.  */
6546   if (!statement)
6547     {
6548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6549         {
6550           cp_parser_parse_tentatively (parser);
6551           /* Try to parse the declaration-statement.  */
6552           cp_parser_declaration_statement (parser);
6553           /* If that worked, we're done.  */
6554           if (cp_parser_parse_definitely (parser))
6555             return;
6556         }
6557       /* Look for an expression-statement instead.  */
6558       statement = cp_parser_expression_statement (parser, in_statement_expr);
6559     }
6560
6561   /* Set the line number for the statement.  */
6562   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6563     SET_EXPR_LOCATION (statement, statement_location);
6564 }
6565
6566 /* Parse the label for a labeled-statement, i.e.
6567
6568    identifier :
6569    case constant-expression :
6570    default :
6571
6572    GNU Extension:
6573    case constant-expression ... constant-expression : statement
6574
6575    When a label is parsed without errors, the label is added to the
6576    parse tree by the finish_* functions, so this function doesn't
6577    have to return the label.  */
6578
6579 static void
6580 cp_parser_label_for_labeled_statement (cp_parser* parser)
6581 {
6582   cp_token *token;
6583
6584   /* The next token should be an identifier.  */
6585   token = cp_lexer_peek_token (parser->lexer);
6586   if (token->type != CPP_NAME
6587       && token->type != CPP_KEYWORD)
6588     {
6589       cp_parser_error (parser, "expected labeled-statement");
6590       return;
6591     }
6592
6593   switch (token->keyword)
6594     {
6595     case RID_CASE:
6596       {
6597         tree expr, expr_hi;
6598         cp_token *ellipsis;
6599
6600         /* Consume the `case' token.  */
6601         cp_lexer_consume_token (parser->lexer);
6602         /* Parse the constant-expression.  */
6603         expr = cp_parser_constant_expression (parser,
6604                                               /*allow_non_constant_p=*/false,
6605                                               NULL);
6606
6607         ellipsis = cp_lexer_peek_token (parser->lexer);
6608         if (ellipsis->type == CPP_ELLIPSIS)
6609           {
6610             /* Consume the `...' token.  */
6611             cp_lexer_consume_token (parser->lexer);
6612             expr_hi =
6613               cp_parser_constant_expression (parser,
6614                                              /*allow_non_constant_p=*/false,
6615                                              NULL);
6616             /* We don't need to emit warnings here, as the common code
6617                will do this for us.  */
6618           }
6619         else
6620           expr_hi = NULL_TREE;
6621
6622         if (parser->in_switch_statement_p)
6623           finish_case_label (expr, expr_hi);
6624         else
6625           error ("case label %qE not within a switch statement", expr);
6626       }
6627       break;
6628
6629     case RID_DEFAULT:
6630       /* Consume the `default' token.  */
6631       cp_lexer_consume_token (parser->lexer);
6632
6633       if (parser->in_switch_statement_p)
6634         finish_case_label (NULL_TREE, NULL_TREE);
6635       else
6636         error ("case label not within a switch statement");
6637       break;
6638
6639     default:
6640       /* Anything else must be an ordinary label.  */
6641       finish_label_stmt (cp_parser_identifier (parser));
6642       break;
6643     }
6644
6645   /* Require the `:' token.  */
6646   cp_parser_require (parser, CPP_COLON, "`:'");
6647 }
6648
6649 /* Parse an expression-statement.
6650
6651    expression-statement:
6652      expression [opt] ;
6653
6654    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6655    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6656    indicates whether this expression-statement is part of an
6657    expression statement.  */
6658
6659 static tree
6660 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6661 {
6662   tree statement = NULL_TREE;
6663
6664   /* If the next token is a ';', then there is no expression
6665      statement.  */
6666   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6667     statement = cp_parser_expression (parser, /*cast_p=*/false);
6668
6669   /* Consume the final `;'.  */
6670   cp_parser_consume_semicolon_at_end_of_statement (parser);
6671
6672   if (in_statement_expr
6673       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6674     /* This is the final expression statement of a statement
6675        expression.  */
6676     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6677   else if (statement)
6678     statement = finish_expr_stmt (statement);
6679   else
6680     finish_stmt ();
6681
6682   return statement;
6683 }
6684
6685 /* Parse a compound-statement.
6686
6687    compound-statement:
6688      { statement-seq [opt] }
6689
6690    Returns a tree representing the statement.  */
6691
6692 static tree
6693 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6694                               /* APPLE LOCAL radar 5982990 */
6695                               bool in_try, bool objc_sjlj_exceptions)
6696 {
6697   tree compound_stmt;
6698
6699   /* Consume the `{'.  */
6700   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6701     return error_mark_node;
6702   /* Begin the compound-statement.  */
6703   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6704   /* Parse an (optional) statement-seq.  */
6705   cp_parser_statement_seq_opt (parser, in_statement_expr);
6706   /* APPLE LOCAL begin radar 5982990 */
6707   if (objc_sjlj_exceptions)
6708     objc_mark_locals_volatile (NULL);
6709   /* APPLE LOCAL end radar 5982990 */
6710   /* Finish the compound-statement.  */
6711   finish_compound_stmt (compound_stmt);
6712   /* Consume the `}'.  */
6713   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6714
6715   return compound_stmt;
6716 }
6717
6718 /* Parse an (optional) statement-seq.
6719
6720    statement-seq:
6721      statement
6722      statement-seq [opt] statement  */
6723
6724 static void
6725 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6726 {
6727   /* APPLE LOCAL begin omit calls to empty destructors 5559195 */
6728   tree class_type = DECL_CONTEXT (current_function_decl);
6729
6730   bool determine_destructor_triviality =
6731     DECL_DESTRUCTOR_P (current_function_decl) && class_type != NULL_TREE
6732     && !CLASSTYPE_DESTRUCTOR_TRIVIALITY_FINAL (class_type);
6733
6734   /* Assume that the destructor is trivial at first, and mark nontrivial if
6735      any statement is parsed. */
6736   if (determine_destructor_triviality)
6737     {
6738       CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (class_type) = 0;
6739       CLASSTYPE_DESTRUCTOR_TRIVIALITY_FINAL (class_type) = 1;
6740     }
6741   /* APPLE LOCAL end omit calls to empty destructors 5559195 */
6742
6743   /* Scan statements until there aren't any more.  */
6744   while (true)
6745     {
6746       cp_token *token = cp_lexer_peek_token (parser->lexer);
6747
6748       /* APPLE LOCAL begin ObjC++ 4185810 */
6749       /* If we're looking at a `}', then we've run out of
6750          statements; the same is true if we have reached the end
6751          of file, or have stumbled upon a stray 'else' or '@end'.  */
6752       if (token->type == CPP_CLOSE_BRACE
6753           || token->type == CPP_EOF
6754           || token->type == CPP_PRAGMA_EOL
6755           || (token->type == CPP_KEYWORD
6756               && (token->keyword == RID_ELSE
6757                   || token->keyword == RID_AT_END)))
6758      /* APPLE LOCAL end ObjC++ 4185810 */
6759         break;
6760
6761       /* APPLE LOCAL begin omit calls to empty destructors 5559195 */
6762       if (determine_destructor_triviality)
6763         CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (class_type) = 1;
6764       /* APPLE LOCAL end omit calls to empty destructors 5559195 */
6765
6766       /* Parse the statement.  */
6767       cp_parser_statement (parser, in_statement_expr, true, NULL);
6768     }
6769 }
6770
6771 /* Parse a selection-statement.
6772
6773    selection-statement:
6774      if ( condition ) statement
6775      if ( condition ) statement else statement
6776      switch ( condition ) statement
6777
6778    Returns the new IF_STMT or SWITCH_STMT.
6779
6780    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6781    is a (possibly labeled) if statement which is not enclosed in
6782    braces and has an else clause.  This is used to implement
6783    -Wparentheses.  */
6784
6785 static tree
6786 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6787 {
6788   cp_token *token;
6789   enum rid keyword;
6790
6791   if (if_p != NULL)
6792     *if_p = false;
6793
6794   /* Peek at the next token.  */
6795   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6796
6797   /* See what kind of keyword it is.  */
6798   keyword = token->keyword;
6799   switch (keyword)
6800     {
6801     case RID_IF:
6802     case RID_SWITCH:
6803       {
6804         tree statement;
6805         tree condition;
6806
6807         /* Look for the `('.  */
6808         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6809           {
6810             cp_parser_skip_to_end_of_statement (parser);
6811             return error_mark_node;
6812           }
6813
6814         /* Begin the selection-statement.  */
6815         if (keyword == RID_IF)
6816           statement = begin_if_stmt ();
6817         else
6818           statement = begin_switch_stmt ();
6819
6820         /* Parse the condition.  */
6821         condition = cp_parser_condition (parser);
6822         /* Look for the `)'.  */
6823         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6824           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6825                                                  /*consume_paren=*/true);
6826
6827         if (keyword == RID_IF)
6828           {
6829             bool nested_if;
6830
6831             /* Add the condition.  */
6832             finish_if_stmt_cond (condition, statement);
6833
6834             /* Parse the then-clause.  */
6835             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6836             finish_then_clause (statement);
6837
6838             /* If the next token is `else', parse the else-clause.  */
6839             if (cp_lexer_next_token_is_keyword (parser->lexer,
6840                                                 RID_ELSE))
6841               {
6842                 /* Consume the `else' keyword.  */
6843                 cp_lexer_consume_token (parser->lexer);
6844                 begin_else_clause (statement);
6845                 /* Parse the else-clause.  */
6846                 cp_parser_implicitly_scoped_statement (parser, NULL);
6847                 finish_else_clause (statement);
6848
6849                 /* If we are currently parsing a then-clause, then
6850                    IF_P will not be NULL.  We set it to true to
6851                    indicate that this if statement has an else clause.
6852                    This may trigger the Wparentheses warning below
6853                    when we get back up to the parent if statement.  */
6854                 if (if_p != NULL)
6855                   *if_p = true;
6856               }
6857             else
6858               {
6859                 /* This if statement does not have an else clause.  If
6860                    NESTED_IF is true, then the then-clause is an if
6861                    statement which does have an else clause.  We warn
6862                    about the potential ambiguity.  */
6863                 if (nested_if)
6864                   warning (OPT_Wparentheses,
6865                            ("%Hsuggest explicit braces "
6866                             "to avoid ambiguous %<else%>"),
6867                            EXPR_LOCUS (statement));
6868               }
6869
6870             /* Now we're all done with the if-statement.  */
6871             finish_if_stmt (statement);
6872           }
6873         else
6874           {
6875             bool in_switch_statement_p;
6876             unsigned char in_statement;
6877
6878             /* Add the condition.  */
6879             finish_switch_cond (condition, statement);
6880
6881             /* Parse the body of the switch-statement.  */
6882             in_switch_statement_p = parser->in_switch_statement_p;
6883             in_statement = parser->in_statement;
6884             parser->in_switch_statement_p = true;
6885             parser->in_statement |= IN_SWITCH_STMT;
6886             cp_parser_implicitly_scoped_statement (parser, NULL);
6887             parser->in_switch_statement_p = in_switch_statement_p;
6888             parser->in_statement = in_statement;
6889
6890             /* Now we're all done with the switch-statement.  */
6891             finish_switch_stmt (statement);
6892           }
6893
6894         return statement;
6895       }
6896       break;
6897
6898     default:
6899       cp_parser_error (parser, "expected selection-statement");
6900       return error_mark_node;
6901     }
6902 }
6903
6904 /* Parse a condition.
6905
6906    condition:
6907      expression
6908      type-specifier-seq declarator = assignment-expression
6909
6910    GNU Extension:
6911
6912    condition:
6913      type-specifier-seq declarator asm-specification [opt]
6914        attributes [opt] = assignment-expression
6915
6916    Returns the expression that should be tested.  */
6917
6918 static tree
6919 cp_parser_condition (cp_parser* parser)
6920 {
6921   cp_decl_specifier_seq type_specifiers;
6922   const char *saved_message;
6923
6924   /* Try the declaration first.  */
6925   cp_parser_parse_tentatively (parser);
6926   /* New types are not allowed in the type-specifier-seq for a
6927      condition.  */
6928   saved_message = parser->type_definition_forbidden_message;
6929   parser->type_definition_forbidden_message
6930     = "types may not be defined in conditions";
6931   /* Parse the type-specifier-seq.  */
6932   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6933                                 &type_specifiers);
6934   /* Restore the saved message.  */
6935   parser->type_definition_forbidden_message = saved_message;
6936   /* If all is well, we might be looking at a declaration.  */
6937   if (!cp_parser_error_occurred (parser))
6938     {
6939       tree decl;
6940       tree asm_specification;
6941       tree attributes;
6942       cp_declarator *declarator;
6943       tree initializer = NULL_TREE;
6944
6945       /* Parse the declarator.  */
6946       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6947                                          /*ctor_dtor_or_conv_p=*/NULL,
6948                                          /*parenthesized_p=*/NULL,
6949                                          /*member_p=*/false);
6950       /* Parse the attributes.  */
6951       attributes = cp_parser_attributes_opt (parser);
6952       /* Parse the asm-specification.  */
6953       asm_specification = cp_parser_asm_specification_opt (parser);
6954       /* If the next token is not an `=', then we might still be
6955          looking at an expression.  For example:
6956
6957            if (A(a).x)
6958
6959          looks like a decl-specifier-seq and a declarator -- but then
6960          there is no `=', so this is an expression.  */
6961       cp_parser_require (parser, CPP_EQ, "`='");
6962       /* If we did see an `=', then we are looking at a declaration
6963          for sure.  */
6964       if (cp_parser_parse_definitely (parser))
6965         {
6966           tree pushed_scope;
6967           bool non_constant_p;
6968
6969           /* Create the declaration.  */
6970           decl = start_decl (declarator, &type_specifiers,
6971                              /*initialized_p=*/true,
6972                              attributes, /*prefix_attributes=*/NULL_TREE,
6973                              &pushed_scope);
6974           /* Parse the assignment-expression.  */
6975           initializer
6976             = cp_parser_constant_expression (parser,
6977                                              /*allow_non_constant_p=*/true,
6978                                              &non_constant_p);
6979           if (!non_constant_p)
6980             initializer = fold_non_dependent_expr (initializer);
6981
6982           /* Process the initializer.  */
6983           cp_finish_decl (decl,
6984                           initializer, !non_constant_p,
6985                           asm_specification,
6986                           LOOKUP_ONLYCONVERTING);
6987
6988           if (pushed_scope)
6989             pop_scope (pushed_scope);
6990
6991           return convert_from_reference (decl);
6992         }
6993     }
6994   /* If we didn't even get past the declarator successfully, we are
6995      definitely not looking at a declaration.  */
6996   else
6997     cp_parser_abort_tentative_parse (parser);
6998
6999   /* Otherwise, we are looking at an expression.  */
7000   return cp_parser_expression (parser, /*cast_p=*/false);
7001 }
7002
7003 /* APPLE LOCAL begin radar 4631818 */
7004 /* This routine looks for objective-c++'s foreach statement by scanning for-loop
7005    header looking for either 1) 'for (type selector in...)' or 2) 'for (selector in...)' 
7006    where selector is already declared in outer scope. If it failed, it undoes the lexical
7007    look-ahead and returns false. If it succeeded, it adds the 'selector' to the statement
7008    list and returns true. At success, lexer points to token following the 'in' keyword.
7009 */
7010
7011 static bool
7012 cp_parser_parse_foreach_stmt (cp_parser *parser)
7013 {
7014   int decl_spec_declares_class_or_enum;
7015   bool is_cv_qualifier;
7016   tree type_spec;
7017   cp_decl_specifier_seq decl_specs;
7018   tree node;
7019   cp_token *token;
7020   bool is_legit_foreach = false;
7021   cp_declarator *declarator;
7022
7023   /* Exclude class/struct/enum type definition in for-loop header, which is 
7024      aparently legal in c++. Otherwise, it causes side-effect (type is enterred
7025      in function's scope) when type is re-parsed. */
7026   token = cp_lexer_peek_token (parser->lexer);
7027   if (cp_parser_token_is_class_key (token) || token->keyword == RID_ENUM)
7028     return false;
7029
7030   cp_parser_parse_tentatively (parser); 
7031   clear_decl_specs (&decl_specs);
7032   type_spec
7033     = cp_parser_type_specifier (parser, CP_PARSER_FLAGS_OPTIONAL,
7034                                  &decl_specs,
7035                                  /*is_declaration=*/true,
7036                                  &decl_spec_declares_class_or_enum,
7037                                  &is_cv_qualifier);
7038   declarator
7039     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7040                              NULL,
7041                              /*parenthesized_p=*/NULL,
7042                              /*member_p=*/false);
7043   if (declarator == cp_error_declarator)
7044     {
7045       cp_parser_abort_tentative_parse (parser);
7046       return false;
7047     }
7048
7049   token = cp_lexer_peek_token (parser->lexer);
7050
7051   node = token->u.value; 
7052   if (node && TREE_CODE (node) == IDENTIFIER_NODE
7053       && node == ridpointers [(int) RID_IN])
7054     {   
7055       enum cpp_ttype nt = cp_lexer_peek_nth_token (parser->lexer, 2)->type;
7056       switch (nt)
7057          {
7058            case CPP_NAME:
7059            case CPP_OPEN_PAREN:
7060            case CPP_MULT:
7061            case CPP_PLUS: case CPP_PLUS_PLUS:
7062            case CPP_MINUS: case CPP_MINUS_MINUS:
7063            case CPP_OPEN_SQUARE:
7064               is_legit_foreach = true;
7065                default:
7066                 break;
7067          }
7068     }
7069   if (is_legit_foreach)
7070     {
7071       tree pushed_scope = NULL;
7072       tree decl;
7073       if (type_spec)
7074         {
7075           /* we have: 'for (type selector in...)' */
7076           cp_parser_commit_to_tentative_parse (parser);
7077            decl = start_decl (declarator, &decl_specs,
7078                               false /*is_initialized*/,
7079                               NULL_TREE /*attributes*/,
7080                               NULL_TREE /*prefix_attributes*/,
7081                               &pushed_scope);
7082            /* APPLE LOCAL begin radar 5130983 */
7083            if (!decl || decl == error_mark_node)
7084              {
7085                error ("selector is undeclared");
7086                is_legit_foreach = false;
7087              }
7088            else
7089              cp_finish_decl (decl,
7090                              NULL_TREE /*initializer*/,
7091                             false /*init_const_expr_p=*/,
7092                              NULL_TREE /*asm_specification*/,
7093                              0 /*flags */);
7094         }
7095       else {
7096          tree statement;
7097          /* we have: 'for (selector in...)' */
7098          /* Parse it as an expression. */
7099          cp_parser_abort_tentative_parse (parser);
7100          statement = cp_parser_expression (parser, /*cast_p=*/false);
7101          add_stmt (statement);
7102       }
7103       /* APPLE LOCAL end radar 5130983 */
7104       /* Consume the 'in' token */
7105       cp_lexer_consume_token (parser->lexer);
7106     }
7107   else
7108     cp_parser_abort_tentative_parse (parser);
7109   return is_legit_foreach;
7110 }
7111 /* APPLE LOCAL end radar 4631818 */
7112
7113 /* Parse an iteration-statement.
7114
7115    iteration-statement:
7116      for ( for-init-statement condition [opt] ; expression [opt] )
7117        statement
7118
7119    APPLE LOCAL begin for-fsf-4_4 3274130 5295549
7120    GNU extension:
7121
7122      while attributes [opt] ( condition ) statement
7123      do attributes [opt] statement while ( expression ) ;
7124      for attributes [opt] 
7125        ( for-init-statement condition [opt] ; expression [opt] )
7126        statement
7127
7128    APPLE LOCAL end for-fsf-4_4 3274130 5295549
7129    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7130
7131 static tree
7132 cp_parser_iteration_statement (cp_parser* parser)
7133 {
7134   cp_token *token;
7135   enum rid keyword;
7136 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7137   tree statement, attributes;
7138 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7139   unsigned char in_statement;
7140
7141 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7142   /* Get the keyword at the start of the loop.  */
7143 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7144   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7145   if (!token)
7146     return error_mark_node;
7147
7148   /* Remember whether or not we are already within an iteration
7149      statement.  */
7150   in_statement = parser->in_statement;
7151
7152 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7153   /* Parse the attributes, if any.  */
7154   attributes = cp_parser_attributes_opt (parser);
7155
7156 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7157   /* See what kind of keyword it is.  */
7158   keyword = token->keyword;
7159   switch (keyword)
7160     {
7161     case RID_WHILE:
7162       {
7163         tree condition;
7164
7165         /* Begin the while-statement.  */
7166 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7167         statement = begin_while_stmt (attributes);
7168 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7169         /* Look for the `('.  */
7170         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7171         /* Parse the condition.  */
7172         condition = cp_parser_condition (parser);
7173         finish_while_stmt_cond (condition, statement);
7174         /* Look for the `)'.  */
7175         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7176         /* Parse the dependent statement.  */
7177         parser->in_statement = IN_ITERATION_STMT;
7178         cp_parser_already_scoped_statement (parser);
7179         parser->in_statement = in_statement;
7180         /* We're done with the while-statement.  */
7181         finish_while_stmt (statement);
7182       }
7183       break;
7184
7185     case RID_DO:
7186       {
7187         tree expression;
7188
7189         /* Begin the do-statement.  */
7190 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7191         statement = begin_do_stmt (attributes);
7192 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7193         /* Parse the body of the do-statement.  */
7194         parser->in_statement = IN_ITERATION_STMT;
7195         cp_parser_implicitly_scoped_statement (parser, NULL);
7196         parser->in_statement = in_statement;
7197         finish_do_body (statement);
7198         /* Look for the `while' keyword.  */
7199         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7200         /* Look for the `('.  */
7201         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7202         /* Parse the expression.  */
7203         expression = cp_parser_expression (parser, /*cast_p=*/false);
7204         /* We're done with the do-statement.  */
7205         finish_do_stmt (expression, statement);
7206         /* Look for the `)'.  */
7207         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7208         /* Look for the `;'.  */
7209         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7210       }
7211       break;
7212
7213     case RID_FOR:
7214       {
7215         tree condition = NULL_TREE;
7216         tree expression = NULL_TREE;
7217
7218         /* Begin the for-statement.  */
7219 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7220         statement = begin_for_stmt (attributes);
7221 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7222         /* Look for the `('.  */
7223         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7224         /* Parse the initialization.  */
7225         cp_parser_for_init_statement (parser);
7226         finish_for_init_stmt (statement);
7227
7228         /* If there's a condition, process it.  */
7229         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7230           condition = cp_parser_condition (parser);
7231         finish_for_cond (condition, statement);
7232         /* Look for the `;'.  */
7233         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7234
7235         /* If there's an expression, process it.  */
7236         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7237           expression = cp_parser_expression (parser, /*cast_p=*/false);
7238         finish_for_expr (expression, statement);
7239         /* Look for the `)'.  */
7240         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7241
7242         /* Parse the body of the for-statement.  */
7243         parser->in_statement = IN_ITERATION_STMT;
7244         cp_parser_already_scoped_statement (parser);
7245         parser->in_statement = in_statement;
7246
7247         /* We're done with the for-statement.  */
7248         finish_for_stmt (statement);
7249       }
7250       break;
7251
7252     default:
7253       cp_parser_error (parser, "expected iteration-statement");
7254       statement = error_mark_node;
7255       break;
7256     }
7257
7258   return statement;
7259 }
7260
7261 /* Parse a for-init-statement.
7262
7263    for-init-statement:
7264      expression-statement
7265      simple-declaration  */
7266
7267 static void
7268 cp_parser_for_init_statement (cp_parser* parser)
7269 {
7270   /* If the next token is a `;', then we have an empty
7271      expression-statement.  Grammatically, this is also a
7272      simple-declaration, but an invalid one, because it does not
7273      declare anything.  Therefore, if we did not handle this case
7274      specially, we would issue an error message about an invalid
7275      declaration.  */
7276   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7277     {
7278       /* We're going to speculatively look for a declaration, falling back
7279          to an expression, if necessary.  */
7280       cp_parser_parse_tentatively (parser);
7281       /* Parse the declaration.  */
7282       cp_parser_simple_declaration (parser,
7283                                     /*function_definition_allowed_p=*/false);
7284       /* If the tentative parse failed, then we shall need to look for an
7285          expression-statement.  */
7286       if (cp_parser_parse_definitely (parser))
7287         return;
7288     }
7289
7290   cp_parser_expression_statement (parser, false);
7291 }
7292
7293 /* Parse a jump-statement.
7294
7295    jump-statement:
7296      break ;
7297      continue ;
7298      return expression [opt] ;
7299      goto identifier ;
7300
7301    GNU extension:
7302
7303    jump-statement:
7304      goto * expression ;
7305
7306    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7307
7308 static tree
7309 cp_parser_jump_statement (cp_parser* parser)
7310 {
7311   tree statement = error_mark_node;
7312   cp_token *token;
7313   enum rid keyword;
7314
7315   /* Peek at the next token.  */
7316   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7317   if (!token)
7318     return error_mark_node;
7319
7320   /* See what kind of keyword it is.  */
7321   keyword = token->keyword;
7322   switch (keyword)
7323     {
7324     case RID_BREAK:
7325       switch (parser->in_statement)
7326         {
7327         case 0:
7328           error ("break statement not within loop or switch");
7329           break;
7330         default:
7331           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
7332                       || parser->in_statement == IN_ITERATION_STMT);
7333           statement = finish_break_stmt ();
7334           break;
7335         case IN_OMP_BLOCK:
7336           error ("invalid exit from OpenMP structured block");
7337           break;
7338         case IN_OMP_FOR:
7339           error ("break statement used with OpenMP for loop");
7340           break;
7341         }
7342       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7343       break;
7344
7345     case RID_CONTINUE:
7346       switch (parser->in_statement & ~IN_SWITCH_STMT)
7347         {
7348         case 0:
7349           error ("continue statement not within a loop");
7350           break;
7351         case IN_ITERATION_STMT:
7352         case IN_OMP_FOR:
7353           statement = finish_continue_stmt ();
7354           break;
7355         case IN_OMP_BLOCK:
7356           error ("invalid exit from OpenMP structured block");
7357           break;
7358         default:
7359           gcc_unreachable ();
7360         }
7361       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7362       break;
7363
7364     case RID_RETURN:
7365       {
7366         tree expr;
7367
7368         /* If the next token is a `;', then there is no
7369            expression.  */
7370         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7371           expr = cp_parser_expression (parser, /*cast_p=*/false);
7372         else
7373           expr = NULL_TREE;
7374         /* Build the return-statement.  */
7375         statement = finish_return_stmt (expr);
7376         /* Look for the final `;'.  */
7377         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7378       }
7379       break;
7380
7381     case RID_GOTO:
7382       /* APPLE LOCAL begin blocks 6040305 (cb) */
7383       if (cur_block)
7384         error ("goto not allowed in block literal");
7385       /* APPLE LOCAL end blocks 6040305 (cb) */
7386       /* Create the goto-statement.  */
7387       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7388         {
7389           /* Issue a warning about this use of a GNU extension.  */
7390           if (pedantic)
7391             pedwarn ("ISO C++ forbids computed gotos");
7392           /* Consume the '*' token.  */
7393           cp_lexer_consume_token (parser->lexer);
7394           /* Parse the dependent expression.  */
7395           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7396         }
7397       else
7398         finish_goto_stmt (cp_parser_identifier (parser));
7399       /* Look for the final `;'.  */
7400       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7401       break;
7402
7403     default:
7404       cp_parser_error (parser, "expected jump-statement");
7405       break;
7406     }
7407
7408   return statement;
7409 }
7410
7411 /* Parse a declaration-statement.
7412
7413    declaration-statement:
7414      block-declaration  */
7415
7416 static void
7417 cp_parser_declaration_statement (cp_parser* parser)
7418 {
7419   void *p;
7420
7421   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7422   p = obstack_alloc (&declarator_obstack, 0);
7423
7424  /* Parse the block-declaration.  */
7425   cp_parser_block_declaration (parser, /*statement_p=*/true);
7426
7427   /* Free any declarators allocated.  */
7428   obstack_free (&declarator_obstack, p);
7429
7430   /* Finish off the statement.  */
7431   finish_stmt ();
7432 }
7433
7434 /* Some dependent statements (like `if (cond) statement'), are
7435    implicitly in their own scope.  In other words, if the statement is
7436    a single statement (as opposed to a compound-statement), it is
7437    none-the-less treated as if it were enclosed in braces.  Any
7438    declarations appearing in the dependent statement are out of scope
7439    after control passes that point.  This function parses a statement,
7440    but ensures that is in its own scope, even if it is not a
7441    compound-statement.
7442
7443    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7444    is a (possibly labeled) if statement which is not enclosed in
7445    braces and has an else clause.  This is used to implement
7446    -Wparentheses.
7447
7448    Returns the new statement.  */
7449
7450 static tree
7451 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7452 {
7453   tree statement;
7454
7455   if (if_p != NULL)
7456     *if_p = false;
7457
7458   /* Mark if () ; with a special NOP_EXPR.  */
7459   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7460     {
7461       cp_lexer_consume_token (parser->lexer);
7462       statement = add_stmt (build_empty_stmt ());
7463     }
7464   /* if a compound is opened, we simply parse the statement directly.  */
7465   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7466     /* APPLE LOCAL radar 5982990 */
7467     statement = cp_parser_compound_statement (parser, NULL, false, false);
7468   /* If the token is not a `{', then we must take special action.  */
7469   else
7470     {
7471       /* Create a compound-statement.  */
7472       statement = begin_compound_stmt (0);
7473       /* Parse the dependent-statement.  */
7474       cp_parser_statement (parser, NULL_TREE, false, if_p);
7475       /* Finish the dummy compound-statement.  */
7476       finish_compound_stmt (statement);
7477     }
7478
7479   /* Return the statement.  */
7480   return statement;
7481 }
7482
7483 /* For some dependent statements (like `while (cond) statement'), we
7484    have already created a scope.  Therefore, even if the dependent
7485    statement is a compound-statement, we do not want to create another
7486    scope.  */
7487
7488 static void
7489 cp_parser_already_scoped_statement (cp_parser* parser)
7490 {
7491   /* If the token is a `{', then we must take special action.  */
7492   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7493     cp_parser_statement (parser, NULL_TREE, false, NULL);
7494   else
7495     {
7496       /* Avoid calling cp_parser_compound_statement, so that we
7497          don't create a new scope.  Do everything else by hand.  */
7498       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7499       cp_parser_statement_seq_opt (parser, NULL_TREE);
7500       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7501     }
7502 }
7503
7504 /* Declarations [gram.dcl.dcl] */
7505
7506 /* Parse an optional declaration-sequence.
7507
7508    declaration-seq:
7509      declaration
7510      declaration-seq declaration  */
7511
7512 static void
7513 cp_parser_declaration_seq_opt (cp_parser* parser)
7514 {
7515   while (true)
7516     {
7517       cp_token *token;
7518
7519       token = cp_lexer_peek_token (parser->lexer);
7520
7521       if (token->type == CPP_CLOSE_BRACE
7522           || token->type == CPP_EOF
7523           || token->type == CPP_PRAGMA_EOL)
7524         break;
7525
7526       if (token->type == CPP_SEMICOLON)
7527         {
7528           /* A declaration consisting of a single semicolon is
7529              invalid.  Allow it unless we're being pedantic.  */
7530           cp_lexer_consume_token (parser->lexer);
7531           if (pedantic && !in_system_header)
7532             pedwarn ("extra %<;%>");
7533           continue;
7534         }
7535
7536       /* If we're entering or exiting a region that's implicitly
7537          extern "C", modify the lang context appropriately.  */
7538       if (!parser->implicit_extern_c && token->implicit_extern_c)
7539         {
7540           push_lang_context (lang_name_c);
7541           parser->implicit_extern_c = true;
7542         }
7543       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7544         {
7545           pop_lang_context ();
7546           parser->implicit_extern_c = false;
7547         }
7548
7549       if (token->type == CPP_PRAGMA)
7550         {
7551           /* A top-level declaration can consist solely of a #pragma.
7552              A nested declaration cannot, so this is done here and not
7553              in cp_parser_declaration.  (A #pragma at block scope is
7554              handled in cp_parser_statement.)  */
7555           cp_parser_pragma (parser, pragma_external);
7556           continue;
7557         }
7558
7559       /* Parse the declaration itself.  */
7560       cp_parser_declaration (parser);
7561     }
7562 }
7563
7564 /* Parse a declaration.
7565
7566    declaration:
7567      block-declaration
7568      function-definition
7569      template-declaration
7570      explicit-instantiation
7571      explicit-specialization
7572      linkage-specification
7573      namespace-definition
7574
7575    GNU extension:
7576
7577    declaration:
7578       __extension__ declaration */
7579
7580 static void
7581 cp_parser_declaration (cp_parser* parser)
7582 {
7583   cp_token token1;
7584   cp_token token2;
7585   int saved_pedantic;
7586   void *p;
7587
7588   /* Check for the `__extension__' keyword.  */
7589   if (cp_parser_extension_opt (parser, &saved_pedantic))
7590     {
7591       /* Parse the qualified declaration.  */
7592       cp_parser_declaration (parser);
7593       /* Restore the PEDANTIC flag.  */
7594       pedantic = saved_pedantic;
7595
7596       return;
7597     }
7598
7599   /* Try to figure out what kind of declaration is present.  */
7600   token1 = *cp_lexer_peek_token (parser->lexer);
7601
7602   if (token1.type != CPP_EOF)
7603     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7604   else
7605     {
7606       token2.type = CPP_EOF;
7607       token2.keyword = RID_MAX;
7608     }
7609
7610   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7611   p = obstack_alloc (&declarator_obstack, 0);
7612
7613   /* If the next token is `extern' and the following token is a string
7614      literal, then we have a linkage specification.  */
7615   if (token1.keyword == RID_EXTERN
7616       && cp_parser_is_string_literal (&token2))
7617     cp_parser_linkage_specification (parser);
7618   /* If the next token is `template', then we have either a template
7619      declaration, an explicit instantiation, or an explicit
7620      specialization.  */
7621   else if (token1.keyword == RID_TEMPLATE)
7622     {
7623       /* `template <>' indicates a template specialization.  */
7624       if (token2.type == CPP_LESS
7625           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7626         cp_parser_explicit_specialization (parser);
7627       /* `template <' indicates a template declaration.  */
7628       else if (token2.type == CPP_LESS)
7629         cp_parser_template_declaration (parser, /*member_p=*/false);
7630       /* Anything else must be an explicit instantiation.  */
7631       else
7632         cp_parser_explicit_instantiation (parser);
7633     }
7634   /* If the next token is `export', then we have a template
7635      declaration.  */
7636   else if (token1.keyword == RID_EXPORT)
7637     cp_parser_template_declaration (parser, /*member_p=*/false);
7638   /* If the next token is `extern', 'static' or 'inline' and the one
7639      after that is `template', we have a GNU extended explicit
7640      instantiation directive.  */
7641   else if (cp_parser_allow_gnu_extensions_p (parser)
7642            && (token1.keyword == RID_EXTERN
7643                || token1.keyword == RID_STATIC
7644                || token1.keyword == RID_INLINE)
7645            && token2.keyword == RID_TEMPLATE)
7646     cp_parser_explicit_instantiation (parser);
7647   /* If the next token is `namespace', check for a named or unnamed
7648      namespace definition.  */
7649   else if (token1.keyword == RID_NAMESPACE
7650            && (/* A named namespace definition.  */
7651                (token2.type == CPP_NAME
7652                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7653                     != CPP_EQ))
7654                /* An unnamed namespace definition.  */
7655                || token2.type == CPP_OPEN_BRACE
7656                || token2.keyword == RID_ATTRIBUTE))
7657     cp_parser_namespace_definition (parser);
7658   /* Objective-C++ declaration/definition.  */
7659   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7660     cp_parser_objc_declaration (parser);
7661   /* We must have either a block declaration or a function
7662      definition.  */
7663   else
7664     /* Try to parse a block-declaration, or a function-definition.  */
7665     cp_parser_block_declaration (parser, /*statement_p=*/false);
7666
7667   /* Free any declarators allocated.  */
7668   obstack_free (&declarator_obstack, p);
7669 }
7670
7671 /* Parse a block-declaration.
7672
7673    block-declaration:
7674      simple-declaration
7675      asm-definition
7676      namespace-alias-definition
7677      using-declaration
7678      using-directive
7679
7680    GNU Extension:
7681
7682    block-declaration:
7683      __extension__ block-declaration
7684      label-declaration
7685
7686    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7687    part of a declaration-statement.  */
7688
7689 static void
7690 cp_parser_block_declaration (cp_parser *parser,
7691                              bool      statement_p)
7692 {
7693   cp_token *token1;
7694   int saved_pedantic;
7695
7696   /* Check for the `__extension__' keyword.  */
7697   if (cp_parser_extension_opt (parser, &saved_pedantic))
7698     {
7699       /* Parse the qualified declaration.  */
7700       cp_parser_block_declaration (parser, statement_p);
7701       /* Restore the PEDANTIC flag.  */
7702       pedantic = saved_pedantic;
7703
7704       return;
7705     }
7706
7707   /* Peek at the next token to figure out which kind of declaration is
7708      present.  */
7709   token1 = cp_lexer_peek_token (parser->lexer);
7710
7711   /* If the next keyword is `asm', we have an asm-definition.  */
7712   if (token1->keyword == RID_ASM)
7713     {
7714       if (statement_p)
7715         cp_parser_commit_to_tentative_parse (parser);
7716       cp_parser_asm_definition (parser);
7717     }
7718   /* If the next keyword is `namespace', we have a
7719      namespace-alias-definition.  */
7720   else if (token1->keyword == RID_NAMESPACE)
7721     cp_parser_namespace_alias_definition (parser);
7722   /* If the next keyword is `using', we have either a
7723      using-declaration or a using-directive.  */
7724   else if (token1->keyword == RID_USING)
7725     {
7726       cp_token *token2;
7727
7728       if (statement_p)
7729         cp_parser_commit_to_tentative_parse (parser);
7730       /* If the token after `using' is `namespace', then we have a
7731          using-directive.  */
7732       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7733       if (token2->keyword == RID_NAMESPACE)
7734         cp_parser_using_directive (parser);
7735       /* Otherwise, it's a using-declaration.  */
7736       else
7737         cp_parser_using_declaration (parser,
7738                                      /*access_declaration_p=*/false);
7739     }
7740   /* If the next keyword is `__label__' we have a label declaration.  */
7741   else if (token1->keyword == RID_LABEL)
7742     {
7743       if (statement_p)
7744         cp_parser_commit_to_tentative_parse (parser);
7745       cp_parser_label_declaration (parser);
7746     }
7747   /* Anything else must be a simple-declaration.  */
7748   else
7749     cp_parser_simple_declaration (parser, !statement_p);
7750 }
7751
7752 /* Parse a simple-declaration.
7753
7754    simple-declaration:
7755      decl-specifier-seq [opt] init-declarator-list [opt] ;
7756
7757    init-declarator-list:
7758      init-declarator
7759      init-declarator-list , init-declarator
7760
7761    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7762    function-definition as a simple-declaration.  */
7763
7764 static void
7765 cp_parser_simple_declaration (cp_parser* parser,
7766                               bool function_definition_allowed_p)
7767 {
7768   cp_decl_specifier_seq decl_specifiers;
7769   int declares_class_or_enum;
7770   bool saw_declarator;
7771
7772   /* Defer access checks until we know what is being declared; the
7773      checks for names appearing in the decl-specifier-seq should be
7774      done as if we were in the scope of the thing being declared.  */
7775   push_deferring_access_checks (dk_deferred);
7776
7777   /* Parse the decl-specifier-seq.  We have to keep track of whether
7778      or not the decl-specifier-seq declares a named class or
7779      enumeration type, since that is the only case in which the
7780      init-declarator-list is allowed to be empty.
7781
7782      [dcl.dcl]
7783
7784      In a simple-declaration, the optional init-declarator-list can be
7785      omitted only when declaring a class or enumeration, that is when
7786      the decl-specifier-seq contains either a class-specifier, an
7787      elaborated-type-specifier, or an enum-specifier.  */
7788   cp_parser_decl_specifier_seq (parser,
7789                                 CP_PARSER_FLAGS_OPTIONAL,
7790                                 &decl_specifiers,
7791                                 &declares_class_or_enum);
7792   /* We no longer need to defer access checks.  */
7793   stop_deferring_access_checks ();
7794
7795   /* In a block scope, a valid declaration must always have a
7796      decl-specifier-seq.  By not trying to parse declarators, we can
7797      resolve the declaration/expression ambiguity more quickly.  */
7798   if (!function_definition_allowed_p
7799       && !decl_specifiers.any_specifiers_p)
7800     {
7801       cp_parser_error (parser, "expected declaration");
7802       goto done;
7803     }
7804
7805   /* If the next two tokens are both identifiers, the code is
7806      erroneous. The usual cause of this situation is code like:
7807
7808        T t;
7809
7810      where "T" should name a type -- but does not.  */
7811   if (!decl_specifiers.type
7812       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7813     {
7814       /* If parsing tentatively, we should commit; we really are
7815          looking at a declaration.  */
7816       cp_parser_commit_to_tentative_parse (parser);
7817       /* Give up.  */
7818       goto done;
7819     }
7820
7821   /* If we have seen at least one decl-specifier, and the next token
7822      is not a parenthesis, then we must be looking at a declaration.
7823      (After "int (" we might be looking at a functional cast.)  */
7824   if (decl_specifiers.any_specifiers_p
7825       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7826     cp_parser_commit_to_tentative_parse (parser);
7827
7828   /* Keep going until we hit the `;' at the end of the simple
7829      declaration.  */
7830   saw_declarator = false;
7831   while (cp_lexer_next_token_is_not (parser->lexer,
7832                                      CPP_SEMICOLON))
7833     {
7834       cp_token *token;
7835       bool function_definition_p;
7836       tree decl;
7837
7838       if (saw_declarator)
7839         {
7840           /* If we are processing next declarator, coma is expected */
7841           token = cp_lexer_peek_token (parser->lexer);
7842           gcc_assert (token->type == CPP_COMMA);
7843           cp_lexer_consume_token (parser->lexer);
7844         }
7845       else
7846         saw_declarator = true;
7847
7848       /* Parse the init-declarator.  */
7849       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7850                                         /*checks=*/NULL,
7851                                         function_definition_allowed_p,
7852                                         /*member_p=*/false,
7853                                         declares_class_or_enum,
7854                                         &function_definition_p);
7855       /* If an error occurred while parsing tentatively, exit quickly.
7856          (That usually happens when in the body of a function; each
7857          statement is treated as a declaration-statement until proven
7858          otherwise.)  */
7859       if (cp_parser_error_occurred (parser))
7860         goto done;
7861       /* Handle function definitions specially.  */
7862       if (function_definition_p)
7863         {
7864           /* If the next token is a `,', then we are probably
7865              processing something like:
7866
7867                void f() {}, *p;
7868
7869              which is erroneous.  */
7870           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7871             error ("mixing declarations and function-definitions is forbidden");
7872           /* Otherwise, we're done with the list of declarators.  */
7873           else
7874             {
7875               pop_deferring_access_checks ();
7876               return;
7877             }
7878         }
7879       /* The next token should be either a `,' or a `;'.  */
7880       token = cp_lexer_peek_token (parser->lexer);
7881       /* If it's a `,', there are more declarators to come.  */
7882       if (token->type == CPP_COMMA)
7883         /* will be consumed next time around */;
7884       /* If it's a `;', we are done.  */
7885       else if (token->type == CPP_SEMICOLON)
7886         break;
7887       /* Anything else is an error.  */
7888       else
7889         {
7890           /* If we have already issued an error message we don't need
7891              to issue another one.  */
7892           if (decl != error_mark_node
7893               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7894             cp_parser_error (parser, "expected %<,%> or %<;%>");
7895           /* Skip tokens until we reach the end of the statement.  */
7896           cp_parser_skip_to_end_of_statement (parser);
7897           /* If the next token is now a `;', consume it.  */
7898           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7899             cp_lexer_consume_token (parser->lexer);
7900           goto done;
7901         }
7902       /* After the first time around, a function-definition is not
7903          allowed -- even if it was OK at first.  For example:
7904
7905            int i, f() {}
7906
7907          is not valid.  */
7908       function_definition_allowed_p = false;
7909     }
7910
7911   /* Issue an error message if no declarators are present, and the
7912      decl-specifier-seq does not itself declare a class or
7913      enumeration.  */
7914   if (!saw_declarator)
7915     {
7916       if (cp_parser_declares_only_class_p (parser))
7917         shadow_tag (&decl_specifiers);
7918       /* Perform any deferred access checks.  */
7919       perform_deferred_access_checks ();
7920     }
7921
7922   /* Consume the `;'.  */
7923   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7924
7925  done:
7926   pop_deferring_access_checks ();
7927 }
7928
7929 /* Parse a decl-specifier-seq.
7930
7931    decl-specifier-seq:
7932      decl-specifier-seq [opt] decl-specifier
7933
7934    decl-specifier:
7935      storage-class-specifier
7936      type-specifier
7937      function-specifier
7938      friend
7939      typedef
7940
7941    GNU Extension:
7942
7943    decl-specifier:
7944      attributes
7945
7946    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7947
7948    The parser flags FLAGS is used to control type-specifier parsing.
7949
7950    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7951    flags:
7952
7953      1: one of the decl-specifiers is an elaborated-type-specifier
7954         (i.e., a type declaration)
7955      2: one of the decl-specifiers is an enum-specifier or a
7956         class-specifier (i.e., a type definition)
7957
7958    */
7959
7960 static void
7961 cp_parser_decl_specifier_seq (cp_parser* parser,
7962                               cp_parser_flags flags,
7963                               cp_decl_specifier_seq *decl_specs,
7964                               int* declares_class_or_enum)
7965 {
7966   bool constructor_possible_p = !parser->in_declarator_p;
7967
7968   /* Clear DECL_SPECS.  */
7969   clear_decl_specs (decl_specs);
7970
7971   /* Assume no class or enumeration type is declared.  */
7972   *declares_class_or_enum = 0;
7973
7974   /* Keep reading specifiers until there are no more to read.  */
7975   while (true)
7976     {
7977       bool constructor_p;
7978       bool found_decl_spec;
7979       cp_token *token;
7980
7981       /* Peek at the next token.  */
7982       token = cp_lexer_peek_token (parser->lexer);
7983       /* Handle attributes.  */
7984       if (token->keyword == RID_ATTRIBUTE)
7985         {
7986           /* Parse the attributes.  */
7987           decl_specs->attributes
7988             = chainon (decl_specs->attributes,
7989                        cp_parser_attributes_opt (parser));
7990           continue;
7991         }
7992       /* Assume we will find a decl-specifier keyword.  */
7993       found_decl_spec = true;
7994       /* If the next token is an appropriate keyword, we can simply
7995          add it to the list.  */
7996       switch (token->keyword)
7997         {
7998           /* decl-specifier:
7999                friend  */
8000         case RID_FRIEND:
8001           if (!at_class_scope_p ())
8002             {
8003               error ("%<friend%> used outside of class");
8004               cp_lexer_purge_token (parser->lexer);
8005             }
8006           else
8007             {
8008               ++decl_specs->specs[(int) ds_friend];
8009               /* Consume the token.  */
8010               cp_lexer_consume_token (parser->lexer);
8011             }
8012           break;
8013
8014           /* function-specifier:
8015                inline
8016                virtual
8017                explicit  */
8018         case RID_INLINE:
8019         case RID_VIRTUAL:
8020         case RID_EXPLICIT:
8021           cp_parser_function_specifier_opt (parser, decl_specs);
8022           break;
8023
8024           /* decl-specifier:
8025                typedef  */
8026         case RID_TYPEDEF:
8027           ++decl_specs->specs[(int) ds_typedef];
8028           /* Consume the token.  */
8029           cp_lexer_consume_token (parser->lexer);
8030           /* A constructor declarator cannot appear in a typedef.  */
8031           constructor_possible_p = false;
8032           /* The "typedef" keyword can only occur in a declaration; we
8033              may as well commit at this point.  */
8034           cp_parser_commit_to_tentative_parse (parser);
8035
8036           if (decl_specs->storage_class != sc_none)
8037             decl_specs->conflicting_specifiers_p = true;
8038           break;
8039
8040           /* storage-class-specifier:
8041                auto
8042                register
8043                static
8044                extern
8045                mutable
8046
8047              GNU Extension:
8048                thread  */
8049         case RID_AUTO:
8050         case RID_REGISTER:
8051         case RID_STATIC:
8052         case RID_EXTERN:
8053         case RID_MUTABLE:
8054           /* Consume the token.  */
8055           cp_lexer_consume_token (parser->lexer);
8056           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8057           break;
8058         case RID_THREAD:
8059           /* Consume the token.  */
8060           cp_lexer_consume_token (parser->lexer);
8061           ++decl_specs->specs[(int) ds_thread];
8062           break;
8063
8064         default:
8065           /* We did not yet find a decl-specifier yet.  */
8066           found_decl_spec = false;
8067           break;
8068         }
8069
8070       /* Constructors are a special case.  The `S' in `S()' is not a
8071          decl-specifier; it is the beginning of the declarator.  */
8072       constructor_p
8073         = (!found_decl_spec
8074            && constructor_possible_p
8075            && (cp_parser_constructor_declarator_p
8076                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8077
8078       /* If we don't have a DECL_SPEC yet, then we must be looking at
8079          a type-specifier.  */
8080       if (!found_decl_spec && !constructor_p)
8081         {
8082           int decl_spec_declares_class_or_enum;
8083           bool is_cv_qualifier;
8084           tree type_spec;
8085
8086           type_spec
8087             = cp_parser_type_specifier (parser, flags,
8088                                         decl_specs,
8089                                         /*is_declaration=*/true,
8090                                         &decl_spec_declares_class_or_enum,
8091                                         &is_cv_qualifier);
8092
8093           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8094
8095           /* If this type-specifier referenced a user-defined type
8096              (a typedef, class-name, etc.), then we can't allow any
8097              more such type-specifiers henceforth.
8098
8099              [dcl.spec]
8100
8101              The longest sequence of decl-specifiers that could
8102              possibly be a type name is taken as the
8103              decl-specifier-seq of a declaration.  The sequence shall
8104              be self-consistent as described below.
8105
8106              [dcl.type]
8107
8108              As a general rule, at most one type-specifier is allowed
8109              in the complete decl-specifier-seq of a declaration.  The
8110              only exceptions are the following:
8111
8112              -- const or volatile can be combined with any other
8113                 type-specifier.
8114
8115              -- signed or unsigned can be combined with char, long,
8116                 short, or int.
8117
8118              -- ..
8119
8120              Example:
8121
8122                typedef char* Pc;
8123                void g (const int Pc);
8124
8125              Here, Pc is *not* part of the decl-specifier seq; it's
8126              the declarator.  Therefore, once we see a type-specifier
8127              (other than a cv-qualifier), we forbid any additional
8128              user-defined types.  We *do* still allow things like `int
8129              int' to be considered a decl-specifier-seq, and issue the
8130              error message later.  */
8131           if (type_spec && !is_cv_qualifier)
8132             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8133           /* A constructor declarator cannot follow a type-specifier.  */
8134           if (type_spec)
8135             {
8136               constructor_possible_p = false;
8137               found_decl_spec = true;
8138             }
8139         }
8140
8141       /* If we still do not have a DECL_SPEC, then there are no more
8142          decl-specifiers.  */
8143       if (!found_decl_spec)
8144         break;
8145
8146       decl_specs->any_specifiers_p = true;
8147       /* After we see one decl-specifier, further decl-specifiers are
8148          always optional.  */
8149       flags |= CP_PARSER_FLAGS_OPTIONAL;
8150     }
8151
8152   cp_parser_check_decl_spec (decl_specs);
8153
8154   /* Don't allow a friend specifier with a class definition.  */
8155   if (decl_specs->specs[(int) ds_friend] != 0
8156       && (*declares_class_or_enum & 2))
8157     error ("class definition may not be declared a friend");
8158 }
8159
8160 /* Parse an (optional) storage-class-specifier.
8161
8162    storage-class-specifier:
8163      auto
8164      register
8165      static
8166      extern
8167      mutable
8168
8169    GNU Extension:
8170
8171    storage-class-specifier:
8172      thread
8173
8174    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8175
8176 static tree
8177 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8178 {
8179   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8180     {
8181     case RID_AUTO:
8182     case RID_REGISTER:
8183     case RID_STATIC:
8184     case RID_EXTERN:
8185     case RID_MUTABLE:
8186     case RID_THREAD:
8187       /* Consume the token.  */
8188       return cp_lexer_consume_token (parser->lexer)->u.value;
8189
8190     default:
8191       return NULL_TREE;
8192     }
8193 }
8194
8195 /* Parse an (optional) function-specifier.
8196
8197    function-specifier:
8198      inline
8199      virtual
8200      explicit
8201
8202    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8203    Updates DECL_SPECS, if it is non-NULL.  */
8204
8205 static tree
8206 cp_parser_function_specifier_opt (cp_parser* parser,
8207                                   cp_decl_specifier_seq *decl_specs)
8208 {
8209   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8210     {
8211     case RID_INLINE:
8212       if (decl_specs)
8213         ++decl_specs->specs[(int) ds_inline];
8214       break;
8215
8216     case RID_VIRTUAL:
8217       /* 14.5.2.3 [temp.mem]
8218
8219          A member function template shall not be virtual.  */
8220       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8221         error ("templates may not be %<virtual%>");
8222       else if (decl_specs)
8223         ++decl_specs->specs[(int) ds_virtual];
8224       break;
8225
8226     case RID_EXPLICIT:
8227       if (decl_specs)
8228         ++decl_specs->specs[(int) ds_explicit];
8229       break;
8230
8231     default:
8232       return NULL_TREE;
8233     }
8234
8235   /* Consume the token.  */
8236   return cp_lexer_consume_token (parser->lexer)->u.value;
8237 }
8238
8239 /* Parse a linkage-specification.
8240
8241    linkage-specification:
8242      extern string-literal { declaration-seq [opt] }
8243      extern string-literal declaration  */
8244
8245 static void
8246 cp_parser_linkage_specification (cp_parser* parser)
8247 {
8248   tree linkage;
8249
8250   /* Look for the `extern' keyword.  */
8251   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8252
8253   /* Look for the string-literal.  */
8254   linkage = cp_parser_string_literal (parser, false, false);
8255
8256   /* Transform the literal into an identifier.  If the literal is a
8257      wide-character string, or contains embedded NULs, then we can't
8258      handle it as the user wants.  */
8259   if (strlen (TREE_STRING_POINTER (linkage))
8260       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8261     {
8262       cp_parser_error (parser, "invalid linkage-specification");
8263       /* Assume C++ linkage.  */
8264       linkage = lang_name_cplusplus;
8265     }
8266   else
8267     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8268
8269   /* We're now using the new linkage.  */
8270   push_lang_context (linkage);
8271
8272   /* If the next token is a `{', then we're using the first
8273      production.  */
8274   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8275     {
8276       /* Consume the `{' token.  */
8277       cp_lexer_consume_token (parser->lexer);
8278       /* Parse the declarations.  */
8279       cp_parser_declaration_seq_opt (parser);
8280       /* Look for the closing `}'.  */
8281       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8282     }
8283   /* Otherwise, there's just one declaration.  */
8284   else
8285     {
8286       bool saved_in_unbraced_linkage_specification_p;
8287
8288       saved_in_unbraced_linkage_specification_p
8289         = parser->in_unbraced_linkage_specification_p;
8290       parser->in_unbraced_linkage_specification_p = true;
8291       cp_parser_declaration (parser);
8292       parser->in_unbraced_linkage_specification_p
8293         = saved_in_unbraced_linkage_specification_p;
8294     }
8295
8296   /* We're done with the linkage-specification.  */
8297   pop_lang_context ();
8298 }
8299
8300 /* Special member functions [gram.special] */
8301
8302 /* Parse a conversion-function-id.
8303
8304    conversion-function-id:
8305      operator conversion-type-id
8306
8307    Returns an IDENTIFIER_NODE representing the operator.  */
8308
8309 static tree
8310 cp_parser_conversion_function_id (cp_parser* parser)
8311 {
8312   tree type;
8313   tree saved_scope;
8314   tree saved_qualifying_scope;
8315   tree saved_object_scope;
8316   tree pushed_scope = NULL_TREE;
8317
8318   /* Look for the `operator' token.  */
8319   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8320     return error_mark_node;
8321   /* When we parse the conversion-type-id, the current scope will be
8322      reset.  However, we need that information in able to look up the
8323      conversion function later, so we save it here.  */
8324   saved_scope = parser->scope;
8325   saved_qualifying_scope = parser->qualifying_scope;
8326   saved_object_scope = parser->object_scope;
8327   /* We must enter the scope of the class so that the names of
8328      entities declared within the class are available in the
8329      conversion-type-id.  For example, consider:
8330
8331        struct S {
8332          typedef int I;
8333          operator I();
8334        };
8335
8336        S::operator I() { ... }
8337
8338      In order to see that `I' is a type-name in the definition, we
8339      must be in the scope of `S'.  */
8340   if (saved_scope)
8341     pushed_scope = push_scope (saved_scope);
8342   /* Parse the conversion-type-id.  */
8343   type = cp_parser_conversion_type_id (parser);
8344   /* Leave the scope of the class, if any.  */
8345   if (pushed_scope)
8346     pop_scope (pushed_scope);
8347   /* Restore the saved scope.  */
8348   parser->scope = saved_scope;
8349   parser->qualifying_scope = saved_qualifying_scope;
8350   parser->object_scope = saved_object_scope;
8351   /* If the TYPE is invalid, indicate failure.  */
8352   if (type == error_mark_node)
8353     return error_mark_node;
8354   return mangle_conv_op_name_for_type (type);
8355 }
8356
8357 /* Parse a conversion-type-id:
8358
8359    conversion-type-id:
8360      type-specifier-seq conversion-declarator [opt]
8361
8362    Returns the TYPE specified.  */
8363
8364 static tree
8365 cp_parser_conversion_type_id (cp_parser* parser)
8366 {
8367   tree attributes;
8368   cp_decl_specifier_seq type_specifiers;
8369   cp_declarator *declarator;
8370   tree type_specified;
8371
8372   /* Parse the attributes.  */
8373   attributes = cp_parser_attributes_opt (parser);
8374   /* Parse the type-specifiers.  */
8375   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8376                                 &type_specifiers);
8377   /* If that didn't work, stop.  */
8378   if (type_specifiers.type == error_mark_node)
8379     return error_mark_node;
8380   /* Parse the conversion-declarator.  */
8381   declarator = cp_parser_conversion_declarator_opt (parser);
8382
8383   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8384                                     /*initialized=*/0, &attributes);
8385   if (attributes)
8386     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8387   return type_specified;
8388 }
8389
8390 /* Parse an (optional) conversion-declarator.
8391
8392    conversion-declarator:
8393      ptr-operator conversion-declarator [opt]
8394
8395    */
8396
8397 static cp_declarator *
8398 cp_parser_conversion_declarator_opt (cp_parser* parser)
8399 {
8400   enum tree_code code;
8401   tree class_type;
8402   cp_cv_quals cv_quals;
8403
8404   /* We don't know if there's a ptr-operator next, or not.  */
8405   cp_parser_parse_tentatively (parser);
8406   /* Try the ptr-operator.  */
8407   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8408   /* If it worked, look for more conversion-declarators.  */
8409   if (cp_parser_parse_definitely (parser))
8410     {
8411       cp_declarator *declarator;
8412
8413       /* Parse another optional declarator.  */
8414       declarator = cp_parser_conversion_declarator_opt (parser);
8415
8416       /* Create the representation of the declarator.  */
8417       if (class_type)
8418         declarator = make_ptrmem_declarator (cv_quals, class_type,
8419                                              declarator);
8420       else if (code == INDIRECT_REF)
8421         declarator = make_pointer_declarator (cv_quals, declarator);
8422       else
8423         declarator = make_reference_declarator (cv_quals, declarator);
8424
8425       return declarator;
8426    }
8427
8428   return NULL;
8429 }
8430
8431 /* Parse an (optional) ctor-initializer.
8432
8433    ctor-initializer:
8434      : mem-initializer-list
8435
8436    Returns TRUE iff the ctor-initializer was actually present.  */
8437
8438 static bool
8439 cp_parser_ctor_initializer_opt (cp_parser* parser)
8440 {
8441   /* If the next token is not a `:', then there is no
8442      ctor-initializer.  */
8443   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8444     {
8445       /* Do default initialization of any bases and members.  */
8446       if (DECL_CONSTRUCTOR_P (current_function_decl))
8447         finish_mem_initializers (NULL_TREE);
8448
8449       return false;
8450     }
8451
8452   /* Consume the `:' token.  */
8453   cp_lexer_consume_token (parser->lexer);
8454   /* And the mem-initializer-list.  */
8455   cp_parser_mem_initializer_list (parser);
8456
8457   return true;
8458 }
8459
8460 /* Parse a mem-initializer-list.
8461
8462    mem-initializer-list:
8463      mem-initializer
8464      mem-initializer , mem-initializer-list  */
8465
8466 static void
8467 cp_parser_mem_initializer_list (cp_parser* parser)
8468 {
8469   tree mem_initializer_list = NULL_TREE;
8470
8471   /* Let the semantic analysis code know that we are starting the
8472      mem-initializer-list.  */
8473   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8474     error ("only constructors take base initializers");
8475
8476   /* Loop through the list.  */
8477   while (true)
8478     {
8479       tree mem_initializer;
8480
8481       /* Parse the mem-initializer.  */
8482       mem_initializer = cp_parser_mem_initializer (parser);
8483       /* Add it to the list, unless it was erroneous.  */
8484       if (mem_initializer != error_mark_node)
8485         {
8486           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8487           mem_initializer_list = mem_initializer;
8488         }
8489       /* If the next token is not a `,', we're done.  */
8490       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8491         break;
8492       /* Consume the `,' token.  */
8493       cp_lexer_consume_token (parser->lexer);
8494     }
8495
8496   /* Perform semantic analysis.  */
8497   if (DECL_CONSTRUCTOR_P (current_function_decl))
8498     finish_mem_initializers (mem_initializer_list);
8499 }
8500
8501 /* Parse a mem-initializer.
8502
8503    mem-initializer:
8504      mem-initializer-id ( expression-list [opt] )
8505
8506    GNU extension:
8507
8508    mem-initializer:
8509      ( expression-list [opt] )
8510
8511    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8512    class) or FIELD_DECL (for a non-static data member) to initialize;
8513    the TREE_VALUE is the expression-list.  An empty initialization
8514    list is represented by void_list_node.  */
8515
8516 static tree
8517 cp_parser_mem_initializer (cp_parser* parser)
8518 {
8519   tree mem_initializer_id;
8520   tree expression_list;
8521   tree member;
8522
8523   /* Find out what is being initialized.  */
8524   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8525     {
8526       pedwarn ("anachronistic old-style base class initializer");
8527       mem_initializer_id = NULL_TREE;
8528     }
8529   else
8530     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8531   member = expand_member_init (mem_initializer_id);
8532   if (member && !DECL_P (member))
8533     in_base_initializer = 1;
8534
8535   expression_list
8536     = cp_parser_parenthesized_expression_list (parser, false,
8537                                                /*cast_p=*/false,
8538                                                /*non_constant_p=*/NULL);
8539   if (expression_list == error_mark_node)
8540     return error_mark_node;
8541   if (!expression_list)
8542     expression_list = void_type_node;
8543
8544   in_base_initializer = 0;
8545
8546   return member ? build_tree_list (member, expression_list) : error_mark_node;
8547 }
8548
8549 /* Parse a mem-initializer-id.
8550
8551    mem-initializer-id:
8552      :: [opt] nested-name-specifier [opt] class-name
8553      identifier
8554
8555    Returns a TYPE indicating the class to be initializer for the first
8556    production.  Returns an IDENTIFIER_NODE indicating the data member
8557    to be initialized for the second production.  */
8558
8559 static tree
8560 cp_parser_mem_initializer_id (cp_parser* parser)
8561 {
8562   bool global_scope_p;
8563   bool nested_name_specifier_p;
8564   bool template_p = false;
8565   tree id;
8566
8567   /* `typename' is not allowed in this context ([temp.res]).  */
8568   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8569     {
8570       error ("keyword %<typename%> not allowed in this context (a qualified "
8571              "member initializer is implicitly a type)");
8572       cp_lexer_consume_token (parser->lexer);
8573     }
8574   /* Look for the optional `::' operator.  */
8575   global_scope_p
8576     = (cp_parser_global_scope_opt (parser,
8577                                    /*current_scope_valid_p=*/false)
8578        != NULL_TREE);
8579   /* Look for the optional nested-name-specifier.  The simplest way to
8580      implement:
8581
8582        [temp.res]
8583
8584        The keyword `typename' is not permitted in a base-specifier or
8585        mem-initializer; in these contexts a qualified name that
8586        depends on a template-parameter is implicitly assumed to be a
8587        type name.
8588
8589      is to assume that we have seen the `typename' keyword at this
8590      point.  */
8591   nested_name_specifier_p
8592     = (cp_parser_nested_name_specifier_opt (parser,
8593                                             /*typename_keyword_p=*/true,
8594                                             /*check_dependency_p=*/true,
8595                                             /*type_p=*/true,
8596                                             /*is_declaration=*/true)
8597        != NULL_TREE);
8598   if (nested_name_specifier_p)
8599     template_p = cp_parser_optional_template_keyword (parser);
8600   /* If there is a `::' operator or a nested-name-specifier, then we
8601      are definitely looking for a class-name.  */
8602   if (global_scope_p || nested_name_specifier_p)
8603     return cp_parser_class_name (parser,
8604                                  /*typename_keyword_p=*/true,
8605                                  /*template_keyword_p=*/template_p,
8606                                  none_type,
8607                                  /*check_dependency_p=*/true,
8608                                  /*class_head_p=*/false,
8609                                  /*is_declaration=*/true);
8610   /* Otherwise, we could also be looking for an ordinary identifier.  */
8611   cp_parser_parse_tentatively (parser);
8612   /* Try a class-name.  */
8613   id = cp_parser_class_name (parser,
8614                              /*typename_keyword_p=*/true,
8615                              /*template_keyword_p=*/false,
8616                              none_type,
8617                              /*check_dependency_p=*/true,
8618                              /*class_head_p=*/false,
8619                              /*is_declaration=*/true);
8620   /* If we found one, we're done.  */
8621   if (cp_parser_parse_definitely (parser))
8622     return id;
8623   /* Otherwise, look for an ordinary identifier.  */
8624   return cp_parser_identifier (parser);
8625 }
8626
8627 /* Overloading [gram.over] */
8628
8629 /* Parse an operator-function-id.
8630
8631    operator-function-id:
8632      operator operator
8633
8634    Returns an IDENTIFIER_NODE for the operator which is a
8635    human-readable spelling of the identifier, e.g., `operator +'.  */
8636
8637 static tree
8638 cp_parser_operator_function_id (cp_parser* parser)
8639 {
8640   /* Look for the `operator' keyword.  */
8641   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8642     return error_mark_node;
8643   /* And then the name of the operator itself.  */
8644   return cp_parser_operator (parser);
8645 }
8646
8647 /* Parse an operator.
8648
8649    operator:
8650      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8651      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8652      || ++ -- , ->* -> () []
8653
8654    GNU Extensions:
8655
8656    operator:
8657      <? >? <?= >?=
8658
8659    Returns an IDENTIFIER_NODE for the operator which is a
8660    human-readable spelling of the identifier, e.g., `operator +'.  */
8661
8662 static tree
8663 cp_parser_operator (cp_parser* parser)
8664 {
8665   tree id = NULL_TREE;
8666   cp_token *token;
8667
8668   /* Peek at the next token.  */
8669   token = cp_lexer_peek_token (parser->lexer);
8670   /* Figure out which operator we have.  */
8671   switch (token->type)
8672     {
8673     case CPP_KEYWORD:
8674       {
8675         enum tree_code op;
8676
8677         /* The keyword should be either `new' or `delete'.  */
8678         if (token->keyword == RID_NEW)
8679           op = NEW_EXPR;
8680         else if (token->keyword == RID_DELETE)
8681           op = DELETE_EXPR;
8682         else
8683           break;
8684
8685         /* Consume the `new' or `delete' token.  */
8686         cp_lexer_consume_token (parser->lexer);
8687
8688         /* Peek at the next token.  */
8689         token = cp_lexer_peek_token (parser->lexer);
8690         /* If it's a `[' token then this is the array variant of the
8691            operator.  */
8692         if (token->type == CPP_OPEN_SQUARE)
8693           {
8694             /* Consume the `[' token.  */
8695             cp_lexer_consume_token (parser->lexer);
8696             /* Look for the `]' token.  */
8697             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8698             id = ansi_opname (op == NEW_EXPR
8699                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8700           }
8701         /* Otherwise, we have the non-array variant.  */
8702         else
8703           id = ansi_opname (op);
8704
8705         return id;
8706       }
8707
8708     case CPP_PLUS:
8709       id = ansi_opname (PLUS_EXPR);
8710       break;
8711
8712     case CPP_MINUS:
8713       id = ansi_opname (MINUS_EXPR);
8714       break;
8715
8716     case CPP_MULT:
8717       id = ansi_opname (MULT_EXPR);
8718       break;
8719
8720     case CPP_DIV:
8721       id = ansi_opname (TRUNC_DIV_EXPR);
8722       break;
8723
8724     case CPP_MOD:
8725       id = ansi_opname (TRUNC_MOD_EXPR);
8726       break;
8727
8728     case CPP_XOR:
8729       id = ansi_opname (BIT_XOR_EXPR);
8730       break;
8731
8732     case CPP_AND:
8733       id = ansi_opname (BIT_AND_EXPR);
8734       break;
8735
8736     case CPP_OR:
8737       id = ansi_opname (BIT_IOR_EXPR);
8738       break;
8739
8740     case CPP_COMPL:
8741       id = ansi_opname (BIT_NOT_EXPR);
8742       break;
8743
8744     case CPP_NOT:
8745       id = ansi_opname (TRUTH_NOT_EXPR);
8746       break;
8747
8748     case CPP_EQ:
8749       id = ansi_assopname (NOP_EXPR);
8750       break;
8751
8752     case CPP_LESS:
8753       id = ansi_opname (LT_EXPR);
8754       break;
8755
8756     case CPP_GREATER:
8757       id = ansi_opname (GT_EXPR);
8758       break;
8759
8760     case CPP_PLUS_EQ:
8761       id = ansi_assopname (PLUS_EXPR);
8762       break;
8763
8764     case CPP_MINUS_EQ:
8765       id = ansi_assopname (MINUS_EXPR);
8766       break;
8767
8768     case CPP_MULT_EQ:
8769       id = ansi_assopname (MULT_EXPR);
8770       break;
8771
8772     case CPP_DIV_EQ:
8773       id = ansi_assopname (TRUNC_DIV_EXPR);
8774       break;
8775
8776     case CPP_MOD_EQ:
8777       id = ansi_assopname (TRUNC_MOD_EXPR);
8778       break;
8779
8780     case CPP_XOR_EQ:
8781       id = ansi_assopname (BIT_XOR_EXPR);
8782       break;
8783
8784     case CPP_AND_EQ:
8785       id = ansi_assopname (BIT_AND_EXPR);
8786       break;
8787
8788     case CPP_OR_EQ:
8789       id = ansi_assopname (BIT_IOR_EXPR);
8790       break;
8791
8792     case CPP_LSHIFT:
8793       id = ansi_opname (LSHIFT_EXPR);
8794       break;
8795
8796     case CPP_RSHIFT:
8797       id = ansi_opname (RSHIFT_EXPR);
8798       break;
8799
8800     case CPP_LSHIFT_EQ:
8801       id = ansi_assopname (LSHIFT_EXPR);
8802       break;
8803
8804     case CPP_RSHIFT_EQ:
8805       id = ansi_assopname (RSHIFT_EXPR);
8806       break;
8807
8808     case CPP_EQ_EQ:
8809       id = ansi_opname (EQ_EXPR);
8810       break;
8811
8812     case CPP_NOT_EQ:
8813       id = ansi_opname (NE_EXPR);
8814       break;
8815
8816     case CPP_LESS_EQ:
8817       id = ansi_opname (LE_EXPR);
8818       break;
8819
8820     case CPP_GREATER_EQ:
8821       id = ansi_opname (GE_EXPR);
8822       break;
8823
8824     case CPP_AND_AND:
8825       id = ansi_opname (TRUTH_ANDIF_EXPR);
8826       break;
8827
8828     case CPP_OR_OR:
8829       id = ansi_opname (TRUTH_ORIF_EXPR);
8830       break;
8831
8832     case CPP_PLUS_PLUS:
8833       id = ansi_opname (POSTINCREMENT_EXPR);
8834       break;
8835
8836     case CPP_MINUS_MINUS:
8837       id = ansi_opname (PREDECREMENT_EXPR);
8838       break;
8839
8840     case CPP_COMMA:
8841       id = ansi_opname (COMPOUND_EXPR);
8842       break;
8843
8844     case CPP_DEREF_STAR:
8845       id = ansi_opname (MEMBER_REF);
8846       break;
8847
8848     case CPP_DEREF:
8849       id = ansi_opname (COMPONENT_REF);
8850       break;
8851
8852     case CPP_OPEN_PAREN:
8853       /* Consume the `('.  */
8854       cp_lexer_consume_token (parser->lexer);
8855       /* Look for the matching `)'.  */
8856       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8857       return ansi_opname (CALL_EXPR);
8858
8859     case CPP_OPEN_SQUARE:
8860       /* Consume the `['.  */
8861       cp_lexer_consume_token (parser->lexer);
8862       /* Look for the matching `]'.  */
8863       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8864       return ansi_opname (ARRAY_REF);
8865
8866     default:
8867       /* Anything else is an error.  */
8868       break;
8869     }
8870
8871   /* If we have selected an identifier, we need to consume the
8872      operator token.  */
8873   if (id)
8874     cp_lexer_consume_token (parser->lexer);
8875   /* Otherwise, no valid operator name was present.  */
8876   else
8877     {
8878       cp_parser_error (parser, "expected operator");
8879       id = error_mark_node;
8880     }
8881
8882   return id;
8883 }
8884
8885 /* Parse a template-declaration.
8886
8887    template-declaration:
8888      export [opt] template < template-parameter-list > declaration
8889
8890    If MEMBER_P is TRUE, this template-declaration occurs within a
8891    class-specifier.
8892
8893    The grammar rule given by the standard isn't correct.  What
8894    is really meant is:
8895
8896    template-declaration:
8897      export [opt] template-parameter-list-seq
8898        decl-specifier-seq [opt] init-declarator [opt] ;
8899      export [opt] template-parameter-list-seq
8900        function-definition
8901
8902    template-parameter-list-seq:
8903      template-parameter-list-seq [opt]
8904      template < template-parameter-list >  */
8905
8906 static void
8907 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8908 {
8909   /* Check for `export'.  */
8910   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8911     {
8912       /* Consume the `export' token.  */
8913       cp_lexer_consume_token (parser->lexer);
8914       /* Warn that we do not support `export'.  */
8915       warning (0, "keyword %<export%> not implemented, and will be ignored");
8916     }
8917
8918   cp_parser_template_declaration_after_export (parser, member_p);
8919 }
8920
8921 /* Parse a template-parameter-list.
8922
8923    template-parameter-list:
8924      template-parameter
8925      template-parameter-list , template-parameter
8926
8927    Returns a TREE_LIST.  Each node represents a template parameter.
8928    The nodes are connected via their TREE_CHAINs.  */
8929
8930 static tree
8931 cp_parser_template_parameter_list (cp_parser* parser)
8932 {
8933   tree parameter_list = NULL_TREE;
8934
8935   begin_template_parm_list ();
8936   while (true)
8937     {
8938       tree parameter;
8939       cp_token *token;
8940       bool is_non_type;
8941
8942       /* Parse the template-parameter.  */
8943       parameter = cp_parser_template_parameter (parser, &is_non_type);
8944       /* Add it to the list.  */
8945       if (parameter != error_mark_node)
8946         parameter_list = process_template_parm (parameter_list,
8947                                                 parameter,
8948                                                 is_non_type);
8949       else
8950        {
8951          tree err_parm = build_tree_list (parameter, parameter);
8952          TREE_VALUE (err_parm) = error_mark_node;
8953          parameter_list = chainon (parameter_list, err_parm);
8954        }
8955
8956       /* Peek at the next token.  */
8957       token = cp_lexer_peek_token (parser->lexer);
8958       /* If it's not a `,', we're done.  */
8959       if (token->type != CPP_COMMA)
8960         break;
8961       /* Otherwise, consume the `,' token.  */
8962       cp_lexer_consume_token (parser->lexer);
8963     }
8964
8965   return end_template_parm_list (parameter_list);
8966 }
8967
8968 /* Parse a template-parameter.
8969
8970    template-parameter:
8971      type-parameter
8972      parameter-declaration
8973
8974    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8975    the parameter.  The TREE_PURPOSE is the default value, if any.
8976    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8977    iff this parameter is a non-type parameter.  */
8978
8979 static tree
8980 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8981 {
8982   cp_token *token;
8983   cp_parameter_declarator *parameter_declarator;
8984   tree parm;
8985
8986   /* Assume it is a type parameter or a template parameter.  */
8987   *is_non_type = false;
8988   /* Peek at the next token.  */
8989   token = cp_lexer_peek_token (parser->lexer);
8990   /* If it is `class' or `template', we have a type-parameter.  */
8991   if (token->keyword == RID_TEMPLATE)
8992     return cp_parser_type_parameter (parser);
8993   /* If it is `class' or `typename' we do not know yet whether it is a
8994      type parameter or a non-type parameter.  Consider:
8995
8996        template <typename T, typename T::X X> ...
8997
8998      or:
8999
9000        template <class C, class D*> ...
9001
9002      Here, the first parameter is a type parameter, and the second is
9003      a non-type parameter.  We can tell by looking at the token after
9004      the identifier -- if it is a `,', `=', or `>' then we have a type
9005      parameter.  */
9006   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9007     {
9008       /* Peek at the token after `class' or `typename'.  */
9009       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9010       /* If it's an identifier, skip it.  */
9011       if (token->type == CPP_NAME)
9012         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9013       /* Now, see if the token looks like the end of a template
9014          parameter.  */
9015       if (token->type == CPP_COMMA
9016           || token->type == CPP_EQ
9017           || token->type == CPP_GREATER)
9018         return cp_parser_type_parameter (parser);
9019     }
9020
9021   /* Otherwise, it is a non-type parameter.
9022
9023      [temp.param]
9024
9025      When parsing a default template-argument for a non-type
9026      template-parameter, the first non-nested `>' is taken as the end
9027      of the template parameter-list rather than a greater-than
9028      operator.  */
9029   *is_non_type = true;
9030   parameter_declarator
9031      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9032                                         /*parenthesized_p=*/NULL);
9033   parm = grokdeclarator (parameter_declarator->declarator,
9034                          &parameter_declarator->decl_specifiers,
9035                          PARM, /*initialized=*/0,
9036                          /*attrlist=*/NULL);
9037   if (parm == error_mark_node)
9038     return error_mark_node;
9039   return build_tree_list (parameter_declarator->default_argument, parm);
9040 }
9041
9042 /* Parse a type-parameter.
9043
9044    type-parameter:
9045      class identifier [opt]
9046      class identifier [opt] = type-id
9047      typename identifier [opt]
9048      typename identifier [opt] = type-id
9049      template < template-parameter-list > class identifier [opt]
9050      template < template-parameter-list > class identifier [opt]
9051        = id-expression
9052
9053    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9054    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9055    the declaration of the parameter.  */
9056
9057 static tree
9058 cp_parser_type_parameter (cp_parser* parser)
9059 {
9060   cp_token *token;
9061   tree parameter;
9062
9063   /* Look for a keyword to tell us what kind of parameter this is.  */
9064   token = cp_parser_require (parser, CPP_KEYWORD,
9065                              "`class', `typename', or `template'");
9066   if (!token)
9067     return error_mark_node;
9068
9069   switch (token->keyword)
9070     {
9071     case RID_CLASS:
9072     case RID_TYPENAME:
9073       {
9074         tree identifier;
9075         tree default_argument;
9076
9077         /* If the next token is an identifier, then it names the
9078            parameter.  */
9079         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9080           identifier = cp_parser_identifier (parser);
9081         else
9082           identifier = NULL_TREE;
9083
9084         /* Create the parameter.  */
9085         parameter = finish_template_type_parm (class_type_node, identifier);
9086
9087         /* If the next token is an `=', we have a default argument.  */
9088         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9089           {
9090             /* Consume the `=' token.  */
9091             cp_lexer_consume_token (parser->lexer);
9092             /* Parse the default-argument.  */
9093             push_deferring_access_checks (dk_no_deferred);
9094             default_argument = cp_parser_type_id (parser);
9095             pop_deferring_access_checks ();
9096           }
9097         else
9098           default_argument = NULL_TREE;
9099
9100         /* Create the combined representation of the parameter and the
9101            default argument.  */
9102         parameter = build_tree_list (default_argument, parameter);
9103       }
9104       break;
9105
9106     case RID_TEMPLATE:
9107       {
9108         tree parameter_list;
9109         tree identifier;
9110         tree default_argument;
9111
9112         /* Look for the `<'.  */
9113         cp_parser_require (parser, CPP_LESS, "`<'");
9114         /* Parse the template-parameter-list.  */
9115         parameter_list = cp_parser_template_parameter_list (parser);
9116         /* Look for the `>'.  */
9117         cp_parser_require (parser, CPP_GREATER, "`>'");
9118         /* Look for the `class' keyword.  */
9119         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9120         /* If the next token is an `=', then there is a
9121            default-argument.  If the next token is a `>', we are at
9122            the end of the parameter-list.  If the next token is a `,',
9123            then we are at the end of this parameter.  */
9124         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9125             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9126             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9127           {
9128             identifier = cp_parser_identifier (parser);
9129             /* Treat invalid names as if the parameter were nameless.  */
9130             if (identifier == error_mark_node)
9131               identifier = NULL_TREE;
9132           }
9133         else
9134           identifier = NULL_TREE;
9135
9136         /* Create the template parameter.  */
9137         parameter = finish_template_template_parm (class_type_node,
9138                                                    identifier);
9139
9140         /* If the next token is an `=', then there is a
9141            default-argument.  */
9142         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9143           {
9144             bool is_template;
9145
9146             /* Consume the `='.  */
9147             cp_lexer_consume_token (parser->lexer);
9148             /* Parse the id-expression.  */
9149             push_deferring_access_checks (dk_no_deferred);
9150             default_argument
9151               = cp_parser_id_expression (parser,
9152                                          /*template_keyword_p=*/false,
9153                                          /*check_dependency_p=*/true,
9154                                          /*template_p=*/&is_template,
9155                                          /*declarator_p=*/false,
9156                                          /*optional_p=*/false);
9157             if (TREE_CODE (default_argument) == TYPE_DECL)
9158               /* If the id-expression was a template-id that refers to
9159                  a template-class, we already have the declaration here,
9160                  so no further lookup is needed.  */
9161                  ;
9162             else
9163               /* Look up the name.  */
9164               default_argument
9165                 = cp_parser_lookup_name (parser, default_argument,
9166                                          none_type,
9167                                          /*is_template=*/is_template,
9168                                          /*is_namespace=*/false,
9169                                          /*check_dependency=*/true,
9170                                          /*ambiguous_decls=*/NULL);
9171             /* See if the default argument is valid.  */
9172             default_argument
9173               = check_template_template_default_arg (default_argument);
9174             pop_deferring_access_checks ();
9175           }
9176         else
9177           default_argument = NULL_TREE;
9178
9179         /* Create the combined representation of the parameter and the
9180            default argument.  */
9181         parameter = build_tree_list (default_argument, parameter);
9182       }
9183       break;
9184
9185     default:
9186       gcc_unreachable ();
9187       break;
9188     }
9189
9190   return parameter;
9191 }
9192
9193 /* Parse a template-id.
9194
9195    template-id:
9196      template-name < template-argument-list [opt] >
9197
9198    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9199    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9200    returned.  Otherwise, if the template-name names a function, or set
9201    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9202    names a class, returns a TYPE_DECL for the specialization.
9203
9204    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9205    uninstantiated templates.  */
9206
9207 static tree
9208 cp_parser_template_id (cp_parser *parser,
9209                        bool template_keyword_p,
9210                        bool check_dependency_p,
9211                        bool is_declaration)
9212 {
9213   int i;
9214   tree template;
9215   tree arguments;
9216   tree template_id;
9217   cp_token_position start_of_id = 0;
9218   deferred_access_check *chk;
9219   VEC (deferred_access_check,gc) *access_check;
9220   cp_token *next_token, *next_token_2;
9221   bool is_identifier;
9222
9223   /* If the next token corresponds to a template-id, there is no need
9224      to reparse it.  */
9225   next_token = cp_lexer_peek_token (parser->lexer);
9226   if (next_token->type == CPP_TEMPLATE_ID)
9227     {
9228       struct tree_check *check_value;
9229
9230       /* Get the stored value.  */
9231       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9232       /* Perform any access checks that were deferred.  */
9233       access_check = check_value->checks;
9234       if (access_check)
9235         {
9236           for (i = 0 ;
9237                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9238                ++i)
9239             {
9240               perform_or_defer_access_check (chk->binfo,
9241                                              chk->decl,
9242                                              chk->diag_decl);
9243             }
9244         }
9245       /* Return the stored value.  */
9246       return check_value->value;
9247     }
9248
9249   /* Avoid performing name lookup if there is no possibility of
9250      finding a template-id.  */
9251   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9252       || (next_token->type == CPP_NAME
9253           && !cp_parser_nth_token_starts_template_argument_list_p
9254                (parser, 2)))
9255     {
9256       cp_parser_error (parser, "expected template-id");
9257       return error_mark_node;
9258     }
9259
9260   /* Remember where the template-id starts.  */
9261   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9262     start_of_id = cp_lexer_token_position (parser->lexer, false);
9263
9264   push_deferring_access_checks (dk_deferred);
9265
9266   /* Parse the template-name.  */
9267   is_identifier = false;
9268   template = cp_parser_template_name (parser, template_keyword_p,
9269                                       check_dependency_p,
9270                                       is_declaration,
9271                                       &is_identifier);
9272   if (template == error_mark_node || is_identifier)
9273     {
9274       pop_deferring_access_checks ();
9275       return template;
9276     }
9277
9278   /* If we find the sequence `[:' after a template-name, it's probably
9279      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9280      parse correctly the argument list.  */
9281   next_token = cp_lexer_peek_token (parser->lexer);
9282   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9283   if (next_token->type == CPP_OPEN_SQUARE
9284       && next_token->flags & DIGRAPH
9285       && next_token_2->type == CPP_COLON
9286       && !(next_token_2->flags & PREV_WHITE))
9287     {
9288       cp_parser_parse_tentatively (parser);
9289       /* Change `:' into `::'.  */
9290       next_token_2->type = CPP_SCOPE;
9291       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9292          CPP_LESS.  */
9293       cp_lexer_consume_token (parser->lexer);
9294       /* Parse the arguments.  */
9295       arguments = cp_parser_enclosed_template_argument_list (parser);
9296       if (!cp_parser_parse_definitely (parser))
9297         {
9298           /* If we couldn't parse an argument list, then we revert our changes
9299              and return simply an error. Maybe this is not a template-id
9300              after all.  */
9301           next_token_2->type = CPP_COLON;
9302           cp_parser_error (parser, "expected %<<%>");
9303           pop_deferring_access_checks ();
9304           return error_mark_node;
9305         }
9306       /* Otherwise, emit an error about the invalid digraph, but continue
9307          parsing because we got our argument list.  */
9308       pedwarn ("%<<::%> cannot begin a template-argument list");
9309       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9310               "between %<<%> and %<::%>");
9311       if (!flag_permissive)
9312         {
9313           static bool hint;
9314           if (!hint)
9315             {
9316               inform ("(if you use -fpermissive G++ will accept your code)");
9317               hint = true;
9318             }
9319         }
9320     }
9321   else
9322     {
9323       /* Look for the `<' that starts the template-argument-list.  */
9324       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9325         {
9326           pop_deferring_access_checks ();
9327           return error_mark_node;
9328         }
9329       /* Parse the arguments.  */
9330       arguments = cp_parser_enclosed_template_argument_list (parser);
9331     }
9332
9333   /* Build a representation of the specialization.  */
9334   if (TREE_CODE (template) == IDENTIFIER_NODE)
9335     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9336   else if (DECL_CLASS_TEMPLATE_P (template)
9337            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9338     {
9339       bool entering_scope;
9340       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9341          template (rather than some instantiation thereof) only if
9342          is not nested within some other construct.  For example, in
9343          "template <typename T> void f(T) { A<T>::", A<T> is just an
9344          instantiation of A.  */
9345       entering_scope = (template_parm_scope_p ()
9346                         && cp_lexer_next_token_is (parser->lexer,
9347                                                    CPP_SCOPE));
9348       template_id
9349         = finish_template_type (template, arguments, entering_scope);
9350     }
9351   else
9352     {
9353       /* If it's not a class-template or a template-template, it should be
9354          a function-template.  */
9355       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9356                    || TREE_CODE (template) == OVERLOAD
9357                    || BASELINK_P (template)));
9358
9359       template_id = lookup_template_function (template, arguments);
9360     }
9361
9362   /* If parsing tentatively, replace the sequence of tokens that makes
9363      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9364      should we re-parse the token stream, we will not have to repeat
9365      the effort required to do the parse, nor will we issue duplicate
9366      error messages about problems during instantiation of the
9367      template.  */
9368   if (start_of_id)
9369     {
9370       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9371
9372       /* Reset the contents of the START_OF_ID token.  */
9373       token->type = CPP_TEMPLATE_ID;
9374       /* Retrieve any deferred checks.  Do not pop this access checks yet
9375          so the memory will not be reclaimed during token replacing below.  */
9376       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9377       token->u.tree_check_value->value = template_id;
9378       token->u.tree_check_value->checks = get_deferred_access_checks ();
9379       token->keyword = RID_MAX;
9380
9381       /* Purge all subsequent tokens.  */
9382       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9383
9384       /* ??? Can we actually assume that, if template_id ==
9385          error_mark_node, we will have issued a diagnostic to the
9386          user, as opposed to simply marking the tentative parse as
9387          failed?  */
9388       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9389         error ("parse error in template argument list");
9390     }
9391
9392   pop_deferring_access_checks ();
9393   return template_id;
9394 }
9395
9396 /* Parse a template-name.
9397
9398    template-name:
9399      identifier
9400
9401    The standard should actually say:
9402
9403    template-name:
9404      identifier
9405      operator-function-id
9406
9407    A defect report has been filed about this issue.
9408
9409    A conversion-function-id cannot be a template name because they cannot
9410    be part of a template-id. In fact, looking at this code:
9411
9412    a.operator K<int>()
9413
9414    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9415    It is impossible to call a templated conversion-function-id with an
9416    explicit argument list, since the only allowed template parameter is
9417    the type to which it is converting.
9418
9419    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9420    `template' keyword, in a construction like:
9421
9422      T::template f<3>()
9423
9424    In that case `f' is taken to be a template-name, even though there
9425    is no way of knowing for sure.
9426
9427    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9428    name refers to a set of overloaded functions, at least one of which
9429    is a template, or an IDENTIFIER_NODE with the name of the template,
9430    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9431    names are looked up inside uninstantiated templates.  */
9432
9433 static tree
9434 cp_parser_template_name (cp_parser* parser,
9435                          bool template_keyword_p,
9436                          bool check_dependency_p,
9437                          bool is_declaration,
9438                          bool *is_identifier)
9439 {
9440   tree identifier;
9441   tree decl;
9442   tree fns;
9443
9444   /* If the next token is `operator', then we have either an
9445      operator-function-id or a conversion-function-id.  */
9446   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9447     {
9448       /* We don't know whether we're looking at an
9449          operator-function-id or a conversion-function-id.  */
9450       cp_parser_parse_tentatively (parser);
9451       /* Try an operator-function-id.  */
9452       identifier = cp_parser_operator_function_id (parser);
9453       /* If that didn't work, try a conversion-function-id.  */
9454       if (!cp_parser_parse_definitely (parser))
9455         {
9456           cp_parser_error (parser, "expected template-name");
9457           return error_mark_node;
9458         }
9459     }
9460   /* Look for the identifier.  */
9461   else
9462     identifier = cp_parser_identifier (parser);
9463
9464   /* If we didn't find an identifier, we don't have a template-id.  */
9465   if (identifier == error_mark_node)
9466     return error_mark_node;
9467
9468   /* If the name immediately followed the `template' keyword, then it
9469      is a template-name.  However, if the next token is not `<', then
9470      we do not treat it as a template-name, since it is not being used
9471      as part of a template-id.  This enables us to handle constructs
9472      like:
9473
9474        template <typename T> struct S { S(); };
9475        template <typename T> S<T>::S();
9476
9477      correctly.  We would treat `S' as a template -- if it were `S<T>'
9478      -- but we do not if there is no `<'.  */
9479
9480   if (processing_template_decl
9481       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9482     {
9483       /* In a declaration, in a dependent context, we pretend that the
9484          "template" keyword was present in order to improve error
9485          recovery.  For example, given:
9486
9487            template <typename T> void f(T::X<int>);
9488
9489          we want to treat "X<int>" as a template-id.  */
9490       if (is_declaration
9491           && !template_keyword_p
9492           && parser->scope && TYPE_P (parser->scope)
9493           && check_dependency_p
9494           && dependent_type_p (parser->scope)
9495           /* Do not do this for dtors (or ctors), since they never
9496              need the template keyword before their name.  */
9497           && !constructor_name_p (identifier, parser->scope))
9498         {
9499           cp_token_position start = 0;
9500
9501           /* Explain what went wrong.  */
9502           error ("non-template %qD used as template", identifier);
9503           inform ("use %<%T::template %D%> to indicate that it is a template",
9504                   parser->scope, identifier);
9505           /* If parsing tentatively, find the location of the "<" token.  */
9506           if (cp_parser_simulate_error (parser))
9507             start = cp_lexer_token_position (parser->lexer, true);
9508           /* Parse the template arguments so that we can issue error
9509              messages about them.  */
9510           cp_lexer_consume_token (parser->lexer);
9511           cp_parser_enclosed_template_argument_list (parser);
9512           /* Skip tokens until we find a good place from which to
9513              continue parsing.  */
9514           cp_parser_skip_to_closing_parenthesis (parser,
9515                                                  /*recovering=*/true,
9516                                                  /*or_comma=*/true,
9517                                                  /*consume_paren=*/false);
9518           /* If parsing tentatively, permanently remove the
9519              template argument list.  That will prevent duplicate
9520              error messages from being issued about the missing
9521              "template" keyword.  */
9522           if (start)
9523             cp_lexer_purge_tokens_after (parser->lexer, start);
9524           if (is_identifier)
9525             *is_identifier = true;
9526           return identifier;
9527         }
9528
9529       /* If the "template" keyword is present, then there is generally
9530          no point in doing name-lookup, so we just return IDENTIFIER.
9531          But, if the qualifying scope is non-dependent then we can
9532          (and must) do name-lookup normally.  */
9533       if (template_keyword_p
9534           && (!parser->scope
9535               || (TYPE_P (parser->scope)
9536                   && dependent_type_p (parser->scope))))
9537         return identifier;
9538     }
9539
9540   /* Look up the name.  */
9541   decl = cp_parser_lookup_name (parser, identifier,
9542                                 none_type,
9543                                 /*is_template=*/false,
9544                                 /*is_namespace=*/false,
9545                                 check_dependency_p,
9546                                 /*ambiguous_decls=*/NULL);
9547   decl = maybe_get_template_decl_from_type_decl (decl);
9548
9549   /* If DECL is a template, then the name was a template-name.  */
9550   if (TREE_CODE (decl) == TEMPLATE_DECL)
9551     ;
9552   else
9553     {
9554       tree fn = NULL_TREE;
9555
9556       /* The standard does not explicitly indicate whether a name that
9557          names a set of overloaded declarations, some of which are
9558          templates, is a template-name.  However, such a name should
9559          be a template-name; otherwise, there is no way to form a
9560          template-id for the overloaded templates.  */
9561       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9562       if (TREE_CODE (fns) == OVERLOAD)
9563         for (fn = fns; fn; fn = OVL_NEXT (fn))
9564           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9565             break;
9566
9567       if (!fn)
9568         {
9569           /* The name does not name a template.  */
9570           cp_parser_error (parser, "expected template-name");
9571           return error_mark_node;
9572         }
9573     }
9574
9575   /* If DECL is dependent, and refers to a function, then just return
9576      its name; we will look it up again during template instantiation.  */
9577   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9578     {
9579       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9580       if (TYPE_P (scope) && dependent_type_p (scope))
9581         return identifier;
9582     }
9583
9584   return decl;
9585 }
9586
9587 /* Parse a template-argument-list.
9588
9589    template-argument-list:
9590      template-argument
9591      template-argument-list , template-argument
9592
9593    Returns a TREE_VEC containing the arguments.  */
9594
9595 static tree
9596 cp_parser_template_argument_list (cp_parser* parser)
9597 {
9598   tree fixed_args[10];
9599   unsigned n_args = 0;
9600   unsigned alloced = 10;
9601   tree *arg_ary = fixed_args;
9602   tree vec;
9603   bool saved_in_template_argument_list_p;
9604   bool saved_ice_p;
9605   bool saved_non_ice_p;
9606
9607   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9608   parser->in_template_argument_list_p = true;
9609   /* Even if the template-id appears in an integral
9610      constant-expression, the contents of the argument list do
9611      not.  */
9612   saved_ice_p = parser->integral_constant_expression_p;
9613   parser->integral_constant_expression_p = false;
9614   saved_non_ice_p = parser->non_integral_constant_expression_p;
9615   parser->non_integral_constant_expression_p = false;
9616   /* Parse the arguments.  */
9617   do
9618     {
9619       tree argument;
9620
9621       if (n_args)
9622         /* Consume the comma.  */
9623         cp_lexer_consume_token (parser->lexer);
9624
9625       /* Parse the template-argument.  */
9626       argument = cp_parser_template_argument (parser);
9627       if (n_args == alloced)
9628         {
9629           alloced *= 2;
9630
9631           if (arg_ary == fixed_args)
9632             {
9633               arg_ary = XNEWVEC (tree, alloced);
9634               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9635             }
9636           else
9637             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9638         }
9639       arg_ary[n_args++] = argument;
9640     }
9641   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9642
9643   vec = make_tree_vec (n_args);
9644
9645   while (n_args--)
9646     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9647
9648   if (arg_ary != fixed_args)
9649     free (arg_ary);
9650   parser->non_integral_constant_expression_p = saved_non_ice_p;
9651   parser->integral_constant_expression_p = saved_ice_p;
9652   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9653   return vec;
9654 }
9655
9656 /* Parse a template-argument.
9657
9658    template-argument:
9659      assignment-expression
9660      type-id
9661      id-expression
9662
9663    The representation is that of an assignment-expression, type-id, or
9664    id-expression -- except that the qualified id-expression is
9665    evaluated, so that the value returned is either a DECL or an
9666    OVERLOAD.
9667
9668    Although the standard says "assignment-expression", it forbids
9669    throw-expressions or assignments in the template argument.
9670    Therefore, we use "conditional-expression" instead.  */
9671
9672 static tree
9673 cp_parser_template_argument (cp_parser* parser)
9674 {
9675   tree argument;
9676   bool template_p;
9677   bool address_p;
9678   bool maybe_type_id = false;
9679   cp_token *token;
9680   cp_id_kind idk;
9681
9682   /* There's really no way to know what we're looking at, so we just
9683      try each alternative in order.
9684
9685        [temp.arg]
9686
9687        In a template-argument, an ambiguity between a type-id and an
9688        expression is resolved to a type-id, regardless of the form of
9689        the corresponding template-parameter.
9690
9691      Therefore, we try a type-id first.  */
9692   cp_parser_parse_tentatively (parser);
9693   argument = cp_parser_type_id (parser);
9694   /* If there was no error parsing the type-id but the next token is a '>>',
9695      we probably found a typo for '> >'. But there are type-id which are
9696      also valid expressions. For instance:
9697
9698      struct X { int operator >> (int); };
9699      template <int V> struct Foo {};
9700      Foo<X () >> 5> r;
9701
9702      Here 'X()' is a valid type-id of a function type, but the user just
9703      wanted to write the expression "X() >> 5". Thus, we remember that we
9704      found a valid type-id, but we still try to parse the argument as an
9705      expression to see what happens.  */
9706   if (!cp_parser_error_occurred (parser)
9707       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9708     {
9709       maybe_type_id = true;
9710       cp_parser_abort_tentative_parse (parser);
9711     }
9712   else
9713     {
9714       /* If the next token isn't a `,' or a `>', then this argument wasn't
9715       really finished. This means that the argument is not a valid
9716       type-id.  */
9717       if (!cp_parser_next_token_ends_template_argument_p (parser))
9718         cp_parser_error (parser, "expected template-argument");
9719       /* If that worked, we're done.  */
9720       if (cp_parser_parse_definitely (parser))
9721         return argument;
9722     }
9723   /* We're still not sure what the argument will be.  */
9724   cp_parser_parse_tentatively (parser);
9725   /* Try a template.  */
9726   argument = cp_parser_id_expression (parser,
9727                                       /*template_keyword_p=*/false,
9728                                       /*check_dependency_p=*/true,
9729                                       &template_p,
9730                                       /*declarator_p=*/false,
9731                                       /*optional_p=*/false);
9732   /* If the next token isn't a `,' or a `>', then this argument wasn't
9733      really finished.  */
9734   if (!cp_parser_next_token_ends_template_argument_p (parser))
9735     cp_parser_error (parser, "expected template-argument");
9736   if (!cp_parser_error_occurred (parser))
9737     {
9738       /* Figure out what is being referred to.  If the id-expression
9739          was for a class template specialization, then we will have a
9740          TYPE_DECL at this point.  There is no need to do name lookup
9741          at this point in that case.  */
9742       if (TREE_CODE (argument) != TYPE_DECL)
9743         argument = cp_parser_lookup_name (parser, argument,
9744                                           none_type,
9745                                           /*is_template=*/template_p,
9746                                           /*is_namespace=*/false,
9747                                           /*check_dependency=*/true,
9748                                           /*ambiguous_decls=*/NULL);
9749       if (TREE_CODE (argument) != TEMPLATE_DECL
9750           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9751         cp_parser_error (parser, "expected template-name");
9752     }
9753   if (cp_parser_parse_definitely (parser))
9754     return argument;
9755   /* It must be a non-type argument.  There permitted cases are given
9756      in [temp.arg.nontype]:
9757
9758      -- an integral constant-expression of integral or enumeration
9759         type; or
9760
9761      -- the name of a non-type template-parameter; or
9762
9763      -- the name of an object or function with external linkage...
9764
9765      -- the address of an object or function with external linkage...
9766
9767      -- a pointer to member...  */
9768   /* Look for a non-type template parameter.  */
9769   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9770     {
9771       cp_parser_parse_tentatively (parser);
9772       argument = cp_parser_primary_expression (parser,
9773                                                /*adress_p=*/false,
9774                                                /*cast_p=*/false,
9775                                                /*template_arg_p=*/true,
9776                                                &idk);
9777       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9778           || !cp_parser_next_token_ends_template_argument_p (parser))
9779         cp_parser_simulate_error (parser);
9780       if (cp_parser_parse_definitely (parser))
9781         return argument;
9782     }
9783
9784   /* If the next token is "&", the argument must be the address of an
9785      object or function with external linkage.  */
9786   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9787   if (address_p)
9788     cp_lexer_consume_token (parser->lexer);
9789   /* See if we might have an id-expression.  */
9790   token = cp_lexer_peek_token (parser->lexer);
9791   if (token->type == CPP_NAME
9792       || token->keyword == RID_OPERATOR
9793       || token->type == CPP_SCOPE
9794       || token->type == CPP_TEMPLATE_ID
9795       || token->type == CPP_NESTED_NAME_SPECIFIER)
9796     {
9797       cp_parser_parse_tentatively (parser);
9798       argument = cp_parser_primary_expression (parser,
9799                                                address_p,
9800                                                /*cast_p=*/false,
9801                                                /*template_arg_p=*/true,
9802                                                &idk);
9803       if (cp_parser_error_occurred (parser)
9804           || !cp_parser_next_token_ends_template_argument_p (parser))
9805         cp_parser_abort_tentative_parse (parser);
9806       else
9807         {
9808           if (TREE_CODE (argument) == INDIRECT_REF)
9809             {
9810               gcc_assert (REFERENCE_REF_P (argument));
9811               argument = TREE_OPERAND (argument, 0);
9812             }
9813
9814           if (TREE_CODE (argument) == VAR_DECL)
9815             {
9816               /* A variable without external linkage might still be a
9817                  valid constant-expression, so no error is issued here
9818                  if the external-linkage check fails.  */
9819               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9820                 cp_parser_simulate_error (parser);
9821             }
9822           else if (is_overloaded_fn (argument))
9823             /* All overloaded functions are allowed; if the external
9824                linkage test does not pass, an error will be issued
9825                later.  */
9826             ;
9827           else if (address_p
9828                    && (TREE_CODE (argument) == OFFSET_REF
9829                        || TREE_CODE (argument) == SCOPE_REF))
9830             /* A pointer-to-member.  */
9831             ;
9832           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9833             ;
9834           else
9835             cp_parser_simulate_error (parser);
9836
9837           if (cp_parser_parse_definitely (parser))
9838             {
9839               if (address_p)
9840                 argument = build_x_unary_op (ADDR_EXPR, argument);
9841               return argument;
9842             }
9843         }
9844     }
9845   /* If the argument started with "&", there are no other valid
9846      alternatives at this point.  */
9847   if (address_p)
9848     {
9849       cp_parser_error (parser, "invalid non-type template argument");
9850       return error_mark_node;
9851     }
9852
9853   /* If the argument wasn't successfully parsed as a type-id followed
9854      by '>>', the argument can only be a constant expression now.
9855      Otherwise, we try parsing the constant-expression tentatively,
9856      because the argument could really be a type-id.  */
9857   if (maybe_type_id)
9858     cp_parser_parse_tentatively (parser);
9859   argument = cp_parser_constant_expression (parser,
9860                                             /*allow_non_constant_p=*/false,
9861                                             /*non_constant_p=*/NULL);
9862   argument = fold_non_dependent_expr (argument);
9863   if (!maybe_type_id)
9864     return argument;
9865   if (!cp_parser_next_token_ends_template_argument_p (parser))
9866     cp_parser_error (parser, "expected template-argument");
9867   if (cp_parser_parse_definitely (parser))
9868     return argument;
9869   /* We did our best to parse the argument as a non type-id, but that
9870      was the only alternative that matched (albeit with a '>' after
9871      it). We can assume it's just a typo from the user, and a
9872      diagnostic will then be issued.  */
9873   return cp_parser_type_id (parser);
9874 }
9875
9876 /* Parse an explicit-instantiation.
9877
9878    explicit-instantiation:
9879      template declaration
9880
9881    Although the standard says `declaration', what it really means is:
9882
9883    explicit-instantiation:
9884      template decl-specifier-seq [opt] declarator [opt] ;
9885
9886    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9887    supposed to be allowed.  A defect report has been filed about this
9888    issue.
9889
9890    GNU Extension:
9891
9892    explicit-instantiation:
9893      storage-class-specifier template
9894        decl-specifier-seq [opt] declarator [opt] ;
9895      function-specifier template
9896        decl-specifier-seq [opt] declarator [opt] ;  */
9897
9898 static void
9899 cp_parser_explicit_instantiation (cp_parser* parser)
9900 {
9901   int declares_class_or_enum;
9902   cp_decl_specifier_seq decl_specifiers;
9903   tree extension_specifier = NULL_TREE;
9904
9905   /* Look for an (optional) storage-class-specifier or
9906      function-specifier.  */
9907   if (cp_parser_allow_gnu_extensions_p (parser))
9908     {
9909       extension_specifier
9910         = cp_parser_storage_class_specifier_opt (parser);
9911       if (!extension_specifier)
9912         extension_specifier
9913           = cp_parser_function_specifier_opt (parser,
9914                                               /*decl_specs=*/NULL);
9915     }
9916
9917   /* Look for the `template' keyword.  */
9918   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9919   /* Let the front end know that we are processing an explicit
9920      instantiation.  */
9921   begin_explicit_instantiation ();
9922   /* [temp.explicit] says that we are supposed to ignore access
9923      control while processing explicit instantiation directives.  */
9924   push_deferring_access_checks (dk_no_check);
9925   /* Parse a decl-specifier-seq.  */
9926   cp_parser_decl_specifier_seq (parser,
9927                                 CP_PARSER_FLAGS_OPTIONAL,
9928                                 &decl_specifiers,
9929                                 &declares_class_or_enum);
9930   /* If there was exactly one decl-specifier, and it declared a class,
9931      and there's no declarator, then we have an explicit type
9932      instantiation.  */
9933   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9934     {
9935       tree type;
9936
9937       type = check_tag_decl (&decl_specifiers);
9938       /* Turn access control back on for names used during
9939          template instantiation.  */
9940       pop_deferring_access_checks ();
9941       if (type)
9942         do_type_instantiation (type, extension_specifier,
9943                                /*complain=*/tf_error);
9944     }
9945   else
9946     {
9947       cp_declarator *declarator;
9948       tree decl;
9949
9950       /* Parse the declarator.  */
9951       declarator
9952         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9953                                 /*ctor_dtor_or_conv_p=*/NULL,
9954                                 /*parenthesized_p=*/NULL,
9955                                 /*member_p=*/false);
9956       if (declares_class_or_enum & 2)
9957         cp_parser_check_for_definition_in_return_type (declarator,
9958                                                        decl_specifiers.type);
9959       if (declarator != cp_error_declarator)
9960         {
9961           decl = grokdeclarator (declarator, &decl_specifiers,
9962                                  NORMAL, 0, &decl_specifiers.attributes);
9963           /* Turn access control back on for names used during
9964              template instantiation.  */
9965           pop_deferring_access_checks ();
9966           /* Do the explicit instantiation.  */
9967           do_decl_instantiation (decl, extension_specifier);
9968         }
9969       else
9970         {
9971           pop_deferring_access_checks ();
9972           /* Skip the body of the explicit instantiation.  */
9973           cp_parser_skip_to_end_of_statement (parser);
9974         }
9975     }
9976   /* We're done with the instantiation.  */
9977   end_explicit_instantiation ();
9978
9979   cp_parser_consume_semicolon_at_end_of_statement (parser);
9980 }
9981
9982 /* Parse an explicit-specialization.
9983
9984    explicit-specialization:
9985      template < > declaration
9986
9987    Although the standard says `declaration', what it really means is:
9988
9989    explicit-specialization:
9990      template <> decl-specifier [opt] init-declarator [opt] ;
9991      template <> function-definition
9992      template <> explicit-specialization
9993      template <> template-declaration  */
9994
9995 static void
9996 cp_parser_explicit_specialization (cp_parser* parser)
9997 {
9998   bool need_lang_pop;
9999   /* Look for the `template' keyword.  */
10000   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10001   /* Look for the `<'.  */
10002   cp_parser_require (parser, CPP_LESS, "`<'");
10003   /* Look for the `>'.  */
10004   cp_parser_require (parser, CPP_GREATER, "`>'");
10005   /* We have processed another parameter list.  */
10006   ++parser->num_template_parameter_lists;
10007   /* [temp]
10008
10009      A template ... explicit specialization ... shall not have C
10010      linkage.  */
10011   if (current_lang_name == lang_name_c)
10012     {
10013       error ("template specialization with C linkage");
10014       /* Give it C++ linkage to avoid confusing other parts of the
10015          front end.  */
10016       push_lang_context (lang_name_cplusplus);
10017       need_lang_pop = true;
10018     }
10019   else
10020     need_lang_pop = false;
10021   /* Let the front end know that we are beginning a specialization.  */
10022   if (!begin_specialization ())
10023     {
10024       end_specialization ();
10025       cp_parser_skip_to_end_of_block_or_statement (parser);
10026       return;
10027     }
10028
10029   /* If the next keyword is `template', we need to figure out whether
10030      or not we're looking a template-declaration.  */
10031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10032     {
10033       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10034           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10035         cp_parser_template_declaration_after_export (parser,
10036                                                      /*member_p=*/false);
10037       else
10038         cp_parser_explicit_specialization (parser);
10039     }
10040   else
10041     /* Parse the dependent declaration.  */
10042     cp_parser_single_declaration (parser,
10043                                   /*checks=*/NULL,
10044                                   /*member_p=*/false,
10045                                   /*friend_p=*/NULL);
10046   /* We're done with the specialization.  */
10047   end_specialization ();
10048   /* For the erroneous case of a template with C linkage, we pushed an
10049      implicit C++ linkage scope; exit that scope now.  */
10050   if (need_lang_pop)
10051     pop_lang_context ();
10052   /* We're done with this parameter list.  */
10053   --parser->num_template_parameter_lists;
10054 }
10055
10056 /* Parse a type-specifier.
10057
10058    type-specifier:
10059      simple-type-specifier
10060      class-specifier
10061      enum-specifier
10062      elaborated-type-specifier
10063      cv-qualifier
10064
10065    GNU Extension:
10066
10067    type-specifier:
10068      __complex__
10069
10070    Returns a representation of the type-specifier.  For a
10071    class-specifier, enum-specifier, or elaborated-type-specifier, a
10072    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10073
10074    The parser flags FLAGS is used to control type-specifier parsing.
10075
10076    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10077    in a decl-specifier-seq.
10078
10079    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10080    class-specifier, enum-specifier, or elaborated-type-specifier, then
10081    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10082    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10083    zero.
10084
10085    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10086    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10087    is set to FALSE.  */
10088
10089 static tree
10090 cp_parser_type_specifier (cp_parser* parser,
10091                           cp_parser_flags flags,
10092                           cp_decl_specifier_seq *decl_specs,
10093                           bool is_declaration,
10094                           int* declares_class_or_enum,
10095                           bool* is_cv_qualifier)
10096 {
10097   tree type_spec = NULL_TREE;
10098   cp_token *token;
10099   enum rid keyword;
10100   cp_decl_spec ds = ds_last;
10101
10102   /* Assume this type-specifier does not declare a new type.  */
10103   if (declares_class_or_enum)
10104     *declares_class_or_enum = 0;
10105   /* And that it does not specify a cv-qualifier.  */
10106   if (is_cv_qualifier)
10107     *is_cv_qualifier = false;
10108   /* Peek at the next token.  */
10109   token = cp_lexer_peek_token (parser->lexer);
10110
10111   /* If we're looking at a keyword, we can use that to guide the
10112      production we choose.  */
10113   keyword = token->keyword;
10114   switch (keyword)
10115     {
10116     case RID_ENUM:
10117       /* Look for the enum-specifier.  */
10118       type_spec = cp_parser_enum_specifier (parser);
10119       /* If that worked, we're done.  */
10120       if (type_spec)
10121         {
10122           if (declares_class_or_enum)
10123             *declares_class_or_enum = 2;
10124           if (decl_specs)
10125             cp_parser_set_decl_spec_type (decl_specs,
10126                                           type_spec,
10127                                           /*user_defined_p=*/true);
10128           return type_spec;
10129         }
10130       else
10131         goto elaborated_type_specifier;
10132
10133       /* Any of these indicate either a class-specifier, or an
10134          elaborated-type-specifier.  */
10135     case RID_CLASS:
10136     case RID_STRUCT:
10137     case RID_UNION:
10138       /* Parse tentatively so that we can back up if we don't find a
10139          class-specifier.  */
10140       cp_parser_parse_tentatively (parser);
10141       /* Look for the class-specifier.  */
10142       type_spec = cp_parser_class_specifier (parser);
10143       /* If that worked, we're done.  */
10144       if (cp_parser_parse_definitely (parser))
10145         {
10146           if (declares_class_or_enum)
10147             *declares_class_or_enum = 2;
10148           if (decl_specs)
10149             cp_parser_set_decl_spec_type (decl_specs,
10150                                           type_spec,
10151                                           /*user_defined_p=*/true);
10152           return type_spec;
10153         }
10154
10155       /* Fall through.  */
10156     elaborated_type_specifier:
10157       /* We're declaring (not defining) a class or enum.  */
10158       if (declares_class_or_enum)
10159         *declares_class_or_enum = 1;
10160
10161       /* Fall through.  */
10162     case RID_TYPENAME:
10163       /* Look for an elaborated-type-specifier.  */
10164       type_spec
10165         = (cp_parser_elaborated_type_specifier
10166            (parser,
10167             decl_specs && decl_specs->specs[(int) ds_friend],
10168             is_declaration));
10169       if (decl_specs)
10170         cp_parser_set_decl_spec_type (decl_specs,
10171                                       type_spec,
10172                                       /*user_defined_p=*/true);
10173       return type_spec;
10174
10175     case RID_CONST:
10176       ds = ds_const;
10177       if (is_cv_qualifier)
10178         *is_cv_qualifier = true;
10179       break;
10180
10181     case RID_VOLATILE:
10182       ds = ds_volatile;
10183       if (is_cv_qualifier)
10184         *is_cv_qualifier = true;
10185       break;
10186
10187     case RID_RESTRICT:
10188       ds = ds_restrict;
10189       if (is_cv_qualifier)
10190         *is_cv_qualifier = true;
10191       break;
10192
10193     case RID_COMPLEX:
10194       /* The `__complex__' keyword is a GNU extension.  */
10195       ds = ds_complex;
10196       break;
10197
10198     default:
10199       break;
10200     }
10201
10202   /* Handle simple keywords.  */
10203   if (ds != ds_last)
10204     {
10205       if (decl_specs)
10206         {
10207           ++decl_specs->specs[(int)ds];
10208           decl_specs->any_specifiers_p = true;
10209         }
10210       return cp_lexer_consume_token (parser->lexer)->u.value;
10211     }
10212
10213   /* If we do not already have a type-specifier, assume we are looking
10214      at a simple-type-specifier.  */
10215   type_spec = cp_parser_simple_type_specifier (parser,
10216                                                decl_specs,
10217                                                flags);
10218
10219   /* If we didn't find a type-specifier, and a type-specifier was not
10220      optional in this context, issue an error message.  */
10221   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10222     {
10223       cp_parser_error (parser, "expected type specifier");
10224       return error_mark_node;
10225     }
10226
10227   return type_spec;
10228 }
10229
10230 /* Parse a simple-type-specifier.
10231
10232    simple-type-specifier:
10233      :: [opt] nested-name-specifier [opt] type-name
10234      :: [opt] nested-name-specifier template template-id
10235      char
10236      wchar_t
10237      bool
10238      short
10239      int
10240      long
10241      signed
10242      unsigned
10243      float
10244      double
10245      void
10246
10247    GNU Extension:
10248
10249    simple-type-specifier:
10250      __typeof__ unary-expression
10251      __typeof__ ( type-id )
10252
10253    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10254    appropriately updated.  */
10255
10256 static tree
10257 cp_parser_simple_type_specifier (cp_parser* parser,
10258                                  cp_decl_specifier_seq *decl_specs,
10259                                  cp_parser_flags flags)
10260 {
10261   tree type = NULL_TREE;
10262   cp_token *token;
10263
10264   /* Peek at the next token.  */
10265   token = cp_lexer_peek_token (parser->lexer);
10266
10267   /* If we're looking at a keyword, things are easy.  */
10268   switch (token->keyword)
10269     {
10270     case RID_CHAR:
10271       if (decl_specs)
10272         decl_specs->explicit_char_p = true;
10273       type = char_type_node;
10274       break;
10275     case RID_WCHAR:
10276       type = wchar_type_node;
10277       break;
10278     case RID_BOOL:
10279       type = boolean_type_node;
10280       break;
10281     case RID_SHORT:
10282       if (decl_specs)
10283         ++decl_specs->specs[(int) ds_short];
10284       type = short_integer_type_node;
10285       break;
10286     case RID_INT:
10287       if (decl_specs)
10288         decl_specs->explicit_int_p = true;
10289       type = integer_type_node;
10290       break;
10291     case RID_LONG:
10292       if (decl_specs)
10293         ++decl_specs->specs[(int) ds_long];
10294       type = long_integer_type_node;
10295       break;
10296     case RID_SIGNED:
10297       if (decl_specs)
10298         ++decl_specs->specs[(int) ds_signed];
10299       type = integer_type_node;
10300       break;
10301     case RID_UNSIGNED:
10302       if (decl_specs)
10303         ++decl_specs->specs[(int) ds_unsigned];
10304       type = unsigned_type_node;
10305       break;
10306     case RID_FLOAT:
10307       type = float_type_node;
10308       break;
10309     case RID_DOUBLE:
10310       type = double_type_node;
10311       break;
10312     case RID_VOID:
10313       type = void_type_node;
10314       break;
10315
10316     case RID_TYPEOF:
10317       /* Consume the `typeof' token.  */
10318       cp_lexer_consume_token (parser->lexer);
10319       /* Parse the operand to `typeof'.  */
10320       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10321       /* If it is not already a TYPE, take its type.  */
10322       if (!TYPE_P (type))
10323         type = finish_typeof (type);
10324
10325       if (decl_specs)
10326         cp_parser_set_decl_spec_type (decl_specs, type,
10327                                       /*user_defined_p=*/true);
10328
10329       return type;
10330
10331     default:
10332       break;
10333     }
10334
10335   /* If the type-specifier was for a built-in type, we're done.  */
10336   if (type)
10337     {
10338       tree id;
10339
10340       /* Record the type.  */
10341       if (decl_specs
10342           && (token->keyword != RID_SIGNED
10343               && token->keyword != RID_UNSIGNED
10344               && token->keyword != RID_SHORT
10345               && token->keyword != RID_LONG))
10346         cp_parser_set_decl_spec_type (decl_specs,
10347                                       type,
10348                                       /*user_defined=*/false);
10349       if (decl_specs)
10350         decl_specs->any_specifiers_p = true;
10351
10352       /* Consume the token.  */
10353       id = cp_lexer_consume_token (parser->lexer)->u.value;
10354
10355       /* There is no valid C++ program where a non-template type is
10356          followed by a "<".  That usually indicates that the user thought
10357          that the type was a template.  */
10358       cp_parser_check_for_invalid_template_id (parser, type);
10359
10360       return TYPE_NAME (type);
10361     }
10362
10363   /* The type-specifier must be a user-defined type.  */
10364   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10365     {
10366       bool qualified_p;
10367       bool global_p;
10368
10369       /* Don't gobble tokens or issue error messages if this is an
10370          optional type-specifier.  */
10371       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10372         cp_parser_parse_tentatively (parser);
10373
10374       /* Look for the optional `::' operator.  */
10375       global_p
10376         = (cp_parser_global_scope_opt (parser,
10377                                        /*current_scope_valid_p=*/false)
10378            != NULL_TREE);
10379       /* Look for the nested-name specifier.  */
10380       qualified_p
10381         = (cp_parser_nested_name_specifier_opt (parser,
10382                                                 /*typename_keyword_p=*/false,
10383                                                 /*check_dependency_p=*/true,
10384                                                 /*type_p=*/false,
10385                                                 /*is_declaration=*/false)
10386            != NULL_TREE);
10387       /* If we have seen a nested-name-specifier, and the next token
10388          is `template', then we are using the template-id production.  */
10389       if (parser->scope
10390           && cp_parser_optional_template_keyword (parser))
10391         {
10392           /* Look for the template-id.  */
10393           type = cp_parser_template_id (parser,
10394                                         /*template_keyword_p=*/true,
10395                                         /*check_dependency_p=*/true,
10396                                         /*is_declaration=*/false);
10397           /* If the template-id did not name a type, we are out of
10398              luck.  */
10399           if (TREE_CODE (type) != TYPE_DECL)
10400             {
10401               cp_parser_error (parser, "expected template-id for type");
10402               type = NULL_TREE;
10403             }
10404         }
10405       /* Otherwise, look for a type-name.  */
10406       else
10407         type = cp_parser_type_name (parser);
10408       /* Keep track of all name-lookups performed in class scopes.  */
10409       if (type
10410           && !global_p
10411           && !qualified_p
10412           && TREE_CODE (type) == TYPE_DECL
10413           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10414         maybe_note_name_used_in_class (DECL_NAME (type), type);
10415       /* If it didn't work out, we don't have a TYPE.  */
10416       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10417           && !cp_parser_parse_definitely (parser))
10418         type = NULL_TREE;
10419       if (type && decl_specs)
10420         cp_parser_set_decl_spec_type (decl_specs, type,
10421                                       /*user_defined=*/true);
10422     }
10423
10424   /* If we didn't get a type-name, issue an error message.  */
10425   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10426     {
10427       cp_parser_error (parser, "expected type-name");
10428       return error_mark_node;
10429     }
10430
10431   /* There is no valid C++ program where a non-template type is
10432      followed by a "<".  That usually indicates that the user thought
10433      that the type was a template.  */
10434   if (type && type != error_mark_node)
10435     {
10436       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10437          If it is, then the '<'...'>' enclose protocol names rather than
10438          template arguments, and so everything is fine.  */
10439       if (c_dialect_objc ()
10440           && (objc_is_id (type) || objc_is_class_name (type)))
10441         {
10442           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10443           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10444
10445           /* Clobber the "unqualified" type previously entered into
10446              DECL_SPECS with the new, improved protocol-qualified version.  */
10447           if (decl_specs)
10448             decl_specs->type = qual_type;
10449
10450           return qual_type;
10451         }
10452
10453       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10454     }
10455
10456   return type;
10457 }
10458
10459 /* Parse a type-name.
10460
10461    type-name:
10462      class-name
10463      enum-name
10464      typedef-name
10465
10466    enum-name:
10467      identifier
10468
10469    typedef-name:
10470      identifier
10471
10472    Returns a TYPE_DECL for the type.  */
10473
10474 static tree
10475 cp_parser_type_name (cp_parser* parser)
10476 {
10477   tree type_decl;
10478   tree identifier;
10479
10480   /* We can't know yet whether it is a class-name or not.  */
10481   cp_parser_parse_tentatively (parser);
10482   /* Try a class-name.  */
10483   type_decl = cp_parser_class_name (parser,
10484                                     /*typename_keyword_p=*/false,
10485                                     /*template_keyword_p=*/false,
10486                                     none_type,
10487                                     /*check_dependency_p=*/true,
10488                                     /*class_head_p=*/false,
10489                                     /*is_declaration=*/false);
10490   /* If it's not a class-name, keep looking.  */
10491   if (!cp_parser_parse_definitely (parser))
10492     {
10493       /* It must be a typedef-name or an enum-name.  */
10494       identifier = cp_parser_identifier (parser);
10495       if (identifier == error_mark_node)
10496         return error_mark_node;
10497
10498       /* Look up the type-name.  */
10499       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10500
10501       if (TREE_CODE (type_decl) != TYPE_DECL
10502           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10503         {
10504           /* See if this is an Objective-C type.  */
10505           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10506           tree type = objc_get_protocol_qualified_type (identifier, protos);
10507           if (type)
10508             type_decl = TYPE_NAME (type);
10509         }
10510
10511       /* Issue an error if we did not find a type-name.  */
10512       if (TREE_CODE (type_decl) != TYPE_DECL)
10513         {
10514           if (!cp_parser_simulate_error (parser))
10515             cp_parser_name_lookup_error (parser, identifier, type_decl,
10516                                          "is not a type");
10517           type_decl = error_mark_node;
10518         }
10519       /* Remember that the name was used in the definition of the
10520          current class so that we can check later to see if the
10521          meaning would have been different after the class was
10522          entirely defined.  */
10523       else if (type_decl != error_mark_node
10524                && !parser->scope)
10525         maybe_note_name_used_in_class (identifier, type_decl);
10526     }
10527
10528   return type_decl;
10529 }
10530
10531
10532 /* Parse an elaborated-type-specifier.  Note that the grammar given
10533    here incorporates the resolution to DR68.
10534
10535    elaborated-type-specifier:
10536      class-key :: [opt] nested-name-specifier [opt] identifier
10537      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10538      enum :: [opt] nested-name-specifier [opt] identifier
10539      typename :: [opt] nested-name-specifier identifier
10540      typename :: [opt] nested-name-specifier template [opt]
10541        template-id
10542
10543    GNU extension:
10544
10545    elaborated-type-specifier:
10546      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10547      class-key attributes :: [opt] nested-name-specifier [opt]
10548                template [opt] template-id
10549      enum attributes :: [opt] nested-name-specifier [opt] identifier
10550
10551    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10552    declared `friend'.  If IS_DECLARATION is TRUE, then this
10553    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10554    something is being declared.
10555
10556    Returns the TYPE specified.  */
10557
10558 static tree
10559 cp_parser_elaborated_type_specifier (cp_parser* parser,
10560                                      bool is_friend,
10561                                      bool is_declaration)
10562 {
10563   enum tag_types tag_type;
10564   tree identifier;
10565   tree type = NULL_TREE;
10566   tree attributes = NULL_TREE;
10567
10568   /* See if we're looking at the `enum' keyword.  */
10569   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10570     {
10571       /* Consume the `enum' token.  */
10572       cp_lexer_consume_token (parser->lexer);
10573       /* Remember that it's an enumeration type.  */
10574       tag_type = enum_type;
10575       /* Parse the attributes.  */
10576       attributes = cp_parser_attributes_opt (parser);
10577     }
10578   /* Or, it might be `typename'.  */
10579   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10580                                            RID_TYPENAME))
10581     {
10582       /* Consume the `typename' token.  */
10583       cp_lexer_consume_token (parser->lexer);
10584       /* Remember that it's a `typename' type.  */
10585       tag_type = typename_type;
10586       /* The `typename' keyword is only allowed in templates.  */
10587       if (!processing_template_decl)
10588         pedwarn ("using %<typename%> outside of template");
10589     }
10590   /* Otherwise it must be a class-key.  */
10591   else
10592     {
10593       tag_type = cp_parser_class_key (parser);
10594       if (tag_type == none_type)
10595         return error_mark_node;
10596       /* Parse the attributes.  */
10597       attributes = cp_parser_attributes_opt (parser);
10598     }
10599
10600   /* Look for the `::' operator.  */
10601   cp_parser_global_scope_opt (parser,
10602                               /*current_scope_valid_p=*/false);
10603   /* Look for the nested-name-specifier.  */
10604   if (tag_type == typename_type)
10605     {
10606       if (!cp_parser_nested_name_specifier (parser,
10607                                            /*typename_keyword_p=*/true,
10608                                            /*check_dependency_p=*/true,
10609                                            /*type_p=*/true,
10610                                             is_declaration))
10611         return error_mark_node;
10612     }
10613   else
10614     /* Even though `typename' is not present, the proposed resolution
10615        to Core Issue 180 says that in `class A<T>::B', `B' should be
10616        considered a type-name, even if `A<T>' is dependent.  */
10617     cp_parser_nested_name_specifier_opt (parser,
10618                                          /*typename_keyword_p=*/true,
10619                                          /*check_dependency_p=*/true,
10620                                          /*type_p=*/true,
10621                                          is_declaration);
10622   /* For everything but enumeration types, consider a template-id.
10623      For an enumeration type, consider only a plain identifier.  */
10624   if (tag_type != enum_type)
10625     {
10626       bool template_p = false;
10627       tree decl;
10628
10629       /* Allow the `template' keyword.  */
10630       template_p = cp_parser_optional_template_keyword (parser);
10631       /* If we didn't see `template', we don't know if there's a
10632          template-id or not.  */
10633       if (!template_p)
10634         cp_parser_parse_tentatively (parser);
10635       /* Parse the template-id.  */
10636       decl = cp_parser_template_id (parser, template_p,
10637                                     /*check_dependency_p=*/true,
10638                                     is_declaration);
10639       /* If we didn't find a template-id, look for an ordinary
10640          identifier.  */
10641       if (!template_p && !cp_parser_parse_definitely (parser))
10642         ;
10643       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10644          in effect, then we must assume that, upon instantiation, the
10645          template will correspond to a class.  */
10646       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10647                && tag_type == typename_type)
10648         type = make_typename_type (parser->scope, decl,
10649                                    typename_type,
10650                                    /*complain=*/tf_error);
10651       else
10652         type = TREE_TYPE (decl);
10653     }
10654
10655   if (!type)
10656     {
10657       identifier = cp_parser_identifier (parser);
10658
10659       if (identifier == error_mark_node)
10660         {
10661           parser->scope = NULL_TREE;
10662           return error_mark_node;
10663         }
10664
10665       /* For a `typename', we needn't call xref_tag.  */
10666       if (tag_type == typename_type
10667           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10668         return cp_parser_make_typename_type (parser, parser->scope,
10669                                              identifier);
10670       /* Look up a qualified name in the usual way.  */
10671       if (parser->scope)
10672         {
10673           tree decl;
10674           tree ambiguous_decls;
10675
10676           decl = cp_parser_lookup_name (parser, identifier,
10677                                         tag_type,
10678                                         /*is_template=*/false,
10679                                         /*is_namespace=*/false,
10680                                         /*check_dependency=*/true,
10681                                         &ambiguous_decls);
10682
10683           /* If the lookup was ambiguous, an error will already have been
10684              issued.  */
10685           if (ambiguous_decls)
10686             return error_mark_node;
10687
10688           /* If we are parsing friend declaration, DECL may be a
10689              TEMPLATE_DECL tree node here.  However, we need to check
10690              whether this TEMPLATE_DECL results in valid code.  Consider
10691              the following example:
10692
10693                namespace N {
10694                  template <class T> class C {};
10695                }
10696                class X {
10697                  template <class T> friend class N::C; // #1, valid code
10698                };
10699                template <class T> class Y {
10700                  friend class N::C;                    // #2, invalid code
10701                };
10702
10703              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10704              name lookup of `N::C'.  We see that friend declaration must
10705              be template for the code to be valid.  Note that
10706              processing_template_decl does not work here since it is
10707              always 1 for the above two cases.  */
10708
10709           decl = (cp_parser_maybe_treat_template_as_class
10710                   (decl, /*tag_name_p=*/is_friend
10711                          && parser->num_template_parameter_lists));
10712
10713           if (TREE_CODE (decl) != TYPE_DECL)
10714             {
10715               cp_parser_diagnose_invalid_type_name (parser,
10716                                                     parser->scope,
10717                                                     identifier);
10718               return error_mark_node;
10719             }
10720
10721           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10722             {
10723               bool allow_template = (parser->num_template_parameter_lists
10724                                       || DECL_SELF_REFERENCE_P (decl));
10725               type = check_elaborated_type_specifier (tag_type, decl, 
10726                                                       allow_template);
10727
10728               if (type == error_mark_node)
10729                 return error_mark_node;
10730             }
10731
10732           type = TREE_TYPE (decl);
10733         }
10734       else
10735         {
10736           /* An elaborated-type-specifier sometimes introduces a new type and
10737              sometimes names an existing type.  Normally, the rule is that it
10738              introduces a new type only if there is not an existing type of
10739              the same name already in scope.  For example, given:
10740
10741                struct S {};
10742                void f() { struct S s; }
10743
10744              the `struct S' in the body of `f' is the same `struct S' as in
10745              the global scope; the existing definition is used.  However, if
10746              there were no global declaration, this would introduce a new
10747              local class named `S'.
10748
10749              An exception to this rule applies to the following code:
10750
10751                namespace N { struct S; }
10752
10753              Here, the elaborated-type-specifier names a new type
10754              unconditionally; even if there is already an `S' in the
10755              containing scope this declaration names a new type.
10756              This exception only applies if the elaborated-type-specifier
10757              forms the complete declaration:
10758
10759                [class.name]
10760
10761                A declaration consisting solely of `class-key identifier ;' is
10762                either a redeclaration of the name in the current scope or a
10763                forward declaration of the identifier as a class name.  It
10764                introduces the name into the current scope.
10765
10766              We are in this situation precisely when the next token is a `;'.
10767
10768              An exception to the exception is that a `friend' declaration does
10769              *not* name a new type; i.e., given:
10770
10771                struct S { friend struct T; };
10772
10773              `T' is not a new type in the scope of `S'.
10774
10775              Also, `new struct S' or `sizeof (struct S)' never results in the
10776              definition of a new type; a new type can only be declared in a
10777              declaration context.  */
10778
10779           tag_scope ts;
10780           bool template_p;
10781
10782           if (is_friend)
10783             /* Friends have special name lookup rules.  */
10784             ts = ts_within_enclosing_non_class;
10785           else if (is_declaration
10786                    && cp_lexer_next_token_is (parser->lexer,
10787                                               CPP_SEMICOLON))
10788             /* This is a `class-key identifier ;' */
10789             ts = ts_current;
10790           else
10791             ts = ts_global;
10792
10793           template_p =
10794             (parser->num_template_parameter_lists
10795              && (cp_parser_next_token_starts_class_definition_p (parser)
10796                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10797           /* An unqualified name was used to reference this type, so
10798              there were no qualifying templates.  */
10799           if (!cp_parser_check_template_parameters (parser,
10800                                                     /*num_templates=*/0))
10801             return error_mark_node;
10802           type = xref_tag (tag_type, identifier, ts, template_p);
10803         }
10804     }
10805
10806   if (type == error_mark_node)
10807     return error_mark_node;
10808
10809   /* Allow attributes on forward declarations of classes.  */
10810   if (attributes)
10811     {
10812       if (TREE_CODE (type) == TYPENAME_TYPE)
10813         warning (OPT_Wattributes,
10814                  "attributes ignored on uninstantiated type");
10815       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10816                && ! processing_explicit_instantiation)
10817         warning (OPT_Wattributes,
10818                  "attributes ignored on template instantiation");
10819       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10820         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10821       else
10822         warning (OPT_Wattributes,
10823                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10824     }
10825
10826   if (tag_type != enum_type)
10827     cp_parser_check_class_key (tag_type, type);
10828
10829   /* A "<" cannot follow an elaborated type specifier.  If that
10830      happens, the user was probably trying to form a template-id.  */
10831   cp_parser_check_for_invalid_template_id (parser, type);
10832
10833   return type;
10834 }
10835
10836 /* Parse an enum-specifier.
10837
10838    enum-specifier:
10839      enum identifier [opt] { enumerator-list [opt] }
10840
10841    GNU Extensions:
10842      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10843        attributes[opt]
10844
10845    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10846    if the token stream isn't an enum-specifier after all.  */
10847
10848 static tree
10849 cp_parser_enum_specifier (cp_parser* parser)
10850 {
10851   tree identifier;
10852   tree type;
10853   tree attributes;
10854
10855   /* Parse tentatively so that we can back up if we don't find a
10856      enum-specifier.  */
10857   cp_parser_parse_tentatively (parser);
10858
10859   /* Caller guarantees that the current token is 'enum', an identifier
10860      possibly follows, and the token after that is an opening brace.
10861      If we don't have an identifier, fabricate an anonymous name for
10862      the enumeration being defined.  */
10863   cp_lexer_consume_token (parser->lexer);
10864
10865   attributes = cp_parser_attributes_opt (parser);
10866
10867   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10868     identifier = cp_parser_identifier (parser);
10869   else
10870     identifier = make_anon_name ();
10871
10872   /* Look for the `{' but don't consume it yet.  */
10873   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10874     cp_parser_simulate_error (parser);
10875
10876   if (!cp_parser_parse_definitely (parser))
10877     return NULL_TREE;
10878
10879   /* Issue an error message if type-definitions are forbidden here.  */
10880   if (!cp_parser_check_type_definition (parser))
10881     type = error_mark_node;
10882   else
10883     /* Create the new type.  We do this before consuming the opening
10884        brace so the enum will be recorded as being on the line of its
10885        tag (or the 'enum' keyword, if there is no tag).  */
10886     type = start_enum (identifier);
10887   
10888   /* Consume the opening brace.  */
10889   cp_lexer_consume_token (parser->lexer);
10890
10891   if (type == error_mark_node)
10892     {
10893       cp_parser_skip_to_end_of_block_or_statement (parser);
10894       return error_mark_node;
10895     }
10896
10897   /* If the next token is not '}', then there are some enumerators.  */
10898   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10899     cp_parser_enumerator_list (parser, type);
10900
10901   /* Consume the final '}'.  */
10902   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10903
10904   /* Look for trailing attributes to apply to this enumeration, and
10905      apply them if appropriate.  */
10906   if (cp_parser_allow_gnu_extensions_p (parser))
10907     {
10908       tree trailing_attr = cp_parser_attributes_opt (parser);
10909       cplus_decl_attributes (&type,
10910                              trailing_attr,
10911                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10912     }
10913
10914   /* Finish up the enumeration.  */
10915   finish_enum (type);
10916
10917   return type;
10918 }
10919
10920 /* Parse an enumerator-list.  The enumerators all have the indicated
10921    TYPE.
10922
10923    enumerator-list:
10924      enumerator-definition
10925      enumerator-list , enumerator-definition  */
10926
10927 static void
10928 cp_parser_enumerator_list (cp_parser* parser, tree type)
10929 {
10930   while (true)
10931     {
10932       /* Parse an enumerator-definition.  */
10933       cp_parser_enumerator_definition (parser, type);
10934
10935       /* If the next token is not a ',', we've reached the end of
10936          the list.  */
10937       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10938         break;
10939       /* Otherwise, consume the `,' and keep going.  */
10940       cp_lexer_consume_token (parser->lexer);
10941       /* If the next token is a `}', there is a trailing comma.  */
10942       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10943         {
10944           if (pedantic && !in_system_header)
10945             pedwarn ("comma at end of enumerator list");
10946           break;
10947         }
10948     }
10949 }
10950
10951 /* Parse an enumerator-definition.  The enumerator has the indicated
10952    TYPE.
10953
10954    enumerator-definition:
10955      enumerator
10956      enumerator = constant-expression
10957
10958    enumerator:
10959      identifier  */
10960
10961 static void
10962 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10963 {
10964   tree identifier;
10965   tree value;
10966
10967   /* Look for the identifier.  */
10968   identifier = cp_parser_identifier (parser);
10969   if (identifier == error_mark_node)
10970     return;
10971
10972   /* If the next token is an '=', then there is an explicit value.  */
10973   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10974     {
10975       /* Consume the `=' token.  */
10976       cp_lexer_consume_token (parser->lexer);
10977       /* Parse the value.  */
10978       value = cp_parser_constant_expression (parser,
10979                                              /*allow_non_constant_p=*/false,
10980                                              NULL);
10981     }
10982   else
10983     value = NULL_TREE;
10984
10985   /* Create the enumerator.  */
10986   build_enumerator (identifier, value, type);
10987 }
10988
10989 /* Parse a namespace-name.
10990
10991    namespace-name:
10992      original-namespace-name
10993      namespace-alias
10994
10995    Returns the NAMESPACE_DECL for the namespace.  */
10996
10997 static tree
10998 cp_parser_namespace_name (cp_parser* parser)
10999 {
11000   tree identifier;
11001   tree namespace_decl;
11002
11003   /* Get the name of the namespace.  */
11004   identifier = cp_parser_identifier (parser);
11005   if (identifier == error_mark_node)
11006     return error_mark_node;
11007
11008   /* Look up the identifier in the currently active scope.  Look only
11009      for namespaces, due to:
11010
11011        [basic.lookup.udir]
11012
11013        When looking up a namespace-name in a using-directive or alias
11014        definition, only namespace names are considered.
11015
11016      And:
11017
11018        [basic.lookup.qual]
11019
11020        During the lookup of a name preceding the :: scope resolution
11021        operator, object, function, and enumerator names are ignored.
11022
11023      (Note that cp_parser_class_or_namespace_name only calls this
11024      function if the token after the name is the scope resolution
11025      operator.)  */
11026   namespace_decl = cp_parser_lookup_name (parser, identifier,
11027                                           none_type,
11028                                           /*is_template=*/false,
11029                                           /*is_namespace=*/true,
11030                                           /*check_dependency=*/true,
11031                                           /*ambiguous_decls=*/NULL);
11032   /* If it's not a namespace, issue an error.  */
11033   if (namespace_decl == error_mark_node
11034       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11035     {
11036       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11037         error ("%qD is not a namespace-name", identifier);
11038       cp_parser_error (parser, "expected namespace-name");
11039       namespace_decl = error_mark_node;
11040     }
11041
11042   return namespace_decl;
11043 }
11044
11045 /* Parse a namespace-definition.
11046
11047    namespace-definition:
11048      named-namespace-definition
11049      unnamed-namespace-definition
11050
11051    named-namespace-definition:
11052      original-namespace-definition
11053      extension-namespace-definition
11054
11055    original-namespace-definition:
11056      namespace identifier { namespace-body }
11057
11058    extension-namespace-definition:
11059      namespace original-namespace-name { namespace-body }
11060
11061    unnamed-namespace-definition:
11062      namespace { namespace-body } */
11063
11064 static void
11065 cp_parser_namespace_definition (cp_parser* parser)
11066 {
11067   tree identifier, attribs;
11068
11069   /* Look for the `namespace' keyword.  */
11070   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11071
11072   /* Get the name of the namespace.  We do not attempt to distinguish
11073      between an original-namespace-definition and an
11074      extension-namespace-definition at this point.  The semantic
11075      analysis routines are responsible for that.  */
11076   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11077     identifier = cp_parser_identifier (parser);
11078   else
11079     identifier = NULL_TREE;
11080
11081   /* Parse any specified attributes.  */
11082   attribs = cp_parser_attributes_opt (parser);
11083
11084   /* Look for the `{' to start the namespace.  */
11085   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11086   /* Start the namespace.  */
11087   push_namespace_with_attribs (identifier, attribs);
11088   /* Parse the body of the namespace.  */
11089   cp_parser_namespace_body (parser);
11090   /* Finish the namespace.  */
11091   pop_namespace ();
11092   /* Look for the final `}'.  */
11093   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11094 }
11095
11096 /* Parse a namespace-body.
11097
11098    namespace-body:
11099      declaration-seq [opt]  */
11100
11101 static void
11102 cp_parser_namespace_body (cp_parser* parser)
11103 {
11104   cp_parser_declaration_seq_opt (parser);
11105 }
11106
11107 /* Parse a namespace-alias-definition.
11108
11109    namespace-alias-definition:
11110      namespace identifier = qualified-namespace-specifier ;  */
11111
11112 static void
11113 cp_parser_namespace_alias_definition (cp_parser* parser)
11114 {
11115   tree identifier;
11116   tree namespace_specifier;
11117
11118   /* Look for the `namespace' keyword.  */
11119   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11120   /* Look for the identifier.  */
11121   identifier = cp_parser_identifier (parser);
11122   if (identifier == error_mark_node)
11123     return;
11124   /* Look for the `=' token.  */
11125   cp_parser_require (parser, CPP_EQ, "`='");
11126   /* Look for the qualified-namespace-specifier.  */
11127   namespace_specifier
11128     = cp_parser_qualified_namespace_specifier (parser);
11129   /* Look for the `;' token.  */
11130   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11131
11132   /* Register the alias in the symbol table.  */
11133   do_namespace_alias (identifier, namespace_specifier);
11134 }
11135
11136 /* Parse a qualified-namespace-specifier.
11137
11138    qualified-namespace-specifier:
11139      :: [opt] nested-name-specifier [opt] namespace-name
11140
11141    Returns a NAMESPACE_DECL corresponding to the specified
11142    namespace.  */
11143
11144 static tree
11145 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11146 {
11147   /* Look for the optional `::'.  */
11148   cp_parser_global_scope_opt (parser,
11149                               /*current_scope_valid_p=*/false);
11150
11151   /* Look for the optional nested-name-specifier.  */
11152   cp_parser_nested_name_specifier_opt (parser,
11153                                        /*typename_keyword_p=*/false,
11154                                        /*check_dependency_p=*/true,
11155                                        /*type_p=*/false,
11156                                        /*is_declaration=*/true);
11157
11158   return cp_parser_namespace_name (parser);
11159 }
11160
11161 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11162    access declaration.
11163
11164    using-declaration:
11165      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11166      using :: unqualified-id ;  
11167
11168    access-declaration:
11169      qualified-id ;  
11170
11171    */
11172
11173 static bool
11174 cp_parser_using_declaration (cp_parser* parser, 
11175                              bool access_declaration_p)
11176 {
11177   cp_token *token;
11178   bool typename_p = false;
11179   bool global_scope_p;
11180   tree decl;
11181   tree identifier;
11182   tree qscope;
11183
11184   if (access_declaration_p)
11185     cp_parser_parse_tentatively (parser);
11186   else
11187     {
11188       /* Look for the `using' keyword.  */
11189       cp_parser_require_keyword (parser, RID_USING, "`using'");
11190       
11191       /* Peek at the next token.  */
11192       token = cp_lexer_peek_token (parser->lexer);
11193       /* See if it's `typename'.  */
11194       if (token->keyword == RID_TYPENAME)
11195         {
11196           /* Remember that we've seen it.  */
11197           typename_p = true;
11198           /* Consume the `typename' token.  */
11199           cp_lexer_consume_token (parser->lexer);
11200         }
11201     }
11202
11203   /* Look for the optional global scope qualification.  */
11204   global_scope_p
11205     = (cp_parser_global_scope_opt (parser,
11206                                    /*current_scope_valid_p=*/false)
11207        != NULL_TREE);
11208
11209   /* If we saw `typename', or didn't see `::', then there must be a
11210      nested-name-specifier present.  */
11211   if (typename_p || !global_scope_p)
11212     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11213                                               /*check_dependency_p=*/true,
11214                                               /*type_p=*/false,
11215                                               /*is_declaration=*/true);
11216   /* Otherwise, we could be in either of the two productions.  In that
11217      case, treat the nested-name-specifier as optional.  */
11218   else
11219     qscope = cp_parser_nested_name_specifier_opt (parser,
11220                                                   /*typename_keyword_p=*/false,
11221                                                   /*check_dependency_p=*/true,
11222                                                   /*type_p=*/false,
11223                                                   /*is_declaration=*/true);
11224   if (!qscope)
11225     qscope = global_namespace;
11226
11227   if (access_declaration_p && cp_parser_error_occurred (parser))
11228     /* Something has already gone wrong; there's no need to parse
11229        further.  Since an error has occurred, the return value of
11230        cp_parser_parse_definitely will be false, as required.  */
11231     return cp_parser_parse_definitely (parser);
11232
11233   /* Parse the unqualified-id.  */
11234   identifier = cp_parser_unqualified_id (parser,
11235                                          /*template_keyword_p=*/false,
11236                                          /*check_dependency_p=*/true,
11237                                          /*declarator_p=*/true,
11238                                          /*optional_p=*/false);
11239
11240   if (access_declaration_p)
11241     {
11242       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11243         cp_parser_simulate_error (parser);
11244       if (!cp_parser_parse_definitely (parser))
11245         return false;
11246     }
11247
11248   /* The function we call to handle a using-declaration is different
11249      depending on what scope we are in.  */
11250   if (qscope == error_mark_node || identifier == error_mark_node)
11251     ;
11252   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11253            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11254     /* [namespace.udecl]
11255
11256        A using declaration shall not name a template-id.  */
11257     error ("a template-id may not appear in a using-declaration");
11258   else
11259     {
11260       if (at_class_scope_p ())
11261         {
11262           /* Create the USING_DECL.  */
11263           decl = do_class_using_decl (parser->scope, identifier);
11264           /* Add it to the list of members in this class.  */
11265           finish_member_declaration (decl);
11266         }
11267       else
11268         {
11269           decl = cp_parser_lookup_name_simple (parser, identifier);
11270           if (decl == error_mark_node)
11271             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11272           else if (!at_namespace_scope_p ())
11273             do_local_using_decl (decl, qscope, identifier);
11274           else
11275             do_toplevel_using_decl (decl, qscope, identifier);
11276         }
11277     }
11278
11279   /* Look for the final `;'.  */
11280   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11281   
11282   return true;
11283 }
11284
11285 /* Parse a using-directive.
11286
11287    using-directive:
11288      using namespace :: [opt] nested-name-specifier [opt]
11289        namespace-name ;  */
11290
11291 static void
11292 cp_parser_using_directive (cp_parser* parser)
11293 {
11294   tree namespace_decl;
11295   tree attribs;
11296
11297   /* Look for the `using' keyword.  */
11298   cp_parser_require_keyword (parser, RID_USING, "`using'");
11299   /* And the `namespace' keyword.  */
11300   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11301   /* Look for the optional `::' operator.  */
11302   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11303   /* And the optional nested-name-specifier.  */
11304   cp_parser_nested_name_specifier_opt (parser,
11305                                        /*typename_keyword_p=*/false,
11306                                        /*check_dependency_p=*/true,
11307                                        /*type_p=*/false,
11308                                        /*is_declaration=*/true);
11309   /* Get the namespace being used.  */
11310   namespace_decl = cp_parser_namespace_name (parser);
11311   /* And any specified attributes.  */
11312   attribs = cp_parser_attributes_opt (parser);
11313   /* Update the symbol table.  */
11314   parse_using_directive (namespace_decl, attribs);
11315   /* Look for the final `;'.  */
11316   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11317 }
11318
11319 /* Parse an asm-definition.
11320
11321    asm-definition:
11322      asm ( string-literal ) ;
11323
11324    GNU Extension:
11325
11326    asm-definition:
11327      asm volatile [opt] ( string-literal ) ;
11328      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11329      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11330                           : asm-operand-list [opt] ) ;
11331      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11332                           : asm-operand-list [opt]
11333                           : asm-operand-list [opt] ) ;  */
11334
11335 static void
11336 cp_parser_asm_definition (cp_parser* parser)
11337 {
11338   tree string;
11339   tree outputs = NULL_TREE;
11340   tree inputs = NULL_TREE;
11341   tree clobbers = NULL_TREE;
11342   tree asm_stmt;
11343   bool volatile_p = false;
11344   bool extended_p = false;
11345
11346   /* Look for the `asm' keyword.  */
11347   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11348   /* See if the next token is `volatile'.  */
11349   if (cp_parser_allow_gnu_extensions_p (parser)
11350       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11351     {
11352       /* Remember that we saw the `volatile' keyword.  */
11353       volatile_p = true;
11354       /* Consume the token.  */
11355       cp_lexer_consume_token (parser->lexer);
11356     }
11357   /* Look for the opening `('.  */
11358   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11359     return;
11360   /* Look for the string.  */
11361   string = cp_parser_string_literal (parser, false, false);
11362   if (string == error_mark_node)
11363     {
11364       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11365                                              /*consume_paren=*/true);
11366       return;
11367     }
11368
11369   /* If we're allowing GNU extensions, check for the extended assembly
11370      syntax.  Unfortunately, the `:' tokens need not be separated by
11371      a space in C, and so, for compatibility, we tolerate that here
11372      too.  Doing that means that we have to treat the `::' operator as
11373      two `:' tokens.  */
11374   if (cp_parser_allow_gnu_extensions_p (parser)
11375       && parser->in_function_body
11376       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11377           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11378     {
11379       bool inputs_p = false;
11380       bool clobbers_p = false;
11381
11382       /* The extended syntax was used.  */
11383       extended_p = true;
11384
11385       /* Look for outputs.  */
11386       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11387         {
11388           /* Consume the `:'.  */
11389           cp_lexer_consume_token (parser->lexer);
11390           /* Parse the output-operands.  */
11391           if (cp_lexer_next_token_is_not (parser->lexer,
11392                                           CPP_COLON)
11393               && cp_lexer_next_token_is_not (parser->lexer,
11394                                              CPP_SCOPE)
11395               && cp_lexer_next_token_is_not (parser->lexer,
11396                                              CPP_CLOSE_PAREN))
11397             outputs = cp_parser_asm_operand_list (parser);
11398         }
11399       /* If the next token is `::', there are no outputs, and the
11400          next token is the beginning of the inputs.  */
11401       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11402         /* The inputs are coming next.  */
11403         inputs_p = true;
11404
11405       /* Look for inputs.  */
11406       if (inputs_p
11407           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11408         {
11409           /* Consume the `:' or `::'.  */
11410           cp_lexer_consume_token (parser->lexer);
11411           /* Parse the output-operands.  */
11412           if (cp_lexer_next_token_is_not (parser->lexer,
11413                                           CPP_COLON)
11414               && cp_lexer_next_token_is_not (parser->lexer,
11415                                              CPP_CLOSE_PAREN))
11416             inputs = cp_parser_asm_operand_list (parser);
11417         }
11418       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11419         /* The clobbers are coming next.  */
11420         clobbers_p = true;
11421
11422       /* Look for clobbers.  */
11423       if (clobbers_p
11424           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11425         {
11426           /* Consume the `:' or `::'.  */
11427           cp_lexer_consume_token (parser->lexer);
11428           /* Parse the clobbers.  */
11429           if (cp_lexer_next_token_is_not (parser->lexer,
11430                                           CPP_CLOSE_PAREN))
11431             clobbers = cp_parser_asm_clobber_list (parser);
11432         }
11433     }
11434   /* Look for the closing `)'.  */
11435   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11436     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11437                                            /*consume_paren=*/true);
11438   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11439
11440   /* Create the ASM_EXPR.  */
11441   if (parser->in_function_body)
11442     {
11443       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11444                                   inputs, clobbers);
11445       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11446       if (!extended_p)
11447         {
11448           tree temp = asm_stmt;
11449           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11450             temp = TREE_OPERAND (temp, 0);
11451
11452           ASM_INPUT_P (temp) = 1;
11453         }
11454     }
11455   else
11456     cgraph_add_asm_node (string);
11457 }
11458
11459 /* Declarators [gram.dcl.decl] */
11460
11461 /* Parse an init-declarator.
11462
11463    init-declarator:
11464      declarator initializer [opt]
11465
11466    GNU Extension:
11467
11468    init-declarator:
11469      declarator asm-specification [opt] attributes [opt] initializer [opt]
11470
11471    function-definition:
11472      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11473        function-body
11474      decl-specifier-seq [opt] declarator function-try-block
11475
11476    GNU Extension:
11477
11478    function-definition:
11479      __extension__ function-definition
11480
11481    The DECL_SPECIFIERS apply to this declarator.  Returns a
11482    representation of the entity declared.  If MEMBER_P is TRUE, then
11483    this declarator appears in a class scope.  The new DECL created by
11484    this declarator is returned.
11485
11486    The CHECKS are access checks that should be performed once we know
11487    what entity is being declared (and, therefore, what classes have
11488    befriended it).
11489
11490    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11491    for a function-definition here as well.  If the declarator is a
11492    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11493    be TRUE upon return.  By that point, the function-definition will
11494    have been completely parsed.
11495
11496    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11497    is FALSE.  */
11498
11499 static tree
11500 cp_parser_init_declarator (cp_parser* parser,
11501                            cp_decl_specifier_seq *decl_specifiers,
11502                            VEC (deferred_access_check,gc)* checks,
11503                            bool function_definition_allowed_p,
11504                            bool member_p,
11505                            int declares_class_or_enum,
11506                            bool* function_definition_p)
11507 {
11508   cp_token *token;
11509   cp_declarator *declarator;
11510   tree prefix_attributes;
11511   tree attributes;
11512   tree asm_specification;
11513   tree initializer;
11514   tree decl = NULL_TREE;
11515   tree scope;
11516   bool is_initialized;
11517   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11518      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11519      "(...)".  */
11520   enum cpp_ttype initialization_kind;
11521   bool is_parenthesized_init = false;
11522   bool is_non_constant_init;
11523   int ctor_dtor_or_conv_p;
11524   bool friend_p;
11525   tree pushed_scope = NULL;
11526
11527   /* Gather the attributes that were provided with the
11528      decl-specifiers.  */
11529   prefix_attributes = decl_specifiers->attributes;
11530
11531   /* Assume that this is not the declarator for a function
11532      definition.  */
11533   if (function_definition_p)
11534     *function_definition_p = false;
11535
11536   /* Defer access checks while parsing the declarator; we cannot know
11537      what names are accessible until we know what is being
11538      declared.  */
11539   resume_deferring_access_checks ();
11540
11541   /* Parse the declarator.  */
11542   declarator
11543     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11544                             &ctor_dtor_or_conv_p,
11545                             /*parenthesized_p=*/NULL,
11546                             /*member_p=*/false);
11547   /* Gather up the deferred checks.  */
11548   stop_deferring_access_checks ();
11549
11550   /* If the DECLARATOR was erroneous, there's no need to go
11551      further.  */
11552   if (declarator == cp_error_declarator)
11553     return error_mark_node;
11554
11555   /* Check that the number of template-parameter-lists is OK.  */
11556   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11557     return error_mark_node;
11558
11559   if (declares_class_or_enum & 2)
11560     cp_parser_check_for_definition_in_return_type (declarator,
11561                                                    decl_specifiers->type);
11562
11563   /* Figure out what scope the entity declared by the DECLARATOR is
11564      located in.  `grokdeclarator' sometimes changes the scope, so
11565      we compute it now.  */
11566   scope = get_scope_of_declarator (declarator);
11567
11568   /* If we're allowing GNU extensions, look for an asm-specification
11569      and attributes.  */
11570   if (cp_parser_allow_gnu_extensions_p (parser))
11571     {
11572       /* Look for an asm-specification.  */
11573       asm_specification = cp_parser_asm_specification_opt (parser);
11574       /* And attributes.  */
11575       attributes = cp_parser_attributes_opt (parser);
11576     }
11577   else
11578     {
11579       asm_specification = NULL_TREE;
11580       attributes = NULL_TREE;
11581     }
11582
11583   /* Peek at the next token.  */
11584   token = cp_lexer_peek_token (parser->lexer);
11585   /* Check to see if the token indicates the start of a
11586      function-definition.  */
11587   if (cp_parser_token_starts_function_definition_p (token))
11588     {
11589       if (!function_definition_allowed_p)
11590         {
11591           /* If a function-definition should not appear here, issue an
11592              error message.  */
11593           cp_parser_error (parser,
11594                            "a function-definition is not allowed here");
11595           return error_mark_node;
11596         }
11597       else
11598         {
11599           /* Neither attributes nor an asm-specification are allowed
11600              on a function-definition.  */
11601           if (asm_specification)
11602             error ("an asm-specification is not allowed on a function-definition");
11603           if (attributes)
11604             error ("attributes are not allowed on a function-definition");
11605           /* This is a function-definition.  */
11606           *function_definition_p = true;
11607
11608           /* Parse the function definition.  */
11609           if (member_p)
11610             decl = cp_parser_save_member_function_body (parser,
11611                                                         decl_specifiers,
11612                                                         declarator,
11613                                                         prefix_attributes);
11614           else
11615             decl
11616               = (cp_parser_function_definition_from_specifiers_and_declarator
11617                  (parser, decl_specifiers, prefix_attributes, declarator));
11618
11619           return decl;
11620         }
11621     }
11622
11623   /* [dcl.dcl]
11624
11625      Only in function declarations for constructors, destructors, and
11626      type conversions can the decl-specifier-seq be omitted.
11627
11628      We explicitly postpone this check past the point where we handle
11629      function-definitions because we tolerate function-definitions
11630      that are missing their return types in some modes.  */
11631   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11632     {
11633       cp_parser_error (parser,
11634                        "expected constructor, destructor, or type conversion");
11635       return error_mark_node;
11636     }
11637
11638   /* An `=' or an `(' indicates an initializer.  */
11639   if (token->type == CPP_EQ
11640       || token->type == CPP_OPEN_PAREN)
11641     {
11642       is_initialized = true;
11643       initialization_kind = token->type;
11644     }
11645   else
11646     {
11647       /* If the init-declarator isn't initialized and isn't followed by a
11648          `,' or `;', it's not a valid init-declarator.  */
11649       if (token->type != CPP_COMMA
11650           && token->type != CPP_SEMICOLON)
11651         {
11652           cp_parser_error (parser, "expected initializer");
11653           return error_mark_node;
11654         }
11655       is_initialized = false;
11656       initialization_kind = CPP_EOF;
11657     }
11658
11659   /* Because start_decl has side-effects, we should only call it if we
11660      know we're going ahead.  By this point, we know that we cannot
11661      possibly be looking at any other construct.  */
11662   cp_parser_commit_to_tentative_parse (parser);
11663
11664   /* If the decl specifiers were bad, issue an error now that we're
11665      sure this was intended to be a declarator.  Then continue
11666      declaring the variable(s), as int, to try to cut down on further
11667      errors.  */
11668   if (decl_specifiers->any_specifiers_p
11669       && decl_specifiers->type == error_mark_node)
11670     {
11671       cp_parser_error (parser, "invalid type in declaration");
11672       decl_specifiers->type = integer_type_node;
11673     }
11674
11675   /* Check to see whether or not this declaration is a friend.  */
11676   friend_p = cp_parser_friend_p (decl_specifiers);
11677
11678   /* Enter the newly declared entry in the symbol table.  If we're
11679      processing a declaration in a class-specifier, we wait until
11680      after processing the initializer.  */
11681   if (!member_p)
11682     {
11683       if (parser->in_unbraced_linkage_specification_p)
11684         decl_specifiers->storage_class = sc_extern;
11685       decl = start_decl (declarator, decl_specifiers,
11686                          is_initialized, attributes, prefix_attributes,
11687                          &pushed_scope);
11688     }
11689   else if (scope)
11690     /* Enter the SCOPE.  That way unqualified names appearing in the
11691        initializer will be looked up in SCOPE.  */
11692     pushed_scope = push_scope (scope);
11693
11694   /* Perform deferred access control checks, now that we know in which
11695      SCOPE the declared entity resides.  */
11696   if (!member_p && decl)
11697     {
11698       tree saved_current_function_decl = NULL_TREE;
11699
11700       /* If the entity being declared is a function, pretend that we
11701          are in its scope.  If it is a `friend', it may have access to
11702          things that would not otherwise be accessible.  */
11703       if (TREE_CODE (decl) == FUNCTION_DECL)
11704         {
11705           saved_current_function_decl = current_function_decl;
11706           current_function_decl = decl;
11707         }
11708
11709       /* Perform access checks for template parameters.  */
11710       cp_parser_perform_template_parameter_access_checks (checks);
11711
11712       /* Perform the access control checks for the declarator and the
11713          the decl-specifiers.  */
11714       perform_deferred_access_checks ();
11715
11716       /* Restore the saved value.  */
11717       if (TREE_CODE (decl) == FUNCTION_DECL)
11718         current_function_decl = saved_current_function_decl;
11719     }
11720
11721   /* Parse the initializer.  */
11722   initializer = NULL_TREE;
11723   is_parenthesized_init = false;
11724   is_non_constant_init = true;
11725   if (is_initialized)
11726     {
11727       if (function_declarator_p (declarator))
11728         {
11729            if (initialization_kind == CPP_EQ)
11730              initializer = cp_parser_pure_specifier (parser);
11731            else
11732              {
11733                /* If the declaration was erroneous, we don't really
11734                   know what the user intended, so just silently
11735                   consume the initializer.  */
11736                if (decl != error_mark_node)
11737                  error ("initializer provided for function");
11738                cp_parser_skip_to_closing_parenthesis (parser,
11739                                                       /*recovering=*/true,
11740                                                       /*or_comma=*/false,
11741                                                       /*consume_paren=*/true);
11742              }
11743         }
11744       else
11745         initializer = cp_parser_initializer (parser,
11746                                              &is_parenthesized_init,
11747                                              &is_non_constant_init);
11748     }
11749
11750   /* The old parser allows attributes to appear after a parenthesized
11751      initializer.  Mark Mitchell proposed removing this functionality
11752      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11753      attributes -- but ignores them.  */
11754   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11755     if (cp_parser_attributes_opt (parser))
11756       warning (OPT_Wattributes,
11757                "attributes after parenthesized initializer ignored");
11758
11759   /* For an in-class declaration, use `grokfield' to create the
11760      declaration.  */
11761   if (member_p)
11762     {
11763       if (pushed_scope)
11764         {
11765           pop_scope (pushed_scope);
11766           pushed_scope = false;
11767         }
11768       decl = grokfield (declarator, decl_specifiers,
11769                         initializer, !is_non_constant_init,
11770                         /*asmspec=*/NULL_TREE,
11771                         prefix_attributes);
11772       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11773         cp_parser_save_default_args (parser, decl);
11774     }
11775
11776   /* Finish processing the declaration.  But, skip friend
11777      declarations.  */
11778   if (!friend_p && decl && decl != error_mark_node)
11779     {
11780       cp_finish_decl (decl,
11781                       initializer, !is_non_constant_init,
11782                       asm_specification,
11783                       /* If the initializer is in parentheses, then this is
11784                          a direct-initialization, which means that an
11785                          `explicit' constructor is OK.  Otherwise, an
11786                          `explicit' constructor cannot be used.  */
11787                       ((is_parenthesized_init || !is_initialized)
11788                      ? 0 : LOOKUP_ONLYCONVERTING));
11789     }
11790   if (!friend_p && pushed_scope)
11791     pop_scope (pushed_scope);
11792
11793   return decl;
11794 }
11795
11796 /* APPLE LOCAL begin blocks 6040305 (cc) */
11797 static cp_cv_quals
11798 cp_parser_cv_qualifier_or_attribute_seq_opt (cp_parser *parser, tree *attrs_p)
11799 {
11800   cp_cv_quals quals = TYPE_UNQUALIFIED;
11801   cp_cv_quals q;
11802   cp_token *token;
11803
11804   *attrs_p = NULL_TREE;
11805   while (true)
11806     {
11807       /* Peek at the next token.  */
11808       token = cp_lexer_peek_token (parser->lexer);
11809       /* Handle attributes.  */
11810       if (token->keyword == RID_ATTRIBUTE)
11811         {
11812           /* Parse the attributes.  */
11813           *attrs_p = chainon (*attrs_p,
11814                               cp_parser_attributes_opt (parser));
11815           continue;
11816         }
11817       
11818       q = cp_parser_cv_qualifier_seq_opt (parser);
11819       if (q == TYPE_UNQUALIFIED)
11820         break;
11821       quals |= q;
11822     }
11823   return quals;
11824 }
11825 /* APPLE LOCAL end blocks 6040305 (cc) */
11826
11827 /* Parse a declarator.
11828
11829    declarator:
11830      direct-declarator
11831      ptr-operator declarator
11832
11833    abstract-declarator:
11834      ptr-operator abstract-declarator [opt]
11835      direct-abstract-declarator
11836
11837    GNU Extensions:
11838
11839    declarator:
11840      attributes [opt] direct-declarator
11841      attributes [opt] ptr-operator declarator
11842
11843    abstract-declarator:
11844      attributes [opt] ptr-operator abstract-declarator [opt]
11845      attributes [opt] direct-abstract-declarator
11846
11847      APPLE LOCAL begin blocks 6339747
11848    block-declarator:
11849      attributes [opt] ptr-operator block-declarator [opt]
11850      attributes [opt] direct-block-declarator
11851      APPLE LOCAL end blocks 6339747
11852
11853    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11854    detect constructor, destructor or conversion operators. It is set
11855    to -1 if the declarator is a name, and +1 if it is a
11856    function. Otherwise it is set to zero. Usually you just want to
11857    test for >0, but internally the negative value is used.
11858
11859    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11860    a decl-specifier-seq unless it declares a constructor, destructor,
11861    or conversion.  It might seem that we could check this condition in
11862    semantic analysis, rather than parsing, but that makes it difficult
11863    to handle something like `f()'.  We want to notice that there are
11864    no decl-specifiers, and therefore realize that this is an
11865    expression, not a declaration.)
11866
11867    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11868    the declarator is a direct-declarator of the form "(...)".
11869
11870    MEMBER_P is true iff this declarator is a member-declarator.  */
11871
11872 static cp_declarator *
11873 cp_parser_declarator (cp_parser* parser,
11874                       cp_parser_declarator_kind dcl_kind,
11875                       int* ctor_dtor_or_conv_p,
11876                       bool* parenthesized_p,
11877                       bool member_p)
11878 {
11879   cp_token *token;
11880   cp_declarator *declarator;
11881   enum tree_code code;
11882   cp_cv_quals cv_quals;
11883   tree class_type;
11884   tree attributes = NULL_TREE;
11885
11886   /* Assume this is not a constructor, destructor, or type-conversion
11887      operator.  */
11888   if (ctor_dtor_or_conv_p)
11889     *ctor_dtor_or_conv_p = 0;
11890
11891   if (cp_parser_allow_gnu_extensions_p (parser))
11892     attributes = cp_parser_attributes_opt (parser);
11893
11894   /* Peek at the next token.  */
11895   token = cp_lexer_peek_token (parser->lexer);
11896
11897   /* APPLE LOCAL begin blocks 6040305 (cc) */
11898   if (flag_blocks && token->type == CPP_XOR)
11899     {
11900       cp_cv_quals quals;
11901       cp_declarator *inner;
11902       tree attrs;
11903
11904       cp_lexer_consume_token (parser->lexer);
11905
11906       /* cp_parse_declspecs (parser, quals_attrs, false, false, true); */
11907       quals = cp_parser_cv_qualifier_or_attribute_seq_opt (parser, &attrs);
11908
11909       inner = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, 
11910                                     /*ctor_dtor_or_conv_p=*/NULL,
11911                                     /*parenthesized_p=*/NULL,
11912                                     /*member_p=*/false);
11913       if (inner == cp_error_declarator)
11914         return inner;
11915       return make_block_pointer_declarator (attrs, quals, inner);
11916     }
11917   /* APPLE LOCAL end blocks 6040305 (cc) */
11918
11919   /* Check for the ptr-operator production.  */
11920   cp_parser_parse_tentatively (parser);
11921   /* Parse the ptr-operator.  */
11922   code = cp_parser_ptr_operator (parser,
11923                                  &class_type,
11924                                  &cv_quals);
11925   /* If that worked, then we have a ptr-operator.  */
11926   if (cp_parser_parse_definitely (parser))
11927     {
11928       /* If a ptr-operator was found, then this declarator was not
11929          parenthesized.  */
11930       if (parenthesized_p)
11931         *parenthesized_p = true;
11932       /* The dependent declarator is optional if we are parsing an
11933          abstract-declarator.  */
11934       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11935         cp_parser_parse_tentatively (parser);
11936
11937       /* Parse the dependent declarator.  */
11938       declarator = cp_parser_declarator (parser, dcl_kind,
11939                                          /*ctor_dtor_or_conv_p=*/NULL,
11940                                          /*parenthesized_p=*/NULL,
11941                                          /*member_p=*/false);
11942
11943       /* If we are parsing an abstract-declarator, we must handle the
11944          case where the dependent declarator is absent.  */
11945       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11946           && !cp_parser_parse_definitely (parser))
11947         declarator = NULL;
11948
11949       /* Build the representation of the ptr-operator.  */
11950       if (class_type)
11951         declarator = make_ptrmem_declarator (cv_quals,
11952                                              class_type,
11953                                              declarator);
11954       else if (code == INDIRECT_REF)
11955         declarator = make_pointer_declarator (cv_quals, declarator);
11956       else
11957         declarator = make_reference_declarator (cv_quals, declarator);
11958     }
11959   /* Everything else is a direct-declarator.  */
11960   else
11961     {
11962       if (parenthesized_p)
11963         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11964                                                    CPP_OPEN_PAREN);
11965       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11966                                                 ctor_dtor_or_conv_p,
11967                                                 member_p);
11968     }
11969
11970   if (attributes && declarator && declarator != cp_error_declarator)
11971     declarator->attributes = attributes;
11972
11973   return declarator;
11974 }
11975
11976 /* Parse a direct-declarator or direct-abstract-declarator.
11977
11978    direct-declarator:
11979      declarator-id
11980      direct-declarator ( parameter-declaration-clause )
11981        cv-qualifier-seq [opt]
11982        exception-specification [opt]
11983      direct-declarator [ constant-expression [opt] ]
11984      ( declarator )
11985
11986    direct-abstract-declarator:
11987      direct-abstract-declarator [opt]
11988        ( parameter-declaration-clause )
11989        cv-qualifier-seq [opt]
11990        exception-specification [opt]
11991      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11992      ( abstract-declarator )
11993
11994      APPLE LOCAL begin blocks 6339747
11995    GNU Extensions:
11996
11997    direct-block-declarator:
11998      direct-block-declarator [opt]
11999         ( parameter-declaration-clause ) [opt]
12000         exception-specification [opt]
12001      direct-block-declarator [opt] [ constant-expression [opt] ]
12002      ( block-declarator )
12003      APPLE LOCAL end blocks 6339747
12004
12005    Returns a representation of the declarator.  DCL_KIND is
12006    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12007    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12008    we are parsing a direct-declarator.  It is
12009    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12010    of ambiguity we prefer an abstract declarator, as per
12011    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12012    cp_parser_declarator.  */
12013
12014 static cp_declarator *
12015 cp_parser_direct_declarator (cp_parser* parser,
12016                              cp_parser_declarator_kind dcl_kind,
12017                              int* ctor_dtor_or_conv_p,
12018                              bool member_p)
12019 {
12020   cp_token *token;
12021   cp_declarator *declarator = NULL;
12022   tree scope = NULL_TREE;
12023   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12024   bool saved_in_declarator_p = parser->in_declarator_p;
12025   bool first = true;
12026   tree pushed_scope = NULL_TREE;
12027
12028   while (true)
12029     {
12030       /* Peek at the next token.  */
12031       token = cp_lexer_peek_token (parser->lexer);
12032       if (token->type == CPP_OPEN_PAREN)
12033         {
12034           /* This is either a parameter-declaration-clause, or a
12035              parenthesized declarator. When we know we are parsing a
12036              named declarator, it must be a parenthesized declarator
12037              if FIRST is true. For instance, `(int)' is a
12038              parameter-declaration-clause, with an omitted
12039              direct-abstract-declarator. But `((*))', is a
12040              parenthesized abstract declarator. Finally, when T is a
12041              template parameter `(T)' is a
12042              parameter-declaration-clause, and not a parenthesized
12043              named declarator.
12044
12045              We first try and parse a parameter-declaration-clause,
12046              and then try a nested declarator (if FIRST is true).
12047
12048              It is not an error for it not to be a
12049              parameter-declaration-clause, even when FIRST is
12050              false. Consider,
12051
12052                int i (int);
12053                int i (3);
12054
12055              The first is the declaration of a function while the
12056              second is a the definition of a variable, including its
12057              initializer.
12058
12059              Having seen only the parenthesis, we cannot know which of
12060              these two alternatives should be selected.  Even more
12061              complex are examples like:
12062
12063                int i (int (a));
12064                int i (int (3));
12065
12066              The former is a function-declaration; the latter is a
12067              variable initialization.
12068
12069              Thus again, we try a parameter-declaration-clause, and if
12070              that fails, we back out and return.  */
12071
12072           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12073             {
12074               cp_parameter_declarator *params;
12075               unsigned saved_num_template_parameter_lists;
12076
12077               /* In a member-declarator, the only valid interpretation
12078                  of a parenthesis is the start of a
12079                  parameter-declaration-clause.  (It is invalid to
12080                  initialize a static data member with a parenthesized
12081                  initializer; only the "=" form of initialization is
12082                  permitted.)  */
12083               if (!member_p)
12084                 cp_parser_parse_tentatively (parser);
12085
12086               /* Consume the `('.  */
12087               cp_lexer_consume_token (parser->lexer);
12088               if (first)
12089                 {
12090                   /* If this is going to be an abstract declarator, we're
12091                      in a declarator and we can't have default args.  */
12092                   parser->default_arg_ok_p = false;
12093                   parser->in_declarator_p = true;
12094                 }
12095
12096               /* Inside the function parameter list, surrounding
12097                  template-parameter-lists do not apply.  */
12098               saved_num_template_parameter_lists
12099                 = parser->num_template_parameter_lists;
12100               parser->num_template_parameter_lists = 0;
12101
12102               /* Parse the parameter-declaration-clause.  */
12103               params = cp_parser_parameter_declaration_clause (parser);
12104
12105               parser->num_template_parameter_lists
12106                 = saved_num_template_parameter_lists;
12107
12108               /* If all went well, parse the cv-qualifier-seq and the
12109                  exception-specification.  */
12110               if (member_p || cp_parser_parse_definitely (parser))
12111                 {
12112                   cp_cv_quals cv_quals;
12113                   tree exception_specification;
12114
12115                   if (ctor_dtor_or_conv_p)
12116                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12117                   first = false;
12118                   /* Consume the `)'.  */
12119                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12120
12121                   /* APPLE LOCAL begin blocks 6339747 */
12122                   if (dcl_kind != BLOCKDEF)
12123                     {
12124                       /* Parse the cv-qualifier-seq.  */
12125                       cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12126                     }
12127                   else
12128                     cv_quals = TYPE_UNQUALIFIED;
12129                   /* APPLE LOCAL end blocks 6339747 */
12130
12131                   /* And the exception-specification.  */
12132                   exception_specification
12133                     = cp_parser_exception_specification_opt (parser);
12134
12135                   /* Create the function-declarator.  */
12136                   declarator = make_call_declarator (declarator,
12137                                                      params,
12138                                                      cv_quals,
12139                                                      exception_specification);
12140                   /* Any subsequent parameter lists are to do with
12141                      return type, so are not those of the declared
12142                      function.  */
12143                   parser->default_arg_ok_p = false;
12144
12145                   /* Repeat the main loop.  */
12146                   continue;
12147                 }
12148             }
12149
12150           /* If this is the first, we can try a parenthesized
12151              declarator.  */
12152           if (first)
12153             {
12154               bool saved_in_type_id_in_expr_p;
12155
12156               parser->default_arg_ok_p = saved_default_arg_ok_p;
12157               parser->in_declarator_p = saved_in_declarator_p;
12158
12159               /* Consume the `('.  */
12160               cp_lexer_consume_token (parser->lexer);
12161               /* Parse the nested declarator.  */
12162               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12163               parser->in_type_id_in_expr_p = true;
12164               declarator
12165                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12166                                         /*parenthesized_p=*/NULL,
12167                                         member_p);
12168               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12169               first = false;
12170               /* Expect a `)'.  */
12171               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12172                 declarator = cp_error_declarator;
12173               if (declarator == cp_error_declarator)
12174                 break;
12175
12176               goto handle_declarator;
12177             }
12178           /* Otherwise, we must be done.  */
12179           else
12180             break;
12181         }
12182       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12183                && token->type == CPP_OPEN_SQUARE)
12184         {
12185           /* Parse an array-declarator.  */
12186           tree bounds;
12187
12188           if (ctor_dtor_or_conv_p)
12189             *ctor_dtor_or_conv_p = 0;
12190
12191           first = false;
12192           parser->default_arg_ok_p = false;
12193           parser->in_declarator_p = true;
12194           /* Consume the `['.  */
12195           cp_lexer_consume_token (parser->lexer);
12196           /* Peek at the next token.  */
12197           token = cp_lexer_peek_token (parser->lexer);
12198           /* If the next token is `]', then there is no
12199              constant-expression.  */
12200           if (token->type != CPP_CLOSE_SQUARE)
12201             {
12202               bool non_constant_p;
12203
12204               bounds
12205                 = cp_parser_constant_expression (parser,
12206                                                  /*allow_non_constant=*/true,
12207                                                  &non_constant_p);
12208               if (!non_constant_p)
12209                 bounds = fold_non_dependent_expr (bounds);
12210               /* Normally, the array bound must be an integral constant
12211                  expression.  However, as an extension, we allow VLAs
12212                  in function scopes.  */
12213               else if (!parser->in_function_body)
12214                 {
12215                   error ("array bound is not an integer constant");
12216                   bounds = error_mark_node;
12217                 }
12218             }
12219           else
12220             bounds = NULL_TREE;
12221           /* Look for the closing `]'.  */
12222           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12223             {
12224               declarator = cp_error_declarator;
12225               break;
12226             }
12227
12228           declarator = make_array_declarator (declarator, bounds);
12229         }
12230       /* APPLE LOCAL begin blocks 6339747 */
12231       else if (first && (dcl_kind == CP_PARSER_DECLARATOR_NAMED
12232                          || dcl_kind == CP_PARSER_DECLARATOR_EITHER))
12233       /* APPLE LOCAL end blocks 6339747 */
12234         {
12235           tree qualifying_scope;
12236           tree unqualified_name;
12237           special_function_kind sfk;
12238           bool abstract_ok;
12239
12240           /* Parse a declarator-id */
12241           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12242           if (abstract_ok)
12243             cp_parser_parse_tentatively (parser);
12244           unqualified_name
12245             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12246           qualifying_scope = parser->scope;
12247           if (abstract_ok)
12248             {
12249               if (!cp_parser_parse_definitely (parser))
12250                 unqualified_name = error_mark_node;
12251               else if (unqualified_name
12252                        && (qualifying_scope
12253                            || (TREE_CODE (unqualified_name)
12254                                != IDENTIFIER_NODE)))
12255                 {
12256                   cp_parser_error (parser, "expected unqualified-id");
12257                   unqualified_name = error_mark_node;
12258                 }
12259             }
12260
12261           if (!unqualified_name)
12262             return NULL;
12263           if (unqualified_name == error_mark_node)
12264             {
12265               declarator = cp_error_declarator;
12266               break;
12267             }
12268
12269           if (qualifying_scope && at_namespace_scope_p ()
12270               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12271             {
12272               /* In the declaration of a member of a template class
12273                  outside of the class itself, the SCOPE will sometimes
12274                  be a TYPENAME_TYPE.  For example, given:
12275
12276                  template <typename T>
12277                  int S<T>::R::i = 3;
12278
12279                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12280                  this context, we must resolve S<T>::R to an ordinary
12281                  type, rather than a typename type.
12282
12283                  The reason we normally avoid resolving TYPENAME_TYPEs
12284                  is that a specialization of `S' might render
12285                  `S<T>::R' not a type.  However, if `S' is
12286                  specialized, then this `i' will not be used, so there
12287                  is no harm in resolving the types here.  */
12288               tree type;
12289
12290               /* Resolve the TYPENAME_TYPE.  */
12291               type = resolve_typename_type (qualifying_scope,
12292                                             /*only_current_p=*/false);
12293               /* If that failed, the declarator is invalid.  */
12294               if (type == error_mark_node)
12295                 error ("%<%T::%D%> is not a type",
12296                        TYPE_CONTEXT (qualifying_scope),
12297                        TYPE_IDENTIFIER (qualifying_scope));
12298               qualifying_scope = type;
12299             }
12300
12301           sfk = sfk_none;
12302           if (unqualified_name)
12303             {
12304               tree class_type;
12305
12306               if (qualifying_scope
12307                   && CLASS_TYPE_P (qualifying_scope))
12308                 class_type = qualifying_scope;
12309               else
12310                 class_type = current_class_type;
12311
12312               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12313                 {
12314                   tree name_type = TREE_TYPE (unqualified_name);
12315                   if (class_type && same_type_p (name_type, class_type))
12316                     {
12317                       if (qualifying_scope
12318                           && CLASSTYPE_USE_TEMPLATE (name_type))
12319                         {
12320                           error ("invalid use of constructor as a template");
12321                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12322                                   "name the constructor in a qualified name",
12323                                   class_type,
12324                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12325                                   class_type, name_type);
12326                           declarator = cp_error_declarator;
12327                           break;
12328                         }
12329                       else
12330                         unqualified_name = constructor_name (class_type);
12331                     }
12332                   else
12333                     {
12334                       /* We do not attempt to print the declarator
12335                          here because we do not have enough
12336                          information about its original syntactic
12337                          form.  */
12338                       cp_parser_error (parser, "invalid declarator");
12339                       declarator = cp_error_declarator;
12340                       break;
12341                     }
12342                 }
12343
12344               if (class_type)
12345                 {
12346                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12347                     sfk = sfk_destructor;
12348                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12349                     sfk = sfk_conversion;
12350                   else if (/* There's no way to declare a constructor
12351                               for an anonymous type, even if the type
12352                               got a name for linkage purposes.  */
12353                            !TYPE_WAS_ANONYMOUS (class_type)
12354                            && constructor_name_p (unqualified_name,
12355                                                   class_type))
12356                     {
12357                       unqualified_name = constructor_name (class_type);
12358                       sfk = sfk_constructor;
12359                     }
12360
12361                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12362                     *ctor_dtor_or_conv_p = -1;
12363                 }
12364             }
12365           declarator = make_id_declarator (qualifying_scope,
12366                                            unqualified_name,
12367                                            sfk);
12368           declarator->id_loc = token->location;
12369
12370         handle_declarator:;
12371           scope = get_scope_of_declarator (declarator);
12372           if (scope)
12373             /* Any names that appear after the declarator-id for a
12374                member are looked up in the containing scope.  */
12375             pushed_scope = push_scope (scope);
12376           parser->in_declarator_p = true;
12377           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12378               || (declarator && declarator->kind == cdk_id))
12379             /* Default args are only allowed on function
12380                declarations.  */
12381             parser->default_arg_ok_p = saved_default_arg_ok_p;
12382           else
12383             parser->default_arg_ok_p = false;
12384
12385           first = false;
12386         }
12387       /* We're done.  */
12388       else
12389         break;
12390     }
12391
12392   /* For an abstract declarator, we might wind up with nothing at this
12393      point.  That's an error; the declarator is not optional.  */
12394   /* APPLE LOCAL blocks 6339747 */
12395   if (!declarator && dcl_kind != CP_PARSER_DECLARATOR_BLOCK)
12396     cp_parser_error (parser, "expected declarator");
12397
12398   /* If we entered a scope, we must exit it now.  */
12399   if (pushed_scope)
12400     pop_scope (pushed_scope);
12401
12402   parser->default_arg_ok_p = saved_default_arg_ok_p;
12403   parser->in_declarator_p = saved_in_declarator_p;
12404
12405   return declarator;
12406 }
12407
12408 /* Parse a ptr-operator.
12409
12410    ptr-operator:
12411      * cv-qualifier-seq [opt]
12412      &
12413      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12414
12415    GNU Extension:
12416
12417    ptr-operator:
12418      & cv-qualifier-seq [opt]
12419      APPLE LOCAL blocks 6040305 (cc)
12420      ^
12421
12422    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12423    Returns ADDR_EXPR if a reference was used.  In the case of a
12424    pointer-to-member, *TYPE is filled in with the TYPE containing the
12425    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12426    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12427    ERROR_MARK if an error occurred.  */
12428
12429 static enum tree_code
12430 cp_parser_ptr_operator (cp_parser* parser,
12431                         tree* type,
12432                         cp_cv_quals *cv_quals)
12433 {
12434   enum tree_code code = ERROR_MARK;
12435   cp_token *token;
12436
12437   /* Assume that it's not a pointer-to-member.  */
12438   *type = NULL_TREE;
12439   /* And that there are no cv-qualifiers.  */
12440   *cv_quals = TYPE_UNQUALIFIED;
12441
12442   /* Peek at the next token.  */
12443   token = cp_lexer_peek_token (parser->lexer);
12444   /* If it's a `*' or `&' we have a pointer or reference.  */
12445   if (token->type == CPP_MULT || token->type == CPP_AND)
12446     {
12447       /* Remember which ptr-operator we were processing.  */
12448       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12449
12450       /* Consume the `*' or `&'.  */
12451       cp_lexer_consume_token (parser->lexer);
12452
12453       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12454          `&', if we are allowing GNU extensions.  (The only qualifier
12455          that can legally appear after `&' is `restrict', but that is
12456          enforced during semantic analysis.  */
12457       if (code == INDIRECT_REF
12458           || cp_parser_allow_gnu_extensions_p (parser))
12459         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12460     }
12461   else
12462     {
12463       /* Try the pointer-to-member case.  */
12464       cp_parser_parse_tentatively (parser);
12465       /* Look for the optional `::' operator.  */
12466       cp_parser_global_scope_opt (parser,
12467                                   /*current_scope_valid_p=*/false);
12468       /* Look for the nested-name specifier.  */
12469       cp_parser_nested_name_specifier (parser,
12470                                        /*typename_keyword_p=*/false,
12471                                        /*check_dependency_p=*/true,
12472                                        /*type_p=*/false,
12473                                        /*is_declaration=*/false);
12474       /* If we found it, and the next token is a `*', then we are
12475          indeed looking at a pointer-to-member operator.  */
12476       if (!cp_parser_error_occurred (parser)
12477           && cp_parser_require (parser, CPP_MULT, "`*'"))
12478         {
12479           /* Indicate that the `*' operator was used.  */
12480           code = INDIRECT_REF;
12481
12482           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12483             error ("%qD is a namespace", parser->scope);
12484           else
12485             {
12486               /* The type of which the member is a member is given by the
12487                  current SCOPE.  */
12488               *type = parser->scope;
12489               /* The next name will not be qualified.  */
12490               parser->scope = NULL_TREE;
12491               parser->qualifying_scope = NULL_TREE;
12492               parser->object_scope = NULL_TREE;
12493               /* Look for the optional cv-qualifier-seq.  */
12494               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12495             }
12496         }
12497       /* If that didn't work we don't have a ptr-operator.  */
12498       if (!cp_parser_parse_definitely (parser))
12499         cp_parser_error (parser, "expected ptr-operator");
12500     }
12501
12502   return code;
12503 }
12504
12505 /* Parse an (optional) cv-qualifier-seq.
12506
12507    cv-qualifier-seq:
12508      cv-qualifier cv-qualifier-seq [opt]
12509
12510    cv-qualifier:
12511      const
12512      volatile
12513
12514    GNU Extension:
12515
12516    cv-qualifier:
12517      __restrict__
12518
12519    Returns a bitmask representing the cv-qualifiers.  */
12520
12521 static cp_cv_quals
12522 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12523 {
12524   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12525
12526   while (true)
12527     {
12528       cp_token *token;
12529       cp_cv_quals cv_qualifier;
12530
12531       /* Peek at the next token.  */
12532       token = cp_lexer_peek_token (parser->lexer);
12533       /* See if it's a cv-qualifier.  */
12534       switch (token->keyword)
12535         {
12536         case RID_CONST:
12537           cv_qualifier = TYPE_QUAL_CONST;
12538           break;
12539
12540         case RID_VOLATILE:
12541           cv_qualifier = TYPE_QUAL_VOLATILE;
12542           break;
12543
12544         case RID_RESTRICT:
12545           cv_qualifier = TYPE_QUAL_RESTRICT;
12546           break;
12547
12548         default:
12549           cv_qualifier = TYPE_UNQUALIFIED;
12550           break;
12551         }
12552
12553       if (!cv_qualifier)
12554         break;
12555
12556       if (cv_quals & cv_qualifier)
12557         {
12558           error ("duplicate cv-qualifier");
12559           cp_lexer_purge_token (parser->lexer);
12560         }
12561       else
12562         {
12563           cp_lexer_consume_token (parser->lexer);
12564           cv_quals |= cv_qualifier;
12565         }
12566     }
12567
12568   return cv_quals;
12569 }
12570
12571 /* Parse a declarator-id.
12572
12573    declarator-id:
12574      id-expression
12575      :: [opt] nested-name-specifier [opt] type-name
12576
12577    In the `id-expression' case, the value returned is as for
12578    cp_parser_id_expression if the id-expression was an unqualified-id.
12579    If the id-expression was a qualified-id, then a SCOPE_REF is
12580    returned.  The first operand is the scope (either a NAMESPACE_DECL
12581    or TREE_TYPE), but the second is still just a representation of an
12582    unqualified-id.  */
12583
12584 static tree
12585 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12586 {
12587   tree id;
12588   /* The expression must be an id-expression.  Assume that qualified
12589      names are the names of types so that:
12590
12591        template <class T>
12592        int S<T>::R::i = 3;
12593
12594      will work; we must treat `S<T>::R' as the name of a type.
12595      Similarly, assume that qualified names are templates, where
12596      required, so that:
12597
12598        template <class T>
12599        int S<T>::R<T>::i = 3;
12600
12601      will work, too.  */
12602   id = cp_parser_id_expression (parser,
12603                                 /*template_keyword_p=*/false,
12604                                 /*check_dependency_p=*/false,
12605                                 /*template_p=*/NULL,
12606                                 /*declarator_p=*/true,
12607                                 optional_p);
12608   if (id && BASELINK_P (id))
12609     id = BASELINK_FUNCTIONS (id);
12610   return id;
12611 }
12612
12613 /* Parse a type-id.
12614
12615    type-id:
12616      type-specifier-seq abstract-declarator [opt]
12617
12618    Returns the TYPE specified.  */
12619
12620 static tree
12621 cp_parser_type_id (cp_parser* parser)
12622 {
12623   cp_decl_specifier_seq type_specifier_seq;
12624   cp_declarator *abstract_declarator;
12625
12626   /* Parse the type-specifier-seq.  */
12627   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12628                                 &type_specifier_seq);
12629   if (type_specifier_seq.type == error_mark_node)
12630     return error_mark_node;
12631
12632   /* There might or might not be an abstract declarator.  */
12633   cp_parser_parse_tentatively (parser);
12634   /* Look for the declarator.  */
12635   abstract_declarator
12636     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12637                             /*parenthesized_p=*/NULL,
12638                             /*member_p=*/false);
12639   /* Check to see if there really was a declarator.  */
12640   if (!cp_parser_parse_definitely (parser))
12641     abstract_declarator = NULL;
12642
12643   return groktypename (&type_specifier_seq, abstract_declarator);
12644 }
12645
12646 /* Parse a type-specifier-seq.
12647
12648    type-specifier-seq:
12649      type-specifier type-specifier-seq [opt]
12650
12651    GNU extension:
12652
12653    type-specifier-seq:
12654      attributes type-specifier-seq [opt]
12655
12656    If IS_CONDITION is true, we are at the start of a "condition",
12657    e.g., we've just seen "if (".
12658
12659    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12660
12661 static void
12662 cp_parser_type_specifier_seq (cp_parser* parser,
12663                               bool is_condition,
12664                               cp_decl_specifier_seq *type_specifier_seq)
12665 {
12666   bool seen_type_specifier = false;
12667   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12668
12669   /* Clear the TYPE_SPECIFIER_SEQ.  */
12670   clear_decl_specs (type_specifier_seq);
12671
12672   /* Parse the type-specifiers and attributes.  */
12673   while (true)
12674     {
12675       tree type_specifier;
12676       bool is_cv_qualifier;
12677
12678       /* Check for attributes first.  */
12679       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12680         {
12681           type_specifier_seq->attributes =
12682             chainon (type_specifier_seq->attributes,
12683                      cp_parser_attributes_opt (parser));
12684           continue;
12685         }
12686
12687       /* Look for the type-specifier.  */
12688       type_specifier = cp_parser_type_specifier (parser,
12689                                                  flags,
12690                                                  type_specifier_seq,
12691                                                  /*is_declaration=*/false,
12692                                                  NULL,
12693                                                  &is_cv_qualifier);
12694       if (!type_specifier)
12695         {
12696           /* If the first type-specifier could not be found, this is not a
12697              type-specifier-seq at all.  */
12698           if (!seen_type_specifier)
12699             {
12700               cp_parser_error (parser, "expected type-specifier");
12701               type_specifier_seq->type = error_mark_node;
12702               return;
12703             }
12704           /* If subsequent type-specifiers could not be found, the
12705              type-specifier-seq is complete.  */
12706           break;
12707         }
12708
12709       seen_type_specifier = true;
12710       /* The standard says that a condition can be:
12711
12712             type-specifier-seq declarator = assignment-expression
12713
12714          However, given:
12715
12716            struct S {};
12717            if (int S = ...)
12718
12719          we should treat the "S" as a declarator, not as a
12720          type-specifier.  The standard doesn't say that explicitly for
12721          type-specifier-seq, but it does say that for
12722          decl-specifier-seq in an ordinary declaration.  Perhaps it
12723          would be clearer just to allow a decl-specifier-seq here, and
12724          then add a semantic restriction that if any decl-specifiers
12725          that are not type-specifiers appear, the program is invalid.  */
12726       if (is_condition && !is_cv_qualifier)
12727         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12728     }
12729
12730   cp_parser_check_decl_spec (type_specifier_seq);
12731 }
12732
12733 /* Parse a parameter-declaration-clause.
12734
12735    parameter-declaration-clause:
12736      parameter-declaration-list [opt] ... [opt]
12737      parameter-declaration-list , ...
12738
12739    Returns a representation for the parameter declarations.  A return
12740    value of NULL indicates a parameter-declaration-clause consisting
12741    only of an ellipsis.  */
12742
12743 static cp_parameter_declarator *
12744 cp_parser_parameter_declaration_clause (cp_parser* parser)
12745 {
12746   cp_parameter_declarator *parameters;
12747   cp_token *token;
12748   bool ellipsis_p;
12749   bool is_error;
12750
12751   /* Peek at the next token.  */
12752   token = cp_lexer_peek_token (parser->lexer);
12753   /* Check for trivial parameter-declaration-clauses.  */
12754   if (token->type == CPP_ELLIPSIS)
12755     {
12756       /* Consume the `...' token.  */
12757       cp_lexer_consume_token (parser->lexer);
12758       return NULL;
12759     }
12760   else if (token->type == CPP_CLOSE_PAREN)
12761     /* There are no parameters.  */
12762     {
12763 #ifndef NO_IMPLICIT_EXTERN_C
12764       if (in_system_header && current_class_type == NULL
12765           && current_lang_name == lang_name_c)
12766         return NULL;
12767       else
12768 #endif
12769         return no_parameters;
12770     }
12771   /* Check for `(void)', too, which is a special case.  */
12772   else if (token->keyword == RID_VOID
12773            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12774                == CPP_CLOSE_PAREN))
12775     {
12776       /* Consume the `void' token.  */
12777       cp_lexer_consume_token (parser->lexer);
12778       /* There are no parameters.  */
12779       return no_parameters;
12780     }
12781
12782   /* Parse the parameter-declaration-list.  */
12783   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12784   /* If a parse error occurred while parsing the
12785      parameter-declaration-list, then the entire
12786      parameter-declaration-clause is erroneous.  */
12787   if (is_error)
12788     return NULL;
12789
12790   /* Peek at the next token.  */
12791   token = cp_lexer_peek_token (parser->lexer);
12792   /* If it's a `,', the clause should terminate with an ellipsis.  */
12793   if (token->type == CPP_COMMA)
12794     {
12795       /* Consume the `,'.  */
12796       cp_lexer_consume_token (parser->lexer);
12797       /* Expect an ellipsis.  */
12798       ellipsis_p
12799         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12800     }
12801   /* It might also be `...' if the optional trailing `,' was
12802      omitted.  */
12803   else if (token->type == CPP_ELLIPSIS)
12804     {
12805       /* Consume the `...' token.  */
12806       cp_lexer_consume_token (parser->lexer);
12807       /* And remember that we saw it.  */
12808       ellipsis_p = true;
12809     }
12810   else
12811     ellipsis_p = false;
12812
12813   /* Finish the parameter list.  */
12814   if (parameters && ellipsis_p)
12815     parameters->ellipsis_p = true;
12816
12817   return parameters;
12818 }
12819
12820 /* Parse a parameter-declaration-list.
12821
12822    parameter-declaration-list:
12823      parameter-declaration
12824      parameter-declaration-list , parameter-declaration
12825
12826    Returns a representation of the parameter-declaration-list, as for
12827    cp_parser_parameter_declaration_clause.  However, the
12828    `void_list_node' is never appended to the list.  Upon return,
12829    *IS_ERROR will be true iff an error occurred.  */
12830
12831 static cp_parameter_declarator *
12832 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12833 {
12834   cp_parameter_declarator *parameters = NULL;
12835   cp_parameter_declarator **tail = &parameters;
12836   bool saved_in_unbraced_linkage_specification_p;
12837
12838   /* Assume all will go well.  */
12839   *is_error = false;
12840   /* The special considerations that apply to a function within an
12841      unbraced linkage specifications do not apply to the parameters
12842      to the function.  */
12843   saved_in_unbraced_linkage_specification_p 
12844     = parser->in_unbraced_linkage_specification_p;
12845   parser->in_unbraced_linkage_specification_p = false;
12846
12847   /* Look for more parameters.  */
12848   while (true)
12849     {
12850       cp_parameter_declarator *parameter;
12851       bool parenthesized_p;
12852       /* Parse the parameter.  */
12853       parameter
12854         = cp_parser_parameter_declaration (parser,
12855                                            /*template_parm_p=*/false,
12856                                            &parenthesized_p);
12857
12858       /* If a parse error occurred parsing the parameter declaration,
12859          then the entire parameter-declaration-list is erroneous.  */
12860       if (!parameter)
12861         {
12862           *is_error = true;
12863           parameters = NULL;
12864           break;
12865         }
12866       /* Add the new parameter to the list.  */
12867       *tail = parameter;
12868       tail = &parameter->next;
12869
12870       /* Peek at the next token.  */
12871       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12872           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12873           /* These are for Objective-C++ */
12874           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12875           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12876         /* The parameter-declaration-list is complete.  */
12877         break;
12878       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12879         {
12880           cp_token *token;
12881
12882           /* Peek at the next token.  */
12883           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12884           /* If it's an ellipsis, then the list is complete.  */
12885           if (token->type == CPP_ELLIPSIS)
12886             break;
12887           /* Otherwise, there must be more parameters.  Consume the
12888              `,'.  */
12889           cp_lexer_consume_token (parser->lexer);
12890           /* When parsing something like:
12891
12892                 int i(float f, double d)
12893
12894              we can tell after seeing the declaration for "f" that we
12895              are not looking at an initialization of a variable "i",
12896              but rather at the declaration of a function "i".
12897
12898              Due to the fact that the parsing of template arguments
12899              (as specified to a template-id) requires backtracking we
12900              cannot use this technique when inside a template argument
12901              list.  */
12902           if (!parser->in_template_argument_list_p
12903               && !parser->in_type_id_in_expr_p
12904               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12905               /* However, a parameter-declaration of the form
12906                  "foat(f)" (which is a valid declaration of a
12907                  parameter "f") can also be interpreted as an
12908                  expression (the conversion of "f" to "float").  */
12909               && !parenthesized_p)
12910             cp_parser_commit_to_tentative_parse (parser);
12911         }
12912       else
12913         {
12914           cp_parser_error (parser, "expected %<,%> or %<...%>");
12915           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12916             cp_parser_skip_to_closing_parenthesis (parser,
12917                                                    /*recovering=*/true,
12918                                                    /*or_comma=*/false,
12919                                                    /*consume_paren=*/false);
12920           break;
12921         }
12922     }
12923
12924   parser->in_unbraced_linkage_specification_p
12925     = saved_in_unbraced_linkage_specification_p;
12926
12927   return parameters;
12928 }
12929
12930 /* Parse a parameter declaration.
12931
12932    parameter-declaration:
12933      decl-specifier-seq declarator
12934      decl-specifier-seq declarator = assignment-expression
12935      decl-specifier-seq abstract-declarator [opt]
12936      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12937
12938    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12939    declares a template parameter.  (In that case, a non-nested `>'
12940    token encountered during the parsing of the assignment-expression
12941    is not interpreted as a greater-than operator.)
12942
12943    Returns a representation of the parameter, or NULL if an error
12944    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12945    true iff the declarator is of the form "(p)".  */
12946
12947 static cp_parameter_declarator *
12948 cp_parser_parameter_declaration (cp_parser *parser,
12949                                  bool template_parm_p,
12950                                  bool *parenthesized_p)
12951 {
12952   int declares_class_or_enum;
12953   bool greater_than_is_operator_p;
12954   cp_decl_specifier_seq decl_specifiers;
12955   cp_declarator *declarator;
12956   tree default_argument;
12957   cp_token *token;
12958   const char *saved_message;
12959
12960   /* In a template parameter, `>' is not an operator.
12961
12962      [temp.param]
12963
12964      When parsing a default template-argument for a non-type
12965      template-parameter, the first non-nested `>' is taken as the end
12966      of the template parameter-list rather than a greater-than
12967      operator.  */
12968   greater_than_is_operator_p = !template_parm_p;
12969
12970   /* Type definitions may not appear in parameter types.  */
12971   saved_message = parser->type_definition_forbidden_message;
12972   parser->type_definition_forbidden_message
12973     = "types may not be defined in parameter types";
12974
12975   /* Parse the declaration-specifiers.  */
12976   cp_parser_decl_specifier_seq (parser,
12977                                 CP_PARSER_FLAGS_NONE,
12978                                 &decl_specifiers,
12979                                 &declares_class_or_enum);
12980   /* If an error occurred, there's no reason to attempt to parse the
12981      rest of the declaration.  */
12982   if (cp_parser_error_occurred (parser))
12983     {
12984       parser->type_definition_forbidden_message = saved_message;
12985       return NULL;
12986     }
12987
12988   /* Peek at the next token.  */
12989   token = cp_lexer_peek_token (parser->lexer);
12990   /* If the next token is a `)', `,', `=', `>', or `...', then there
12991      is no declarator.  */
12992   if (token->type == CPP_CLOSE_PAREN
12993       || token->type == CPP_COMMA
12994       || token->type == CPP_EQ
12995       || token->type == CPP_ELLIPSIS
12996       || token->type == CPP_GREATER)
12997     {
12998       declarator = NULL;
12999       if (parenthesized_p)
13000         *parenthesized_p = false;
13001     }
13002   /* Otherwise, there should be a declarator.  */
13003   else
13004     {
13005       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13006       parser->default_arg_ok_p = false;
13007
13008       /* After seeing a decl-specifier-seq, if the next token is not a
13009          "(", there is no possibility that the code is a valid
13010          expression.  Therefore, if parsing tentatively, we commit at
13011          this point.  */
13012       if (!parser->in_template_argument_list_p
13013           /* In an expression context, having seen:
13014
13015                (int((char ...
13016
13017              we cannot be sure whether we are looking at a
13018              function-type (taking a "char" as a parameter) or a cast
13019              of some object of type "char" to "int".  */
13020           && !parser->in_type_id_in_expr_p
13021           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13022           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13023         cp_parser_commit_to_tentative_parse (parser);
13024       /* Parse the declarator.  */
13025       declarator = cp_parser_declarator (parser,
13026                                          CP_PARSER_DECLARATOR_EITHER,
13027                                          /*ctor_dtor_or_conv_p=*/NULL,
13028                                          parenthesized_p,
13029                                          /*member_p=*/false);
13030       parser->default_arg_ok_p = saved_default_arg_ok_p;
13031       /* After the declarator, allow more attributes.  */
13032       decl_specifiers.attributes
13033         = chainon (decl_specifiers.attributes,
13034                    cp_parser_attributes_opt (parser));
13035     }
13036
13037   /* The restriction on defining new types applies only to the type
13038      of the parameter, not to the default argument.  */
13039   parser->type_definition_forbidden_message = saved_message;
13040
13041   /* If the next token is `=', then process a default argument.  */
13042   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13043     {
13044       bool saved_greater_than_is_operator_p;
13045       /* Consume the `='.  */
13046       cp_lexer_consume_token (parser->lexer);
13047
13048       /* If we are defining a class, then the tokens that make up the
13049          default argument must be saved and processed later.  */
13050       if (!template_parm_p && at_class_scope_p ()
13051           && TYPE_BEING_DEFINED (current_class_type))
13052         {
13053           unsigned depth = 0;
13054           cp_token *first_token;
13055           cp_token *token;
13056
13057           /* Add tokens until we have processed the entire default
13058              argument.  We add the range [first_token, token).  */
13059           first_token = cp_lexer_peek_token (parser->lexer);
13060           while (true)
13061             {
13062               bool done = false;
13063
13064               /* Peek at the next token.  */
13065               token = cp_lexer_peek_token (parser->lexer);
13066               /* What we do depends on what token we have.  */
13067               switch (token->type)
13068                 {
13069                   /* In valid code, a default argument must be
13070                      immediately followed by a `,' `)', or `...'.  */
13071                 case CPP_COMMA:
13072                 case CPP_CLOSE_PAREN:
13073                 case CPP_ELLIPSIS:
13074                   /* If we run into a non-nested `;', `}', or `]',
13075                      then the code is invalid -- but the default
13076                      argument is certainly over.  */
13077                 case CPP_SEMICOLON:
13078                 case CPP_CLOSE_BRACE:
13079                 case CPP_CLOSE_SQUARE:
13080                   if (depth == 0)
13081                     done = true;
13082                   /* Update DEPTH, if necessary.  */
13083                   else if (token->type == CPP_CLOSE_PAREN
13084                            || token->type == CPP_CLOSE_BRACE
13085                            || token->type == CPP_CLOSE_SQUARE)
13086                     --depth;
13087                   break;
13088
13089                 case CPP_OPEN_PAREN:
13090                 case CPP_OPEN_SQUARE:
13091                 case CPP_OPEN_BRACE:
13092                   ++depth;
13093                   break;
13094
13095                 case CPP_GREATER:
13096                   /* If we see a non-nested `>', and `>' is not an
13097                      operator, then it marks the end of the default
13098                      argument.  */
13099                   if (!depth && !greater_than_is_operator_p)
13100                     done = true;
13101                   break;
13102
13103                   /* If we run out of tokens, issue an error message.  */
13104                 case CPP_EOF:
13105                 case CPP_PRAGMA_EOL:
13106                   error ("file ends in default argument");
13107                   done = true;
13108                   break;
13109
13110                 case CPP_NAME:
13111                 case CPP_SCOPE:
13112                   /* In these cases, we should look for template-ids.
13113                      For example, if the default argument is
13114                      `X<int, double>()', we need to do name lookup to
13115                      figure out whether or not `X' is a template; if
13116                      so, the `,' does not end the default argument.
13117
13118                      That is not yet done.  */
13119                   break;
13120
13121                 default:
13122                   break;
13123                 }
13124
13125               /* If we've reached the end, stop.  */
13126               if (done)
13127                 break;
13128
13129               /* Add the token to the token block.  */
13130               token = cp_lexer_consume_token (parser->lexer);
13131             }
13132
13133           /* Create a DEFAULT_ARG to represented the unparsed default
13134              argument.  */
13135           default_argument = make_node (DEFAULT_ARG);
13136           DEFARG_TOKENS (default_argument)
13137             = cp_token_cache_new (first_token, token);
13138           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13139         }
13140       /* Outside of a class definition, we can just parse the
13141          assignment-expression.  */
13142       else
13143         {
13144           bool saved_local_variables_forbidden_p;
13145
13146           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13147              set correctly.  */
13148           saved_greater_than_is_operator_p
13149             = parser->greater_than_is_operator_p;
13150           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13151           /* Local variable names (and the `this' keyword) may not
13152              appear in a default argument.  */
13153           saved_local_variables_forbidden_p
13154             = parser->local_variables_forbidden_p;
13155           parser->local_variables_forbidden_p = true;
13156           /* The default argument expression may cause implicitly
13157              defined member functions to be synthesized, which will
13158              result in garbage collection.  We must treat this
13159              situation as if we were within the body of function so as
13160              to avoid collecting live data on the stack.  */
13161           ++function_depth;
13162           /* Parse the assignment-expression.  */
13163           if (template_parm_p)
13164             push_deferring_access_checks (dk_no_deferred);
13165           default_argument
13166             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13167           if (template_parm_p)
13168             pop_deferring_access_checks ();
13169           /* Restore saved state.  */
13170           --function_depth;
13171           parser->greater_than_is_operator_p
13172             = saved_greater_than_is_operator_p;
13173           parser->local_variables_forbidden_p
13174             = saved_local_variables_forbidden_p;
13175         }
13176       if (!parser->default_arg_ok_p)
13177         {
13178           if (!flag_pedantic_errors)
13179             warning (0, "deprecated use of default argument for parameter of non-function");
13180           else
13181             {
13182               error ("default arguments are only permitted for function parameters");
13183               default_argument = NULL_TREE;
13184             }
13185         }
13186     }
13187   else
13188     default_argument = NULL_TREE;
13189
13190   return make_parameter_declarator (&decl_specifiers,
13191                                     declarator,
13192                                     default_argument);
13193 }
13194
13195 /* Parse a function-body.
13196
13197    function-body:
13198      compound_statement  */
13199
13200 static void
13201 cp_parser_function_body (cp_parser *parser)
13202 {
13203   /* APPLE LOCAL radar 5982990 */
13204   cp_parser_compound_statement (parser, NULL, false, false);
13205 }
13206
13207 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13208    true if a ctor-initializer was present.  */
13209
13210 static bool
13211 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13212 {
13213   tree body;
13214   bool ctor_initializer_p;
13215
13216   /* Begin the function body.  */
13217   body = begin_function_body ();
13218   /* Parse the optional ctor-initializer.  */
13219   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13220   /* Parse the function-body.  */
13221   cp_parser_function_body (parser);
13222   /* Finish the function body.  */
13223   finish_function_body (body);
13224
13225   return ctor_initializer_p;
13226 }
13227
13228 /* Parse an initializer.
13229
13230    initializer:
13231      = initializer-clause
13232      ( expression-list )
13233
13234    Returns an expression representing the initializer.  If no
13235    initializer is present, NULL_TREE is returned.
13236
13237    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13238    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13239    set to FALSE if there is no initializer present.  If there is an
13240    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13241    is set to true; otherwise it is set to false.  */
13242
13243 static tree
13244 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13245                        bool* non_constant_p)
13246 {
13247   cp_token *token;
13248   tree init;
13249
13250   /* Peek at the next token.  */
13251   token = cp_lexer_peek_token (parser->lexer);
13252
13253   /* Let our caller know whether or not this initializer was
13254      parenthesized.  */
13255   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13256   /* Assume that the initializer is constant.  */
13257   *non_constant_p = false;
13258
13259   if (token->type == CPP_EQ)
13260     {
13261       /* Consume the `='.  */
13262       cp_lexer_consume_token (parser->lexer);
13263       /* Parse the initializer-clause.  */
13264       init = cp_parser_initializer_clause (parser, non_constant_p);
13265     }
13266   else if (token->type == CPP_OPEN_PAREN)
13267     init = cp_parser_parenthesized_expression_list (parser, false,
13268                                                     /*cast_p=*/false,
13269                                                     non_constant_p);
13270   else
13271     {
13272       /* Anything else is an error.  */
13273       cp_parser_error (parser, "expected initializer");
13274       init = error_mark_node;
13275     }
13276
13277   return init;
13278 }
13279
13280 /* Parse an initializer-clause.
13281
13282    initializer-clause:
13283      assignment-expression
13284      { initializer-list , [opt] }
13285      { }
13286
13287    Returns an expression representing the initializer.
13288
13289    If the `assignment-expression' production is used the value
13290    returned is simply a representation for the expression.
13291
13292    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13293    the elements of the initializer-list (or NULL, if the last
13294    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13295    NULL_TREE.  There is no way to detect whether or not the optional
13296    trailing `,' was provided.  NON_CONSTANT_P is as for
13297    cp_parser_initializer.  */
13298
13299 static tree
13300 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13301 {
13302   tree initializer;
13303
13304   /* Assume the expression is constant.  */
13305   *non_constant_p = false;
13306
13307   /* If it is not a `{', then we are looking at an
13308      assignment-expression.  */
13309   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13310     {
13311       initializer
13312         = cp_parser_constant_expression (parser,
13313                                         /*allow_non_constant_p=*/true,
13314                                         non_constant_p);
13315       if (!*non_constant_p)
13316         initializer = fold_non_dependent_expr (initializer);
13317     }
13318   else
13319     {
13320       /* Consume the `{' token.  */
13321       cp_lexer_consume_token (parser->lexer);
13322       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13323       initializer = make_node (CONSTRUCTOR);
13324       /* If it's not a `}', then there is a non-trivial initializer.  */
13325       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13326         {
13327           /* Parse the initializer list.  */
13328           CONSTRUCTOR_ELTS (initializer)
13329             = cp_parser_initializer_list (parser, non_constant_p);
13330           /* A trailing `,' token is allowed.  */
13331           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13332             cp_lexer_consume_token (parser->lexer);
13333         }
13334       /* Now, there should be a trailing `}'.  */
13335       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13336     }
13337
13338   return initializer;
13339 }
13340
13341 /* Parse an initializer-list.
13342
13343    initializer-list:
13344      initializer-clause
13345      initializer-list , initializer-clause
13346
13347    GNU Extension:
13348
13349    initializer-list:
13350      identifier : initializer-clause
13351      initializer-list, identifier : initializer-clause
13352
13353    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13354    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13355    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13356    as for cp_parser_initializer.  */
13357
13358 static VEC(constructor_elt,gc) *
13359 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13360 {
13361   VEC(constructor_elt,gc) *v = NULL;
13362
13363   /* Assume all of the expressions are constant.  */
13364   *non_constant_p = false;
13365
13366   /* Parse the rest of the list.  */
13367   while (true)
13368     {
13369       cp_token *token;
13370       tree identifier;
13371       tree initializer;
13372       bool clause_non_constant_p;
13373
13374       /* If the next token is an identifier and the following one is a
13375          colon, we are looking at the GNU designated-initializer
13376          syntax.  */
13377       if (cp_parser_allow_gnu_extensions_p (parser)
13378           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13379           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13380         {
13381           /* Warn the user that they are using an extension.  */
13382           if (pedantic)
13383             pedwarn ("ISO C++ does not allow designated initializers");
13384           /* Consume the identifier.  */
13385           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13386           /* Consume the `:'.  */
13387           cp_lexer_consume_token (parser->lexer);
13388         }
13389       else
13390         identifier = NULL_TREE;
13391
13392       /* Parse the initializer.  */
13393       initializer = cp_parser_initializer_clause (parser,
13394                                                   &clause_non_constant_p);
13395       /* If any clause is non-constant, so is the entire initializer.  */
13396       if (clause_non_constant_p)
13397         *non_constant_p = true;
13398
13399       /* Add it to the vector.  */
13400       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13401
13402       /* If the next token is not a comma, we have reached the end of
13403          the list.  */
13404       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13405         break;
13406
13407       /* Peek at the next token.  */
13408       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13409       /* If the next token is a `}', then we're still done.  An
13410          initializer-clause can have a trailing `,' after the
13411          initializer-list and before the closing `}'.  */
13412       if (token->type == CPP_CLOSE_BRACE)
13413         break;
13414
13415       /* Consume the `,' token.  */
13416       cp_lexer_consume_token (parser->lexer);
13417     }
13418
13419   return v;
13420 }
13421
13422 /* Classes [gram.class] */
13423
13424 /* Parse a class-name.
13425
13426    class-name:
13427      identifier
13428      template-id
13429
13430    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13431    to indicate that names looked up in dependent types should be
13432    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13433    keyword has been used to indicate that the name that appears next
13434    is a template.  TAG_TYPE indicates the explicit tag given before
13435    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13436    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13437    is the class being defined in a class-head.
13438
13439    Returns the TYPE_DECL representing the class.  */
13440
13441 static tree
13442 cp_parser_class_name (cp_parser *parser,
13443                       bool typename_keyword_p,
13444                       bool template_keyword_p,
13445                       enum tag_types tag_type,
13446                       bool check_dependency_p,
13447                       bool class_head_p,
13448                       bool is_declaration)
13449 {
13450   tree decl;
13451   tree scope;
13452   bool typename_p;
13453   cp_token *token;
13454
13455   /* All class-names start with an identifier.  */
13456   token = cp_lexer_peek_token (parser->lexer);
13457   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13458     {
13459       cp_parser_error (parser, "expected class-name");
13460       return error_mark_node;
13461     }
13462
13463   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13464      to a template-id, so we save it here.  */
13465   scope = parser->scope;
13466   if (scope == error_mark_node)
13467     return error_mark_node;
13468
13469   /* Any name names a type if we're following the `typename' keyword
13470      in a qualified name where the enclosing scope is type-dependent.  */
13471   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13472                 && dependent_type_p (scope));
13473   /* Handle the common case (an identifier, but not a template-id)
13474      efficiently.  */
13475   if (token->type == CPP_NAME
13476       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13477     {
13478       cp_token *identifier_token;
13479       tree identifier;
13480       bool ambiguous_p;
13481
13482       /* Look for the identifier.  */
13483       identifier_token = cp_lexer_peek_token (parser->lexer);
13484       ambiguous_p = identifier_token->ambiguous_p;
13485       identifier = cp_parser_identifier (parser);
13486       /* If the next token isn't an identifier, we are certainly not
13487          looking at a class-name.  */
13488       if (identifier == error_mark_node)
13489         decl = error_mark_node;
13490       /* If we know this is a type-name, there's no need to look it
13491          up.  */
13492       else if (typename_p)
13493         decl = identifier;
13494       else
13495         {
13496           tree ambiguous_decls;
13497           /* If we already know that this lookup is ambiguous, then
13498              we've already issued an error message; there's no reason
13499              to check again.  */
13500           if (ambiguous_p)
13501             {
13502               cp_parser_simulate_error (parser);
13503               return error_mark_node;
13504             }
13505           /* If the next token is a `::', then the name must be a type
13506              name.
13507
13508              [basic.lookup.qual]
13509
13510              During the lookup for a name preceding the :: scope
13511              resolution operator, object, function, and enumerator
13512              names are ignored.  */
13513           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13514             tag_type = typename_type;
13515           /* Look up the name.  */
13516           decl = cp_parser_lookup_name (parser, identifier,
13517                                         tag_type,
13518                                         /*is_template=*/false,
13519                                         /*is_namespace=*/false,
13520                                         check_dependency_p,
13521                                         &ambiguous_decls);
13522           if (ambiguous_decls)
13523             {
13524               error ("reference to %qD is ambiguous", identifier);
13525               print_candidates (ambiguous_decls);
13526               if (cp_parser_parsing_tentatively (parser))
13527                 {
13528                   identifier_token->ambiguous_p = true;
13529                   cp_parser_simulate_error (parser);
13530                 }
13531               return error_mark_node;
13532             }
13533         }
13534     }
13535   else
13536     {
13537       /* Try a template-id.  */
13538       decl = cp_parser_template_id (parser, template_keyword_p,
13539                                     check_dependency_p,
13540                                     is_declaration);
13541       if (decl == error_mark_node)
13542         return error_mark_node;
13543     }
13544
13545   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13546
13547   /* If this is a typename, create a TYPENAME_TYPE.  */
13548   if (typename_p && decl != error_mark_node)
13549     {
13550       decl = make_typename_type (scope, decl, typename_type,
13551                                  /*complain=*/tf_error);
13552       if (decl != error_mark_node)
13553         decl = TYPE_NAME (decl);
13554     }
13555
13556   /* Check to see that it is really the name of a class.  */
13557   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13558       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13559       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13560     /* Situations like this:
13561
13562          template <typename T> struct A {
13563            typename T::template X<int>::I i;
13564          };
13565
13566        are problematic.  Is `T::template X<int>' a class-name?  The
13567        standard does not seem to be definitive, but there is no other
13568        valid interpretation of the following `::'.  Therefore, those
13569        names are considered class-names.  */
13570     {
13571       decl = make_typename_type (scope, decl, tag_type, tf_error);
13572       if (decl != error_mark_node)
13573         decl = TYPE_NAME (decl);
13574     }
13575   else if (TREE_CODE (decl) != TYPE_DECL
13576            || TREE_TYPE (decl) == error_mark_node
13577            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13578     decl = error_mark_node;
13579
13580   if (decl == error_mark_node)
13581     cp_parser_error (parser, "expected class-name");
13582
13583   return decl;
13584 }
13585
13586 /* Parse a class-specifier.
13587
13588    class-specifier:
13589      class-head { member-specification [opt] }
13590
13591    Returns the TREE_TYPE representing the class.  */
13592
13593 static tree
13594 cp_parser_class_specifier (cp_parser* parser)
13595 {
13596   cp_token *token;
13597   tree type;
13598   tree attributes = NULL_TREE;
13599   int has_trailing_semicolon;
13600   bool nested_name_specifier_p;
13601   unsigned saved_num_template_parameter_lists;
13602   bool saved_in_function_body;
13603   tree old_scope = NULL_TREE;
13604   tree scope = NULL_TREE;
13605   tree bases;
13606
13607   push_deferring_access_checks (dk_no_deferred);
13608
13609   /* Parse the class-head.  */
13610   type = cp_parser_class_head (parser,
13611                                &nested_name_specifier_p,
13612                                &attributes,
13613                                &bases);
13614   /* If the class-head was a semantic disaster, skip the entire body
13615      of the class.  */
13616   if (!type)
13617     {
13618       cp_parser_skip_to_end_of_block_or_statement (parser);
13619       pop_deferring_access_checks ();
13620       return error_mark_node;
13621     }
13622
13623   /* Look for the `{'.  */
13624   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13625     {
13626       pop_deferring_access_checks ();
13627       return error_mark_node;
13628     }
13629
13630   /* Process the base classes. If they're invalid, skip the 
13631      entire class body.  */
13632   if (!xref_basetypes (type, bases))
13633     {
13634       cp_parser_skip_to_closing_brace (parser);
13635
13636       /* Consuming the closing brace yields better error messages
13637          later on.  */
13638       cp_lexer_consume_token (parser->lexer);
13639       pop_deferring_access_checks ();
13640       return error_mark_node;
13641     }
13642
13643   /* Issue an error message if type-definitions are forbidden here.  */
13644   cp_parser_check_type_definition (parser);
13645   /* Remember that we are defining one more class.  */
13646   ++parser->num_classes_being_defined;
13647   /* Inside the class, surrounding template-parameter-lists do not
13648      apply.  */
13649   saved_num_template_parameter_lists
13650     = parser->num_template_parameter_lists;
13651   parser->num_template_parameter_lists = 0;
13652   /* We are not in a function body.  */
13653   saved_in_function_body = parser->in_function_body;
13654   parser->in_function_body = false;
13655
13656   /* Start the class.  */
13657   if (nested_name_specifier_p)
13658     {
13659       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13660       old_scope = push_inner_scope (scope);
13661     }
13662   type = begin_class_definition (type, attributes);
13663
13664   if (type == error_mark_node)
13665     /* If the type is erroneous, skip the entire body of the class.  */
13666     cp_parser_skip_to_closing_brace (parser);
13667   else
13668     /* Parse the member-specification.  */
13669     cp_parser_member_specification_opt (parser);
13670
13671   /* Look for the trailing `}'.  */
13672   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13673   /* We get better error messages by noticing a common problem: a
13674      missing trailing `;'.  */
13675   token = cp_lexer_peek_token (parser->lexer);
13676   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13677   /* Look for trailing attributes to apply to this class.  */
13678   if (cp_parser_allow_gnu_extensions_p (parser))
13679     attributes = cp_parser_attributes_opt (parser);
13680   if (type != error_mark_node)
13681     type = finish_struct (type, attributes);
13682   if (nested_name_specifier_p)
13683     pop_inner_scope (old_scope, scope);
13684   /* If this class is not itself within the scope of another class,
13685      then we need to parse the bodies of all of the queued function
13686      definitions.  Note that the queued functions defined in a class
13687      are not always processed immediately following the
13688      class-specifier for that class.  Consider:
13689
13690        struct A {
13691          struct B { void f() { sizeof (A); } };
13692        };
13693
13694      If `f' were processed before the processing of `A' were
13695      completed, there would be no way to compute the size of `A'.
13696      Note that the nesting we are interested in here is lexical --
13697      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13698      for:
13699
13700        struct A { struct B; };
13701        struct A::B { void f() { } };
13702
13703      there is no need to delay the parsing of `A::B::f'.  */
13704   if (--parser->num_classes_being_defined == 0)
13705     {
13706       tree queue_entry;
13707       tree fn;
13708       tree class_type = NULL_TREE;
13709       tree pushed_scope = NULL_TREE;
13710
13711       /* In a first pass, parse default arguments to the functions.
13712          Then, in a second pass, parse the bodies of the functions.
13713          This two-phased approach handles cases like:
13714
13715             struct S {
13716               void f() { g(); }
13717               void g(int i = 3);
13718             };
13719
13720          */
13721       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13722              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13723            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13724            TREE_PURPOSE (parser->unparsed_functions_queues)
13725              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13726         {
13727           fn = TREE_VALUE (queue_entry);
13728           /* If there are default arguments that have not yet been processed,
13729              take care of them now.  */
13730           if (class_type != TREE_PURPOSE (queue_entry))
13731             {
13732               if (pushed_scope)
13733                 pop_scope (pushed_scope);
13734               class_type = TREE_PURPOSE (queue_entry);
13735               pushed_scope = push_scope (class_type);
13736             }
13737           /* Make sure that any template parameters are in scope.  */
13738           maybe_begin_member_template_processing (fn);
13739           /* Parse the default argument expressions.  */
13740           cp_parser_late_parsing_default_args (parser, fn);
13741           /* Remove any template parameters from the symbol table.  */
13742           maybe_end_member_template_processing ();
13743         }
13744       if (pushed_scope)
13745         pop_scope (pushed_scope);
13746       /* Now parse the body of the functions.  */
13747       for (TREE_VALUE (parser->unparsed_functions_queues)
13748              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13749            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13750            TREE_VALUE (parser->unparsed_functions_queues)
13751              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13752         {
13753           /* Figure out which function we need to process.  */
13754           fn = TREE_VALUE (queue_entry);
13755           /* Parse the function.  */
13756           cp_parser_late_parsing_for_member (parser, fn);
13757         }
13758     }
13759
13760   /* Put back any saved access checks.  */
13761   pop_deferring_access_checks ();
13762
13763   /* Restore saved state.  */
13764   parser->in_function_body = saved_in_function_body;
13765   parser->num_template_parameter_lists
13766     = saved_num_template_parameter_lists;
13767
13768   return type;
13769 }
13770
13771 /* Parse a class-head.
13772
13773    class-head:
13774      class-key identifier [opt] base-clause [opt]
13775      class-key nested-name-specifier identifier base-clause [opt]
13776      class-key nested-name-specifier [opt] template-id
13777        base-clause [opt]
13778
13779    GNU Extensions:
13780      class-key attributes identifier [opt] base-clause [opt]
13781      class-key attributes nested-name-specifier identifier base-clause [opt]
13782      class-key attributes nested-name-specifier [opt] template-id
13783        base-clause [opt]
13784
13785    Returns the TYPE of the indicated class.  Sets
13786    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13787    involving a nested-name-specifier was used, and FALSE otherwise.
13788
13789    Returns error_mark_node if this is not a class-head.
13790
13791    Returns NULL_TREE if the class-head is syntactically valid, but
13792    semantically invalid in a way that means we should skip the entire
13793    body of the class.  */
13794
13795 static tree
13796 cp_parser_class_head (cp_parser* parser,
13797                       bool* nested_name_specifier_p,
13798                       tree *attributes_p,
13799                       tree *bases)
13800 {
13801   tree nested_name_specifier;
13802   enum tag_types class_key;
13803   tree id = NULL_TREE;
13804   tree type = NULL_TREE;
13805   tree attributes;
13806   bool template_id_p = false;
13807   bool qualified_p = false;
13808   bool invalid_nested_name_p = false;
13809   bool invalid_explicit_specialization_p = false;
13810   tree pushed_scope = NULL_TREE;
13811   unsigned num_templates;
13812
13813   /* Assume no nested-name-specifier will be present.  */
13814   *nested_name_specifier_p = false;
13815   /* Assume no template parameter lists will be used in defining the
13816      type.  */
13817   num_templates = 0;
13818
13819   /* Look for the class-key.  */
13820   class_key = cp_parser_class_key (parser);
13821   if (class_key == none_type)
13822     return error_mark_node;
13823
13824   /* Parse the attributes.  */
13825   attributes = cp_parser_attributes_opt (parser);
13826
13827   /* If the next token is `::', that is invalid -- but sometimes
13828      people do try to write:
13829
13830        struct ::S {};
13831
13832      Handle this gracefully by accepting the extra qualifier, and then
13833      issuing an error about it later if this really is a
13834      class-head.  If it turns out just to be an elaborated type
13835      specifier, remain silent.  */
13836   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13837     qualified_p = true;
13838
13839   push_deferring_access_checks (dk_no_check);
13840
13841   /* Determine the name of the class.  Begin by looking for an
13842      optional nested-name-specifier.  */
13843   nested_name_specifier
13844     = cp_parser_nested_name_specifier_opt (parser,
13845                                            /*typename_keyword_p=*/false,
13846                                            /*check_dependency_p=*/false,
13847                                            /*type_p=*/false,
13848                                            /*is_declaration=*/false);
13849   /* If there was a nested-name-specifier, then there *must* be an
13850      identifier.  */
13851   if (nested_name_specifier)
13852     {
13853       /* Although the grammar says `identifier', it really means
13854          `class-name' or `template-name'.  You are only allowed to
13855          define a class that has already been declared with this
13856          syntax.
13857
13858          The proposed resolution for Core Issue 180 says that wherever
13859          you see `class T::X' you should treat `X' as a type-name.
13860
13861          It is OK to define an inaccessible class; for example:
13862
13863            class A { class B; };
13864            class A::B {};
13865
13866          We do not know if we will see a class-name, or a
13867          template-name.  We look for a class-name first, in case the
13868          class-name is a template-id; if we looked for the
13869          template-name first we would stop after the template-name.  */
13870       cp_parser_parse_tentatively (parser);
13871       type = cp_parser_class_name (parser,
13872                                    /*typename_keyword_p=*/false,
13873                                    /*template_keyword_p=*/false,
13874                                    class_type,
13875                                    /*check_dependency_p=*/false,
13876                                    /*class_head_p=*/true,
13877                                    /*is_declaration=*/false);
13878       /* If that didn't work, ignore the nested-name-specifier.  */
13879       if (!cp_parser_parse_definitely (parser))
13880         {
13881           invalid_nested_name_p = true;
13882           id = cp_parser_identifier (parser);
13883           if (id == error_mark_node)
13884             id = NULL_TREE;
13885         }
13886       /* If we could not find a corresponding TYPE, treat this
13887          declaration like an unqualified declaration.  */
13888       if (type == error_mark_node)
13889         nested_name_specifier = NULL_TREE;
13890       /* Otherwise, count the number of templates used in TYPE and its
13891          containing scopes.  */
13892       else
13893         {
13894           tree scope;
13895
13896           for (scope = TREE_TYPE (type);
13897                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13898                scope = (TYPE_P (scope)
13899                         ? TYPE_CONTEXT (scope)
13900                         : DECL_CONTEXT (scope)))
13901             if (TYPE_P (scope)
13902                 && CLASS_TYPE_P (scope)
13903                 && CLASSTYPE_TEMPLATE_INFO (scope)
13904                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13905                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13906               ++num_templates;
13907         }
13908     }
13909   /* Otherwise, the identifier is optional.  */
13910   else
13911     {
13912       /* We don't know whether what comes next is a template-id,
13913          an identifier, or nothing at all.  */
13914       cp_parser_parse_tentatively (parser);
13915       /* Check for a template-id.  */
13916       id = cp_parser_template_id (parser,
13917                                   /*template_keyword_p=*/false,
13918                                   /*check_dependency_p=*/true,
13919                                   /*is_declaration=*/true);
13920       /* If that didn't work, it could still be an identifier.  */
13921       if (!cp_parser_parse_definitely (parser))
13922         {
13923           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13924             id = cp_parser_identifier (parser);
13925           else
13926             id = NULL_TREE;
13927         }
13928       else
13929         {
13930           template_id_p = true;
13931           ++num_templates;
13932         }
13933     }
13934
13935   pop_deferring_access_checks ();
13936
13937   if (id)
13938     cp_parser_check_for_invalid_template_id (parser, id);
13939
13940   /* If it's not a `:' or a `{' then we can't really be looking at a
13941      class-head, since a class-head only appears as part of a
13942      class-specifier.  We have to detect this situation before calling
13943      xref_tag, since that has irreversible side-effects.  */
13944   if (!cp_parser_next_token_starts_class_definition_p (parser))
13945     {
13946       cp_parser_error (parser, "expected %<{%> or %<:%>");
13947       return error_mark_node;
13948     }
13949
13950   /* At this point, we're going ahead with the class-specifier, even
13951      if some other problem occurs.  */
13952   cp_parser_commit_to_tentative_parse (parser);
13953   /* Issue the error about the overly-qualified name now.  */
13954   if (qualified_p)
13955     cp_parser_error (parser,
13956                      "global qualification of class name is invalid");
13957   else if (invalid_nested_name_p)
13958     cp_parser_error (parser,
13959                      "qualified name does not name a class");
13960   else if (nested_name_specifier)
13961     {
13962       tree scope;
13963
13964       /* Reject typedef-names in class heads.  */
13965       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13966         {
13967           error ("invalid class name in declaration of %qD", type);
13968           type = NULL_TREE;
13969           goto done;
13970         }
13971
13972       /* Figure out in what scope the declaration is being placed.  */
13973       scope = current_scope ();
13974       /* If that scope does not contain the scope in which the
13975          class was originally declared, the program is invalid.  */
13976       if (scope && !is_ancestor (scope, nested_name_specifier))
13977         {
13978           error ("declaration of %qD in %qD which does not enclose %qD",
13979                  type, scope, nested_name_specifier);
13980           type = NULL_TREE;
13981           goto done;
13982         }
13983       /* [dcl.meaning]
13984
13985          A declarator-id shall not be qualified exception of the
13986          definition of a ... nested class outside of its class
13987          ... [or] a the definition or explicit instantiation of a
13988          class member of a namespace outside of its namespace.  */
13989       if (scope == nested_name_specifier)
13990         {
13991           pedwarn ("extra qualification ignored");
13992           nested_name_specifier = NULL_TREE;
13993           num_templates = 0;
13994         }
13995     }
13996   /* An explicit-specialization must be preceded by "template <>".  If
13997      it is not, try to recover gracefully.  */
13998   if (at_namespace_scope_p ()
13999       && parser->num_template_parameter_lists == 0
14000       && template_id_p)
14001     {
14002       error ("an explicit specialization must be preceded by %<template <>%>");
14003       invalid_explicit_specialization_p = true;
14004       /* Take the same action that would have been taken by
14005          cp_parser_explicit_specialization.  */
14006       ++parser->num_template_parameter_lists;
14007       begin_specialization ();
14008     }
14009   /* There must be no "return" statements between this point and the
14010      end of this function; set "type "to the correct return value and
14011      use "goto done;" to return.  */
14012   /* Make sure that the right number of template parameters were
14013      present.  */
14014   if (!cp_parser_check_template_parameters (parser, num_templates))
14015     {
14016       /* If something went wrong, there is no point in even trying to
14017          process the class-definition.  */
14018       type = NULL_TREE;
14019       goto done;
14020     }
14021
14022   /* Look up the type.  */
14023   if (template_id_p)
14024     {
14025       type = TREE_TYPE (id);
14026       type = maybe_process_partial_specialization (type);
14027       if (nested_name_specifier)
14028         pushed_scope = push_scope (nested_name_specifier);
14029     }
14030   else if (nested_name_specifier)
14031     {
14032       tree class_type;
14033
14034       /* Given:
14035
14036             template <typename T> struct S { struct T };
14037             template <typename T> struct S<T>::T { };
14038
14039          we will get a TYPENAME_TYPE when processing the definition of
14040          `S::T'.  We need to resolve it to the actual type before we
14041          try to define it.  */
14042       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14043         {
14044           class_type = resolve_typename_type (TREE_TYPE (type),
14045                                               /*only_current_p=*/false);
14046           if (class_type != error_mark_node)
14047             type = TYPE_NAME (class_type);
14048           else
14049             {
14050               cp_parser_error (parser, "could not resolve typename type");
14051               type = error_mark_node;
14052             }
14053         }
14054
14055       maybe_process_partial_specialization (TREE_TYPE (type));
14056       class_type = current_class_type;
14057       /* Enter the scope indicated by the nested-name-specifier.  */
14058       pushed_scope = push_scope (nested_name_specifier);
14059       /* Get the canonical version of this type.  */
14060       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14061       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14062           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14063         {
14064           type = push_template_decl (type);
14065           if (type == error_mark_node)
14066             {
14067               type = NULL_TREE;
14068               goto done;
14069             }
14070         }
14071
14072       type = TREE_TYPE (type);
14073       *nested_name_specifier_p = true;
14074     }
14075   else      /* The name is not a nested name.  */
14076     {
14077       /* If the class was unnamed, create a dummy name.  */
14078       if (!id)
14079         id = make_anon_name ();
14080       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14081                        parser->num_template_parameter_lists);
14082     }
14083
14084   /* Indicate whether this class was declared as a `class' or as a
14085      `struct'.  */
14086   if (TREE_CODE (type) == RECORD_TYPE)
14087     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14088   cp_parser_check_class_key (class_key, type);
14089
14090   /* If this type was already complete, and we see another definition,
14091      that's an error.  */
14092   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14093     {
14094       error ("redefinition of %q#T", type);
14095       error ("previous definition of %q+#T", type);
14096       type = NULL_TREE;
14097       goto done;
14098     }
14099   else if (type == error_mark_node)
14100     type = NULL_TREE;
14101
14102   /* We will have entered the scope containing the class; the names of
14103      base classes should be looked up in that context.  For example:
14104
14105        struct A { struct B {}; struct C; };
14106        struct A::C : B {};
14107
14108      is valid.  */
14109   *bases = NULL_TREE;
14110
14111   /* Get the list of base-classes, if there is one.  */
14112   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14113     *bases = cp_parser_base_clause (parser);
14114
14115  done:
14116   /* Leave the scope given by the nested-name-specifier.  We will
14117      enter the class scope itself while processing the members.  */
14118   if (pushed_scope)
14119     pop_scope (pushed_scope);
14120
14121   if (invalid_explicit_specialization_p)
14122     {
14123       end_specialization ();
14124       --parser->num_template_parameter_lists;
14125     }
14126   *attributes_p = attributes;
14127   return type;
14128 }
14129
14130 /* Parse a class-key.
14131
14132    class-key:
14133      class
14134      struct
14135      union
14136
14137    Returns the kind of class-key specified, or none_type to indicate
14138    error.  */
14139
14140 static enum tag_types
14141 cp_parser_class_key (cp_parser* parser)
14142 {
14143   cp_token *token;
14144   enum tag_types tag_type;
14145
14146   /* Look for the class-key.  */
14147   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14148   if (!token)
14149     return none_type;
14150
14151   /* Check to see if the TOKEN is a class-key.  */
14152   tag_type = cp_parser_token_is_class_key (token);
14153   if (!tag_type)
14154     cp_parser_error (parser, "expected class-key");
14155   return tag_type;
14156 }
14157
14158 /* Parse an (optional) member-specification.
14159
14160    member-specification:
14161      member-declaration member-specification [opt]
14162      access-specifier : member-specification [opt]  */
14163
14164 static void
14165 cp_parser_member_specification_opt (cp_parser* parser)
14166 {
14167   while (true)
14168     {
14169       cp_token *token;
14170       enum rid keyword;
14171
14172       /* Peek at the next token.  */
14173       token = cp_lexer_peek_token (parser->lexer);
14174       /* If it's a `}', or EOF then we've seen all the members.  */
14175       if (token->type == CPP_CLOSE_BRACE
14176           || token->type == CPP_EOF
14177           || token->type == CPP_PRAGMA_EOL)
14178         break;
14179
14180       /* See if this token is a keyword.  */
14181       keyword = token->keyword;
14182       switch (keyword)
14183         {
14184         case RID_PUBLIC:
14185         case RID_PROTECTED:
14186         case RID_PRIVATE:
14187           /* Consume the access-specifier.  */
14188           cp_lexer_consume_token (parser->lexer);
14189           /* Remember which access-specifier is active.  */
14190           current_access_specifier = token->u.value;
14191           /* Look for the `:'.  */
14192           cp_parser_require (parser, CPP_COLON, "`:'");
14193           break;
14194
14195         default:
14196           /* Accept #pragmas at class scope.  */
14197           if (token->type == CPP_PRAGMA)
14198             {
14199               cp_parser_pragma (parser, pragma_external);
14200               break;
14201             }
14202
14203           /* Otherwise, the next construction must be a
14204              member-declaration.  */
14205           cp_parser_member_declaration (parser);
14206         }
14207     }
14208 }
14209
14210 /* Parse a member-declaration.
14211
14212    member-declaration:
14213      decl-specifier-seq [opt] member-declarator-list [opt] ;
14214      function-definition ; [opt]
14215      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14216      using-declaration
14217      template-declaration
14218
14219    member-declarator-list:
14220      member-declarator
14221      member-declarator-list , member-declarator
14222
14223    member-declarator:
14224      declarator pure-specifier [opt]
14225      declarator constant-initializer [opt]
14226      identifier [opt] : constant-expression
14227
14228    GNU Extensions:
14229
14230    member-declaration:
14231      __extension__ member-declaration
14232
14233    member-declarator:
14234      declarator attributes [opt] pure-specifier [opt]
14235      declarator attributes [opt] constant-initializer [opt]
14236      identifier [opt] attributes [opt] : constant-expression  */
14237
14238 static void
14239 cp_parser_member_declaration (cp_parser* parser)
14240 {
14241   cp_decl_specifier_seq decl_specifiers;
14242   tree prefix_attributes;
14243   tree decl;
14244   int declares_class_or_enum;
14245   bool friend_p;
14246   cp_token *token;
14247   int saved_pedantic;
14248
14249   /* Check for the `__extension__' keyword.  */
14250   if (cp_parser_extension_opt (parser, &saved_pedantic))
14251     {
14252       /* Recurse.  */
14253       cp_parser_member_declaration (parser);
14254       /* Restore the old value of the PEDANTIC flag.  */
14255       pedantic = saved_pedantic;
14256
14257       return;
14258     }
14259
14260   /* Check for a template-declaration.  */
14261   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14262     {
14263       /* An explicit specialization here is an error condition, and we
14264          expect the specialization handler to detect and report this.  */
14265       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14266           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14267         cp_parser_explicit_specialization (parser);
14268       else
14269         cp_parser_template_declaration (parser, /*member_p=*/true);
14270
14271       return;
14272     }
14273
14274   /* Check for a using-declaration.  */
14275   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14276     {
14277       /* Parse the using-declaration.  */
14278       cp_parser_using_declaration (parser,
14279                                    /*access_declaration_p=*/false);
14280       return;
14281     }
14282
14283   /* Check for @defs.  */
14284   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14285     {
14286       tree ivar, member;
14287       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14288       ivar = ivar_chains;
14289       while (ivar)
14290         {
14291           member = ivar;
14292           ivar = TREE_CHAIN (member);
14293           TREE_CHAIN (member) = NULL_TREE;
14294           finish_member_declaration (member);
14295         }
14296       return;
14297     }
14298
14299   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14300     return;
14301
14302   /* Parse the decl-specifier-seq.  */
14303   cp_parser_decl_specifier_seq (parser,
14304                                 CP_PARSER_FLAGS_OPTIONAL,
14305                                 &decl_specifiers,
14306                                 &declares_class_or_enum);
14307   prefix_attributes = decl_specifiers.attributes;
14308   decl_specifiers.attributes = NULL_TREE;
14309   /* Check for an invalid type-name.  */
14310   if (!decl_specifiers.type
14311       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14312     return;
14313   /* If there is no declarator, then the decl-specifier-seq should
14314      specify a type.  */
14315   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14316     {
14317       /* If there was no decl-specifier-seq, and the next token is a
14318          `;', then we have something like:
14319
14320            struct S { ; };
14321
14322          [class.mem]
14323
14324          Each member-declaration shall declare at least one member
14325          name of the class.  */
14326       if (!decl_specifiers.any_specifiers_p)
14327         {
14328           cp_token *token = cp_lexer_peek_token (parser->lexer);
14329           if (pedantic && !token->in_system_header)
14330             pedwarn ("%Hextra %<;%>", &token->location);
14331         }
14332       else
14333         {
14334           tree type;
14335
14336           /* See if this declaration is a friend.  */
14337           friend_p = cp_parser_friend_p (&decl_specifiers);
14338           /* If there were decl-specifiers, check to see if there was
14339              a class-declaration.  */
14340           type = check_tag_decl (&decl_specifiers);
14341           /* Nested classes have already been added to the class, but
14342              a `friend' needs to be explicitly registered.  */
14343           if (friend_p)
14344             {
14345               /* If the `friend' keyword was present, the friend must
14346                  be introduced with a class-key.  */
14347                if (!declares_class_or_enum)
14348                  error ("a class-key must be used when declaring a friend");
14349                /* In this case:
14350
14351                     template <typename T> struct A {
14352                       friend struct A<T>::B;
14353                     };
14354
14355                   A<T>::B will be represented by a TYPENAME_TYPE, and
14356                   therefore not recognized by check_tag_decl.  */
14357                if (!type
14358                    && decl_specifiers.type
14359                    && TYPE_P (decl_specifiers.type))
14360                  type = decl_specifiers.type;
14361                if (!type || !TYPE_P (type))
14362                  error ("friend declaration does not name a class or "
14363                         "function");
14364                else
14365                  make_friend_class (current_class_type, type,
14366                                     /*complain=*/true);
14367             }
14368           /* If there is no TYPE, an error message will already have
14369              been issued.  */
14370           else if (!type || type == error_mark_node)
14371             ;
14372           /* An anonymous aggregate has to be handled specially; such
14373              a declaration really declares a data member (with a
14374              particular type), as opposed to a nested class.  */
14375           else if (ANON_AGGR_TYPE_P (type))
14376             {
14377               /* Remove constructors and such from TYPE, now that we
14378                  know it is an anonymous aggregate.  */
14379               fixup_anonymous_aggr (type);
14380               /* And make the corresponding data member.  */
14381               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14382               /* Add it to the class.  */
14383               finish_member_declaration (decl);
14384             }
14385           else
14386             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14387         }
14388     }
14389   else
14390     {
14391       /* See if these declarations will be friends.  */
14392       friend_p = cp_parser_friend_p (&decl_specifiers);
14393
14394       /* Keep going until we hit the `;' at the end of the
14395          declaration.  */
14396       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14397         {
14398           tree attributes = NULL_TREE;
14399           tree first_attribute;
14400
14401           /* Peek at the next token.  */
14402           token = cp_lexer_peek_token (parser->lexer);
14403
14404           /* Check for a bitfield declaration.  */
14405           if (token->type == CPP_COLON
14406               || (token->type == CPP_NAME
14407                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14408                   == CPP_COLON))
14409             {
14410               tree identifier;
14411               tree width;
14412
14413               /* Get the name of the bitfield.  Note that we cannot just
14414                  check TOKEN here because it may have been invalidated by
14415                  the call to cp_lexer_peek_nth_token above.  */
14416               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14417                 identifier = cp_parser_identifier (parser);
14418               else
14419                 identifier = NULL_TREE;
14420
14421               /* Consume the `:' token.  */
14422               cp_lexer_consume_token (parser->lexer);
14423               /* Get the width of the bitfield.  */
14424               width
14425                 = cp_parser_constant_expression (parser,
14426                                                  /*allow_non_constant=*/false,
14427                                                  NULL);
14428
14429               /* Look for attributes that apply to the bitfield.  */
14430               attributes = cp_parser_attributes_opt (parser);
14431               /* Remember which attributes are prefix attributes and
14432                  which are not.  */
14433               first_attribute = attributes;
14434               /* Combine the attributes.  */
14435               attributes = chainon (prefix_attributes, attributes);
14436
14437               /* Create the bitfield declaration.  */
14438               decl = grokbitfield (identifier
14439                                    ? make_id_declarator (NULL_TREE,
14440                                                          identifier,
14441                                                          sfk_none)
14442                                    : NULL,
14443                                    &decl_specifiers,
14444                                    width);
14445               /* Apply the attributes.  */
14446               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14447             }
14448           else
14449             {
14450               cp_declarator *declarator;
14451               tree initializer;
14452               tree asm_specification;
14453               int ctor_dtor_or_conv_p;
14454
14455               /* Parse the declarator.  */
14456               declarator
14457                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14458                                         &ctor_dtor_or_conv_p,
14459                                         /*parenthesized_p=*/NULL,
14460                                         /*member_p=*/true);
14461
14462               /* If something went wrong parsing the declarator, make sure
14463                  that we at least consume some tokens.  */
14464               if (declarator == cp_error_declarator)
14465                 {
14466                   /* Skip to the end of the statement.  */
14467                   cp_parser_skip_to_end_of_statement (parser);
14468                   /* If the next token is not a semicolon, that is
14469                      probably because we just skipped over the body of
14470                      a function.  So, we consume a semicolon if
14471                      present, but do not issue an error message if it
14472                      is not present.  */
14473                   if (cp_lexer_next_token_is (parser->lexer,
14474                                               CPP_SEMICOLON))
14475                     cp_lexer_consume_token (parser->lexer);
14476                   return;
14477                 }
14478
14479               if (declares_class_or_enum & 2)
14480                 cp_parser_check_for_definition_in_return_type
14481                   (declarator, decl_specifiers.type);
14482
14483               /* Look for an asm-specification.  */
14484               asm_specification = cp_parser_asm_specification_opt (parser);
14485               /* Look for attributes that apply to the declaration.  */
14486               attributes = cp_parser_attributes_opt (parser);
14487               /* Remember which attributes are prefix attributes and
14488                  which are not.  */
14489               first_attribute = attributes;
14490               /* Combine the attributes.  */
14491               attributes = chainon (prefix_attributes, attributes);
14492
14493               /* If it's an `=', then we have a constant-initializer or a
14494                  pure-specifier.  It is not correct to parse the
14495                  initializer before registering the member declaration
14496                  since the member declaration should be in scope while
14497                  its initializer is processed.  However, the rest of the
14498                  front end does not yet provide an interface that allows
14499                  us to handle this correctly.  */
14500               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14501                 {
14502                   /* In [class.mem]:
14503
14504                      A pure-specifier shall be used only in the declaration of
14505                      a virtual function.
14506
14507                      A member-declarator can contain a constant-initializer
14508                      only if it declares a static member of integral or
14509                      enumeration type.
14510
14511                      Therefore, if the DECLARATOR is for a function, we look
14512                      for a pure-specifier; otherwise, we look for a
14513                      constant-initializer.  When we call `grokfield', it will
14514                      perform more stringent semantics checks.  */
14515                   if (function_declarator_p (declarator))
14516                     initializer = cp_parser_pure_specifier (parser);
14517                   else
14518                     /* Parse the initializer.  */
14519                     initializer = cp_parser_constant_initializer (parser);
14520                 }
14521               /* Otherwise, there is no initializer.  */
14522               else
14523                 initializer = NULL_TREE;
14524
14525               /* See if we are probably looking at a function
14526                  definition.  We are certainly not looking at a
14527                  member-declarator.  Calling `grokfield' has
14528                  side-effects, so we must not do it unless we are sure
14529                  that we are looking at a member-declarator.  */
14530               if (cp_parser_token_starts_function_definition_p
14531                   (cp_lexer_peek_token (parser->lexer)))
14532                 {
14533                   /* The grammar does not allow a pure-specifier to be
14534                      used when a member function is defined.  (It is
14535                      possible that this fact is an oversight in the
14536                      standard, since a pure function may be defined
14537                      outside of the class-specifier.  */
14538                   if (initializer)
14539                     error ("pure-specifier on function-definition");
14540                   decl = cp_parser_save_member_function_body (parser,
14541                                                               &decl_specifiers,
14542                                                               declarator,
14543                                                               attributes);
14544                   /* If the member was not a friend, declare it here.  */
14545                   if (!friend_p)
14546                     finish_member_declaration (decl);
14547                   /* Peek at the next token.  */
14548                   token = cp_lexer_peek_token (parser->lexer);
14549                   /* If the next token is a semicolon, consume it.  */
14550                   if (token->type == CPP_SEMICOLON)
14551                     cp_lexer_consume_token (parser->lexer);
14552                   return;
14553                 }
14554               else
14555                 /* Create the declaration.  */
14556                 decl = grokfield (declarator, &decl_specifiers,
14557                                   initializer, /*init_const_expr_p=*/true,
14558                                   asm_specification,
14559                                   attributes);
14560             }
14561
14562           /* Reset PREFIX_ATTRIBUTES.  */
14563           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14564             attributes = TREE_CHAIN (attributes);
14565           if (attributes)
14566             TREE_CHAIN (attributes) = NULL_TREE;
14567
14568           /* If there is any qualification still in effect, clear it
14569              now; we will be starting fresh with the next declarator.  */
14570           parser->scope = NULL_TREE;
14571           parser->qualifying_scope = NULL_TREE;
14572           parser->object_scope = NULL_TREE;
14573           /* If it's a `,', then there are more declarators.  */
14574           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14575             cp_lexer_consume_token (parser->lexer);
14576           /* If the next token isn't a `;', then we have a parse error.  */
14577           else if (cp_lexer_next_token_is_not (parser->lexer,
14578                                                CPP_SEMICOLON))
14579             {
14580               cp_parser_error (parser, "expected %<;%>");
14581               /* Skip tokens until we find a `;'.  */
14582               cp_parser_skip_to_end_of_statement (parser);
14583
14584               break;
14585             }
14586
14587           if (decl)
14588             {
14589               /* Add DECL to the list of members.  */
14590               if (!friend_p)
14591                 finish_member_declaration (decl);
14592
14593               if (TREE_CODE (decl) == FUNCTION_DECL)
14594                 cp_parser_save_default_args (parser, decl);
14595             }
14596         }
14597     }
14598
14599   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14600 }
14601
14602 /* Parse a pure-specifier.
14603
14604    pure-specifier:
14605      = 0
14606
14607    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14608    Otherwise, ERROR_MARK_NODE is returned.  */
14609
14610 static tree
14611 cp_parser_pure_specifier (cp_parser* parser)
14612 {
14613   cp_token *token;
14614
14615   /* Look for the `=' token.  */
14616   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14617     return error_mark_node;
14618   /* Look for the `0' token.  */
14619   token = cp_lexer_consume_token (parser->lexer);
14620   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14621   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14622     {
14623       cp_parser_error (parser,
14624                        "invalid pure specifier (only `= 0' is allowed)");
14625       cp_parser_skip_to_end_of_statement (parser);
14626       return error_mark_node;
14627     }
14628   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14629     {
14630       error ("templates may not be %<virtual%>");
14631       return error_mark_node;
14632     }
14633
14634   return integer_zero_node;
14635 }
14636
14637 /* Parse a constant-initializer.
14638
14639    constant-initializer:
14640      = constant-expression
14641
14642    Returns a representation of the constant-expression.  */
14643
14644 static tree
14645 cp_parser_constant_initializer (cp_parser* parser)
14646 {
14647   /* Look for the `=' token.  */
14648   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14649     return error_mark_node;
14650
14651   /* It is invalid to write:
14652
14653        struct S { static const int i = { 7 }; };
14654
14655      */
14656   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14657     {
14658       cp_parser_error (parser,
14659                        "a brace-enclosed initializer is not allowed here");
14660       /* Consume the opening brace.  */
14661       cp_lexer_consume_token (parser->lexer);
14662       /* Skip the initializer.  */
14663       cp_parser_skip_to_closing_brace (parser);
14664       /* Look for the trailing `}'.  */
14665       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14666
14667       return error_mark_node;
14668     }
14669
14670   return cp_parser_constant_expression (parser,
14671                                         /*allow_non_constant=*/false,
14672                                         NULL);
14673 }
14674
14675 /* Derived classes [gram.class.derived] */
14676
14677 /* Parse a base-clause.
14678
14679    base-clause:
14680      : base-specifier-list
14681
14682    base-specifier-list:
14683      base-specifier
14684      base-specifier-list , base-specifier
14685
14686    Returns a TREE_LIST representing the base-classes, in the order in
14687    which they were declared.  The representation of each node is as
14688    described by cp_parser_base_specifier.
14689
14690    In the case that no bases are specified, this function will return
14691    NULL_TREE, not ERROR_MARK_NODE.  */
14692
14693 static tree
14694 cp_parser_base_clause (cp_parser* parser)
14695 {
14696   tree bases = NULL_TREE;
14697
14698   /* Look for the `:' that begins the list.  */
14699   cp_parser_require (parser, CPP_COLON, "`:'");
14700
14701   /* Scan the base-specifier-list.  */
14702   while (true)
14703     {
14704       cp_token *token;
14705       tree base;
14706
14707       /* Look for the base-specifier.  */
14708       base = cp_parser_base_specifier (parser);
14709       /* Add BASE to the front of the list.  */
14710       if (base != error_mark_node)
14711         {
14712           TREE_CHAIN (base) = bases;
14713           bases = base;
14714         }
14715       /* Peek at the next token.  */
14716       token = cp_lexer_peek_token (parser->lexer);
14717       /* If it's not a comma, then the list is complete.  */
14718       if (token->type != CPP_COMMA)
14719         break;
14720       /* Consume the `,'.  */
14721       cp_lexer_consume_token (parser->lexer);
14722     }
14723
14724   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14725      base class had a qualified name.  However, the next name that
14726      appears is certainly not qualified.  */
14727   parser->scope = NULL_TREE;
14728   parser->qualifying_scope = NULL_TREE;
14729   parser->object_scope = NULL_TREE;
14730
14731   return nreverse (bases);
14732 }
14733
14734 /* Parse a base-specifier.
14735
14736    base-specifier:
14737      :: [opt] nested-name-specifier [opt] class-name
14738      virtual access-specifier [opt] :: [opt] nested-name-specifier
14739        [opt] class-name
14740      access-specifier virtual [opt] :: [opt] nested-name-specifier
14741        [opt] class-name
14742
14743    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14744    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14745    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14746    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14747
14748 static tree
14749 cp_parser_base_specifier (cp_parser* parser)
14750 {
14751   cp_token *token;
14752   bool done = false;
14753   bool virtual_p = false;
14754   bool duplicate_virtual_error_issued_p = false;
14755   bool duplicate_access_error_issued_p = false;
14756   bool class_scope_p, template_p;
14757   tree access = access_default_node;
14758   tree type;
14759
14760   /* Process the optional `virtual' and `access-specifier'.  */
14761   while (!done)
14762     {
14763       /* Peek at the next token.  */
14764       token = cp_lexer_peek_token (parser->lexer);
14765       /* Process `virtual'.  */
14766       switch (token->keyword)
14767         {
14768         case RID_VIRTUAL:
14769           /* If `virtual' appears more than once, issue an error.  */
14770           if (virtual_p && !duplicate_virtual_error_issued_p)
14771             {
14772               cp_parser_error (parser,
14773                                "%<virtual%> specified more than once in base-specified");
14774               duplicate_virtual_error_issued_p = true;
14775             }
14776
14777           virtual_p = true;
14778
14779           /* Consume the `virtual' token.  */
14780           cp_lexer_consume_token (parser->lexer);
14781
14782           break;
14783
14784         case RID_PUBLIC:
14785         case RID_PROTECTED:
14786         case RID_PRIVATE:
14787           /* If more than one access specifier appears, issue an
14788              error.  */
14789           if (access != access_default_node
14790               && !duplicate_access_error_issued_p)
14791             {
14792               cp_parser_error (parser,
14793                                "more than one access specifier in base-specified");
14794               duplicate_access_error_issued_p = true;
14795             }
14796
14797           access = ridpointers[(int) token->keyword];
14798
14799           /* Consume the access-specifier.  */
14800           cp_lexer_consume_token (parser->lexer);
14801
14802           break;
14803
14804         default:
14805           done = true;
14806           break;
14807         }
14808     }
14809   /* It is not uncommon to see programs mechanically, erroneously, use
14810      the 'typename' keyword to denote (dependent) qualified types
14811      as base classes.  */
14812   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14813     {
14814       if (!processing_template_decl)
14815         error ("keyword %<typename%> not allowed outside of templates");
14816       else
14817         error ("keyword %<typename%> not allowed in this context "
14818                "(the base class is implicitly a type)");
14819       cp_lexer_consume_token (parser->lexer);
14820     }
14821
14822   /* Look for the optional `::' operator.  */
14823   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14824   /* Look for the nested-name-specifier.  The simplest way to
14825      implement:
14826
14827        [temp.res]
14828
14829        The keyword `typename' is not permitted in a base-specifier or
14830        mem-initializer; in these contexts a qualified name that
14831        depends on a template-parameter is implicitly assumed to be a
14832        type name.
14833
14834      is to pretend that we have seen the `typename' keyword at this
14835      point.  */
14836   cp_parser_nested_name_specifier_opt (parser,
14837                                        /*typename_keyword_p=*/true,
14838                                        /*check_dependency_p=*/true,
14839                                        typename_type,
14840                                        /*is_declaration=*/true);
14841   /* If the base class is given by a qualified name, assume that names
14842      we see are type names or templates, as appropriate.  */
14843   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14844   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14845
14846   /* Finally, look for the class-name.  */
14847   type = cp_parser_class_name (parser,
14848                                class_scope_p,
14849                                template_p,
14850                                typename_type,
14851                                /*check_dependency_p=*/true,
14852                                /*class_head_p=*/false,
14853                                /*is_declaration=*/true);
14854
14855   if (type == error_mark_node)
14856     return error_mark_node;
14857
14858   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14859 }
14860
14861 /* Exception handling [gram.exception] */
14862
14863 /* Parse an (optional) exception-specification.
14864
14865    exception-specification:
14866      throw ( type-id-list [opt] )
14867
14868    Returns a TREE_LIST representing the exception-specification.  The
14869    TREE_VALUE of each node is a type.  */
14870
14871 static tree
14872 cp_parser_exception_specification_opt (cp_parser* parser)
14873 {
14874   cp_token *token;
14875   tree type_id_list;
14876
14877   /* Peek at the next token.  */
14878   token = cp_lexer_peek_token (parser->lexer);
14879   /* If it's not `throw', then there's no exception-specification.  */
14880   if (!cp_parser_is_keyword (token, RID_THROW))
14881     return NULL_TREE;
14882
14883   /* Consume the `throw'.  */
14884   cp_lexer_consume_token (parser->lexer);
14885
14886   /* Look for the `('.  */
14887   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14888
14889   /* Peek at the next token.  */
14890   token = cp_lexer_peek_token (parser->lexer);
14891   /* If it's not a `)', then there is a type-id-list.  */
14892   if (token->type != CPP_CLOSE_PAREN)
14893     {
14894       const char *saved_message;
14895
14896       /* Types may not be defined in an exception-specification.  */
14897       saved_message = parser->type_definition_forbidden_message;
14898       parser->type_definition_forbidden_message
14899         = "types may not be defined in an exception-specification";
14900       /* Parse the type-id-list.  */
14901       type_id_list = cp_parser_type_id_list (parser);
14902       /* Restore the saved message.  */
14903       parser->type_definition_forbidden_message = saved_message;
14904     }
14905   else
14906     type_id_list = empty_except_spec;
14907
14908   /* Look for the `)'.  */
14909   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14910
14911   return type_id_list;
14912 }
14913
14914 /* Parse an (optional) type-id-list.
14915
14916    type-id-list:
14917      type-id
14918      type-id-list , type-id
14919
14920    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14921    in the order that the types were presented.  */
14922
14923 static tree
14924 cp_parser_type_id_list (cp_parser* parser)
14925 {
14926   tree types = NULL_TREE;
14927
14928   while (true)
14929     {
14930       cp_token *token;
14931       tree type;
14932
14933       /* Get the next type-id.  */
14934       type = cp_parser_type_id (parser);
14935       /* Add it to the list.  */
14936       types = add_exception_specifier (types, type, /*complain=*/1);
14937       /* Peek at the next token.  */
14938       token = cp_lexer_peek_token (parser->lexer);
14939       /* If it is not a `,', we are done.  */
14940       if (token->type != CPP_COMMA)
14941         break;
14942       /* Consume the `,'.  */
14943       cp_lexer_consume_token (parser->lexer);
14944     }
14945
14946   return nreverse (types);
14947 }
14948
14949 /* Parse a try-block.
14950
14951    try-block:
14952      try compound-statement handler-seq  */
14953
14954 static tree
14955 cp_parser_try_block (cp_parser* parser)
14956 {
14957   tree try_block;
14958
14959   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14960   try_block = begin_try_block ();
14961   /* APPLE LOCAL radar 5982990 */
14962   cp_parser_compound_statement (parser, NULL, true, false);
14963   finish_try_block (try_block);
14964   cp_parser_handler_seq (parser);
14965   finish_handler_sequence (try_block);
14966
14967   return try_block;
14968 }
14969
14970 /* Parse a function-try-block.
14971
14972    function-try-block:
14973      try ctor-initializer [opt] function-body handler-seq  */
14974
14975 static bool
14976 cp_parser_function_try_block (cp_parser* parser)
14977 {
14978   tree compound_stmt;
14979   tree try_block;
14980   bool ctor_initializer_p;
14981
14982   /* Look for the `try' keyword.  */
14983   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14984     return false;
14985   /* Let the rest of the front-end know where we are.  */
14986   try_block = begin_function_try_block (&compound_stmt);
14987   /* Parse the function-body.  */
14988   ctor_initializer_p
14989     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14990   /* We're done with the `try' part.  */
14991   finish_function_try_block (try_block);
14992   /* Parse the handlers.  */
14993   cp_parser_handler_seq (parser);
14994   /* We're done with the handlers.  */
14995   finish_function_handler_sequence (try_block, compound_stmt);
14996
14997   return ctor_initializer_p;
14998 }
14999
15000 /* Parse a handler-seq.
15001
15002    handler-seq:
15003      handler handler-seq [opt]  */
15004
15005 static void
15006 cp_parser_handler_seq (cp_parser* parser)
15007 {
15008   while (true)
15009     {
15010       cp_token *token;
15011
15012       /* Parse the handler.  */
15013       cp_parser_handler (parser);
15014       /* Peek at the next token.  */
15015       token = cp_lexer_peek_token (parser->lexer);
15016       /* If it's not `catch' then there are no more handlers.  */
15017       if (!cp_parser_is_keyword (token, RID_CATCH))
15018         break;
15019     }
15020 }
15021
15022 /* Parse a handler.
15023
15024    handler:
15025      catch ( exception-declaration ) compound-statement  */
15026
15027 static void
15028 cp_parser_handler (cp_parser* parser)
15029 {
15030   tree handler;
15031   tree declaration;
15032
15033   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15034   handler = begin_handler ();
15035   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15036   declaration = cp_parser_exception_declaration (parser);
15037   finish_handler_parms (declaration, handler);
15038   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15039   /* APPLE LOCAL radar 5982990 */
15040   cp_parser_compound_statement (parser, NULL, false, false);
15041   finish_handler (handler);
15042 }
15043
15044 /* Parse an exception-declaration.
15045
15046    exception-declaration:
15047      type-specifier-seq declarator
15048      type-specifier-seq abstract-declarator
15049      type-specifier-seq
15050      ...
15051
15052    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15053    ellipsis variant is used.  */
15054
15055 static tree
15056 cp_parser_exception_declaration (cp_parser* parser)
15057 {
15058   cp_decl_specifier_seq type_specifiers;
15059   cp_declarator *declarator;
15060   const char *saved_message;
15061
15062   /* If it's an ellipsis, it's easy to handle.  */
15063   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15064     {
15065       /* Consume the `...' token.  */
15066       cp_lexer_consume_token (parser->lexer);
15067       return NULL_TREE;
15068     }
15069
15070   /* Types may not be defined in exception-declarations.  */
15071   saved_message = parser->type_definition_forbidden_message;
15072   parser->type_definition_forbidden_message
15073     = "types may not be defined in exception-declarations";
15074
15075   /* Parse the type-specifier-seq.  */
15076   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15077                                 &type_specifiers);
15078   /* If it's a `)', then there is no declarator.  */
15079   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15080     declarator = NULL;
15081   else
15082     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15083                                        /*ctor_dtor_or_conv_p=*/NULL,
15084                                        /*parenthesized_p=*/NULL,
15085                                        /*member_p=*/false);
15086
15087   /* Restore the saved message.  */
15088   parser->type_definition_forbidden_message = saved_message;
15089
15090   if (!type_specifiers.any_specifiers_p)
15091     return error_mark_node;
15092
15093   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15094 }
15095
15096 /* Parse a throw-expression.
15097
15098    throw-expression:
15099      throw assignment-expression [opt]
15100
15101    Returns a THROW_EXPR representing the throw-expression.  */
15102
15103 static tree
15104 cp_parser_throw_expression (cp_parser* parser)
15105 {
15106   tree expression;
15107   cp_token* token;
15108
15109   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15110   token = cp_lexer_peek_token (parser->lexer);
15111   /* Figure out whether or not there is an assignment-expression
15112      following the "throw" keyword.  */
15113   if (token->type == CPP_COMMA
15114       || token->type == CPP_SEMICOLON
15115       || token->type == CPP_CLOSE_PAREN
15116       || token->type == CPP_CLOSE_SQUARE
15117       || token->type == CPP_CLOSE_BRACE
15118       || token->type == CPP_COLON)
15119     expression = NULL_TREE;
15120   else
15121     expression = cp_parser_assignment_expression (parser,
15122                                                   /*cast_p=*/false);
15123
15124   return build_throw (expression);
15125 }
15126
15127 /* GNU Extensions */
15128
15129 /* Parse an (optional) asm-specification.
15130
15131    asm-specification:
15132      asm ( string-literal )
15133
15134    If the asm-specification is present, returns a STRING_CST
15135    corresponding to the string-literal.  Otherwise, returns
15136    NULL_TREE.  */
15137
15138 static tree
15139 cp_parser_asm_specification_opt (cp_parser* parser)
15140 {
15141   cp_token *token;
15142   tree asm_specification;
15143
15144   /* Peek at the next token.  */
15145   token = cp_lexer_peek_token (parser->lexer);
15146   /* If the next token isn't the `asm' keyword, then there's no
15147      asm-specification.  */
15148   if (!cp_parser_is_keyword (token, RID_ASM))
15149     return NULL_TREE;
15150
15151   /* Consume the `asm' token.  */
15152   cp_lexer_consume_token (parser->lexer);
15153   /* Look for the `('.  */
15154   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15155
15156   /* Look for the string-literal.  */
15157   asm_specification = cp_parser_string_literal (parser, false, false);
15158
15159   /* Look for the `)'.  */
15160   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15161
15162   return asm_specification;
15163 }
15164
15165 /* Parse an asm-operand-list.
15166
15167    asm-operand-list:
15168      asm-operand
15169      asm-operand-list , asm-operand
15170
15171    asm-operand:
15172      string-literal ( expression )
15173      [ string-literal ] string-literal ( expression )
15174
15175    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15176    each node is the expression.  The TREE_PURPOSE is itself a
15177    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15178    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15179    is a STRING_CST for the string literal before the parenthesis.  */
15180
15181 static tree
15182 cp_parser_asm_operand_list (cp_parser* parser)
15183 {
15184   tree asm_operands = NULL_TREE;
15185
15186   while (true)
15187     {
15188       tree string_literal;
15189       tree expression;
15190       tree name;
15191
15192       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15193         {
15194           /* Consume the `[' token.  */
15195           cp_lexer_consume_token (parser->lexer);
15196           /* Read the operand name.  */
15197           name = cp_parser_identifier (parser);
15198           if (name != error_mark_node)
15199             name = build_string (IDENTIFIER_LENGTH (name),
15200                                  IDENTIFIER_POINTER (name));
15201           /* Look for the closing `]'.  */
15202           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15203         }
15204       else
15205         name = NULL_TREE;
15206       /* Look for the string-literal.  */
15207       string_literal = cp_parser_string_literal (parser, false, false);
15208
15209       /* Look for the `('.  */
15210       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15211       /* Parse the expression.  */
15212       expression = cp_parser_expression (parser, /*cast_p=*/false);
15213       /* Look for the `)'.  */
15214       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15215
15216       /* Add this operand to the list.  */
15217       asm_operands = tree_cons (build_tree_list (name, string_literal),
15218                                 expression,
15219                                 asm_operands);
15220       /* If the next token is not a `,', there are no more
15221          operands.  */
15222       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15223         break;
15224       /* Consume the `,'.  */
15225       cp_lexer_consume_token (parser->lexer);
15226     }
15227
15228   return nreverse (asm_operands);
15229 }
15230
15231 /* Parse an asm-clobber-list.
15232
15233    asm-clobber-list:
15234      string-literal
15235      asm-clobber-list , string-literal
15236
15237    Returns a TREE_LIST, indicating the clobbers in the order that they
15238    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15239
15240 static tree
15241 cp_parser_asm_clobber_list (cp_parser* parser)
15242 {
15243   tree clobbers = NULL_TREE;
15244
15245   while (true)
15246     {
15247       tree string_literal;
15248
15249       /* Look for the string literal.  */
15250       string_literal = cp_parser_string_literal (parser, false, false);
15251       /* Add it to the list.  */
15252       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15253       /* If the next token is not a `,', then the list is
15254          complete.  */
15255       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15256         break;
15257       /* Consume the `,' token.  */
15258       cp_lexer_consume_token (parser->lexer);
15259     }
15260
15261   return clobbers;
15262 }
15263
15264 /* Parse an (optional) series of attributes.
15265
15266    attributes:
15267      attributes attribute
15268
15269    attribute:
15270      __attribute__ (( attribute-list [opt] ))
15271
15272    The return value is as for cp_parser_attribute_list.  */
15273
15274 static tree
15275 cp_parser_attributes_opt (cp_parser* parser)
15276 {
15277   tree attributes = NULL_TREE;
15278
15279   while (true)
15280     {
15281       cp_token *token;
15282       tree attribute_list;
15283
15284       /* Peek at the next token.  */
15285       token = cp_lexer_peek_token (parser->lexer);
15286       /* If it's not `__attribute__', then we're done.  */
15287       if (token->keyword != RID_ATTRIBUTE)
15288         break;
15289
15290       /* Consume the `__attribute__' keyword.  */
15291       cp_lexer_consume_token (parser->lexer);
15292       /* Look for the two `(' tokens.  */
15293       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15294       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15295
15296       /* Peek at the next token.  */
15297       token = cp_lexer_peek_token (parser->lexer);
15298       if (token->type != CPP_CLOSE_PAREN)
15299         /* Parse the attribute-list.  */
15300         attribute_list = cp_parser_attribute_list (parser);
15301       else
15302         /* If the next token is a `)', then there is no attribute
15303            list.  */
15304         attribute_list = NULL;
15305
15306       /* Look for the two `)' tokens.  */
15307       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15308       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15309
15310       /* Add these new attributes to the list.  */
15311       attributes = chainon (attributes, attribute_list);
15312     }
15313
15314   return attributes;
15315 }
15316
15317 /* Parse an attribute-list.
15318
15319    attribute-list:
15320      attribute
15321      attribute-list , attribute
15322
15323    attribute:
15324      identifier
15325      identifier ( identifier )
15326      identifier ( identifier , expression-list )
15327      identifier ( expression-list )
15328
15329    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15330    to an attribute.  The TREE_PURPOSE of each node is the identifier
15331    indicating which attribute is in use.  The TREE_VALUE represents
15332    the arguments, if any.  */
15333
15334 static tree
15335 cp_parser_attribute_list (cp_parser* parser)
15336 {
15337   tree attribute_list = NULL_TREE;
15338   bool save_translate_strings_p = parser->translate_strings_p;
15339
15340   parser->translate_strings_p = false;
15341   while (true)
15342     {
15343       cp_token *token;
15344       tree identifier;
15345       tree attribute;
15346
15347       /* Look for the identifier.  We also allow keywords here; for
15348          example `__attribute__ ((const))' is legal.  */
15349       token = cp_lexer_peek_token (parser->lexer);
15350       if (token->type == CPP_NAME
15351           || token->type == CPP_KEYWORD)
15352         {
15353           tree arguments = NULL_TREE;
15354
15355           /* Consume the token.  */
15356           token = cp_lexer_consume_token (parser->lexer);
15357
15358           /* Save away the identifier that indicates which attribute
15359              this is.  */
15360           identifier = token->u.value;
15361           attribute = build_tree_list (identifier, NULL_TREE);
15362
15363           /* Peek at the next token.  */
15364           token = cp_lexer_peek_token (parser->lexer);
15365           /* If it's an `(', then parse the attribute arguments.  */
15366           if (token->type == CPP_OPEN_PAREN)
15367             {
15368               arguments = cp_parser_parenthesized_expression_list
15369                           (parser, true, /*cast_p=*/false,
15370                            /*non_constant_p=*/NULL);
15371               /* Save the arguments away.  */
15372               TREE_VALUE (attribute) = arguments;
15373             }
15374
15375           if (arguments != error_mark_node)
15376             {
15377               /* Add this attribute to the list.  */
15378               TREE_CHAIN (attribute) = attribute_list;
15379               attribute_list = attribute;
15380             }
15381
15382           token = cp_lexer_peek_token (parser->lexer);
15383         }
15384       /* Now, look for more attributes.  If the next token isn't a
15385          `,', we're done.  */
15386       if (token->type != CPP_COMMA)
15387         break;
15388
15389       /* Consume the comma and keep going.  */
15390       cp_lexer_consume_token (parser->lexer);
15391     }
15392   parser->translate_strings_p = save_translate_strings_p;
15393
15394   /* We built up the list in reverse order.  */
15395   return nreverse (attribute_list);
15396 }
15397
15398 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15399    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15400    current value of the PEDANTIC flag, regardless of whether or not
15401    the `__extension__' keyword is present.  The caller is responsible
15402    for restoring the value of the PEDANTIC flag.  */
15403
15404 static bool
15405 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15406 {
15407   /* Save the old value of the PEDANTIC flag.  */
15408   *saved_pedantic = pedantic;
15409
15410   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15411     {
15412       /* Consume the `__extension__' token.  */
15413       cp_lexer_consume_token (parser->lexer);
15414       /* We're not being pedantic while the `__extension__' keyword is
15415          in effect.  */
15416       pedantic = 0;
15417
15418       return true;
15419     }
15420
15421   return false;
15422 }
15423
15424 /* Parse a label declaration.
15425
15426    label-declaration:
15427      __label__ label-declarator-seq ;
15428
15429    label-declarator-seq:
15430      identifier , label-declarator-seq
15431      identifier  */
15432
15433 static void
15434 cp_parser_label_declaration (cp_parser* parser)
15435 {
15436   /* Look for the `__label__' keyword.  */
15437   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15438
15439   while (true)
15440     {
15441       tree identifier;
15442
15443       /* Look for an identifier.  */
15444       identifier = cp_parser_identifier (parser);
15445       /* If we failed, stop.  */
15446       if (identifier == error_mark_node)
15447         break;
15448       /* Declare it as a label.  */
15449       finish_label_decl (identifier);
15450       /* If the next token is a `;', stop.  */
15451       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15452         break;
15453       /* Look for the `,' separating the label declarations.  */
15454       cp_parser_require (parser, CPP_COMMA, "`,'");
15455     }
15456
15457   /* Look for the final `;'.  */
15458   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15459 }
15460
15461 /* Support Functions */
15462
15463 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15464    NAME should have one of the representations used for an
15465    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15466    is returned.  If PARSER->SCOPE is a dependent type, then a
15467    SCOPE_REF is returned.
15468
15469    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15470    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15471    was formed.  Abstractly, such entities should not be passed to this
15472    function, because they do not need to be looked up, but it is
15473    simpler to check for this special case here, rather than at the
15474    call-sites.
15475
15476    In cases not explicitly covered above, this function returns a
15477    DECL, OVERLOAD, or baselink representing the result of the lookup.
15478    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15479    is returned.
15480
15481    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15482    (e.g., "struct") that was used.  In that case bindings that do not
15483    refer to types are ignored.
15484
15485    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15486    ignored.
15487
15488    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15489    are ignored.
15490
15491    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15492    types.
15493
15494    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15495    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15496    NULL_TREE otherwise.  */
15497
15498 static tree
15499 cp_parser_lookup_name (cp_parser *parser, tree name,
15500                        enum tag_types tag_type,
15501                        bool is_template,
15502                        bool is_namespace,
15503                        bool check_dependency,
15504                        tree *ambiguous_decls)
15505 {
15506   int flags = 0;
15507   tree decl;
15508   tree object_type = parser->context->object_type;
15509
15510   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15511     flags |= LOOKUP_COMPLAIN;
15512
15513   /* Assume that the lookup will be unambiguous.  */
15514   if (ambiguous_decls)
15515     *ambiguous_decls = NULL_TREE;
15516
15517   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15518      no longer valid.  Note that if we are parsing tentatively, and
15519      the parse fails, OBJECT_TYPE will be automatically restored.  */
15520   parser->context->object_type = NULL_TREE;
15521
15522   if (name == error_mark_node)
15523     return error_mark_node;
15524
15525   /* A template-id has already been resolved; there is no lookup to
15526      do.  */
15527   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15528     return name;
15529   if (BASELINK_P (name))
15530     {
15531       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15532                   == TEMPLATE_ID_EXPR);
15533       return name;
15534     }
15535
15536   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15537      it should already have been checked to make sure that the name
15538      used matches the type being destroyed.  */
15539   if (TREE_CODE (name) == BIT_NOT_EXPR)
15540     {
15541       tree type;
15542
15543       /* Figure out to which type this destructor applies.  */
15544       if (parser->scope)
15545         type = parser->scope;
15546       else if (object_type)
15547         type = object_type;
15548       else
15549         type = current_class_type;
15550       /* If that's not a class type, there is no destructor.  */
15551       if (!type || !CLASS_TYPE_P (type))
15552         return error_mark_node;
15553       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15554         lazily_declare_fn (sfk_destructor, type);
15555       if (!CLASSTYPE_DESTRUCTORS (type))
15556           return error_mark_node;
15557       /* If it was a class type, return the destructor.  */
15558       return CLASSTYPE_DESTRUCTORS (type);
15559     }
15560
15561   /* By this point, the NAME should be an ordinary identifier.  If
15562      the id-expression was a qualified name, the qualifying scope is
15563      stored in PARSER->SCOPE at this point.  */
15564   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15565
15566   /* Perform the lookup.  */
15567   if (parser->scope)
15568     {
15569       bool dependent_p;
15570
15571       if (parser->scope == error_mark_node)
15572         return error_mark_node;
15573
15574       /* If the SCOPE is dependent, the lookup must be deferred until
15575          the template is instantiated -- unless we are explicitly
15576          looking up names in uninstantiated templates.  Even then, we
15577          cannot look up the name if the scope is not a class type; it
15578          might, for example, be a template type parameter.  */
15579       dependent_p = (TYPE_P (parser->scope)
15580                      && !(parser->in_declarator_p
15581                           && currently_open_class (parser->scope))
15582                      && dependent_type_p (parser->scope));
15583       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15584            && dependent_p)
15585         {
15586           if (tag_type)
15587             {
15588               tree type;
15589
15590               /* The resolution to Core Issue 180 says that `struct
15591                  A::B' should be considered a type-name, even if `A'
15592                  is dependent.  */
15593               type = make_typename_type (parser->scope, name, tag_type,
15594                                          /*complain=*/tf_error);
15595               decl = TYPE_NAME (type);
15596             }
15597           else if (is_template
15598                    && (cp_parser_next_token_ends_template_argument_p (parser)
15599                        || cp_lexer_next_token_is (parser->lexer,
15600                                                   CPP_CLOSE_PAREN)))
15601             decl = make_unbound_class_template (parser->scope,
15602                                                 name, NULL_TREE,
15603                                                 /*complain=*/tf_error);
15604           else
15605             decl = build_qualified_name (/*type=*/NULL_TREE,
15606                                          parser->scope, name,
15607                                          is_template);
15608         }
15609       else
15610         {
15611           tree pushed_scope = NULL_TREE;
15612
15613           /* If PARSER->SCOPE is a dependent type, then it must be a
15614              class type, and we must not be checking dependencies;
15615              otherwise, we would have processed this lookup above.  So
15616              that PARSER->SCOPE is not considered a dependent base by
15617              lookup_member, we must enter the scope here.  */
15618           if (dependent_p)
15619             pushed_scope = push_scope (parser->scope);
15620           /* If the PARSER->SCOPE is a template specialization, it
15621              may be instantiated during name lookup.  In that case,
15622              errors may be issued.  Even if we rollback the current
15623              tentative parse, those errors are valid.  */
15624           decl = lookup_qualified_name (parser->scope, name,
15625                                         tag_type != none_type,
15626                                         /*complain=*/true);
15627           if (pushed_scope)
15628             pop_scope (pushed_scope);
15629         }
15630       parser->qualifying_scope = parser->scope;
15631       parser->object_scope = NULL_TREE;
15632     }
15633   else if (object_type)
15634     {
15635       tree object_decl = NULL_TREE;
15636       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15637          OBJECT_TYPE is not a class.  */
15638       if (CLASS_TYPE_P (object_type))
15639         /* If the OBJECT_TYPE is a template specialization, it may
15640            be instantiated during name lookup.  In that case, errors
15641            may be issued.  Even if we rollback the current tentative
15642            parse, those errors are valid.  */
15643         object_decl = lookup_member (object_type,
15644                                      name,
15645                                      /*protect=*/0,
15646                                      tag_type != none_type);
15647       /* Look it up in the enclosing context, too.  */
15648       decl = lookup_name_real (name, tag_type != none_type,
15649                                /*nonclass=*/0,
15650                                /*block_p=*/true, is_namespace, flags);
15651       parser->object_scope = object_type;
15652       parser->qualifying_scope = NULL_TREE;
15653       if (object_decl)
15654         decl = object_decl;
15655     }
15656   else
15657     {
15658       decl = lookup_name_real (name, tag_type != none_type,
15659                                /*nonclass=*/0,
15660                                /*block_p=*/true, is_namespace, flags);
15661       parser->qualifying_scope = NULL_TREE;
15662       parser->object_scope = NULL_TREE;
15663     }
15664
15665   /* If the lookup failed, let our caller know.  */
15666   if (!decl || decl == error_mark_node)
15667     return error_mark_node;
15668
15669   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15670   if (TREE_CODE (decl) == TREE_LIST)
15671     {
15672       if (ambiguous_decls)
15673         *ambiguous_decls = decl;
15674       /* The error message we have to print is too complicated for
15675          cp_parser_error, so we incorporate its actions directly.  */
15676       if (!cp_parser_simulate_error (parser))
15677         {
15678           error ("reference to %qD is ambiguous", name);
15679           print_candidates (decl);
15680         }
15681       return error_mark_node;
15682     }
15683
15684   gcc_assert (DECL_P (decl)
15685               || TREE_CODE (decl) == OVERLOAD
15686               || TREE_CODE (decl) == SCOPE_REF
15687               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15688               || BASELINK_P (decl));
15689
15690   /* If we have resolved the name of a member declaration, check to
15691      see if the declaration is accessible.  When the name resolves to
15692      set of overloaded functions, accessibility is checked when
15693      overload resolution is done.
15694
15695      During an explicit instantiation, access is not checked at all,
15696      as per [temp.explicit].  */
15697   if (DECL_P (decl))
15698     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15699
15700   return decl;
15701 }
15702
15703 /* Like cp_parser_lookup_name, but for use in the typical case where
15704    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15705    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15706
15707 static tree
15708 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15709 {
15710   return cp_parser_lookup_name (parser, name,
15711                                 none_type,
15712                                 /*is_template=*/false,
15713                                 /*is_namespace=*/false,
15714                                 /*check_dependency=*/true,
15715                                 /*ambiguous_decls=*/NULL);
15716 }
15717
15718 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15719    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15720    true, the DECL indicates the class being defined in a class-head,
15721    or declared in an elaborated-type-specifier.
15722
15723    Otherwise, return DECL.  */
15724
15725 static tree
15726 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15727 {
15728   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15729      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15730
15731        struct A {
15732          template <typename T> struct B;
15733        };
15734
15735        template <typename T> struct A::B {};
15736
15737      Similarly, in an elaborated-type-specifier:
15738
15739        namespace N { struct X{}; }
15740
15741        struct A {
15742          template <typename T> friend struct N::X;
15743        };
15744
15745      However, if the DECL refers to a class type, and we are in
15746      the scope of the class, then the name lookup automatically
15747      finds the TYPE_DECL created by build_self_reference rather
15748      than a TEMPLATE_DECL.  For example, in:
15749
15750        template <class T> struct S {
15751          S s;
15752        };
15753
15754      there is no need to handle such case.  */
15755
15756   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15757     return DECL_TEMPLATE_RESULT (decl);
15758
15759   return decl;
15760 }
15761
15762 /* If too many, or too few, template-parameter lists apply to the
15763    declarator, issue an error message.  Returns TRUE if all went well,
15764    and FALSE otherwise.  */
15765
15766 static bool
15767 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15768                                                 cp_declarator *declarator)
15769 {
15770   unsigned num_templates;
15771
15772   /* We haven't seen any classes that involve template parameters yet.  */
15773   num_templates = 0;
15774
15775   switch (declarator->kind)
15776     {
15777     case cdk_id:
15778       if (declarator->u.id.qualifying_scope)
15779         {
15780           tree scope;
15781           tree member;
15782
15783           scope = declarator->u.id.qualifying_scope;
15784           member = declarator->u.id.unqualified_name;
15785
15786           while (scope && CLASS_TYPE_P (scope))
15787             {
15788               /* You're supposed to have one `template <...>'
15789                  for every template class, but you don't need one
15790                  for a full specialization.  For example:
15791
15792                  template <class T> struct S{};
15793                  template <> struct S<int> { void f(); };
15794                  void S<int>::f () {}
15795
15796                  is correct; there shouldn't be a `template <>' for
15797                  the definition of `S<int>::f'.  */
15798               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15799                 /* If SCOPE does not have template information of any
15800                    kind, then it is not a template, nor is it nested
15801                    within a template.  */
15802                 break;
15803               if (explicit_class_specialization_p (scope))
15804                 break;
15805               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15806                 ++num_templates;
15807
15808               scope = TYPE_CONTEXT (scope);
15809             }
15810         }
15811       else if (TREE_CODE (declarator->u.id.unqualified_name)
15812                == TEMPLATE_ID_EXPR)
15813         /* If the DECLARATOR has the form `X<y>' then it uses one
15814            additional level of template parameters.  */
15815         ++num_templates;
15816
15817       return cp_parser_check_template_parameters (parser,
15818                                                   num_templates);
15819
15820     case cdk_function:
15821     case cdk_array:
15822     case cdk_pointer:
15823     case cdk_reference:
15824     case cdk_ptrmem:
15825       /* APPLE LOCAL blocks 6040305 */
15826     case cdk_block_pointer:
15827       return (cp_parser_check_declarator_template_parameters
15828               (parser, declarator->declarator));
15829
15830     case cdk_error:
15831       return true;
15832
15833     default:
15834       gcc_unreachable ();
15835     }
15836   return false;
15837 }
15838
15839 /* NUM_TEMPLATES were used in the current declaration.  If that is
15840    invalid, return FALSE and issue an error messages.  Otherwise,
15841    return TRUE.  */
15842
15843 static bool
15844 cp_parser_check_template_parameters (cp_parser* parser,
15845                                      unsigned num_templates)
15846 {
15847   /* If there are more template classes than parameter lists, we have
15848      something like:
15849
15850        template <class T> void S<T>::R<T>::f ();  */
15851   if (parser->num_template_parameter_lists < num_templates)
15852     {
15853       error ("too few template-parameter-lists");
15854       return false;
15855     }
15856   /* If there are the same number of template classes and parameter
15857      lists, that's OK.  */
15858   if (parser->num_template_parameter_lists == num_templates)
15859     return true;
15860   /* If there are more, but only one more, then we are referring to a
15861      member template.  That's OK too.  */
15862   if (parser->num_template_parameter_lists == num_templates + 1)
15863       return true;
15864   /* Otherwise, there are too many template parameter lists.  We have
15865      something like:
15866
15867      template <class T> template <class U> void S::f();  */
15868   error ("too many template-parameter-lists");
15869   return false;
15870 }
15871
15872 /* Parse an optional `::' token indicating that the following name is
15873    from the global namespace.  If so, PARSER->SCOPE is set to the
15874    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15875    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15876    Returns the new value of PARSER->SCOPE, if the `::' token is
15877    present, and NULL_TREE otherwise.  */
15878
15879 static tree
15880 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15881 {
15882   cp_token *token;
15883
15884   /* Peek at the next token.  */
15885   token = cp_lexer_peek_token (parser->lexer);
15886   /* If we're looking at a `::' token then we're starting from the
15887      global namespace, not our current location.  */
15888   if (token->type == CPP_SCOPE)
15889     {
15890       /* Consume the `::' token.  */
15891       cp_lexer_consume_token (parser->lexer);
15892       /* Set the SCOPE so that we know where to start the lookup.  */
15893       parser->scope = global_namespace;
15894       parser->qualifying_scope = global_namespace;
15895       parser->object_scope = NULL_TREE;
15896
15897       return parser->scope;
15898     }
15899   else if (!current_scope_valid_p)
15900     {
15901       parser->scope = NULL_TREE;
15902       parser->qualifying_scope = NULL_TREE;
15903       parser->object_scope = NULL_TREE;
15904     }
15905
15906   return NULL_TREE;
15907 }
15908
15909 /* Returns TRUE if the upcoming token sequence is the start of a
15910    constructor declarator.  If FRIEND_P is true, the declarator is
15911    preceded by the `friend' specifier.  */
15912
15913 static bool
15914 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15915 {
15916   bool constructor_p;
15917   tree type_decl = NULL_TREE;
15918   bool nested_name_p;
15919   cp_token *next_token;
15920
15921   /* The common case is that this is not a constructor declarator, so
15922      try to avoid doing lots of work if at all possible.  It's not
15923      valid declare a constructor at function scope.  */
15924   if (parser->in_function_body)
15925     return false;
15926   /* And only certain tokens can begin a constructor declarator.  */
15927   next_token = cp_lexer_peek_token (parser->lexer);
15928   if (next_token->type != CPP_NAME
15929       && next_token->type != CPP_SCOPE
15930       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15931       && next_token->type != CPP_TEMPLATE_ID)
15932     return false;
15933
15934   /* Parse tentatively; we are going to roll back all of the tokens
15935      consumed here.  */
15936   cp_parser_parse_tentatively (parser);
15937   /* Assume that we are looking at a constructor declarator.  */
15938   constructor_p = true;
15939
15940   /* Look for the optional `::' operator.  */
15941   cp_parser_global_scope_opt (parser,
15942                               /*current_scope_valid_p=*/false);
15943   /* Look for the nested-name-specifier.  */
15944   nested_name_p
15945     = (cp_parser_nested_name_specifier_opt (parser,
15946                                             /*typename_keyword_p=*/false,
15947                                             /*check_dependency_p=*/false,
15948                                             /*type_p=*/false,
15949                                             /*is_declaration=*/false)
15950        != NULL_TREE);
15951   /* Outside of a class-specifier, there must be a
15952      nested-name-specifier.  */
15953   if (!nested_name_p &&
15954       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15955        || friend_p))
15956     constructor_p = false;
15957   /* If we still think that this might be a constructor-declarator,
15958      look for a class-name.  */
15959   if (constructor_p)
15960     {
15961       /* If we have:
15962
15963            template <typename T> struct S { S(); };
15964            template <typename T> S<T>::S ();
15965
15966          we must recognize that the nested `S' names a class.
15967          Similarly, for:
15968
15969            template <typename T> S<T>::S<T> ();
15970
15971          we must recognize that the nested `S' names a template.  */
15972       type_decl = cp_parser_class_name (parser,
15973                                         /*typename_keyword_p=*/false,
15974                                         /*template_keyword_p=*/false,
15975                                         none_type,
15976                                         /*check_dependency_p=*/false,
15977                                         /*class_head_p=*/false,
15978                                         /*is_declaration=*/false);
15979       /* If there was no class-name, then this is not a constructor.  */
15980       constructor_p = !cp_parser_error_occurred (parser);
15981     }
15982
15983   /* If we're still considering a constructor, we have to see a `(',
15984      to begin the parameter-declaration-clause, followed by either a
15985      `)', an `...', or a decl-specifier.  We need to check for a
15986      type-specifier to avoid being fooled into thinking that:
15987
15988        S::S (f) (int);
15989
15990      is a constructor.  (It is actually a function named `f' that
15991      takes one parameter (of type `int') and returns a value of type
15992      `S::S'.  */
15993   if (constructor_p
15994       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15995     {
15996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15997           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15998           /* A parameter declaration begins with a decl-specifier,
15999              which is either the "attribute" keyword, a storage class
16000              specifier, or (usually) a type-specifier.  */
16001           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16002         {
16003           tree type;
16004           tree pushed_scope = NULL_TREE;
16005           unsigned saved_num_template_parameter_lists;
16006
16007           /* Names appearing in the type-specifier should be looked up
16008              in the scope of the class.  */
16009           if (current_class_type)
16010             type = NULL_TREE;
16011           else
16012             {
16013               type = TREE_TYPE (type_decl);
16014               if (TREE_CODE (type) == TYPENAME_TYPE)
16015                 {
16016                   type = resolve_typename_type (type,
16017                                                 /*only_current_p=*/false);
16018                   if (type == error_mark_node)
16019                     {
16020                       cp_parser_abort_tentative_parse (parser);
16021                       return false;
16022                     }
16023                 }
16024               pushed_scope = push_scope (type);
16025             }
16026
16027           /* Inside the constructor parameter list, surrounding
16028              template-parameter-lists do not apply.  */
16029           saved_num_template_parameter_lists
16030             = parser->num_template_parameter_lists;
16031           parser->num_template_parameter_lists = 0;
16032
16033           /* Look for the type-specifier.  */
16034           cp_parser_type_specifier (parser,
16035                                     CP_PARSER_FLAGS_NONE,
16036                                     /*decl_specs=*/NULL,
16037                                     /*is_declarator=*/true,
16038                                     /*declares_class_or_enum=*/NULL,
16039                                     /*is_cv_qualifier=*/NULL);
16040
16041           parser->num_template_parameter_lists
16042             = saved_num_template_parameter_lists;
16043
16044           /* Leave the scope of the class.  */
16045           if (pushed_scope)
16046             pop_scope (pushed_scope);
16047
16048           constructor_p = !cp_parser_error_occurred (parser);
16049         }
16050     }
16051   else
16052     constructor_p = false;
16053   /* We did not really want to consume any tokens.  */
16054   cp_parser_abort_tentative_parse (parser);
16055
16056   return constructor_p;
16057 }
16058
16059 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16060    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16061    they must be performed once we are in the scope of the function.
16062
16063    Returns the function defined.  */
16064
16065 static tree
16066 cp_parser_function_definition_from_specifiers_and_declarator
16067   (cp_parser* parser,
16068    cp_decl_specifier_seq *decl_specifiers,
16069    tree attributes,
16070    const cp_declarator *declarator)
16071 {
16072   tree fn;
16073   bool success_p;
16074
16075   /* Begin the function-definition.  */
16076   success_p = start_function (decl_specifiers, declarator, attributes);
16077
16078   /* The things we're about to see are not directly qualified by any
16079      template headers we've seen thus far.  */
16080   reset_specialization ();
16081
16082   /* If there were names looked up in the decl-specifier-seq that we
16083      did not check, check them now.  We must wait until we are in the
16084      scope of the function to perform the checks, since the function
16085      might be a friend.  */
16086   perform_deferred_access_checks ();
16087
16088   if (!success_p)
16089     {
16090       /* Skip the entire function.  */
16091       cp_parser_skip_to_end_of_block_or_statement (parser);
16092       fn = error_mark_node;
16093     }
16094   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16095     {
16096       /* Seen already, skip it.  An error message has already been output.  */
16097       cp_parser_skip_to_end_of_block_or_statement (parser);
16098       fn = current_function_decl;
16099       current_function_decl = NULL_TREE;
16100       /* If this is a function from a class, pop the nested class.  */
16101       if (current_class_name)
16102         pop_nested_class ();
16103     }
16104   else
16105     fn = cp_parser_function_definition_after_declarator (parser,
16106                                                          /*inline_p=*/false);
16107
16108   return fn;
16109 }
16110
16111 /* Parse the part of a function-definition that follows the
16112    declarator.  INLINE_P is TRUE iff this function is an inline
16113    function defined with a class-specifier.
16114
16115    Returns the function defined.  */
16116
16117 static tree
16118 cp_parser_function_definition_after_declarator (cp_parser* parser,
16119                                                 bool inline_p)
16120 {
16121   tree fn;
16122   bool ctor_initializer_p = false;
16123   bool saved_in_unbraced_linkage_specification_p;
16124   bool saved_in_function_body;
16125   unsigned saved_num_template_parameter_lists;
16126
16127   saved_in_function_body = parser->in_function_body;
16128   parser->in_function_body = true;
16129   /* If the next token is `return', then the code may be trying to
16130      make use of the "named return value" extension that G++ used to
16131      support.  */
16132   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16133     {
16134       /* Consume the `return' keyword.  */
16135       cp_lexer_consume_token (parser->lexer);
16136       /* Look for the identifier that indicates what value is to be
16137          returned.  */
16138       cp_parser_identifier (parser);
16139       /* Issue an error message.  */
16140       error ("named return values are no longer supported");
16141       /* Skip tokens until we reach the start of the function body.  */
16142       while (true)
16143         {
16144           cp_token *token = cp_lexer_peek_token (parser->lexer);
16145           if (token->type == CPP_OPEN_BRACE
16146               || token->type == CPP_EOF
16147               || token->type == CPP_PRAGMA_EOL)
16148             break;
16149           cp_lexer_consume_token (parser->lexer);
16150         }
16151     }
16152   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16153      anything declared inside `f'.  */
16154   saved_in_unbraced_linkage_specification_p
16155     = parser->in_unbraced_linkage_specification_p;
16156   parser->in_unbraced_linkage_specification_p = false;
16157   /* Inside the function, surrounding template-parameter-lists do not
16158      apply.  */
16159   saved_num_template_parameter_lists
16160     = parser->num_template_parameter_lists;
16161   parser->num_template_parameter_lists = 0;
16162   /* If the next token is `try', then we are looking at a
16163      function-try-block.  */
16164   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16165     ctor_initializer_p = cp_parser_function_try_block (parser);
16166   /* A function-try-block includes the function-body, so we only do
16167      this next part if we're not processing a function-try-block.  */
16168   else
16169     ctor_initializer_p
16170       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16171
16172   /* Finish the function.  */
16173   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16174                         (inline_p ? 2 : 0));
16175   /* Generate code for it, if necessary.  */
16176   expand_or_defer_fn (fn);
16177   /* Restore the saved values.  */
16178   parser->in_unbraced_linkage_specification_p
16179     = saved_in_unbraced_linkage_specification_p;
16180   parser->num_template_parameter_lists
16181     = saved_num_template_parameter_lists;
16182   parser->in_function_body = saved_in_function_body;
16183
16184   return fn;
16185 }
16186
16187 /* Parse a template-declaration, assuming that the `export' (and
16188    `extern') keywords, if present, has already been scanned.  MEMBER_P
16189    is as for cp_parser_template_declaration.  */
16190
16191 static void
16192 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16193 {
16194   tree decl = NULL_TREE;
16195   VEC (deferred_access_check,gc) *checks;
16196   tree parameter_list;
16197   bool friend_p = false;
16198   bool need_lang_pop;
16199
16200   /* Look for the `template' keyword.  */
16201   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16202     return;
16203
16204   /* And the `<'.  */
16205   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16206     return;
16207   if (at_class_scope_p () && current_function_decl)
16208     {
16209       /* 14.5.2.2 [temp.mem]
16210
16211          A local class shall not have member templates.  */
16212       error ("invalid declaration of member template in local class");
16213       cp_parser_skip_to_end_of_block_or_statement (parser);
16214       return;
16215     }
16216   /* [temp]
16217
16218      A template ... shall not have C linkage.  */
16219   if (current_lang_name == lang_name_c)
16220     {
16221       error ("template with C linkage");
16222       /* Give it C++ linkage to avoid confusing other parts of the
16223          front end.  */
16224       push_lang_context (lang_name_cplusplus);
16225       need_lang_pop = true;
16226     }
16227   else
16228     need_lang_pop = false;
16229
16230   /* We cannot perform access checks on the template parameter
16231      declarations until we know what is being declared, just as we
16232      cannot check the decl-specifier list.  */
16233   push_deferring_access_checks (dk_deferred);
16234
16235   /* If the next token is `>', then we have an invalid
16236      specialization.  Rather than complain about an invalid template
16237      parameter, issue an error message here.  */
16238   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16239     {
16240       cp_parser_error (parser, "invalid explicit specialization");
16241       begin_specialization ();
16242       parameter_list = NULL_TREE;
16243     }
16244   else
16245     /* Parse the template parameters.  */
16246     parameter_list = cp_parser_template_parameter_list (parser);
16247
16248   /* Get the deferred access checks from the parameter list.  These
16249      will be checked once we know what is being declared, as for a
16250      member template the checks must be performed in the scope of the
16251      class containing the member.  */
16252   checks = get_deferred_access_checks ();
16253
16254   /* Look for the `>'.  */
16255   cp_parser_skip_to_end_of_template_parameter_list (parser);
16256   /* We just processed one more parameter list.  */
16257   ++parser->num_template_parameter_lists;
16258   /* If the next token is `template', there are more template
16259      parameters.  */
16260   if (cp_lexer_next_token_is_keyword (parser->lexer,
16261                                       RID_TEMPLATE))
16262     cp_parser_template_declaration_after_export (parser, member_p);
16263   else
16264     {
16265       /* There are no access checks when parsing a template, as we do not
16266          know if a specialization will be a friend.  */
16267       push_deferring_access_checks (dk_no_check);
16268       decl = cp_parser_single_declaration (parser,
16269                                            checks,
16270                                            member_p,
16271                                            &friend_p);
16272       pop_deferring_access_checks ();
16273
16274       /* If this is a member template declaration, let the front
16275          end know.  */
16276       if (member_p && !friend_p && decl)
16277         {
16278           if (TREE_CODE (decl) == TYPE_DECL)
16279             cp_parser_check_access_in_redeclaration (decl);
16280
16281           decl = finish_member_template_decl (decl);
16282         }
16283       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16284         make_friend_class (current_class_type, TREE_TYPE (decl),
16285                            /*complain=*/true);
16286     }
16287   /* We are done with the current parameter list.  */
16288   --parser->num_template_parameter_lists;
16289
16290   pop_deferring_access_checks ();
16291
16292   /* Finish up.  */
16293   finish_template_decl (parameter_list);
16294
16295   /* Register member declarations.  */
16296   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16297     finish_member_declaration (decl);
16298   /* For the erroneous case of a template with C linkage, we pushed an
16299      implicit C++ linkage scope; exit that scope now.  */
16300   if (need_lang_pop)
16301     pop_lang_context ();
16302   /* If DECL is a function template, we must return to parse it later.
16303      (Even though there is no definition, there might be default
16304      arguments that need handling.)  */
16305   if (member_p && decl
16306       && (TREE_CODE (decl) == FUNCTION_DECL
16307           || DECL_FUNCTION_TEMPLATE_P (decl)))
16308     TREE_VALUE (parser->unparsed_functions_queues)
16309       = tree_cons (NULL_TREE, decl,
16310                    TREE_VALUE (parser->unparsed_functions_queues));
16311 }
16312
16313 /* Perform the deferred access checks from a template-parameter-list.
16314    CHECKS is a TREE_LIST of access checks, as returned by
16315    get_deferred_access_checks.  */
16316
16317 static void
16318 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16319 {
16320   ++processing_template_parmlist;
16321   perform_access_checks (checks);
16322   --processing_template_parmlist;
16323 }
16324
16325 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16326    `function-definition' sequence.  MEMBER_P is true, this declaration
16327    appears in a class scope.
16328
16329    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16330    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16331
16332 static tree
16333 cp_parser_single_declaration (cp_parser* parser,
16334                               VEC (deferred_access_check,gc)* checks,
16335                               bool member_p,
16336                               bool* friend_p)
16337 {
16338   int declares_class_or_enum;
16339   tree decl = NULL_TREE;
16340   cp_decl_specifier_seq decl_specifiers;
16341   bool function_definition_p = false;
16342
16343   /* This function is only used when processing a template
16344      declaration.  */
16345   gcc_assert (innermost_scope_kind () == sk_template_parms
16346               || innermost_scope_kind () == sk_template_spec);
16347
16348   /* Defer access checks until we know what is being declared.  */
16349   push_deferring_access_checks (dk_deferred);
16350
16351   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16352      alternative.  */
16353   cp_parser_decl_specifier_seq (parser,
16354                                 CP_PARSER_FLAGS_OPTIONAL,
16355                                 &decl_specifiers,
16356                                 &declares_class_or_enum);
16357   if (friend_p)
16358     *friend_p = cp_parser_friend_p (&decl_specifiers);
16359
16360   /* There are no template typedefs.  */
16361   if (decl_specifiers.specs[(int) ds_typedef])
16362     {
16363       error ("template declaration of %qs", "typedef");
16364       decl = error_mark_node;
16365     }
16366
16367   /* Gather up the access checks that occurred the
16368      decl-specifier-seq.  */
16369   stop_deferring_access_checks ();
16370
16371   /* Check for the declaration of a template class.  */
16372   if (declares_class_or_enum)
16373     {
16374       if (cp_parser_declares_only_class_p (parser))
16375         {
16376           decl = shadow_tag (&decl_specifiers);
16377
16378           /* In this case:
16379
16380                struct C {
16381                  friend template <typename T> struct A<T>::B;
16382                };
16383
16384              A<T>::B will be represented by a TYPENAME_TYPE, and
16385              therefore not recognized by shadow_tag.  */
16386           if (friend_p && *friend_p
16387               && !decl
16388               && decl_specifiers.type
16389               && TYPE_P (decl_specifiers.type))
16390             decl = decl_specifiers.type;
16391
16392           if (decl && decl != error_mark_node)
16393             decl = TYPE_NAME (decl);
16394           else
16395             decl = error_mark_node;
16396
16397           /* Perform access checks for template parameters.  */
16398           cp_parser_perform_template_parameter_access_checks (checks);
16399         }
16400     }
16401   /* If it's not a template class, try for a template function.  If
16402      the next token is a `;', then this declaration does not declare
16403      anything.  But, if there were errors in the decl-specifiers, then
16404      the error might well have come from an attempted class-specifier.
16405      In that case, there's no need to warn about a missing declarator.  */
16406   if (!decl
16407       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16408           || decl_specifiers.type != error_mark_node))
16409     decl = cp_parser_init_declarator (parser,
16410                                       &decl_specifiers,
16411                                       checks,
16412                                       /*function_definition_allowed_p=*/true,
16413                                       member_p,
16414                                       declares_class_or_enum,
16415                                       &function_definition_p);
16416
16417   pop_deferring_access_checks ();
16418
16419   /* Clear any current qualification; whatever comes next is the start
16420      of something new.  */
16421   parser->scope = NULL_TREE;
16422   parser->qualifying_scope = NULL_TREE;
16423   parser->object_scope = NULL_TREE;
16424   /* Look for a trailing `;' after the declaration.  */
16425   if (!function_definition_p
16426       && (decl == error_mark_node
16427           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16428     cp_parser_skip_to_end_of_block_or_statement (parser);
16429
16430   return decl;
16431 }
16432
16433 /* Parse a cast-expression that is not the operand of a unary "&".  */
16434
16435 static tree
16436 cp_parser_simple_cast_expression (cp_parser *parser)
16437 {
16438   return cp_parser_cast_expression (parser, /*address_p=*/false,
16439                                     /*cast_p=*/false);
16440 }
16441
16442 /* Parse a functional cast to TYPE.  Returns an expression
16443    representing the cast.  */
16444
16445 static tree
16446 cp_parser_functional_cast (cp_parser* parser, tree type)
16447 {
16448   tree expression_list;
16449   tree cast;
16450
16451   expression_list
16452     = cp_parser_parenthesized_expression_list (parser, false,
16453                                                /*cast_p=*/true,
16454                                                /*non_constant_p=*/NULL);
16455
16456   cast = build_functional_cast (type, expression_list);
16457   /* [expr.const]/1: In an integral constant expression "only type
16458      conversions to integral or enumeration type can be used".  */
16459   if (TREE_CODE (type) == TYPE_DECL)
16460     type = TREE_TYPE (type);
16461   if (cast != error_mark_node
16462       && !cast_valid_in_integral_constant_expression_p (type)
16463       && (cp_parser_non_integral_constant_expression
16464           (parser, "a call to a constructor")))
16465     return error_mark_node;
16466   return cast;
16467 }
16468
16469 /* Save the tokens that make up the body of a member function defined
16470    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16471    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16472    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16473    for the member function.  */
16474
16475 static tree
16476 cp_parser_save_member_function_body (cp_parser* parser,
16477                                      cp_decl_specifier_seq *decl_specifiers,
16478                                      cp_declarator *declarator,
16479                                      tree attributes)
16480 {
16481   cp_token *first;
16482   cp_token *last;
16483   tree fn;
16484
16485   /* Create the function-declaration.  */
16486   fn = start_method (decl_specifiers, declarator, attributes);
16487   /* If something went badly wrong, bail out now.  */
16488   if (fn == error_mark_node)
16489     {
16490       /* If there's a function-body, skip it.  */
16491       if (cp_parser_token_starts_function_definition_p
16492           (cp_lexer_peek_token (parser->lexer)))
16493         cp_parser_skip_to_end_of_block_or_statement (parser);
16494       return error_mark_node;
16495     }
16496
16497   /* Remember it, if there default args to post process.  */
16498   cp_parser_save_default_args (parser, fn);
16499
16500   /* Save away the tokens that make up the body of the
16501      function.  */
16502   first = parser->lexer->next_token;
16503   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16504   /* Handle function try blocks.  */
16505   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16506     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16507   last = parser->lexer->next_token;
16508
16509   /* Save away the inline definition; we will process it when the
16510      class is complete.  */
16511   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16512   DECL_PENDING_INLINE_P (fn) = 1;
16513
16514   /* We need to know that this was defined in the class, so that
16515      friend templates are handled correctly.  */
16516   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16517
16518   /* We're done with the inline definition.  */
16519   finish_method (fn);
16520
16521   /* Add FN to the queue of functions to be parsed later.  */
16522   TREE_VALUE (parser->unparsed_functions_queues)
16523     = tree_cons (NULL_TREE, fn,
16524                  TREE_VALUE (parser->unparsed_functions_queues));
16525
16526   return fn;
16527 }
16528
16529 /* Parse a template-argument-list, as well as the trailing ">" (but
16530    not the opening ">").  See cp_parser_template_argument_list for the
16531    return value.  */
16532
16533 static tree
16534 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16535 {
16536   tree arguments;
16537   tree saved_scope;
16538   tree saved_qualifying_scope;
16539   tree saved_object_scope;
16540   bool saved_greater_than_is_operator_p;
16541   bool saved_skip_evaluation;
16542
16543   /* [temp.names]
16544
16545      When parsing a template-id, the first non-nested `>' is taken as
16546      the end of the template-argument-list rather than a greater-than
16547      operator.  */
16548   saved_greater_than_is_operator_p
16549     = parser->greater_than_is_operator_p;
16550   parser->greater_than_is_operator_p = false;
16551   /* Parsing the argument list may modify SCOPE, so we save it
16552      here.  */
16553   saved_scope = parser->scope;
16554   saved_qualifying_scope = parser->qualifying_scope;
16555   saved_object_scope = parser->object_scope;
16556   /* We need to evaluate the template arguments, even though this
16557      template-id may be nested within a "sizeof".  */
16558   saved_skip_evaluation = skip_evaluation;
16559   skip_evaluation = false;
16560   /* Parse the template-argument-list itself.  */
16561   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16562     arguments = NULL_TREE;
16563   else
16564     arguments = cp_parser_template_argument_list (parser);
16565   /* Look for the `>' that ends the template-argument-list. If we find
16566      a '>>' instead, it's probably just a typo.  */
16567   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16568     {
16569       if (!saved_greater_than_is_operator_p)
16570         {
16571           /* If we're in a nested template argument list, the '>>' has
16572             to be a typo for '> >'. We emit the error message, but we
16573             continue parsing and we push a '>' as next token, so that
16574             the argument list will be parsed correctly.  Note that the
16575             global source location is still on the token before the
16576             '>>', so we need to say explicitly where we want it.  */
16577           cp_token *token = cp_lexer_peek_token (parser->lexer);
16578           error ("%H%<>>%> should be %<> >%> "
16579                  "within a nested template argument list",
16580                  &token->location);
16581
16582           /* ??? Proper recovery should terminate two levels of
16583              template argument list here.  */
16584           token->type = CPP_GREATER;
16585         }
16586       else
16587         {
16588           /* If this is not a nested template argument list, the '>>'
16589             is a typo for '>'. Emit an error message and continue.
16590             Same deal about the token location, but here we can get it
16591             right by consuming the '>>' before issuing the diagnostic.  */
16592           cp_lexer_consume_token (parser->lexer);
16593           error ("spurious %<>>%>, use %<>%> to terminate "
16594                  "a template argument list");
16595         }
16596     }
16597   else
16598     cp_parser_skip_to_end_of_template_parameter_list (parser);
16599   /* The `>' token might be a greater-than operator again now.  */
16600   parser->greater_than_is_operator_p
16601     = saved_greater_than_is_operator_p;
16602   /* Restore the SAVED_SCOPE.  */
16603   parser->scope = saved_scope;
16604   parser->qualifying_scope = saved_qualifying_scope;
16605   parser->object_scope = saved_object_scope;
16606   skip_evaluation = saved_skip_evaluation;
16607
16608   return arguments;
16609 }
16610
16611 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16612    arguments, or the body of the function have not yet been parsed,
16613    parse them now.  */
16614
16615 static void
16616 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16617 {
16618   /* If this member is a template, get the underlying
16619      FUNCTION_DECL.  */
16620   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16621     member_function = DECL_TEMPLATE_RESULT (member_function);
16622
16623   /* There should not be any class definitions in progress at this
16624      point; the bodies of members are only parsed outside of all class
16625      definitions.  */
16626   gcc_assert (parser->num_classes_being_defined == 0);
16627   /* While we're parsing the member functions we might encounter more
16628      classes.  We want to handle them right away, but we don't want
16629      them getting mixed up with functions that are currently in the
16630      queue.  */
16631   parser->unparsed_functions_queues
16632     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16633
16634   /* Make sure that any template parameters are in scope.  */
16635   maybe_begin_member_template_processing (member_function);
16636
16637   /* If the body of the function has not yet been parsed, parse it
16638      now.  */
16639   if (DECL_PENDING_INLINE_P (member_function))
16640     {
16641       tree function_scope;
16642       cp_token_cache *tokens;
16643
16644       /* The function is no longer pending; we are processing it.  */
16645       tokens = DECL_PENDING_INLINE_INFO (member_function);
16646       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16647       DECL_PENDING_INLINE_P (member_function) = 0;
16648
16649       /* If this is a local class, enter the scope of the containing
16650          function.  */
16651       function_scope = current_function_decl;
16652       if (function_scope)
16653         push_function_context_to (function_scope);
16654
16655
16656       /* Push the body of the function onto the lexer stack.  */
16657       cp_parser_push_lexer_for_tokens (parser, tokens);
16658
16659       /* Let the front end know that we going to be defining this
16660          function.  */
16661       start_preparsed_function (member_function, NULL_TREE,
16662                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16663
16664       /* Don't do access checking if it is a templated function.  */
16665       if (processing_template_decl)
16666         push_deferring_access_checks (dk_no_check);
16667
16668       /* Now, parse the body of the function.  */
16669       cp_parser_function_definition_after_declarator (parser,
16670                                                       /*inline_p=*/true);
16671
16672       if (processing_template_decl)
16673         pop_deferring_access_checks ();
16674
16675       /* Leave the scope of the containing function.  */
16676       if (function_scope)
16677         pop_function_context_from (function_scope);
16678       cp_parser_pop_lexer (parser);
16679     }
16680
16681   /* Remove any template parameters from the symbol table.  */
16682   maybe_end_member_template_processing ();
16683
16684   /* Restore the queue.  */
16685   parser->unparsed_functions_queues
16686     = TREE_CHAIN (parser->unparsed_functions_queues);
16687 }
16688
16689 /* If DECL contains any default args, remember it on the unparsed
16690    functions queue.  */
16691
16692 static void
16693 cp_parser_save_default_args (cp_parser* parser, tree decl)
16694 {
16695   tree probe;
16696
16697   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16698        probe;
16699        probe = TREE_CHAIN (probe))
16700     if (TREE_PURPOSE (probe))
16701       {
16702         TREE_PURPOSE (parser->unparsed_functions_queues)
16703           = tree_cons (current_class_type, decl,
16704                        TREE_PURPOSE (parser->unparsed_functions_queues));
16705         break;
16706       }
16707 }
16708
16709 /* FN is a FUNCTION_DECL which may contains a parameter with an
16710    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16711    assumes that the current scope is the scope in which the default
16712    argument should be processed.  */
16713
16714 static void
16715 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16716 {
16717   bool saved_local_variables_forbidden_p;
16718   tree parm;
16719
16720   /* While we're parsing the default args, we might (due to the
16721      statement expression extension) encounter more classes.  We want
16722      to handle them right away, but we don't want them getting mixed
16723      up with default args that are currently in the queue.  */
16724   parser->unparsed_functions_queues
16725     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16726
16727   /* Local variable names (and the `this' keyword) may not appear
16728      in a default argument.  */
16729   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16730   parser->local_variables_forbidden_p = true;
16731
16732   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16733        parm;
16734        parm = TREE_CHAIN (parm))
16735     {
16736       cp_token_cache *tokens;
16737       tree default_arg = TREE_PURPOSE (parm);
16738       tree parsed_arg;
16739       VEC(tree,gc) *insts;
16740       tree copy;
16741       unsigned ix;
16742
16743       if (!default_arg)
16744         continue;
16745
16746       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16747         /* This can happen for a friend declaration for a function
16748            already declared with default arguments.  */
16749         continue;
16750
16751        /* Push the saved tokens for the default argument onto the parser's
16752           lexer stack.  */
16753       tokens = DEFARG_TOKENS (default_arg);
16754       cp_parser_push_lexer_for_tokens (parser, tokens);
16755
16756       /* Parse the assignment-expression.  */
16757       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16758
16759       if (!processing_template_decl)
16760         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16761
16762       TREE_PURPOSE (parm) = parsed_arg;
16763
16764       /* Update any instantiations we've already created.  */
16765       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16766            VEC_iterate (tree, insts, ix, copy); ix++)
16767         TREE_PURPOSE (copy) = parsed_arg;
16768
16769       /* If the token stream has not been completely used up, then
16770          there was extra junk after the end of the default
16771          argument.  */
16772       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16773         cp_parser_error (parser, "expected %<,%>");
16774
16775       /* Revert to the main lexer.  */
16776       cp_parser_pop_lexer (parser);
16777     }
16778
16779   /* Make sure no default arg is missing.  */
16780   check_default_args (fn);
16781
16782   /* Restore the state of local_variables_forbidden_p.  */
16783   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16784
16785   /* Restore the queue.  */
16786   parser->unparsed_functions_queues
16787     = TREE_CHAIN (parser->unparsed_functions_queues);
16788 }
16789
16790 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16791    either a TYPE or an expression, depending on the form of the
16792    input.  The KEYWORD indicates which kind of expression we have
16793    encountered.  */
16794
16795 static tree
16796 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16797 {
16798   static const char *format;
16799   tree expr = NULL_TREE;
16800   const char *saved_message;
16801   bool saved_integral_constant_expression_p;
16802   bool saved_non_integral_constant_expression_p;
16803
16804   /* Initialize FORMAT the first time we get here.  */
16805   if (!format)
16806     format = "types may not be defined in '%s' expressions";
16807
16808   /* Types cannot be defined in a `sizeof' expression.  Save away the
16809      old message.  */
16810   saved_message = parser->type_definition_forbidden_message;
16811   /* And create the new one.  */
16812   parser->type_definition_forbidden_message
16813     = XNEWVEC (const char, strlen (format)
16814                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16815                + 1 /* `\0' */);
16816   sprintf ((char *) parser->type_definition_forbidden_message,
16817            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16818
16819   /* The restrictions on constant-expressions do not apply inside
16820      sizeof expressions.  */
16821   saved_integral_constant_expression_p
16822     = parser->integral_constant_expression_p;
16823   saved_non_integral_constant_expression_p
16824     = parser->non_integral_constant_expression_p;
16825   parser->integral_constant_expression_p = false;
16826
16827   /* Do not actually evaluate the expression.  */
16828   ++skip_evaluation;
16829   /* If it's a `(', then we might be looking at the type-id
16830      construction.  */
16831   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16832     {
16833       tree type;
16834       bool saved_in_type_id_in_expr_p;
16835
16836       /* We can't be sure yet whether we're looking at a type-id or an
16837          expression.  */
16838       cp_parser_parse_tentatively (parser);
16839       /* Consume the `('.  */
16840       cp_lexer_consume_token (parser->lexer);
16841       /* Parse the type-id.  */
16842       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16843       parser->in_type_id_in_expr_p = true;
16844       type = cp_parser_type_id (parser);
16845       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16846       /* Now, look for the trailing `)'.  */
16847       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16848       /* If all went well, then we're done.  */
16849       if (cp_parser_parse_definitely (parser))
16850         {
16851           cp_decl_specifier_seq decl_specs;
16852
16853           /* Build a trivial decl-specifier-seq.  */
16854           clear_decl_specs (&decl_specs);
16855           decl_specs.type = type;
16856
16857           /* Call grokdeclarator to figure out what type this is.  */
16858           expr = grokdeclarator (NULL,
16859                                  &decl_specs,
16860                                  TYPENAME,
16861                                  /*initialized=*/0,
16862                                  /*attrlist=*/NULL);
16863         }
16864     }
16865
16866   /* If the type-id production did not work out, then we must be
16867      looking at the unary-expression production.  */
16868   if (!expr)
16869     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16870                                        /*cast_p=*/false);
16871   /* Go back to evaluating expressions.  */
16872   --skip_evaluation;
16873
16874   /* Free the message we created.  */
16875   free ((char *) parser->type_definition_forbidden_message);
16876   /* And restore the old one.  */
16877   parser->type_definition_forbidden_message = saved_message;
16878   parser->integral_constant_expression_p
16879     = saved_integral_constant_expression_p;
16880   parser->non_integral_constant_expression_p
16881     = saved_non_integral_constant_expression_p;
16882
16883   return expr;
16884 }
16885
16886 /* If the current declaration has no declarator, return true.  */
16887
16888 static bool
16889 cp_parser_declares_only_class_p (cp_parser *parser)
16890 {
16891   /* If the next token is a `;' or a `,' then there is no
16892      declarator.  */
16893   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16894           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16895 }
16896
16897 /* Update the DECL_SPECS to reflect the storage class indicated by
16898    KEYWORD.  */
16899
16900 static void
16901 cp_parser_set_storage_class (cp_parser *parser,
16902                              cp_decl_specifier_seq *decl_specs,
16903                              enum rid keyword)
16904 {
16905   cp_storage_class storage_class;
16906
16907   if (parser->in_unbraced_linkage_specification_p)
16908     {
16909       error ("invalid use of %qD in linkage specification",
16910              ridpointers[keyword]);
16911       return;
16912     }
16913   else if (decl_specs->storage_class != sc_none)
16914     {
16915       decl_specs->conflicting_specifiers_p = true;
16916       return;
16917     }
16918
16919   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16920       && decl_specs->specs[(int) ds_thread])
16921     {
16922       error ("%<__thread%> before %qD", ridpointers[keyword]);
16923       decl_specs->specs[(int) ds_thread] = 0;
16924     }
16925
16926   switch (keyword)
16927     {
16928     case RID_AUTO:
16929       storage_class = sc_auto;
16930       break;
16931     case RID_REGISTER:
16932       storage_class = sc_register;
16933       break;
16934     case RID_STATIC:
16935       storage_class = sc_static;
16936       break;
16937     case RID_EXTERN:
16938       storage_class = sc_extern;
16939       break;
16940     case RID_MUTABLE:
16941       storage_class = sc_mutable;
16942       break;
16943     default:
16944       gcc_unreachable ();
16945     }
16946   decl_specs->storage_class = storage_class;
16947
16948   /* A storage class specifier cannot be applied alongside a typedef 
16949      specifier. If there is a typedef specifier present then set 
16950      conflicting_specifiers_p which will trigger an error later
16951      on in grokdeclarator. */
16952   if (decl_specs->specs[(int)ds_typedef])
16953     decl_specs->conflicting_specifiers_p = true;
16954 }
16955
16956 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16957    is true, the type is a user-defined type; otherwise it is a
16958    built-in type specified by a keyword.  */
16959
16960 static void
16961 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16962                               tree type_spec,
16963                               bool user_defined_p)
16964 {
16965   decl_specs->any_specifiers_p = true;
16966
16967   /* If the user tries to redeclare bool or wchar_t (with, for
16968      example, in "typedef int wchar_t;") we remember that this is what
16969      happened.  In system headers, we ignore these declarations so
16970      that G++ can work with system headers that are not C++-safe.  */
16971   if (decl_specs->specs[(int) ds_typedef]
16972       && !user_defined_p
16973       && (type_spec == boolean_type_node
16974           || type_spec == wchar_type_node)
16975       && (decl_specs->type
16976           || decl_specs->specs[(int) ds_long]
16977           || decl_specs->specs[(int) ds_short]
16978           || decl_specs->specs[(int) ds_unsigned]
16979           || decl_specs->specs[(int) ds_signed]))
16980     {
16981       decl_specs->redefined_builtin_type = type_spec;
16982       if (!decl_specs->type)
16983         {
16984           decl_specs->type = type_spec;
16985           decl_specs->user_defined_type_p = false;
16986         }
16987     }
16988   else if (decl_specs->type)
16989     decl_specs->multiple_types_p = true;
16990   else
16991     {
16992       decl_specs->type = type_spec;
16993       decl_specs->user_defined_type_p = user_defined_p;
16994       decl_specs->redefined_builtin_type = NULL_TREE;
16995     }
16996 }
16997
16998 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16999    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17000
17001 static bool
17002 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17003 {
17004   return decl_specifiers->specs[(int) ds_friend] != 0;
17005 }
17006
17007 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17008    issue an error message indicating that TOKEN_DESC was expected.
17009
17010    Returns the token consumed, if the token had the appropriate type.
17011    Otherwise, returns NULL.  */
17012
17013 static cp_token *
17014 cp_parser_require (cp_parser* parser,
17015                    enum cpp_ttype type,
17016                    const char* token_desc)
17017 {
17018   if (cp_lexer_next_token_is (parser->lexer, type))
17019     return cp_lexer_consume_token (parser->lexer);
17020   else
17021     {
17022       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17023       if (!cp_parser_simulate_error (parser))
17024         {
17025           char *message = concat ("expected ", token_desc, NULL);
17026           cp_parser_error (parser, message);
17027           free (message);
17028         }
17029       return NULL;
17030     }
17031 }
17032
17033 /* An error message is produced if the next token is not '>'.
17034    All further tokens are skipped until the desired token is
17035    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17036
17037 static void
17038 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17039 {
17040   /* Current level of '< ... >'.  */
17041   unsigned level = 0;
17042   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17043   unsigned nesting_depth = 0;
17044
17045   /* Are we ready, yet?  If not, issue error message.  */
17046   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17047     return;
17048
17049   /* Skip tokens until the desired token is found.  */
17050   while (true)
17051     {
17052       /* Peek at the next token.  */
17053       switch (cp_lexer_peek_token (parser->lexer)->type)
17054         {
17055         case CPP_LESS:
17056           if (!nesting_depth)
17057             ++level;
17058           break;
17059
17060         case CPP_GREATER:
17061           if (!nesting_depth && level-- == 0)
17062             {
17063               /* We've reached the token we want, consume it and stop.  */
17064               cp_lexer_consume_token (parser->lexer);
17065               return;
17066             }
17067           break;
17068
17069         case CPP_OPEN_PAREN:
17070         case CPP_OPEN_SQUARE:
17071           ++nesting_depth;
17072           break;
17073
17074         case CPP_CLOSE_PAREN:
17075         case CPP_CLOSE_SQUARE:
17076           if (nesting_depth-- == 0)
17077             return;
17078           break;
17079
17080         case CPP_EOF:
17081         case CPP_PRAGMA_EOL:
17082         case CPP_SEMICOLON:
17083         case CPP_OPEN_BRACE:
17084         case CPP_CLOSE_BRACE:
17085           /* The '>' was probably forgotten, don't look further.  */
17086           return;
17087
17088         default:
17089           break;
17090         }
17091
17092       /* Consume this token.  */
17093       cp_lexer_consume_token (parser->lexer);
17094     }
17095 }
17096
17097 /* If the next token is the indicated keyword, consume it.  Otherwise,
17098    issue an error message indicating that TOKEN_DESC was expected.
17099
17100    Returns the token consumed, if the token had the appropriate type.
17101    Otherwise, returns NULL.  */
17102
17103 static cp_token *
17104 cp_parser_require_keyword (cp_parser* parser,
17105                            enum rid keyword,
17106                            const char* token_desc)
17107 {
17108   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17109
17110   if (token && token->keyword != keyword)
17111     {
17112       dyn_string_t error_msg;
17113
17114       /* Format the error message.  */
17115       error_msg = dyn_string_new (0);
17116       dyn_string_append_cstr (error_msg, "expected ");
17117       dyn_string_append_cstr (error_msg, token_desc);
17118       cp_parser_error (parser, error_msg->s);
17119       dyn_string_delete (error_msg);
17120       return NULL;
17121     }
17122
17123   return token;
17124 }
17125
17126 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17127    function-definition.  */
17128
17129 static bool
17130 cp_parser_token_starts_function_definition_p (cp_token* token)
17131 {
17132   return (/* An ordinary function-body begins with an `{'.  */
17133           token->type == CPP_OPEN_BRACE
17134           /* A ctor-initializer begins with a `:'.  */
17135           || token->type == CPP_COLON
17136           /* A function-try-block begins with `try'.  */
17137           || token->keyword == RID_TRY
17138           /* The named return value extension begins with `return'.  */
17139           || token->keyword == RID_RETURN);
17140 }
17141
17142 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17143    definition.  */
17144
17145 static bool
17146 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17147 {
17148   cp_token *token;
17149
17150   token = cp_lexer_peek_token (parser->lexer);
17151   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17152 }
17153
17154 /* Returns TRUE iff the next token is the "," or ">" ending a
17155    template-argument.  */
17156
17157 static bool
17158 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17159 {
17160   cp_token *token;
17161
17162   token = cp_lexer_peek_token (parser->lexer);
17163   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
17164 }
17165
17166 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17167    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17168
17169 static bool
17170 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17171                                                      size_t n)
17172 {
17173   cp_token *token;
17174
17175   token = cp_lexer_peek_nth_token (parser->lexer, n);
17176   if (token->type == CPP_LESS)
17177     return true;
17178   /* Check for the sequence `<::' in the original code. It would be lexed as
17179      `[:', where `[' is a digraph, and there is no whitespace before
17180      `:'.  */
17181   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17182     {
17183       cp_token *token2;
17184       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17185       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17186         return true;
17187     }
17188   return false;
17189 }
17190
17191 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17192    or none_type otherwise.  */
17193
17194 static enum tag_types
17195 cp_parser_token_is_class_key (cp_token* token)
17196 {
17197   switch (token->keyword)
17198     {
17199     case RID_CLASS:
17200       return class_type;
17201     case RID_STRUCT:
17202       return record_type;
17203     case RID_UNION:
17204       return union_type;
17205
17206     default:
17207       return none_type;
17208     }
17209 }
17210
17211 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17212
17213 static void
17214 cp_parser_check_class_key (enum tag_types class_key, tree type)
17215 {
17216   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17217     pedwarn ("%qs tag used in naming %q#T",
17218             class_key == union_type ? "union"
17219              : class_key == record_type ? "struct" : "class",
17220              type);
17221 }
17222
17223 /* Issue an error message if DECL is redeclared with different
17224    access than its original declaration [class.access.spec/3].
17225    This applies to nested classes and nested class templates.
17226    [class.mem/1].  */
17227
17228 static void
17229 cp_parser_check_access_in_redeclaration (tree decl)
17230 {
17231   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17232     return;
17233
17234   if ((TREE_PRIVATE (decl)
17235        != (current_access_specifier == access_private_node))
17236       || (TREE_PROTECTED (decl)
17237           != (current_access_specifier == access_protected_node)))
17238     error ("%qD redeclared with different access", decl);
17239 }
17240
17241 /* Look for the `template' keyword, as a syntactic disambiguator.
17242    Return TRUE iff it is present, in which case it will be
17243    consumed.  */
17244
17245 static bool
17246 cp_parser_optional_template_keyword (cp_parser *parser)
17247 {
17248   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17249     {
17250       /* The `template' keyword can only be used within templates;
17251          outside templates the parser can always figure out what is a
17252          template and what is not.  */
17253       if (!processing_template_decl)
17254         {
17255           error ("%<template%> (as a disambiguator) is only allowed "
17256                  "within templates");
17257           /* If this part of the token stream is rescanned, the same
17258              error message would be generated.  So, we purge the token
17259              from the stream.  */
17260           cp_lexer_purge_token (parser->lexer);
17261           return false;
17262         }
17263       else
17264         {
17265           /* Consume the `template' keyword.  */
17266           cp_lexer_consume_token (parser->lexer);
17267           return true;
17268         }
17269     }
17270
17271   return false;
17272 }
17273
17274 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17275    set PARSER->SCOPE, and perform other related actions.  */
17276
17277 static void
17278 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17279 {
17280   int i;
17281   struct tree_check *check_value;
17282   deferred_access_check *chk;
17283   VEC (deferred_access_check,gc) *checks;
17284
17285   /* Get the stored value.  */
17286   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17287   /* Perform any access checks that were deferred.  */
17288   checks = check_value->checks;
17289   if (checks)
17290     {
17291       for (i = 0 ;
17292            VEC_iterate (deferred_access_check, checks, i, chk) ;
17293            ++i)
17294         {
17295           perform_or_defer_access_check (chk->binfo,
17296                                          chk->decl,
17297                                          chk->diag_decl);
17298         }
17299     }
17300   /* Set the scope from the stored value.  */
17301   parser->scope = check_value->value;
17302   parser->qualifying_scope = check_value->qualifying_scope;
17303   parser->object_scope = NULL_TREE;
17304 }
17305
17306 /* Consume tokens up through a non-nested END token.  */
17307
17308 static void
17309 cp_parser_cache_group (cp_parser *parser,
17310                        enum cpp_ttype end,
17311                        unsigned depth)
17312 {
17313   while (true)
17314     {
17315       cp_token *token;
17316
17317       /* Abort a parenthesized expression if we encounter a brace.  */
17318       if ((end == CPP_CLOSE_PAREN || depth == 0)
17319           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17320         return;
17321       /* If we've reached the end of the file, stop.  */
17322       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17323           || (end != CPP_PRAGMA_EOL
17324               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17325         return;
17326       /* Consume the next token.  */
17327       token = cp_lexer_consume_token (parser->lexer);
17328       /* See if it starts a new group.  */
17329       if (token->type == CPP_OPEN_BRACE)
17330         {
17331           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17332           if (depth == 0)
17333             return;
17334         }
17335       else if (token->type == CPP_OPEN_PAREN)
17336         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17337       else if (token->type == CPP_PRAGMA)
17338         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17339       else if (token->type == end)
17340         return;
17341     }
17342 }
17343
17344 /* Begin parsing tentatively.  We always save tokens while parsing
17345    tentatively so that if the tentative parsing fails we can restore the
17346    tokens.  */
17347
17348 static void
17349 cp_parser_parse_tentatively (cp_parser* parser)
17350 {
17351   /* Enter a new parsing context.  */
17352   parser->context = cp_parser_context_new (parser->context);
17353   /* Begin saving tokens.  */
17354   cp_lexer_save_tokens (parser->lexer);
17355   /* In order to avoid repetitive access control error messages,
17356      access checks are queued up until we are no longer parsing
17357      tentatively.  */
17358   push_deferring_access_checks (dk_deferred);
17359 }
17360
17361 /* Commit to the currently active tentative parse.  */
17362
17363 static void
17364 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17365 {
17366   cp_parser_context *context;
17367   cp_lexer *lexer;
17368
17369   /* Mark all of the levels as committed.  */
17370   lexer = parser->lexer;
17371   for (context = parser->context; context->next; context = context->next)
17372     {
17373       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17374         break;
17375       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17376       while (!cp_lexer_saving_tokens (lexer))
17377         lexer = lexer->next;
17378       cp_lexer_commit_tokens (lexer);
17379     }
17380 }
17381
17382 /* Abort the currently active tentative parse.  All consumed tokens
17383    will be rolled back, and no diagnostics will be issued.  */
17384
17385 static void
17386 cp_parser_abort_tentative_parse (cp_parser* parser)
17387 {
17388   cp_parser_simulate_error (parser);
17389   /* Now, pretend that we want to see if the construct was
17390      successfully parsed.  */
17391   cp_parser_parse_definitely (parser);
17392 }
17393
17394 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17395    token stream.  Otherwise, commit to the tokens we have consumed.
17396    Returns true if no error occurred; false otherwise.  */
17397
17398 static bool
17399 cp_parser_parse_definitely (cp_parser* parser)
17400 {
17401   bool error_occurred;
17402   cp_parser_context *context;
17403
17404   /* Remember whether or not an error occurred, since we are about to
17405      destroy that information.  */
17406   error_occurred = cp_parser_error_occurred (parser);
17407   /* Remove the topmost context from the stack.  */
17408   context = parser->context;
17409   parser->context = context->next;
17410   /* If no parse errors occurred, commit to the tentative parse.  */
17411   if (!error_occurred)
17412     {
17413       /* Commit to the tokens read tentatively, unless that was
17414          already done.  */
17415       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17416         cp_lexer_commit_tokens (parser->lexer);
17417
17418       pop_to_parent_deferring_access_checks ();
17419     }
17420   /* Otherwise, if errors occurred, roll back our state so that things
17421      are just as they were before we began the tentative parse.  */
17422   else
17423     {
17424       cp_lexer_rollback_tokens (parser->lexer);
17425       pop_deferring_access_checks ();
17426     }
17427   /* Add the context to the front of the free list.  */
17428   context->next = cp_parser_context_free_list;
17429   cp_parser_context_free_list = context;
17430
17431   return !error_occurred;
17432 }
17433
17434 /* Returns true if we are parsing tentatively and are not committed to
17435    this tentative parse.  */
17436
17437 static bool
17438 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17439 {
17440   return (cp_parser_parsing_tentatively (parser)
17441           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17442 }
17443
17444 /* Returns nonzero iff an error has occurred during the most recent
17445    tentative parse.  */
17446
17447 static bool
17448 cp_parser_error_occurred (cp_parser* parser)
17449 {
17450   return (cp_parser_parsing_tentatively (parser)
17451           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17452 }
17453
17454 /* Returns nonzero if GNU extensions are allowed.  */
17455
17456 static bool
17457 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17458 {
17459   return parser->allow_gnu_extensions_p;
17460 }
17461 \f
17462 /* Objective-C++ Productions */
17463
17464
17465 /* Parse an Objective-C expression, which feeds into a primary-expression
17466    above.
17467
17468    objc-expression:
17469      objc-message-expression
17470      objc-string-literal
17471      objc-encode-expression
17472      objc-protocol-expression
17473      objc-selector-expression
17474
17475   Returns a tree representation of the expression.  */
17476
17477 static tree
17478 cp_parser_objc_expression (cp_parser* parser)
17479 {
17480   /* Try to figure out what kind of declaration is present.  */
17481   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17482
17483   switch (kwd->type)
17484     {
17485     case CPP_OPEN_SQUARE:
17486       return cp_parser_objc_message_expression (parser);
17487
17488     case CPP_OBJC_STRING:
17489       kwd = cp_lexer_consume_token (parser->lexer);
17490       return objc_build_string_object (kwd->u.value);
17491
17492     case CPP_KEYWORD:
17493       switch (kwd->keyword)
17494         {
17495         case RID_AT_ENCODE:
17496           return cp_parser_objc_encode_expression (parser);
17497
17498         case RID_AT_PROTOCOL:
17499           return cp_parser_objc_protocol_expression (parser);
17500
17501         case RID_AT_SELECTOR:
17502           return cp_parser_objc_selector_expression (parser);
17503
17504         default:
17505           break;
17506         }
17507     default:
17508       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17509       cp_parser_skip_to_end_of_block_or_statement (parser);
17510     }
17511
17512   return error_mark_node;
17513 }
17514
17515 /* Parse an Objective-C message expression.
17516
17517    objc-message-expression:
17518      [ objc-message-receiver objc-message-args ]
17519
17520    Returns a representation of an Objective-C message.  */
17521
17522 static tree
17523 cp_parser_objc_message_expression (cp_parser* parser)
17524 {
17525   tree receiver, messageargs;
17526
17527   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17528   receiver = cp_parser_objc_message_receiver (parser);
17529   messageargs = cp_parser_objc_message_args (parser);
17530   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17531
17532   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17533 }
17534
17535 /* APPLE LOCAL begin radar 5277239 */
17536 /* Parse an Objective-C dot-syntax class expression.
17537
17538  objc-message-expression:
17539  class-name '.' class-method-name
17540
17541  Returns an objc_property_reference expression. */
17542
17543 static tree
17544 cp_parser_objc_reference_expression (cp_parser* parser, tree type_decl)
17545 {
17546   tree receiver, component;
17547   receiver = objc_get_class_reference (TREE_TYPE (type_decl));
17548   cp_lexer_consume_token (parser->lexer);  /* Eact '.' */
17549   component = cp_parser_objc_message_args (parser);
17550   return objc_build_property_reference_expr (receiver, TREE_PURPOSE (component));
17551 }
17552 /* APPLE LOCAL end radar 5277239 */
17553
17554 /* Parse an objc-message-receiver.
17555
17556    objc-message-receiver:
17557      expression
17558      simple-type-specifier
17559
17560   Returns a representation of the type or expression.  */
17561
17562 static tree
17563 cp_parser_objc_message_receiver (cp_parser* parser)
17564 {
17565   tree rcv;
17566
17567   /* An Objective-C message receiver may be either (1) a type
17568      or (2) an expression.  */
17569   cp_parser_parse_tentatively (parser);
17570   rcv = cp_parser_expression (parser, false);
17571
17572   if (cp_parser_parse_definitely (parser))
17573     return rcv;
17574
17575   rcv = cp_parser_simple_type_specifier (parser,
17576                                          /*decl_specs=*/NULL,
17577                                          CP_PARSER_FLAGS_NONE);
17578
17579   return objc_get_class_reference (rcv);
17580 }
17581
17582 /* Parse the arguments and selectors comprising an Objective-C message.
17583
17584    objc-message-args:
17585      objc-selector
17586      objc-selector-args
17587      objc-selector-args , objc-comma-args
17588
17589    objc-selector-args:
17590      objc-selector [opt] : assignment-expression
17591      objc-selector-args objc-selector [opt] : assignment-expression
17592
17593    objc-comma-args:
17594      assignment-expression
17595      objc-comma-args , assignment-expression
17596
17597    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17598    selector arguments and TREE_VALUE containing a list of comma
17599    arguments.  */
17600
17601 static tree
17602 cp_parser_objc_message_args (cp_parser* parser)
17603 {
17604   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17605   bool maybe_unary_selector_p = true;
17606   cp_token *token = cp_lexer_peek_token (parser->lexer);
17607
17608   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17609     {
17610       tree selector = NULL_TREE, arg;
17611
17612       if (token->type != CPP_COLON)
17613         selector = cp_parser_objc_selector (parser);
17614
17615       /* Detect if we have a unary selector.  */
17616       if (maybe_unary_selector_p
17617           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17618         return build_tree_list (selector, NULL_TREE);
17619
17620       maybe_unary_selector_p = false;
17621       cp_parser_require (parser, CPP_COLON, "`:'");
17622       arg = cp_parser_assignment_expression (parser, false);
17623
17624       sel_args
17625         = chainon (sel_args,
17626                    build_tree_list (selector, arg));
17627
17628       token = cp_lexer_peek_token (parser->lexer);
17629     }
17630
17631   /* Handle non-selector arguments, if any. */
17632   while (token->type == CPP_COMMA)
17633     {
17634       tree arg;
17635
17636       cp_lexer_consume_token (parser->lexer);
17637       arg = cp_parser_assignment_expression (parser, false);
17638
17639       addl_args
17640         = chainon (addl_args,
17641                    build_tree_list (NULL_TREE, arg));
17642
17643       token = cp_lexer_peek_token (parser->lexer);
17644     }
17645
17646   return build_tree_list (sel_args, addl_args);
17647 }
17648
17649 /* Parse an Objective-C encode expression.
17650
17651    objc-encode-expression:
17652      @encode objc-typename
17653
17654    Returns an encoded representation of the type argument.  */
17655
17656 static tree
17657 cp_parser_objc_encode_expression (cp_parser* parser)
17658 {
17659   tree type;
17660
17661   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17662   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17663   type = complete_type (cp_parser_type_id (parser));
17664   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17665
17666   if (!type)
17667     {
17668       error ("%<@encode%> must specify a type as an argument");
17669       return error_mark_node;
17670     }
17671
17672   return objc_build_encode_expr (type);
17673 }
17674
17675 /* Parse an Objective-C @defs expression.  */
17676
17677 static tree
17678 cp_parser_objc_defs_expression (cp_parser *parser)
17679 {
17680   tree name;
17681
17682   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17683   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17684   name = cp_parser_identifier (parser);
17685   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17686
17687   return objc_get_class_ivars (name);
17688 }
17689
17690 /* Parse an Objective-C protocol expression.
17691
17692   objc-protocol-expression:
17693     @protocol ( identifier )
17694
17695   Returns a representation of the protocol expression.  */
17696
17697 static tree
17698 cp_parser_objc_protocol_expression (cp_parser* parser)
17699 {
17700   tree proto;
17701
17702   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17703   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17704   proto = cp_parser_identifier (parser);
17705   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17706
17707   return objc_build_protocol_expr (proto);
17708 }
17709
17710 /* Parse an Objective-C selector expression.
17711
17712    objc-selector-expression:
17713      @selector ( objc-method-signature )
17714
17715    objc-method-signature:
17716      objc-selector
17717      objc-selector-seq
17718
17719    objc-selector-seq:
17720      objc-selector :
17721      objc-selector-seq objc-selector :
17722
17723   Returns a representation of the method selector.  */
17724
17725 static tree
17726 cp_parser_objc_selector_expression (cp_parser* parser)
17727 {
17728   tree sel_seq = NULL_TREE;
17729   bool maybe_unary_selector_p = true;
17730   cp_token *token;
17731
17732   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17733   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17734   token = cp_lexer_peek_token (parser->lexer);
17735
17736   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17737          || token->type == CPP_SCOPE)
17738     {
17739       tree selector = NULL_TREE;
17740
17741       if (token->type != CPP_COLON
17742           || token->type == CPP_SCOPE)
17743         selector = cp_parser_objc_selector (parser);
17744
17745       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17746           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17747         {
17748           /* Detect if we have a unary selector.  */
17749           if (maybe_unary_selector_p)
17750             {
17751               sel_seq = selector;
17752               goto finish_selector;
17753             }
17754           else
17755             {
17756               cp_parser_error (parser, "expected %<:%>");
17757             }
17758         }
17759       maybe_unary_selector_p = false;
17760       token = cp_lexer_consume_token (parser->lexer);
17761
17762       if (token->type == CPP_SCOPE)
17763         {
17764           sel_seq
17765             = chainon (sel_seq,
17766                        build_tree_list (selector, NULL_TREE));
17767           sel_seq
17768             = chainon (sel_seq,
17769                        build_tree_list (NULL_TREE, NULL_TREE));
17770         }
17771       else
17772         sel_seq
17773           = chainon (sel_seq,
17774                      build_tree_list (selector, NULL_TREE));
17775
17776       token = cp_lexer_peek_token (parser->lexer);
17777     }
17778
17779  finish_selector:
17780   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17781
17782   return objc_build_selector_expr (sel_seq);
17783 }
17784
17785 /* Parse a list of identifiers.
17786
17787    objc-identifier-list:
17788      identifier
17789      objc-identifier-list , identifier
17790
17791    Returns a TREE_LIST of identifier nodes.  */
17792
17793 static tree
17794 cp_parser_objc_identifier_list (cp_parser* parser)
17795 {
17796   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17797   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17798
17799   while (sep->type == CPP_COMMA)
17800     {
17801       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17802       list = chainon (list,
17803                       build_tree_list (NULL_TREE,
17804                                        cp_parser_identifier (parser)));
17805       sep = cp_lexer_peek_token (parser->lexer);
17806     }
17807
17808   return list;
17809 }
17810
17811 /* Parse an Objective-C alias declaration.
17812
17813    objc-alias-declaration:
17814      @compatibility_alias identifier identifier ;
17815
17816    This function registers the alias mapping with the Objective-C front-end.
17817    It returns nothing.  */
17818
17819 static void
17820 cp_parser_objc_alias_declaration (cp_parser* parser)
17821 {
17822   tree alias, orig;
17823
17824   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17825   alias = cp_parser_identifier (parser);
17826   orig = cp_parser_identifier (parser);
17827   objc_declare_alias (alias, orig);
17828   cp_parser_consume_semicolon_at_end_of_statement (parser);
17829 }
17830
17831 /* Parse an Objective-C class forward-declaration.
17832
17833    objc-class-declaration:
17834      @class objc-identifier-list ;
17835
17836    The function registers the forward declarations with the Objective-C
17837    front-end.  It returns nothing.  */
17838
17839 static void
17840 cp_parser_objc_class_declaration (cp_parser* parser)
17841 {
17842   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17843   objc_declare_class (cp_parser_objc_identifier_list (parser));
17844   cp_parser_consume_semicolon_at_end_of_statement (parser);
17845 }
17846
17847 /* Parse a list of Objective-C protocol references.
17848
17849    objc-protocol-refs-opt:
17850      objc-protocol-refs [opt]
17851
17852    objc-protocol-refs:
17853      < objc-identifier-list >
17854
17855    Returns a TREE_LIST of identifiers, if any.  */
17856
17857 static tree
17858 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17859 {
17860   tree protorefs = NULL_TREE;
17861
17862   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17863     {
17864       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17865       protorefs = cp_parser_objc_identifier_list (parser);
17866       cp_parser_require (parser, CPP_GREATER, "`>'");
17867     }
17868
17869   return protorefs;
17870 }
17871
17872 /* Parse a Objective-C visibility specification.  */
17873
17874 static void
17875 cp_parser_objc_visibility_spec (cp_parser* parser)
17876 {
17877   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17878
17879   switch (vis->keyword)
17880     {
17881     case RID_AT_PRIVATE:
17882       objc_set_visibility (2);
17883       break;
17884     case RID_AT_PROTECTED:
17885       objc_set_visibility (0);
17886       break;
17887     case RID_AT_PUBLIC:
17888       objc_set_visibility (1);
17889       break;
17890     default:
17891       return;
17892     }
17893
17894   /* Eat '@private'/'@protected'/'@public'.  */
17895   cp_lexer_consume_token (parser->lexer);
17896 }
17897
17898 /* Parse an Objective-C method type.  */
17899
17900 static void
17901 cp_parser_objc_method_type (cp_parser* parser)
17902 {
17903   objc_set_method_type
17904    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17905     ? PLUS_EXPR
17906     : MINUS_EXPR);
17907 }
17908
17909 /* Parse an Objective-C protocol qualifier.  */
17910
17911 static tree
17912 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17913 {
17914   tree quals = NULL_TREE, node;
17915   cp_token *token = cp_lexer_peek_token (parser->lexer);
17916
17917   node = token->u.value;
17918
17919   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17920          && (node == ridpointers [(int) RID_IN]
17921              || node == ridpointers [(int) RID_OUT]
17922              || node == ridpointers [(int) RID_INOUT]
17923              || node == ridpointers [(int) RID_BYCOPY]
17924              || node == ridpointers [(int) RID_BYREF]
17925              || node == ridpointers [(int) RID_ONEWAY]))
17926     {
17927       quals = tree_cons (NULL_TREE, node, quals);
17928       cp_lexer_consume_token (parser->lexer);
17929       token = cp_lexer_peek_token (parser->lexer);
17930       node = token->u.value;
17931     }
17932
17933   return quals;
17934 }
17935
17936 /* Parse an Objective-C typename.  */
17937
17938 static tree
17939 cp_parser_objc_typename (cp_parser* parser)
17940 {
17941   tree typename = NULL_TREE;
17942
17943   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17944     {
17945       tree proto_quals, cp_type = NULL_TREE;
17946
17947       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17948       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17949
17950       /* An ObjC type name may consist of just protocol qualifiers, in which
17951          case the type shall default to 'id'.  */
17952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17953         cp_type = cp_parser_type_id (parser);
17954
17955       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17956       typename = build_tree_list (proto_quals, cp_type);
17957     }
17958
17959   return typename;
17960 }
17961
17962 /* Check to see if TYPE refers to an Objective-C selector name.  */
17963
17964 static bool
17965 cp_parser_objc_selector_p (enum cpp_ttype type)
17966 {
17967   return (type == CPP_NAME || type == CPP_KEYWORD
17968           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17969           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17970           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17971           || type == CPP_XOR || type == CPP_XOR_EQ);
17972 }
17973
17974 /* Parse an Objective-C selector.  */
17975
17976 static tree
17977 cp_parser_objc_selector (cp_parser* parser)
17978 {
17979   cp_token *token = cp_lexer_consume_token (parser->lexer);
17980
17981   if (!cp_parser_objc_selector_p (token->type))
17982     {
17983       error ("invalid Objective-C++ selector name");
17984       return error_mark_node;
17985     }
17986
17987   /* C++ operator names are allowed to appear in ObjC selectors.  */
17988   switch (token->type)
17989     {
17990     case CPP_AND_AND: return get_identifier ("and");
17991     case CPP_AND_EQ: return get_identifier ("and_eq");
17992     case CPP_AND: return get_identifier ("bitand");
17993     case CPP_OR: return get_identifier ("bitor");
17994     case CPP_COMPL: return get_identifier ("compl");
17995     case CPP_NOT: return get_identifier ("not");
17996     case CPP_NOT_EQ: return get_identifier ("not_eq");
17997     case CPP_OR_OR: return get_identifier ("or");
17998     case CPP_OR_EQ: return get_identifier ("or_eq");
17999     case CPP_XOR: return get_identifier ("xor");
18000     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18001     default: return token->u.value;
18002     }
18003 }
18004
18005 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18006 static void 
18007 cp_parser_objc_maybe_attributes (cp_parser* parser, tree* attributes)
18008 {
18009   cp_token *token = cp_lexer_peek_token (parser->lexer);
18010   if (*attributes != NULL_TREE)
18011     {
18012       error ("method attributes must be specified at the end only");
18013       *attributes = NULL_TREE;
18014     }
18015   if (token->keyword == RID_ATTRIBUTE)
18016       *attributes = cp_parser_attributes_opt (parser);
18017 }
18018
18019 /* Parse an Objective-C params list.  */
18020
18021 static tree
18022 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
18023 /* APPLE LOCAL end radar 3803157 - objc attribute */
18024 {
18025   tree params = NULL_TREE;
18026   bool maybe_unary_selector_p = true;
18027   cp_token *token = cp_lexer_peek_token (parser->lexer);
18028
18029   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18030     {
18031       tree selector = NULL_TREE, typename, identifier;
18032       /* APPLE LOCAL radar 4157812 */
18033       tree attr = NULL_TREE;
18034
18035       if (token->type != CPP_COLON)
18036         selector = cp_parser_objc_selector (parser);
18037
18038       /* Detect if we have a unary selector.  */
18039       if (maybe_unary_selector_p
18040         && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18041       /* APPLE LOCAL begin radar 3803157 - objc attribute */
18042       {
18043         cp_parser_objc_maybe_attributes (parser, attributes);
18044         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18045                 return selector;
18046       }
18047       /* APPLE LOCAL end radar 3803157 - objc attribute */
18048
18049       maybe_unary_selector_p = false;
18050       cp_parser_require (parser, CPP_COLON, "`:'");
18051       typename = cp_parser_objc_typename (parser);
18052       /* APPLE LOCAL radar 4157812 */
18053       cp_parser_objc_maybe_attributes (parser, &attr);
18054       identifier = cp_parser_identifier (parser);
18055       /* APPLE LOCAL radar 3803157 - objc attribute */
18056       cp_parser_objc_maybe_attributes (parser, attributes);
18057
18058       params
18059         = chainon (params,
18060                    objc_build_keyword_decl (selector,
18061                                             typename,
18062                                             /* APPLE LOCAL radar 4157812 */
18063                                             identifier, attr));
18064
18065       token = cp_lexer_peek_token (parser->lexer);
18066     }
18067
18068   /* APPLE LOCAL begin radar 4290840 */
18069   if (params == NULL_TREE)
18070     {
18071       cp_parser_error (parser, "objective-c++ method declaration is expected");
18072       return error_mark_node;
18073     }
18074   /* APPLE LOCAL end radar 4290840 */
18075
18076   return params;
18077 }
18078
18079 /* Parse the non-keyword Objective-C params.  */
18080
18081 static tree
18082 /* APPLE LOCAL radar 3803157 - objc attribute */
18083 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, tree* attributes)
18084 {
18085   tree params = make_node (TREE_LIST);
18086   cp_token *token = cp_lexer_peek_token (parser->lexer);
18087   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18088
18089   while (token->type == CPP_COMMA)
18090     {
18091       cp_parameter_declarator *parmdecl;
18092       tree parm;
18093
18094       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18095       token = cp_lexer_peek_token (parser->lexer);
18096
18097       if (token->type == CPP_ELLIPSIS)
18098       {
18099         cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18100         *ellipsisp = true;
18101         /* APPLE LOCAL radar 3803157 - objc attribute */
18102         cp_parser_objc_maybe_attributes (parser, attributes);
18103         break;
18104       }
18105
18106       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18107       parm = grokdeclarator (parmdecl->declarator,
18108                              &parmdecl->decl_specifiers,
18109                              PARM, /*initialized=*/0,
18110                              /*attrlist=*/NULL);
18111
18112       chainon (params, build_tree_list (NULL_TREE, parm));
18113       token = cp_lexer_peek_token (parser->lexer);
18114     }
18115
18116   return params;
18117 }
18118
18119 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18120
18121 static void
18122 cp_parser_objc_interstitial_code (cp_parser* parser)
18123 {
18124   cp_token *token = cp_lexer_peek_token (parser->lexer);
18125
18126   /* If the next token is `extern' and the following token is a string
18127      literal, then we have a linkage specification.  */
18128   if (token->keyword == RID_EXTERN
18129       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18130     cp_parser_linkage_specification (parser);
18131   /* Handle #pragma, if any.  */
18132   else if (token->type == CPP_PRAGMA)
18133     cp_parser_pragma (parser, pragma_external);
18134   /* Allow stray semicolons.  */
18135   else if (token->type == CPP_SEMICOLON)
18136     cp_lexer_consume_token (parser->lexer);
18137   /* Finally, try to parse a block-declaration, or a function-definition.  */
18138   else
18139     cp_parser_block_declaration (parser, /*statement_p=*/false);
18140 }
18141
18142 /* Parse a method signature.  */
18143
18144 static tree
18145 /* APPLE LOCAL radar 3803157 - objc attribute */
18146 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
18147 {
18148   tree rettype, kwdparms, optparms;
18149   bool ellipsis = false;
18150
18151   cp_parser_objc_method_type (parser);
18152   rettype = cp_parser_objc_typename (parser);
18153   /* APPLE LOCAL begin radar 3803157 - objc attribute */
18154   *attributes = NULL_TREE;
18155   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
18156   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
18157   /* APPLE LOCAL end radar 3803157 - objc attribute */
18158
18159   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18160 }
18161
18162 /* Pars an Objective-C method prototype list.  */
18163
18164 static void
18165 cp_parser_objc_method_prototype_list (cp_parser* parser)
18166 {
18167   cp_token *token = cp_lexer_peek_token (parser->lexer);
18168
18169   /* APPLE LOCAL 4093475 */
18170   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
18171     {
18172       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18173       {
18174         /* APPLE LOCAL begin radar 3803157 - objc attribute */
18175         tree attributes, sig;
18176         sig = cp_parser_objc_method_signature (parser, &attributes);
18177         objc_add_method_declaration (sig, attributes);
18178         /* APPLE LOCAL end radar 3803157 - objc attribute */
18179         cp_parser_consume_semicolon_at_end_of_statement (parser);
18180       }
18181       /* APPLE LOCAL begin C* interface */
18182       else if (token->keyword == RID_AT_PROPERTY)
18183         objc_cp_parser_at_property (parser);
18184       /* APPLE LOCAL end C* interface */
18185       else
18186       /* Allow for interspersed non-ObjC++ code.  */
18187         cp_parser_objc_interstitial_code (parser);
18188
18189       token = cp_lexer_peek_token (parser->lexer);
18190     }
18191
18192   /* APPLE LOCAL 4093475 */
18193   cp_parser_require_keyword (parser, RID_AT_END, "`@end'");
18194   objc_finish_interface ();
18195 }
18196
18197 /* Parse an Objective-C method definition list.  */
18198
18199 static void
18200 cp_parser_objc_method_definition_list (cp_parser* parser)
18201 {
18202   cp_token *token = cp_lexer_peek_token (parser->lexer);
18203
18204   /* APPLE LOCAL 4093475 */
18205   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
18206     {
18207       tree meth;
18208
18209       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18210       {
18211         /* APPLE LOCAL radar 4290840 */
18212         cp_token *ptk;
18213         /* APPLE LOCAL begin radar 3803157 - objc attribute */
18214         tree sig, attribute;
18215         push_deferring_access_checks (dk_deferred);
18216         sig = cp_parser_objc_method_signature (parser, &attribute);
18217         objc_start_method_definition (sig, attribute);
18218         /* APPLE LOCAL end radar 3803157 - objc attribute */
18219
18220           /* For historical reasons, we accept an optional semicolon.  */
18221           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18222             cp_lexer_consume_token (parser->lexer);
18223
18224         /* APPLE LOCAL begin radar 4290840 */
18225         /* Check for all possibilities of illegal lookahead tokens. */
18226         ptk = cp_lexer_peek_token (parser->lexer);
18227         /* APPLE LOCAL radar 6271728 */
18228         if (ptk->type == CPP_OPEN_BRACE)
18229         {
18230                 perform_deferred_access_checks ();
18231                 stop_deferring_access_checks ();
18232                 meth = cp_parser_function_definition_after_declarator (parser,
18233                                                                       false);
18234                 pop_deferring_access_checks ();
18235                 objc_finish_method_definition (meth);
18236         }
18237         /* APPLE LOCAL begin radar 6271728 */
18238         else
18239                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
18240         /* APPLE LOCAL end radar 6271728 */
18241         /* APPLE LOCAL end radar 4290840 */
18242       }
18243       /* APPLE LOCAL begin C* interface */
18244       else if (token->keyword == RID_AT_PROPERTY)
18245         objc_cp_parser_at_property (parser);
18246       /* APPLE LOCAL end C* interface */
18247       else
18248         /* Allow for interspersed non-ObjC++ code.  */
18249         cp_parser_objc_interstitial_code (parser);
18250
18251       token = cp_lexer_peek_token (parser->lexer);
18252     }
18253
18254   /* APPLE LOCAL 4093475 */
18255   cp_parser_require_keyword (parser, RID_AT_END, "`@end'");
18256   objc_finish_implementation ();
18257 }
18258
18259 /* Parse Objective-C ivars.  */
18260
18261 static void
18262 cp_parser_objc_class_ivars (cp_parser* parser)
18263 {
18264   cp_token *token = cp_lexer_peek_token (parser->lexer);
18265
18266   if (token->type != CPP_OPEN_BRACE)
18267     return;     /* No ivars specified.  */
18268
18269   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18270   token = cp_lexer_peek_token (parser->lexer);
18271
18272   while (token->type != CPP_CLOSE_BRACE)
18273     {
18274       cp_decl_specifier_seq declspecs;
18275       int decl_class_or_enum_p;
18276       tree prefix_attributes;
18277
18278       cp_parser_objc_visibility_spec (parser);
18279
18280       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18281         break;
18282
18283       cp_parser_decl_specifier_seq (parser,
18284                                     CP_PARSER_FLAGS_OPTIONAL,
18285                                     &declspecs,
18286                                     &decl_class_or_enum_p);
18287       prefix_attributes = declspecs.attributes;
18288       declspecs.attributes = NULL_TREE;
18289
18290       /* Keep going until we hit the `;' at the end of the
18291          declaration.  */
18292       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18293         {
18294           tree width = NULL_TREE, attributes, first_attribute, decl;
18295           cp_declarator *declarator = NULL;
18296           int ctor_dtor_or_conv_p;
18297
18298           /* Check for a (possibly unnamed) bitfield declaration.  */
18299           token = cp_lexer_peek_token (parser->lexer);
18300           if (token->type == CPP_COLON)
18301             goto eat_colon;
18302
18303           if (token->type == CPP_NAME
18304               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18305                   == CPP_COLON))
18306             {
18307               /* Get the name of the bitfield.  */
18308               declarator = make_id_declarator (NULL_TREE,
18309                                                cp_parser_identifier (parser),
18310                                                sfk_none);
18311
18312              eat_colon:
18313               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18314               /* Get the width of the bitfield.  */
18315               width
18316                 = cp_parser_constant_expression (parser,
18317                                                  /*allow_non_constant=*/false,
18318                                                  NULL);
18319             }
18320           else
18321             {
18322               /* Parse the declarator.  */
18323               declarator
18324                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18325                                         &ctor_dtor_or_conv_p,
18326                                         /*parenthesized_p=*/NULL,
18327                                         /*member_p=*/false);
18328             }
18329
18330           /* Look for attributes that apply to the ivar.  */
18331           attributes = cp_parser_attributes_opt (parser);
18332           /* Remember which attributes are prefix attributes and
18333              which are not.  */
18334           first_attribute = attributes;
18335           /* Combine the attributes.  */
18336           attributes = chainon (prefix_attributes, attributes);
18337
18338           if (width)
18339             {
18340               /* Create the bitfield declaration.  */
18341               decl = grokbitfield (declarator, &declspecs, width);
18342               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18343             }
18344           else
18345             decl = grokfield (declarator, &declspecs,
18346                               NULL_TREE, /*init_const_expr_p=*/false,
18347                               NULL_TREE, attributes);
18348
18349           /* Add the instance variable.  */
18350           objc_add_instance_variable (decl);
18351
18352           /* Reset PREFIX_ATTRIBUTES.  */
18353           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18354             attributes = TREE_CHAIN (attributes);
18355           if (attributes)
18356             TREE_CHAIN (attributes) = NULL_TREE;
18357
18358           token = cp_lexer_peek_token (parser->lexer);
18359
18360           if (token->type == CPP_COMMA)
18361             {
18362               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18363               continue;
18364             }
18365           break;
18366         }
18367
18368       cp_parser_consume_semicolon_at_end_of_statement (parser);
18369       token = cp_lexer_peek_token (parser->lexer);
18370     }
18371
18372   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18373   /* For historical reasons, we accept an optional semicolon.  */
18374   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18375     cp_lexer_consume_token (parser->lexer);
18376 }
18377
18378 /* Parse an Objective-C protocol declaration.  */
18379
18380 static void
18381 /* APPLE LOCAL radar 4947311 */
18382 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
18383 {
18384   tree proto, protorefs;
18385   cp_token *tok;
18386
18387   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18388   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18389     {
18390       error ("identifier expected after %<@protocol%>");
18391       goto finish;
18392     }
18393
18394   /* See if we have a forward declaration or a definition.  */
18395   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18396
18397   /* Try a forward declaration first.  */
18398   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18399     {
18400       /* APPLE LOCAL radar 4947311 */
18401       objc_declare_protocols (cp_parser_objc_identifier_list (parser), attributes);
18402   finish:
18403       cp_parser_consume_semicolon_at_end_of_statement (parser);
18404     }
18405
18406   /* Ok, we got a full-fledged definition (or at least should).  */
18407   else
18408     {
18409       proto = cp_parser_identifier (parser);
18410       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18411       /* APPLE LOCAL radar 4947311 */
18412       objc_start_protocol (proto, protorefs, attributes);
18413       cp_parser_objc_method_prototype_list (parser);
18414     }
18415 }
18416
18417 /* Parse an Objective-C superclass or category.  */
18418
18419 /* APPLE LOCAL begin radar 4965989 */
18420 static void
18421 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18422                                                           tree *categ, bool *is_category)
18423 {
18424   cp_token *next = cp_lexer_peek_token (parser->lexer);
18425
18426   *super = *categ = NULL_TREE;
18427   *is_category = false;
18428   if (next->type == CPP_COLON)
18429     {
18430       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18431       *super = cp_parser_identifier (parser);
18432     }
18433   else if (next->type == CPP_OPEN_PAREN)
18434     {
18435       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18436       /* APPLE LOCAL begin radar 4965989 */
18437       next = cp_lexer_peek_token (parser->lexer);
18438       *categ = (next->type == CPP_CLOSE_PAREN) ? NULL_TREE : cp_parser_identifier (parser);
18439       *is_category = true;
18440       /* APPLE LOCAL end radar 4965989 */
18441       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18442     }
18443 }
18444 /* APPLE LOCAL end radar 4965989 */
18445
18446 /* Parse an Objective-C class interface.  */
18447
18448 static void
18449 /* APPLE LOCAL radar 4947311 */
18450 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
18451 {
18452   tree name, super, categ, protos;
18453   /* APPLE LOCAL radar 4965989 */
18454   bool is_categ;
18455   /* APPLE LOCAL radar 4947311 */
18456   /* Code for radar 4548636 removed. */
18457   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18458   name = cp_parser_identifier (parser);
18459   /* APPLE LOCAL radar 4965989 */
18460   cp_parser_objc_superclass_or_category (parser, &super, &categ, &is_categ);
18461   protos = cp_parser_objc_protocol_refs_opt (parser);
18462   
18463   /* We have either a class or a category on our hands.  */
18464   /* APPLE LOCAL radar 4965989 */
18465   if (is_categ)
18466   /* APPLE LOCAL begin radar 4548636 */
18467     {
18468       if (attributes)
18469                 error ("attributes may not be specified on a category");
18470       objc_start_category_interface (name, categ, protos);
18471     }
18472   /* APPLE LOCAL end radar 4548636 */
18473   else
18474     {
18475       /* APPLE LOCAL radar 4548636 */
18476       objc_start_class_interface (name, super, protos, attributes);
18477       /* Handle instance variable declarations, if any.  */
18478       cp_parser_objc_class_ivars (parser);
18479       objc_continue_interface ();
18480     }
18481
18482   cp_parser_objc_method_prototype_list (parser);
18483 }
18484
18485 /* Parse an Objective-C class implementation.  */
18486
18487 static void
18488 cp_parser_objc_class_implementation (cp_parser* parser)
18489 {
18490   tree name, super, categ;
18491   /* APPLE LOCAL radar 4965989 */
18492   bool is_categ;
18493   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18494   name = cp_parser_identifier (parser);
18495   /* APPLE LOCAL radar 4965989 */
18496   cp_parser_objc_superclass_or_category (parser, &super, &categ, &is_categ);
18497
18498   /* We have either a class or a category on our hands.  */
18499   /* APPLE LOCAL begin radar 4965989 */
18500   if (is_categ)
18501     {
18502       if (categ == NULL_TREE)
18503          {
18504         error ("cannot implement anonymous category");
18505         return;
18506          }
18507       objc_start_category_implementation (name, categ);
18508     }
18509   /* APPLE LOCAL end radar 4965989 */
18510   else
18511     {
18512       objc_start_class_implementation (name, super);
18513       /* Handle instance variable declarations, if any.  */
18514       cp_parser_objc_class_ivars (parser);
18515       objc_continue_implementation ();
18516     }
18517
18518   cp_parser_objc_method_definition_list (parser);
18519 }
18520
18521 /* Consume the @end token and finish off the implementation.  */
18522
18523 static void
18524 cp_parser_objc_end_implementation (cp_parser* parser)
18525 {
18526   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18527   objc_finish_implementation ();
18528 }
18529
18530 /* Parse an Objective-C declaration.  */
18531
18532 static void
18533 cp_parser_objc_declaration (cp_parser* parser)
18534 {
18535   /* Try to figure out what kind of declaration is present.  */
18536   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18537
18538   switch (kwd->keyword)
18539     {
18540     case RID_AT_ALIAS:
18541       cp_parser_objc_alias_declaration (parser);
18542       break;
18543     case RID_AT_CLASS:
18544       cp_parser_objc_class_declaration (parser);
18545       break;
18546     case RID_AT_PROTOCOL:
18547       /* APPLE LOCAL radar 4947311 */
18548       cp_parser_objc_protocol_declaration (parser, NULL_TREE);
18549       break;
18550       /* APPLE LOCAL begin radar 4548636 - radar 4947311 */
18551     case RID_ATTRIBUTE:
18552       {
18553         tree attributes = NULL_TREE;
18554         cp_parser_objc_maybe_attributes (parser, &attributes);
18555         if (cp_lexer_peek_token (parser->lexer)->keyword == RID_AT_INTERFACE)
18556           cp_parser_objc_class_interface (parser, attributes);
18557         else if (cp_lexer_peek_token (parser->lexer)->keyword == RID_AT_PROTOCOL)
18558           cp_parser_objc_protocol_declaration (parser, attributes);
18559         break;
18560       }
18561         /* APPLE LOCAL end radar 4548636 - radar 4947311 */
18562     case RID_AT_INTERFACE:
18563         /* APPLE LOCAL radar 4947311 */
18564       cp_parser_objc_class_interface (parser, NULL_TREE);
18565       break;
18566     case RID_AT_IMPLEMENTATION:
18567       cp_parser_objc_class_implementation (parser);
18568       break;
18569     case RID_AT_END:
18570       cp_parser_objc_end_implementation (parser);
18571       break;
18572     default:
18573       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18574       cp_parser_skip_to_end_of_block_or_statement (parser);
18575     }
18576 }
18577
18578 /* Parse an Objective-C try-catch-finally statement.
18579
18580    objc-try-catch-finally-stmt:
18581      @try compound-statement objc-catch-clause-seq [opt]
18582        objc-finally-clause [opt]
18583
18584    objc-catch-clause-seq:
18585      objc-catch-clause objc-catch-clause-seq [opt]
18586
18587    objc-catch-clause:
18588      @catch ( exception-declaration ) compound-statement
18589
18590    objc-finally-clause
18591      @finally compound-statement
18592
18593    Returns NULL_TREE.  */
18594
18595 static tree
18596 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18597   location_t location;
18598   tree stmt;
18599
18600   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18601   location = cp_lexer_peek_token (parser->lexer)->location;
18602   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18603      node, lest it get absorbed into the surrounding block.  */
18604   stmt = push_stmt_list ();
18605   /* APPLE LOCAL radar 5982990 */
18606   cp_parser_compound_statement (parser, NULL, false, false);
18607   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18608
18609   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18610     {
18611       cp_parameter_declarator *parmdecl;
18612       tree parm;
18613
18614       cp_lexer_consume_token (parser->lexer);
18615       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18616       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18617       objc_begin_catch_clause (parm);
18618       /* APPLE LOCAL radar 5982990 */
18619       cp_parser_compound_statement (parser, NULL, false, false);
18620       objc_finish_catch_clause ();
18621     }
18622
18623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18624     {
18625       cp_lexer_consume_token (parser->lexer);
18626       location = cp_lexer_peek_token (parser->lexer)->location;
18627       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18628          node, lest it get absorbed into the surrounding block.  */
18629       stmt = push_stmt_list ();
18630       /* APPLE LOCAL radar 5982990 */
18631       cp_parser_compound_statement (parser, NULL, false, false);
18632       objc_build_finally_clause (location, pop_stmt_list (stmt));
18633     }
18634
18635   return objc_finish_try_stmt ();
18636 }
18637
18638 /* Parse an Objective-C synchronized statement.
18639
18640    objc-synchronized-stmt:
18641      @synchronized ( expression ) compound-statement
18642
18643    Returns NULL_TREE.  */
18644
18645 static tree
18646 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18647   location_t location;
18648   tree lock, stmt;
18649
18650   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18651
18652   location = cp_lexer_peek_token (parser->lexer)->location;
18653   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18654   lock = cp_parser_expression (parser, false);
18655   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18656
18657   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18658      node, lest it get absorbed into the surrounding block.  */
18659   stmt = push_stmt_list ();
18660   /* APPLE LOCAL radar 5982990 */
18661   cp_parser_compound_statement (parser, NULL, false, flag_objc_sjlj_exceptions);
18662
18663   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18664 }
18665
18666 /* Parse an Objective-C throw statement.
18667
18668    objc-throw-stmt:
18669      @throw assignment-expression [opt] ;
18670
18671    Returns a constructed '@throw' statement.  */
18672
18673 static tree
18674 cp_parser_objc_throw_statement (cp_parser *parser) {
18675   tree expr = NULL_TREE;
18676
18677   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18678
18679   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18680     expr = cp_parser_assignment_expression (parser, false);
18681
18682   cp_parser_consume_semicolon_at_end_of_statement (parser);
18683
18684   return objc_build_throw_stmt (expr);
18685 }
18686
18687 /* Parse an Objective-C statement.  */
18688
18689 static tree
18690 cp_parser_objc_statement (cp_parser * parser) {
18691   /* Try to figure out what kind of declaration is present.  */
18692   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18693
18694   switch (kwd->keyword)
18695     {
18696     case RID_AT_TRY:
18697       return cp_parser_objc_try_catch_finally_statement (parser);
18698     case RID_AT_SYNCHRONIZED:
18699       return cp_parser_objc_synchronized_statement (parser);
18700     case RID_AT_THROW:
18701       return cp_parser_objc_throw_statement (parser);
18702     default:
18703       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18704       cp_parser_skip_to_end_of_block_or_statement (parser);
18705     }
18706
18707   return error_mark_node;
18708 }
18709
18710 /* APPLE LOCAL begin C* language */
18711 /* Routine closes up the C*'s foreach statement.
18712 */
18713
18714 static void
18715 objc_finish_foreach_stmt (tree for_stmt)
18716 {
18717   if (flag_new_for_scope > 0)
18718     {
18719       tree scope = TREE_CHAIN (for_stmt);
18720       TREE_CHAIN (for_stmt) = NULL;
18721       add_stmt (do_poplevel (scope));
18722     }
18723
18724   finish_stmt (); 
18725 }
18726
18727 /*
18728   Synthesizer routine for C*'s feareach statement.
18729   
18730   It synthesizes:
18731   for ( type elem in collection) { stmts; }
18732   
18733   Into:
18734     {
18735     type elem;  
18736     __objcFastEnumerationState enumState = { 0 };
18737     id items[16];
18738
18739     unsigned long limit = [collection countByEnumeratingWithState:&enumState objects:items count:16];
18740     if (limit) {
18741       unsigned long startMutations = *enumState.mutationsPtr;
18742       do {
18743           unsigned long counter = 0;
18744           do {
18745             if (startMutations != *enumState.mutationsPtr) objc_enumerationMutation(collection);
18746             elem = enumState.itemsPtr[counter++];
18747             stmts;
18748           } while (counter < limit);
18749      } while (limit = [collection countByEnumeratingWithState:&enumState objects:items count:16]);
18750   }
18751   else
18752     elem = nil; radar 4854605, 5128402
18753
18754 */
18755
18756 static void
18757 objc_foreach_stmt (cp_parser* parser, tree statement)
18758 {
18759   unsigned char in_statement;
18760   tree enumerationMutation_call_exp;
18761   tree countByEnumeratingWithState;
18762   tree receiver;
18763   tree exp, bind;
18764   tree enumState_decl, items_decl;
18765   tree limit_decl, limit_decl_assign_expr;
18766   tree outer_if_stmt,  inner_if_stmt, if_condition, startMutations_decl;
18767   tree outer_do_stmt, inner_do_stmt, do_condition;
18768   tree counter_decl;
18769   tree_stmt_iterator i = tsi_start (TREE_CHAIN (statement));
18770   tree t = tsi_stmt (i);
18771   /* APPLE LOCAL radar 5130983 */
18772   tree elem_decl = TREE_CODE (t) == DECL_EXPR ? DECL_EXPR_DECL (t) : t;
18773
18774   receiver  = cp_parser_condition (parser);
18775   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18776
18777   /* APPLE LOCAL begin radar 4507230 */
18778   if (!objc_type_valid_for_messaging (TREE_TYPE (elem_decl)))
18779     {
18780       error ("selector element does not have a valid object type");
18781       return;
18782     }
18783
18784   if (!objc_type_valid_for_messaging (TREE_TYPE (receiver)))
18785     {
18786       error ("expression does not have a valid object type");
18787       return;
18788     }
18789   /* APPLE LOCAL end radar 4507230 */
18790
18791   enumerationMutation_call_exp = objc_build_foreach_components  (receiver, &enumState_decl, 
18792                                                                  &items_decl, &limit_decl, 
18793                                                                  &startMutations_decl, &counter_decl,
18794                                                                  &countByEnumeratingWithState);
18795
18796   /* __objcFastEnumerationState enumState = { 0 }; */
18797   exp = build_stmt (DECL_EXPR, enumState_decl);
18798   bind = build3 (BIND_EXPR, void_type_node, enumState_decl, exp, NULL);
18799   TREE_SIDE_EFFECTS (bind) = 1;
18800   add_stmt (bind);
18801
18802   /* id items[16]; */
18803   bind = build3 (BIND_EXPR, void_type_node, items_decl, NULL, NULL);
18804   TREE_SIDE_EFFECTS (bind) = 1;
18805   add_stmt (bind);
18806
18807   /* Generate this statement and add it to the list. */
18808   /* limit = [collection countByEnumeratingWithState:&enumState objects:items count:16] */
18809   limit_decl_assign_expr = build2 (MODIFY_EXPR, TREE_TYPE (limit_decl), limit_decl, 
18810                                    countByEnumeratingWithState);
18811   bind = build3 (BIND_EXPR, void_type_node, limit_decl, NULL, NULL);
18812   TREE_SIDE_EFFECTS (bind) = 1;
18813   add_stmt (bind);
18814
18815   /* if (limit) { */
18816   outer_if_stmt = begin_if_stmt ();
18817   /* APPLE LOCAL radar 4547045 */
18818   if_condition = build_binary_op (NE_EXPR, limit_decl_assign_expr,
18819                                    fold_convert (TREE_TYPE (limit_decl), integer_zero_node),
18820                                    1);
18821
18822   finish_if_stmt_cond (if_condition, outer_if_stmt);
18823
18824   /* unsigned long startMutations = *enumState.mutationsPtr; */
18825   exp = objc_build_component_ref (enumState_decl, get_identifier("mutationsPtr"));
18826   exp = build_indirect_ref (exp, "unary *");
18827   exp = build2 (MODIFY_EXPR, void_type_node, startMutations_decl, exp);
18828   bind = build3 (BIND_EXPR, void_type_node, startMutations_decl, exp, NULL);
18829   TREE_SIDE_EFFECTS (bind) = 1;
18830   add_stmt (bind);
18831
18832   /* do { */
18833 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
18834   outer_do_stmt = begin_do_stmt (NULL_TREE);
18835
18836 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
18837   /* Body of the outer do-while loop */
18838   /* unsigned int counter = 0; */
18839   exp = build2 (MODIFY_EXPR, void_type_node, counter_decl,
18840                 fold_convert (TREE_TYPE (counter_decl), integer_zero_node));
18841   bind = build3 (BIND_EXPR, void_type_node, counter_decl, exp, NULL);
18842   TREE_SIDE_EFFECTS (bind) = 1;
18843   add_stmt (bind);
18844
18845   /*   do { */
18846 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
18847   inner_do_stmt = begin_do_stmt (NULL_TREE);
18848
18849 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
18850   /* Body of the inner do-while loop */
18851
18852   /* if (startMutations != *enumState.mutationsPtr) objc_enumerationMutation (collection); */
18853   inner_if_stmt = begin_if_stmt ();
18854   exp = objc_build_component_ref (enumState_decl, get_identifier("mutationsPtr"));
18855   exp = build_indirect_ref (exp, "unary *");
18856   if_condition = build_binary_op (NE_EXPR, startMutations_decl, exp, 1);
18857   finish_if_stmt_cond (if_condition, inner_if_stmt);
18858
18859   add_stmt (enumerationMutation_call_exp);
18860   finish_then_clause (inner_if_stmt);
18861   finish_if_stmt (inner_if_stmt);
18862
18863   /* elem = enumState.itemsPtr [counter]; */
18864   exp = objc_build_component_ref (enumState_decl, get_identifier("itemsPtr"));
18865   exp = build_array_ref (exp, counter_decl);
18866   add_stmt (build2 (MODIFY_EXPR, void_type_node, elem_decl, exp));
18867   /* APPLE LOCAL radar 4538105 */
18868   TREE_USED (elem_decl) = 1;
18869
18870   /* counter++; */
18871   exp = build2 (PLUS_EXPR, TREE_TYPE (counter_decl), counter_decl,
18872                  build_int_cst (NULL_TREE, 1));
18873   add_stmt (build2 (MODIFY_EXPR, void_type_node, counter_decl, exp));
18874
18875   /* ADD << stmts >> from the foreach loop. */
18876   /* Parse the body of the for-statement.  */
18877   in_statement = parser->in_statement;
18878   parser->in_statement = IN_ITERATION_STMT;
18879   cp_parser_already_scoped_statement (parser);
18880   parser->in_statement = in_statement;
18881
18882   finish_do_body (inner_do_stmt);
18883
18884   /*   } while (counter < limit ); */
18885   do_condition  = build_binary_op (LT_EXPR, counter_decl, limit_decl, 1);
18886   finish_do_stmt (do_condition, inner_do_stmt);
18887   DO_FOREACH (inner_do_stmt) = integer_zero_node;
18888   /* APPLE LOCAL radar 4667060 */
18889   DO_FOREACH (outer_do_stmt) = elem_decl;
18890
18891   finish_do_body (outer_do_stmt);
18892
18893   /* } while (limit = [collection countByEnumeratingWithState:&enumState objects:items count:16]);  */
18894  
18895   exp = unshare_expr (limit_decl_assign_expr);
18896   do_condition  = build_binary_op (NE_EXPR, exp,
18897                                     fold_convert (TREE_TYPE (limit_decl), integer_zero_node),
18898                                     1);
18899   finish_do_stmt (do_condition, outer_do_stmt);
18900
18901
18902   finish_then_clause (outer_if_stmt);
18903
18904   /* } */
18905   /* APPLE LOCAL begin radar 4854605 - radar 5128402 */
18906   begin_else_clause (outer_if_stmt);
18907   add_stmt (build2 (MODIFY_EXPR, void_type_node, elem_decl,
18908              fold_convert (TREE_TYPE (elem_decl), integer_zero_node)));
18909   finish_else_clause (outer_if_stmt);
18910   /* APPLE LOCAL end radar 4854605 - radar 5128402 */
18911
18912   finish_if_stmt (outer_if_stmt);
18913
18914   objc_finish_foreach_stmt (statement);
18915 }
18916 /* APPLE LOCAL end C* language */
18917 /* APPLE LOCAL begin blocks 6040305 (ce) */
18918 #define I_SYMBOL_BINDING(t) IDENTIFIER_BINDING(t)
18919
18920 tree build_component_ref (tree e, tree member);
18921 tree
18922 build_component_ref (tree e, tree member)
18923 {
18924   if (!DECL_P (member))
18925     member = lookup_member (TREE_TYPE (e), member, 0, 0);
18926   if (processing_template_decl)
18927     return build3 (COMPONENT_REF, TREE_TYPE (member), e, DECL_NAME (member), NULL_TREE);
18928   return build_class_member_access_expr (e, member,
18929                                          NULL_TREE, false);
18930 }
18931
18932 /* APPLE LOCAL begin radar 6214617 */
18933 static bool
18934 cp_block_requires_copying (tree exp)
18935 {
18936   return (block_requires_copying (exp) 
18937                 || TYPE_HAS_CONSTRUCTOR (TREE_TYPE (exp))
18938                 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)));
18939 }
18940 /* APPLE LOCAL end radar 6214617 */
18941
18942 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
18943 /** build_descriptor_block_decl -
18944  This routine builds a static block_descriptior variable of type:
18945  struct __block_descriptor; and initializes it to:
18946  {0, sizeof(struct literal_block_n), 
18947  copy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
18948  destroy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
18949  }
18950  */
18951 static tree
18952 build_descriptor_block_decl (tree block_struct_type, struct block_sema_info *block_impl)
18953 {
18954   extern tree create_tmp_var_raw (tree, const char *);
18955   static int desc_unique_count;
18956   int size;
18957   tree helper_addr;
18958   tree decl, constructor;
18959   char name [32];
18960   VEC(constructor_elt,gc) *impl_v = NULL;
18961   tree descriptor_type =  
18962     TREE_TYPE (build_block_descriptor_type (block_impl->BlockHasCopyDispose));
18963   
18964   sprintf (name, "__block_descriptor_tmp_%d", ++desc_unique_count);
18965   decl = create_tmp_var_raw (descriptor_type, name);
18966   DECL_CONTEXT (decl) = NULL_TREE;
18967
18968   /* Initialize "reserved" field to 0 for now. */
18969   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (long_unsigned_type_node, 0));
18970
18971   /* Initialize "Size" field. */
18972   size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (block_struct_type));
18973   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (long_unsigned_type_node, size));
18974
18975   if (block_impl->BlockHasCopyDispose)
18976     {
18977       /* Initialize "CopyFuncPtr" and "DestroyFuncPtr" fields. */
18978       /* Helpers were previously generated completeley as a nested
18979         function (and context was required for code gen.) But they are not, 
18980         so context must be set to NULL so initialization logic does not complain. */
18981       DECL_CONTEXT (block_impl->copy_helper_func_decl) = NULL_TREE;
18982       helper_addr = build_fold_addr_expr (block_impl->copy_helper_func_decl);
18983       helper_addr = convert (ptr_type_node, helper_addr);
18984       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
18985
18986       DECL_CONTEXT (block_impl->destroy_helper_func_decl) = NULL_TREE;
18987       helper_addr = build_fold_addr_expr (block_impl->destroy_helper_func_decl);
18988       helper_addr = convert (ptr_type_node, helper_addr);
18989       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
18990     }
18991   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
18992   constructor = make_node (CONSTRUCTOR);
18993   CONSTRUCTOR_ELTS (constructor) = impl_v;
18994   TREE_PUBLIC (decl) = 0;
18995   TREE_STATIC (decl) = 1;
18996   cp_finish_decl (decl, constructor, 0, 0, LOOKUP_ONLYCONVERTING);
18997   return decl;
18998 }
18999
19000 /* APPLE LOCAL begin radar 6300081  */
19001 /* This function builds a "generic" block struct type, to be passed 
19002  into the debug information for blocks pointers, to allow gdb to
19003  find the actual function pointer for the block.  Any time the Blocks
19004  structure layout changes, this may also need to change.
19005
19006  Currently a block pointer is a pointer to a __block_literal_n struct,
19007  the third field of which is a pointer to a __block_descriptor struct,
19008  whose third field is the function pointer.  There are other fields as
19009  well, but these are the ones gdb needs to know about to find the
19010  function pointer.  Therefore a generic block struct currently looks
19011  like this:
19012
19013  struct __block_literal_generic 
19014  {
19015  void * __isa;
19016  int __flags;
19017  int __reserved;
19018  void *__FuncPtr;
19019  struct __block_descriptor 
19020  {
19021  unsigned long int reserved;
19022  unsigned long int Size;
19023  } *__descriptor;
19024  };
19025
19026  IF AT ANY TIME THE STRUCTURE OF A __BLOCK_LITERAL_N CHANGES, THIS
19027  MUST BE CHANGED ALSO!!
19028
19029  */
19030
19031 tree
19032 /* APPLE LOCAL radar 6353006  */
19033 c_build_generic_block_struct_type (void)
19034 {
19035   tree fields = NULL_TREE;
19036   tree field;
19037   tree block_struct_type;
19038
19039   push_to_top_level ();
19040   block_struct_type = xref_tag (record_type, 
19041                                  get_identifier ("__block_literal_generic"),
19042                                  ts_current, false);
19043   xref_basetypes (block_struct_type, NULL_TREE);
19044   CLASSTYPE_DECLARED_CLASS (block_struct_type) = 0;
19045   pushclass (block_struct_type);
19046
19047   field = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
19048   TREE_CHAIN (field) = fields;
19049   fields = field;
19050
19051   field = build_decl (FIELD_DECL, get_identifier ("__flags"),
19052                         integer_type_node);
19053   TREE_CHAIN (field) = fields;
19054   fields = field;
19055
19056   field = build_decl (FIELD_DECL, get_identifier ("__reserved"),
19057                         integer_type_node);
19058   TREE_CHAIN (field) = fields;
19059   fields = field;
19060
19061   field = build_decl (FIELD_DECL, get_identifier ("__FuncPtr"),
19062                         ptr_type_node);
19063   TREE_CHAIN (field) = fields;
19064   fields = field;
19065
19066   field = build_decl (FIELD_DECL, get_identifier ("__descriptor"),
19067                         build_block_descriptor_type (false));
19068   TREE_CHAIN (field) = fields;
19069   fields = field;
19070
19071   TYPE_FIELDS (block_struct_type) = fields;
19072   TYPE_NAME (block_struct_type) = build_decl (TYPE_DECL,
19073                                                get_identifier ("__block_literal_generic"), 
19074                                                                block_struct_type);
19075   TYPE_STUB_DECL (block_struct_type) = TYPE_NAME (block_struct_type);
19076   TYPE_BLOCK_IMPL_STRUCT (block_struct_type) = 1;
19077   finish_struct (block_struct_type, NULL_TREE);
19078   pop_from_top_level ();
19079
19080   return block_struct_type;
19081 }
19082 /* APPLE LOCAL end radar 6300081  */
19083
19084 /** build_block_struct_type -
19085  struct __block_literal_n {
19086  void *__isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
19087  int __flags;
19088  int __reserved;
19089  void *__FuncPtr;
19090
19091  struct __block_descriptor {
19092  unsigned long int reserved;     // NULL
19093  unsigned long int Size;  // sizeof(struct __block_literal_n)
19094
19095  // optional helper functions
19096  void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19097  void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19098  } *__descriptor;
19099
19100  // imported variables
19101  int x; // ref variable list ...
19102  int *y; // byref variable list
19103  };
19104  */
19105 static tree
19106 build_block_struct_type (struct block_sema_info * block_impl)
19107 {
19108   tree fields = NULL_TREE, field, chain;
19109   char buffer[32];
19110   static int unique_count;
19111   tree block_struct_type;
19112   
19113   /* Check and see if this block is required to have a Copy/Dispose
19114      helper function. If yes, set BlockHasCopyDispose to TRUE. */
19115   for (chain = block_impl->block_ref_decl_list; chain;
19116         chain = TREE_CHAIN (chain))
19117     /* APPLE LOCAL begin radar 6214617 */
19118       if (cp_block_requires_copying (TREE_VALUE (chain)))
19119       {
19120         tree type = TREE_TYPE (TREE_VALUE (chain));
19121         block_impl->BlockHasCopyDispose = TRUE;
19122         if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
19123         {
19124             block_impl->BlockImportsCxxObjects = TRUE;
19125             break;
19126         }
19127         /* APPLE LOCAL end radar 6214617 */
19128       }
19129   
19130   /* Further check to see that we have __block variables which require
19131      Copy/Dispose helpers. */
19132   for (chain = block_impl->block_byref_decl_list; chain;
19133         chain = TREE_CHAIN (chain))
19134       if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19135       {
19136         block_impl->BlockHasCopyDispose = TRUE;
19137         break;
19138       }
19139   
19140   sprintf(buffer, "__block_literal_%d", ++unique_count);
19141   push_to_top_level ();
19142   /* APPLE LOCAL begin radar 6243400 */
19143   block_struct_type = xref_tag (record_type, get_identifier (buffer), ts_current, false);
19144   xref_basetypes (block_struct_type, NULL_TREE);
19145   CLASSTYPE_DECLARED_CLASS (block_struct_type) = 0;
19146   pushclass (block_struct_type);
19147   /* APPLE LOCAL end radar 6243400 */
19148   /* void * __isa; */
19149   field = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
19150   TREE_CHAIN (field) = fields;
19151   fields = field;
19152   
19153   /* int __flags. */
19154   field = build_decl (FIELD_DECL, get_identifier ("__flags"), integer_type_node);
19155   TREE_CHAIN (field) = fields;
19156   fields = field;
19157   
19158   /* int __reserved. */
19159   field = build_decl (FIELD_DECL, get_identifier ("__reserved"), integer_type_node);
19160   TREE_CHAIN (field) = fields;
19161   fields = field;
19162   
19163   /* void *__FuncPtr. */
19164   field = build_decl (FIELD_DECL, get_identifier ("__FuncPtr"),
19165                                 ptr_type_node);
19166   TREE_CHAIN (field) = fields;
19167   fields = field;
19168   
19169   /* struct __block_descriptor *__descriptor */
19170   field = build_decl (FIELD_DECL, get_identifier ("__descriptor"),
19171                                 build_block_descriptor_type (block_impl->BlockHasCopyDispose));
19172   TREE_CHAIN (field) = fields;
19173   fields = field;
19174   
19175   if (block_impl->BlockHasCopyDispose)
19176   {
19177       /* If inner block of a nested block has BlockHasCopyDispose, so
19178         does its outer block. */
19179       if (block_impl->prev_block_info)
19180         block_impl->prev_block_info->BlockHasCopyDispose = TRUE;
19181   }
19182
19183   /* int x; // ref variable list ... */
19184   for (chain = block_impl->block_ref_decl_list; chain; chain = TREE_CHAIN (chain))
19185   {
19186       tree p = TREE_VALUE (chain);
19187       /* Note! const-ness of copied in variable must not be carried over to the
19188         type of the synthesized struct field. It prevents to assign to this
19189         field when copy constructor is synthesized. */
19190       field = build_decl (FIELD_DECL, DECL_NAME (p),
19191                            c_build_qualified_type (TREE_TYPE (p),
19192                                                    TYPE_UNQUALIFIED));
19193       TREE_CHAIN (field) = fields;
19194       fields = field;
19195   }
19196
19197   /* int *y; // byref variable list */
19198   for (chain = block_impl->block_byref_decl_list; chain; chain = TREE_CHAIN (chain))
19199   {
19200       tree p = TREE_VALUE (chain);
19201       field = build_decl (FIELD_DECL, DECL_NAME (p),
19202                            TREE_TYPE (p));
19203       TREE_CHAIN (field) = fields;
19204       fields = field;
19205   }
19206
19207   /* APPLE LOCAL begin radar 6243400 */
19208   TYPE_FIELDS (block_struct_type) = fields;
19209   TYPE_NAME (block_struct_type) = 
19210     build_decl (TYPE_DECL, get_identifier (buffer), block_struct_type);
19211   TYPE_STUB_DECL (block_struct_type) = TYPE_NAME (block_struct_type);
19212   finish_struct (block_struct_type, NULL_TREE);
19213   pop_from_top_level ();
19214   /* APPLE LOCAL end radar 6243400 */
19215   return block_struct_type;
19216 }
19217
19218 /**
19219  build_block_struct_initlist - builds the initializer list:
19220  { &_NSConcreteStackBlock or &_NSConcreteGlobalBlock // __isa,
19221    BLOCK_USE_STRET | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL // __flags,
19222    0, // __reserved,
19223    &helper_1, // __FuncPtr,
19224    &static_descriptor_variable // __descriptor,
19225    x, // user variables.
19226    &y
19227    ...
19228  }
19229 */
19230 /* APPLE LOCAL begin radar 6169527 */
19231 /* This routine is entirely rewritten as we now have to deal with full-blown
19232    c++ classes with fields which may require construction. */
19233 static VEC(constructor_elt,gc) *
19234 build_block_struct_initlist (tree block_struct_type,
19235                              struct block_sema_info *block_impl)
19236 {
19237   tree expr, chain, helper_addr;
19238   /* APPLE LOCAL radar 7735196 */
19239   unsigned flags = 0;
19240   static tree NSConcreteStackBlock_decl = NULL_TREE;
19241   static tree NSConcreteGlobalBlock_decl = NULL_TREE;
19242   VEC(constructor_elt,gc) *impl_v = NULL;
19243   tree descriptor_block_decl = build_descriptor_block_decl (block_struct_type, block_impl);
19244
19245   if (block_impl->BlockHasCopyDispose)
19246     /* Note! setting of this flag merely indicates to the runtime that
19247         we have destroy_helper_block/copy_helper_block helper
19248         routines. */
19249     flags |= BLOCK_HAS_COPY_DISPOSE;
19250   /* APPLE LOCAL begin radar 6214617 */
19251   /* Set BLOCK_HAS_CXX_OBJ if block is importing a cxx object. */
19252   if (block_impl->BlockImportsCxxObjects)
19253     flags |= BLOCK_HAS_CXX_OBJ;
19254   /* APPLE LOCAL end radar 6214617 */
19255 /* APPLE LOCAL begin radar 7735196 */
19256   if (block_impl->return_type && aggregate_value_p(block_impl->return_type, 0))
19257     flags |= BLOCK_USE_STRET;
19258   /* APPLE LOCAL end 7735196 */
19259   /* APPLE LOCAL begin radar 6230297 */
19260   if (!current_function_decl ||
19261       (block_impl->block_ref_decl_list == NULL_TREE &&
19262         block_impl->block_byref_decl_list == NULL_TREE))
19263   /* APPLE LOCAL end radar 6230297 */
19264     {
19265       /* This is a global block. */
19266       /* Find an existing declaration for _NSConcreteGlobalBlock or declare
19267          extern void *_NSConcreteGlobalBlock; */
19268       if (NSConcreteGlobalBlock_decl == NULL_TREE)
19269         {
19270           tree name_id = get_identifier("_NSConcreteGlobalBlock");
19271           NSConcreteGlobalBlock_decl = lookup_name (name_id);
19272           if (!NSConcreteGlobalBlock_decl)
19273             {
19274               NSConcreteGlobalBlock_decl = build_decl (VAR_DECL, name_id, ptr_type_node);
19275               DECL_EXTERNAL (NSConcreteGlobalBlock_decl) = 1;
19276               TREE_PUBLIC (NSConcreteGlobalBlock_decl) = 1;
19277               pushdecl_top_level (NSConcreteGlobalBlock_decl);
19278               rest_of_decl_compilation (NSConcreteGlobalBlock_decl, 0, 0);
19279             }
19280         }
19281       /* APPLE LOCAL begin radar 6457359 */
19282       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE,
19283                               convert (ptr_type_node,
19284                                        build_fold_addr_expr (NSConcreteGlobalBlock_decl)));
19285       /* APPLE LOCAL end radar 6457359 */
19286       flags |= BLOCK_IS_GLOBAL;
19287     }
19288   else
19289     {
19290       /* Find an existing declaration for _NSConcreteStackBlock or declare
19291          extern void *_NSConcreteStackBlock; */
19292       if (NSConcreteStackBlock_decl == NULL_TREE)
19293         {
19294           tree name_id = get_identifier("_NSConcreteStackBlock");
19295           NSConcreteStackBlock_decl = lookup_name (name_id);
19296           if (!NSConcreteStackBlock_decl)
19297             {
19298               NSConcreteStackBlock_decl = build_decl (VAR_DECL, name_id, ptr_type_node);
19299               DECL_EXTERNAL (NSConcreteStackBlock_decl) = 1;
19300               TREE_PUBLIC (NSConcreteStackBlock_decl) = 1;
19301               pushdecl_top_level (NSConcreteStackBlock_decl);
19302               rest_of_decl_compilation (NSConcreteStackBlock_decl, 0, 0);
19303             }
19304         }
19305       /* APPLE LOCAL begin radar 6457359 */
19306       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE,
19307                               convert (ptr_type_node,
19308                                        build_fold_addr_expr (NSConcreteStackBlock_decl)));
19309       /* APPLE LOCAL end radar 6457359 */
19310     }
19311
19312   /* __flags */
19313   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (integer_type_node, flags));
19314   /* __reserved */
19315   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (integer_type_node, 0));
19316   /* __FuncPtr */
19317   helper_addr = build_fold_addr_expr (block_impl->helper_func_decl);
19318   helper_addr = convert (ptr_type_node, helper_addr);
19319   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
19320
19321   /* &static_descriptor_variable initializer */
19322   expr = build_fold_addr_expr (descriptor_block_decl);
19323   CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, expr);
19324
19325   for (chain = block_impl->block_original_ref_decl_list; chain;
19326         chain = TREE_CHAIN (chain))
19327     {
19328       tree y = TREE_VALUE (chain);
19329       TREE_USED (y) = 1;
19330       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, y);
19331     }
19332   for (chain = block_impl->block_byref_decl_list; chain;
19333         chain = TREE_CHAIN (chain))
19334     {
19335       tree y = lookup_name (DECL_NAME (TREE_VALUE (chain)));
19336       tree forwarding_expr;
19337       gcc_assert (y);
19338       TREE_USED (y) = 1;
19339       if (COPYABLE_BYREF_LOCAL_VAR (y))
19340          {
19341            /* For variables declared __block, either the original one
19342               at the point of declaration or the imported version (which is
19343               initialized in the helper function's prologue) is used to
19344               initilize the byref variable field in the temporary. */
19345            if (TREE_CODE (TREE_TYPE (y)) != RECORD_TYPE)
19346              y = build_indirect_ref (y, "unary *");
19347            /* We will be using the __block_struct_variable.__forwarding as the
19348               initializer. */
19349            forwarding_expr = build_component_ref (y, get_identifier ("__forwarding"));
19350          }
19351       else
19352          /* Global variable is always assumed passed by its address. */
19353          forwarding_expr = build_fold_addr_expr (y);
19354
19355       CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, forwarding_expr);
19356     }
19357   return impl_v;
19358 }
19359 /* APPLE LOCAL end radar 6169527 */
19360 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
19361
19362 /**
19363  build_block_literal_tmp - This routine:
19364
19365  1) builds block type:
19366  struct __block_literal_n {
19367   void *__isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
19368   int __flags;
19369   int __reserved;
19370   void *__FuncPtr;
19371
19372   struct __block_descriptor {
19373     unsigned long int reserved;     // NULL
19374     unsigned long int Size;  // sizeof(struct Block_literal_1)
19375
19376     // optional helper functions
19377     void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19378     void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19379  } *__descriptor;
19380
19381  // imported variables
19382  int x; // ref variable list ...
19383  int *y; // byref variable list
19384  };
19385
19386  2) build function prototype:
19387  double helper_1(struct block_1 *ii, int z);
19388
19389  3) build the temporary initialization:
19390  struct block_1 I = {
19391  { &_NSConcreteStackBlock or &_NSConcreteGlobalBlock // isa,
19392    BLOCK_HAS_CXX_OBJ | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL // flags,
19393    0, // reserved,
19394    &helper_1,
19395    &{
19396       NULL,
19397       sizeof(struct block_1),
19398       copy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
19399       destroy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
19400     },
19401  x,
19402  &y
19403 };
19404
19405 It return the temporary.
19406 */
19407 /* APPLE LOCAL begin radar 6169527 */
19408 static tree
19409 build_block_literal_tmp (const char *name,
19410                          struct block_sema_info * block_impl)
19411 {
19412   extern tree create_tmp_var_raw (tree, const char *);
19413   tree block_holder_tmp_decl;
19414   tree constructor;
19415   tree block_struct_type = TREE_TYPE (block_impl->block_arg_ptr_type);
19416   /* APPLE LOCAL begin radar 6230297 */
19417   bool staticBlockTmp = (block_impl->block_ref_decl_list == NULL_TREE &&
19418                           block_impl->block_byref_decl_list == NULL_TREE);
19419
19420   block_holder_tmp_decl = create_tmp_var_raw (block_struct_type, name);
19421   /* Context will not be known until when the literal is synthesized.
19422      This is more so in the case of nested block literal blocks.  */
19423   maybe_push_decl (block_holder_tmp_decl);
19424   DECL_CONTEXT (block_holder_tmp_decl) = staticBlockTmp ? NULL_TREE
19425                                                          : current_function_decl;
19426   if (staticBlockTmp)
19427     DECL_CONTEXT (block_impl->helper_func_decl) = NULL_TREE;
19428   /* APPLE LOCAL end radar 6230297 */
19429   DECL_ARTIFICIAL (block_holder_tmp_decl) = 1;
19430
19431   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
19432   constructor = make_node (CONSTRUCTOR);
19433
19434   CONSTRUCTOR_ELTS (constructor) = build_block_struct_initlist (block_struct_type,
19435                                                                  block_impl);
19436   /* Temporary representing a global block is made global static.  */
19437   /* APPLE LOCAL radar 6230297 */
19438   if (staticBlockTmp || global_bindings_p ()) {
19439     TREE_PUBLIC (block_holder_tmp_decl) = 0;
19440     TREE_STATIC (block_holder_tmp_decl) = 1;
19441   }
19442   cp_finish_decl (block_holder_tmp_decl, constructor, 0, 0, LOOKUP_ONLYCONVERTING);
19443   return block_holder_tmp_decl;
19444 }
19445 /* APPLE LOCAL end radar 6169527 */
19446
19447 static tree
19448 clean_and_exit (tree block)
19449 {
19450   pop_function_context ();
19451   pop_lang_context ();
19452   if (current_function_decl)
19453     free (finish_block (block));
19454   return error_mark_node;
19455 }
19456
19457 /** synth_copy_helper_block_func - This function synthesizes
19458  void copy_helper_block (struct block* _dest, struct block *_src) function.
19459  */
19460
19461 static void
19462 synth_copy_helper_block_func (struct block_sema_info * block_impl)
19463 {
19464   tree stmt, chain;
19465   tree dst_arg, src_arg;
19466   /* struct c_arg_info * arg_info; */
19467   /* Set up: (struct block* _dest, struct block *_src) parameters. */
19468   dst_arg = build_decl (PARM_DECL, get_identifier ("_dst"),
19469                                   block_impl->block_arg_ptr_type);
19470   DECL_CONTEXT (dst_arg) = cur_block->copy_helper_func_decl;
19471   TREE_USED (dst_arg) = 1;
19472   DECL_ARG_TYPE (dst_arg) = block_impl->block_arg_ptr_type;
19473   src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
19474                                   block_impl->block_arg_ptr_type);
19475   DECL_CONTEXT (src_arg) = cur_block->copy_helper_func_decl;
19476   TREE_USED (src_arg) = 1;
19477   DECL_ARG_TYPE (src_arg) = block_impl->block_arg_ptr_type;
19478   /* arg_info = xcalloc (1, sizeof (struct c_arg_info)); */
19479   TREE_CHAIN (dst_arg) = src_arg;
19480   
19481   pushdecl (cur_block->copy_helper_func_decl);
19482   /* arg_info->parms = dst_arg; */
19483   /* arg_info->types = tree_cons (NULL_TREE, block_impl->block_arg_ptr_type,
19484    tree_cons (NULL_TREE,
19485    block_impl->block_arg_ptr_type,
19486    NULL_TREE)); */
19487   DECL_ARGUMENTS (cur_block->copy_helper_func_decl) = dst_arg;
19488   /* function header synthesis. */
19489   push_function_context ();
19490   /* start_block_helper_function (cur_block->copy_helper_func_decl, true); */
19491   /* store_parm_decls (arg_info); */
19492   start_preparsed_function (cur_block->copy_helper_func_decl,
19493                                           /*attrs*/NULL_TREE,
19494                                           SF_PRE_PARSED);
19495
19496   /* Body of the function. */
19497   stmt = begin_compound_stmt (BCS_FN_BODY);
19498   for (chain = block_impl->block_ref_decl_list; chain;
19499         chain = TREE_CHAIN (chain))
19500     /* APPLE LOCAL radar 6214617 */
19501       if (cp_block_requires_copying (TREE_VALUE (chain)))
19502       {
19503         /* APPLE LOCAL begin radar 6175959 */
19504         int flag = 0;
19505         tree p = TREE_VALUE (chain);
19506         tree dst_block_component, src_block_component;
19507         dst_block_component = build_component_ref (build_indirect_ref (dst_arg, "->"),
19508                                                    DECL_NAME (p));
19509         src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19510                                                    DECL_NAME (p));
19511         
19512         if (TREE_CODE (TREE_TYPE (p)) == BLOCK_POINTER_TYPE)
19513         /* _Block_object_assign(&_dest->myImportedBlock, _src->myImportedClosure, BLOCK_FIELD_IS_BLOCK) */
19514                 flag = BLOCK_FIELD_IS_BLOCK;
19515         /* APPLE LOCAL begin radar 6214617 */
19516         else if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (p)) 
19517                          || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
19518         {
19519                 tree call_exp = build_aggr_init (dst_block_component, src_block_component,
19520                                                  LOOKUP_ONLYCONVERTING);
19521                 add_stmt (call_exp);
19522         }
19523         /* APPLE LOCAL end radar 6214617 */
19524         else
19525         /* _Block_object_assign(&_dest->myImportedBlock, _src->myImportedClosure, BLOCK_FIELD_IS_OBJECT) */
19526           flag = BLOCK_FIELD_IS_OBJECT;
19527         if (flag)
19528         {
19529                 tree call_exp;
19530                 dst_block_component = build_fold_addr_expr (dst_block_component);
19531                 call_exp = build_block_object_assign_call_exp (dst_block_component, src_block_component, flag);
19532                 add_stmt (call_exp);
19533         }
19534         /* APPLE LOCAL end radar 6175959 */
19535       }
19536
19537   /* For each __block declared variable used in |...| Must generate call to:
19538      _Block_object_assign(&_dest->myImportedBlock, _src->myImportedBlock, BLOCK_FIELD_IS_BYREF [|BLOCK_FIELD_IS_WEAK])
19539    */
19540   for (chain = block_impl->block_byref_decl_list; chain;
19541           chain = TREE_CHAIN (chain))
19542       if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19543       {
19544         int flag = BLOCK_FIELD_IS_BYREF;
19545         tree call_exp;
19546         tree p = TREE_VALUE (chain);
19547         tree dst_block_component, src_block_component;
19548         dst_block_component = build_component_ref (build_indirect_ref (dst_arg, "->"),
19549                                                    DECL_NAME (p));
19550         src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19551                                                    DECL_NAME (p));
19552         
19553         /* _Block_object_assign(&_dest->myImportedClosure, _src->myImportedClosure, BLOCK_FIELD_IS_BYREF [|BLOCK_FIELD_IS_WEAK]) */
19554         if (COPYABLE_WEAK_BLOCK (p))
19555                 flag |= BLOCK_FIELD_IS_WEAK;
19556         
19557         dst_block_component = build_fold_addr_expr (dst_block_component);
19558         call_exp = build_block_object_assign_call_exp (dst_block_component, src_block_component, flag);
19559         add_stmt (call_exp);
19560       }
19561
19562   finish_compound_stmt (stmt);
19563   /* APPLE LOCAL radar 6169580 */
19564   finish_function (4);
19565   /* Hum, would be nice if someone else did this for us.  */
19566   if (global_bindings_p ())
19567       cgraph_finalize_function (block_impl->copy_helper_func_decl, false);
19568   pop_function_context ();
19569   /* free (arg_info); */
19570 }
19571
19572 static void
19573 synth_destroy_helper_block_func (struct block_sema_info * block_impl)
19574 {
19575   tree stmt, chain;
19576   tree src_arg;
19577   /* struct c_arg_info * arg_info; */
19578   /* Set up: (struct block *_src) parameter. */
19579   src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
19580                                   block_impl->block_arg_ptr_type);
19581   DECL_CONTEXT (src_arg) = cur_block->destroy_helper_func_decl;
19582   TREE_USED (src_arg) = 1;
19583   DECL_ARG_TYPE (src_arg) = block_impl->block_arg_ptr_type;
19584   /* arg_info = xcalloc (1, sizeof (struct c_arg_info)); */
19585
19586   pushdecl (cur_block->destroy_helper_func_decl);
19587   /* arg_info->parms = src_arg; */
19588   /* arg_info->types = tree_cons (NULL_TREE, block_impl->block_arg_ptr_type,
19589    NULL_TREE); */
19590   DECL_ARGUMENTS (cur_block->destroy_helper_func_decl) = src_arg;
19591   
19592   /* function header synthesis. */
19593   push_function_context ();
19594   /* start_block_helper_function (cur_block->destroy_helper_func_decl, true); */
19595   /* store_parm_decls_from (arg_info); */
19596   start_preparsed_function (cur_block->destroy_helper_func_decl,
19597                                           /*attrs*/NULL_TREE,
19598                                           SF_PRE_PARSED);
19599
19600   /* Body of the function. */
19601   stmt = begin_compound_stmt (BCS_FN_BODY);
19602   for (chain = block_impl->block_ref_decl_list; chain;
19603         chain = TREE_CHAIN (chain))
19604     /* APPLE LOCAL begin radar 6214617 */
19605       if (block_requires_copying (TREE_VALUE (chain))
19606         || (TREE_CODE (TREE_TYPE (TREE_VALUE (chain))) == RECORD_TYPE 
19607                 && CLASSTYPE_DESTRUCTORS (TREE_TYPE (TREE_VALUE (chain)))))
19608       /* APPLE LOCAL end radar 6214617 */
19609       {
19610         int flag = 0;
19611         tree rel_exp;
19612         tree p = TREE_VALUE (chain);
19613         tree src_block_component;
19614         src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19615                                                    DECL_NAME (p));
19616         
19617         if (TREE_CODE (TREE_TYPE (p)) == BLOCK_POINTER_TYPE)
19618         /* _Block_object_dispose(_src->imported_object_0, BLOCK_FIELD_IS_BLOCK); */
19619                 flag = BLOCK_FIELD_IS_BLOCK;
19620         /* APPLE LOCAL begin radar 6214617 */
19621         else if (TREE_CODE (TREE_TYPE (p)) == RECORD_TYPE
19622                          && CLASSTYPE_DESTRUCTORS (TREE_TYPE (p)))
19623         {
19624                 tree call_exp = cxx_maybe_build_cleanup (src_block_component);
19625                 gcc_assert (call_exp);
19626                 add_stmt (call_exp);
19627         }
19628         /* APPLE LOCAL end radar 6214617 */
19629         else
19630         /* _Block_object_dispose(_src->imported_object_0, BLOCK_FIELD_IS_OBJECT); */
19631                 flag = BLOCK_FIELD_IS_OBJECT;
19632         if (flag)
19633         {
19634                 rel_exp = build_block_object_dispose_call_exp (src_block_component, flag);
19635                 add_stmt (rel_exp);
19636         }
19637       }
19638
19639   /* For each __block declared variable used in |...| Must generate call to:
19640      _Block_object_dispose(_src->myImportedClosure, BLOCK_FIELD_IS_BYREF[|BLOCK_FIELD_IS_WEAK])
19641    */
19642   for (chain = block_impl->block_byref_decl_list; chain;
19643         chain = TREE_CHAIN (chain))
19644       if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19645       {
19646         tree call_exp;
19647         int flag = BLOCK_FIELD_IS_BYREF;
19648         tree p = TREE_VALUE (chain);
19649         tree src_block_component;
19650         
19651         src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19652                                                    DECL_NAME (p));
19653         if (COPYABLE_WEAK_BLOCK (p))
19654                 flag |= BLOCK_FIELD_IS_WEAK;
19655         /* _Block_object_dispose(_src->myImportedClosure, BLOCK_FIELD_IS_BYREF[|BLOCK_FIELD_IS_WEAK]) */
19656         call_exp = build_block_object_dispose_call_exp (src_block_component, flag);
19657         add_stmt (call_exp);
19658       }
19659
19660   finish_compound_stmt (stmt);
19661   /* APPLE LOCAL radar 6169580 */
19662   finish_function (4);
19663   /* Hum, would be nice if someone else did this for us.  */
19664   if (global_bindings_p ())
19665       cgraph_finalize_function (block_impl->destroy_helper_func_decl, false);
19666   pop_function_context ();
19667 }
19668
19669 /* Parse a block-id.
19670  
19671  GNU Extension:
19672
19673  block-id:
19674  type-specifier-seq block-declarator
19675  
19676  Returns the DECL specified or implied.  */
19677
19678 static tree
19679 cp_parser_block_id (cp_parser* parser)
19680 {
19681   cp_decl_specifier_seq type_specifier_seq;
19682   cp_declarator *declarator;
19683   
19684   /* Parse the type-specifier-seq.  */
19685   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19686                                                   &type_specifier_seq);
19687   if (type_specifier_seq.type == error_mark_node)
19688       return error_mark_node;
19689   
19690   /* Look for the block-declarator.  */
19691   declarator
19692     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_BLOCK, NULL,
19693                              /*parenthesized_p=*/NULL,
19694                              /*member_p=*/false);
19695
19696   return grokblockdecl (&type_specifier_seq, declarator);
19697 }
19698
19699 /* Parse a block-literal-expr.
19700
19701  GNU Extension:
19702
19703  block-literal-expr:
19704  ^ parameter-declation-clause exception-specification [opt] compound-statement
19705  ^ block-id compound-statement
19706
19707  It synthesizes the helper function for later generation and builds
19708  the necessary data to represent the block literal where it is
19709  declared.  */
19710 static tree
19711 cp_parser_block_literal_expr (cp_parser* parser)
19712 {
19713   char name [32];
19714   static int global_unique_count;
19715   int unique_count = ++global_unique_count;
19716   tree block_helper_function_decl;
19717   tree expr, type, arglist = NULL_TREE, ftype;
19718   tree self_arg, stmt;
19719   /* struct c_arg_info *args = NULL; */
19720   cp_parameter_declarator *args = NULL;
19721   tree arg_type = void_list_node;
19722   struct block_sema_info *block_impl;
19723   tree tmp;
19724   tree restype;
19725   tree typelist;
19726   tree helper_function_type;
19727   tree block;
19728   /* APPLE LOCAL radar 6185344 */
19729   tree declared_block_return_type = NULL_TREE;
19730   /* APPLE LOCAL radar 6237713 */
19731   tree attributes = NULL_TREE;
19732   /* APPLE LOCAL radar 6169580 */
19733   int context_is_nonstatic_method;
19734   tree raises = NULL_TREE;
19735
19736   cp_lexer_consume_token (parser->lexer); /* eat '^' */
19737   
19738   /* APPLE LOCAL begin radar 6237713 */
19739   if (cp_lexer_peek_token (parser->lexer)->keyword == RID_ATTRIBUTE)
19740       attributes = cp_parser_attributes_opt (parser);
19741   /* APPLE LOCAL end radar 6237713 */
19742
19743   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19744     {
19745       /* Parse the optional argument list */
19746       cp_lexer_consume_token (parser->lexer);
19747       /* Open the scope to collect parameter decls */
19748       /* push_scope (); */
19749       /* args = c_parser_parms_declarator (parser, true, NULL_TREE); */
19750       /* Parse the parameter-declaration-clause.  */
19751       args = cp_parser_parameter_declaration_clause (parser);
19752       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19753       arg_type = grokparms (args, &arglist);
19754       /* Check for args as it might be NULL due to error. */
19755       if (! args)
19756       {
19757         return error_mark_node;
19758       }
19759       raises = cp_parser_exception_specification_opt (parser);
19760     }
19761   /* APPLE LOCAL begin radar 6185344 */
19762   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19763     {
19764       /* Parse user declared return type. */
19765       tree decl;
19766       
19767       /* APPLE LOCAL begin radar 6237713 */
19768       if (attributes)
19769       {
19770         warning (0, "attributes before block type are ignored");
19771         attributes = NULL_TREE;
19772       }
19773       /* APPLE LOCAL end radar 6237713 */    
19774       
19775       decl = cp_parser_block_id (parser);
19776       
19777       if (decl && decl != error_mark_node)
19778       {
19779         arg_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
19780         arglist = DECL_ARGUMENTS (decl);
19781         raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
19782         declared_block_return_type = TREE_TYPE (TREE_TYPE (decl));
19783       }
19784     }
19785   /* APPLE LOCAL end radar 6185344 */
19786
19787   block = begin_block ();
19788   /* APPLE LOCAL begin radar 6169580 */
19789   context_is_nonstatic_method = (current_function_decl 
19790                                    && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl));
19791   /* APPLE LOCAL end radar 6169580 */
19792   
19793   /* cur_block->arg_info = NULL; */
19794   /* APPLE LOCAL begin radar 6185344 */
19795   if (declared_block_return_type)
19796     {
19797       cur_block->return_type  = TYPE_MAIN_VARIANT (declared_block_return_type);
19798       cur_block->block_has_return_type = true;
19799     }
19800   else
19801       cur_block->return_type = NULL_TREE;
19802   /* APPLE LOCAL end radar 6185344 */
19803   
19804   /* Must also build hidden parameter .block_descriptor added to the helper
19805      function, even though we do not know its type yet. */
19806   /* APPLE LOCAL radar 6404979 */
19807   self_arg = build_artificial_parm (get_identifier (".block_descriptor"), ptr_type_node);
19808   
19809   /* TREE_CHAIN (self_arg) = cur_block->arg_info->parms; */
19810   TREE_CHAIN (self_arg) = arglist;
19811   arg_type = tree_cons (NULL_TREE, ptr_type_node, arg_type);
19812   arglist = self_arg;
19813   
19814   /* APPLE LOCAL begin radar 6185344 */
19815   /* Build the declaration of the helper function (if we do not know its result
19816      type yet, assume it is 'void'. If user provided it, use it).
19817      Treat this as a nested function and use nested function infrastructure for
19818      its generation. */
19819
19820   push_lang_context (lang_name_c);
19821
19822   ftype = build_function_type ((!cur_block->block_has_return_type
19823                                   ? void_type_node : cur_block->return_type),
19824                                  arg_type);
19825   /* APPLE LOCAL end radar 6185344 */
19826   if (raises)
19827       ftype = build_exception_variant (ftype, raises);
19828   /* APPLE LOCAL radar 6160536 */
19829   block_helper_function_decl = build_helper_func_decl (build_block_helper_name (unique_count),
19830                                                          ftype);
19831   DECL_CONTEXT (block_helper_function_decl) = current_function_decl;
19832   cur_block->helper_func_decl = block_helper_function_decl;
19833
19834   DECL_ARGUMENTS (block_helper_function_decl) = arglist;
19835
19836   push_function_context ();
19837   /* start_block_helper_function (cur_block->helper_func_decl, false); */
19838   /* Enter parameter list to the scope of the helper function. */
19839   /* store_parm_decls_from (cur_block->arg_info); */
19840   start_preparsed_function (cur_block->helper_func_decl,
19841                              /*attrs*/NULL_TREE,
19842                              SF_PRE_PARSED);
19843   /* APPLE LOCAL begin radar 6237713 */
19844   if (cp_lexer_peek_token (parser->lexer)->keyword == RID_ATTRIBUTE)
19845       attributes = cp_parser_attributes_opt (parser);
19846   /* APPLE LOCAL radar 6246527 */
19847   any_recognized_block_attribute (attributes);
19848   decl_attributes (&cur_block->helper_func_decl, attributes, 0);
19849   /* APPLE LOCAL end radar 6237713 */  
19850   
19851   /* Start parsing body or expression part of the block literal. */
19852   {
19853       unsigned save = parser->in_statement;
19854       /* Indicate no valid break/continue context.  We'll notice and
19855         emit the proper error message in c_finish_bc_stmt.  */
19856       parser->in_statement = 0;
19857       stmt = begin_compound_stmt (BCS_FN_BODY);
19858       /* Set block's scope to the scope of the helper function's main body.
19859         This is primarily used when nested blocks are declared. */
19860       cur_block->cp_the_scope = current_binding_level;
19861       /* APPLE LOCAL begin radar 6169580 */
19862       if (context_is_nonstatic_method)
19863       {
19864         tree this_decl = lookup_name (this_identifier);
19865         gcc_assert (this_decl);
19866         build_block_ref_decl (this_identifier, this_decl);
19867       }
19868       /* APPLE LOCAL end radar 6169580 */
19869       cp_parser_compound_statement (parser, NULL, false, false);
19870       parser->in_statement = save;
19871   }
19872   
19873   cur_block->block_arg_ptr_type =
19874     build_pointer_type (build_block_struct_type (cur_block));
19875
19876   restype = !cur_block->return_type ? void_type_node
19877   : cur_block->return_type;
19878   if (restype == error_mark_node)
19879       return clean_and_exit (block);
19880
19881   /* Now that we know type of the hidden .block_descriptor argument, fix its type. */
19882   TREE_TYPE (self_arg) = cur_block->block_arg_ptr_type;
19883   DECL_ARG_TYPE (self_arg) = cur_block->block_arg_ptr_type;
19884   
19885   /* The DECL_RESULT should already have the correct type by now.  */
19886   gcc_assert (TREE_TYPE (DECL_RESULT (current_function_decl))
19887                 == restype);
19888   
19889   cur_block->block_body = stmt;
19890   block_build_prologue (cur_block);
19891   
19892   finish_compound_stmt (stmt);
19893   /* add_stmt (fnbody); */
19894
19895   /* We are done parsing of the block body. Return type of block is now known.
19896      We also know all we need to know about the helper function. So, fix its
19897    type here. */
19898   /* We moved this here because for global blocks, helper function body is
19899      not nested and is gimplified in call to finish_function() and return type 
19900      of the function must be correct. */
19901   ftype = build_function_type (restype, TREE_CHAIN (arg_type));
19902   if (raises)
19903       ftype = build_exception_variant (ftype, raises);
19904   /* Declare helper function; as in:
19905      double helper_1(struct block_1 *ii, int z); */
19906   typelist = TYPE_ARG_TYPES (ftype);
19907   /* (struct block_1 *ii, int z, ...) */
19908   typelist = tree_cons (NULL_TREE, cur_block->block_arg_ptr_type,
19909                                   typelist);
19910   helper_function_type = build_function_type (TREE_TYPE (ftype), typelist);
19911   if (raises)
19912       helper_function_type = build_exception_variant (helper_function_type, raises);
19913   TREE_TYPE (cur_block->helper_func_decl) = helper_function_type;
19914   finish_function (4);
19915   pop_function_context ();
19916   /* Hum, would be nice if someone else did this for us.  */
19917   if (global_bindings_p ())
19918       cgraph_finalize_function (cur_block->helper_func_decl, false);
19919   pop_lang_context ();
19920   
19921   /* Build the declaration for copy_helper_block and destroy_helper_block
19922    helper functions for later use. */
19923   
19924   if (cur_block->BlockHasCopyDispose)
19925   {
19926       tree s_ftype;
19927       
19928       push_lang_context (lang_name_c);
19929       /* void copy_helper_block (struct block*, struct block *); */
19930       s_ftype = build_function_type (void_type_node,
19931                                       tree_cons (NULL_TREE, cur_block->block_arg_ptr_type,
19932                                                  tree_cons (NULL_TREE,
19933                                                             cur_block->block_arg_ptr_type,
19934                                                             void_list_node)));
19935       sprintf (name, "__copy_helper_block_%d", unique_count);
19936       cur_block->copy_helper_func_decl =
19937       build_helper_func_decl (get_identifier (name), s_ftype);
19938       DECL_CONTEXT (cur_block->copy_helper_func_decl) = current_function_decl;
19939       synth_copy_helper_block_func (cur_block);
19940       
19941       /* void destroy_helper_block (struct block*); */
19942       s_ftype = build_function_type (void_type_node,
19943                                            tree_cons (NULL_TREE,
19944                                                       cur_block->block_arg_ptr_type, void_list_node));
19945       sprintf (name, "__destroy_helper_block_%d", unique_count);
19946       cur_block->destroy_helper_func_decl =
19947       build_helper_func_decl (get_identifier (name), s_ftype);
19948       DECL_CONTEXT (cur_block->destroy_helper_func_decl) = current_function_decl;
19949       synth_destroy_helper_block_func (cur_block);
19950       pop_lang_context ();
19951   }
19952
19953   block_impl = finish_block (block);
19954   
19955   /* Build unqiue name of the temporary used in code gen. */
19956   sprintf (name, "__block_holder_tmp_%d", unique_count);
19957   tmp = build_block_literal_tmp (name, block_impl);
19958   tmp = build_fold_addr_expr (tmp);
19959   type = build_block_pointer_type (ftype);
19960   expr = convert (type, convert (ptr_type_node, tmp));
19961   free (block_impl);
19962   return expr;
19963 }
19964 /* APPLE LOCAL end blocks 6040305 (ce) */
19965
19966 /* APPLE LOCAL begin blocks 6040305 (ch) */
19967 /* build_byref_local_var_access - converts EXPR to:
19968  EXPR.__forwarding-><decl-name>.
19969  */
19970 tree
19971 build_byref_local_var_access (tree expr, tree decl_name)
19972 {
19973   tree exp = build_component_ref (expr, get_identifier ("__forwarding"));
19974   exp = build_indirect_ref (exp, "unary *");
19975   exp = build_component_ref (exp, decl_name);
19976   return exp;
19977 }
19978
19979 #define BINDING_VALUE(b) ((b)->value)
19980
19981 /**
19982  build_block_byref_decl - This routine inserts a variable declared as a
19983  'byref' variable using the |...| syntax in helper function's outer-most scope.
19984  */
19985 tree
19986 build_block_byref_decl (tree name, tree decl, tree exp)
19987 {
19988   tree ptr_type, byref_decl;
19989   /* APPLE LOCAL begin radar 6225809 */
19990   if (cur_block->prev_block_info) {
19991       /* Traverse enclosing blocks. Insert a __block variable in
19992         each enclosing block which has no declaration of this
19993         variable. This is to ensure that the current (inner) block
19994         gets the __block version of the variable; */
19995       struct block_sema_info *cb = cur_block->prev_block_info;
19996       while (cb) {
19997         struct cxx_binding *b = I_SYMBOL_BINDING (name);
19998         gcc_assert (b);
19999         gcc_assert (BINDING_VALUE (b));
20000         gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20001                                 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20002         /* Find the first declaration not in current block. */
20003         while (b && BINDING_VALUE (b)
20004                    && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20005                            || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20006                    && DECL_CONTEXT (BINDING_VALUE (b)) == cur_block->helper_func_decl)
20007         {
20008                 /* FIXME: This can't happen?!  */
20009                 abort ();
20010                 /* b = b->previous; */
20011         }
20012
20013         gcc_assert (b);
20014         gcc_assert (BINDING_VALUE (b));
20015         gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20016                                 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20017
20018         /* Is the next declaration not in the enclosing block? */
20019         if (b && BINDING_VALUE (b)
20020                 && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20021                         || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20022                 && DECL_CONTEXT (BINDING_VALUE (b)) != cb->helper_func_decl)
20023         {
20024                 /* No declaration of variable seen in the block. Must insert one. */
20025                 /* FIXME: does this push enough?  scope?  */
20026                 struct cp_binding_level *save_scope = current_binding_level;
20027                 struct block_sema_info *save_current_block = cur_block;
20028                 tree save_current_function_decl = current_function_decl;
20029                 current_binding_level = cb->cp_the_scope;
20030                 cur_block = cb;
20031                 current_function_decl = cb->helper_func_decl;
20032                 decl = build_block_byref_decl (name, decl, exp);
20033                 cur_block = save_current_block;
20034                 current_binding_level = save_scope;
20035                 current_function_decl = save_current_function_decl;
20036         }
20037         cb = cb->prev_block_info; 
20038       }
20039   }
20040   /* APPLE LOCAL end radar 6225809 */
20041
20042   /* If it is already a byref declaration, do not add the pointer type
20043      because such declarations already have the pointer type
20044      added. This happens when we have two nested byref declarations in
20045      nested blocks. */
20046   ptr_type = (TREE_CODE (decl) == VAR_DECL && BLOCK_DECL_BYREF (decl))
20047   ? TREE_TYPE (decl) : build_pointer_type (TREE_TYPE (decl));
20048   byref_decl = build_decl (VAR_DECL, name, ptr_type);
20049   DECL_CONTEXT (byref_decl) = current_function_decl;
20050   BLOCK_DECL_BYREF (byref_decl) = 1;
20051
20052   if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
20053     {
20054       COPYABLE_BYREF_LOCAL_VAR (byref_decl) = 1;
20055       COPYABLE_BYREF_LOCAL_NONPOD (byref_decl) = COPYABLE_BYREF_LOCAL_NONPOD (decl);
20056       /* APPLE LOCAL radar 5847976 */
20057       COPYABLE_WEAK_BLOCK (byref_decl) = COPYABLE_WEAK_BLOCK (decl);
20058     }
20059
20060   /* Current scope must be that of the main function body. */
20061   /* FIXME gcc_assert (current_scope->function_body);*/
20062   pushdecl (byref_decl);
20063   mark_used (byref_decl);
20064   /* APPLE LOCAL begin radar 6083129 -  byref escapes (cp) */
20065   /* FIXME: finish this off, ensure the decl is scoped appropriately
20066      for when we want the cleanup to run.  */
20067   /* APPLE LOCAL end radar 6083129 -  byref escapes (cp) */
20068   cur_block->block_byref_decl_list =
20069     tree_cons (NULL_TREE, byref_decl, cur_block->block_byref_decl_list);
20070   /* APPLE LOCAL radar 5847213 */
20071   /* build of block_original_byref_decl_list us removed. */
20072   /* APPLE LOCAL begin radar 6144664  */
20073   DECL_SOURCE_LOCATION (byref_decl)
20074     = DECL_SOURCE_LOCATION (cur_block->helper_func_decl);
20075   /* APPLE LOCAL end radar 6144664  */
20076   return byref_decl;
20077 }
20078
20079 /**
20080  build_block_ref_decl - This routine inserts a copied-in variable (a variable
20081  referenced in the block but whose scope is outside the block) in helper
20082  function's outer-most scope. It also sets its type to 'const' as such
20083  variables are read-only.
20084  */
20085 tree
20086 build_block_ref_decl (tree name, tree decl)
20087 {
20088   /* FIXME - Broken, should be found via objc runtime testcases.  */
20089   /* FIXME - Don't use DECL_CONTEXT on any helpers */
20090   tree ref_decl;
20091   /* APPLE LOCAL radar 6212722 */
20092   tree type, exp;
20093   /* 'decl' was previously declared as __block.  Simply, copy the value
20094      embedded in the above variable. */
20095   if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
20096       decl = build_byref_local_var_access (decl, DECL_NAME (decl));
20097   else {
20098       if (cur_block->prev_block_info) {
20099         /* Traverse enclosing blocks. Insert a copied-in variable in
20100          each enclosing block which has no declaration of this
20101          variable. This is to ensure that the current (inner) block
20102          has the 'frozen' value of the copied-in variable; which means
20103          the value of the copied in variable is at the point of the
20104          block declaration and *not* when the inner block is
20105          invoked.  */
20106         struct block_sema_info *cb = cur_block->prev_block_info;
20107         while (cb) {
20108                 struct cxx_binding *b = I_SYMBOL_BINDING (name);
20109                 gcc_assert (b);
20110                 gcc_assert (BINDING_VALUE (b));
20111                 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20112                                         || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20113                 /* Find the first declaration not in current block. */
20114                 while (b && BINDING_VALUE (b)
20115                            && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20116                                    || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20117                            && DECL_CONTEXT (BINDING_VALUE (b)) == cur_block->helper_func_decl)
20118                 {
20119                         /* FIXME: This can't happen?!  */
20120                         abort ();
20121                         /* b = b->previous; */
20122                 }
20123
20124                 gcc_assert (b);
20125                 gcc_assert (BINDING_VALUE (b));
20126                 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20127                                         || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20128
20129                 /* Is the next declaration not in the enclosing block? */
20130                 if (b && BINDING_VALUE (b)
20131                         && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL 
20132                                 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20133                         && DECL_CONTEXT (BINDING_VALUE (b)) != cb->helper_func_decl)
20134                 {
20135                         /* No declaration of variable seen in the block. Must
20136                          insert one, so it 'freezes' the variable in this
20137                          block. */
20138                         /* FIXME: does this push enough?  scope?  */
20139                         struct cp_binding_level *save_scope = current_binding_level;
20140                         struct block_sema_info *save_current_block = cur_block;
20141                         tree save_current_function_decl = current_function_decl;
20142                         current_binding_level = cb->cp_the_scope;
20143                         cur_block = cb;
20144                         current_function_decl = cb->helper_func_decl;
20145                         decl = build_block_ref_decl (name, decl);
20146                         cur_block = save_current_block;
20147                         current_binding_level = save_scope;
20148                         current_function_decl = save_current_function_decl;
20149                 }
20150                 cb = cb->prev_block_info; 
20151         }
20152       }
20153   }
20154   /* APPLE LOCAL begin radar 6212722 */
20155   exp = decl;
20156   type = TREE_TYPE (exp);
20157   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE) {
20158       exp = decay_conversion (exp);
20159       type = TREE_TYPE (exp);
20160   }
20161   ref_decl = build_decl (VAR_DECL, name,
20162                           build_qualified_type (type, TYPE_QUAL_CONST));
20163   /* APPLE LOCAL end radar 6212722 */
20164   /* APPLE LOCAL begin radar 6144664  */
20165   DECL_SOURCE_LOCATION (ref_decl) = DECL_SOURCE_LOCATION 
20166   (cur_block->helper_func_decl);
20167   /* APPLE LOCAL end radar 6144664  */
20168   DECL_CONTEXT (ref_decl) = current_function_decl;
20169   DECL_INITIAL (ref_decl) = error_mark_node;
20170   c_apply_type_quals_to_decl (TYPE_QUAL_CONST, ref_decl);
20171   BLOCK_DECL_COPIED (ref_decl) = 1;
20172
20173   /* Find the scope for function body (outer-most scope) and insert
20174      this variable in that scope. This is to avoid duplicate
20175      declaration of the save variable. */
20176   {
20177       struct cp_binding_level *b = current_binding_level;
20178       while (b->level_chain->kind != sk_function_parms)
20179         b = b->level_chain;
20180       pushdecl_with_scope (ref_decl, b, /*is_friend=*/false);
20181       /* APPLE LOCAL radar 6169527 */
20182       add_decl_expr (ref_decl);
20183   }
20184   cur_block->block_ref_decl_list =
20185     tree_cons (NULL_TREE, ref_decl, cur_block->block_ref_decl_list);
20186   cur_block->block_original_ref_decl_list =
20187     /* APPLE LOCAL radar 6212722 */
20188     tree_cons (NULL_TREE, exp, cur_block->block_original_ref_decl_list);
20189   return ref_decl;
20190 }
20191
20192 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
20193 static GTY (())  tree descriptor_ptr_type;
20194 static GTY (())  tree descriptor_ptr_type_with_copydispose;
20195 /** build_block_descriptor_type - This routine builds following internal type:
20196  struct __block_descriptor {
20197  unsigned long int reserved;     // NULL
20198  unsigned long int Size;  // sizeof(struct Block_literal_1)
20199
20200  // optional helper functions
20201  void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
20202  void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
20203  } *descriptor_ptr_type;
20204
20205  Objects of this type will always be static. This is one main component of abi change.
20206  */
20207 tree
20208 build_block_descriptor_type (bool withCopyDispose)
20209 {
20210   tree field_decl_chain = NULL_TREE, field_decl;
20211   tree main_type;
20212
20213   if (withCopyDispose && descriptor_ptr_type_with_copydispose)
20214       return descriptor_ptr_type_with_copydispose;
20215   if (!withCopyDispose && descriptor_ptr_type)
20216       return descriptor_ptr_type;
20217
20218   main_type = make_aggr_type (RECORD_TYPE);
20219   xref_basetypes (main_type, NULL_TREE);
20220
20221   /* unsigned long int reserved; */
20222   field_decl = build_decl (FIELD_DECL, get_identifier ("reserved"), long_unsigned_type_node);
20223   TREE_CHAIN (field_decl) = field_decl_chain;
20224   field_decl_chain = field_decl;
20225
20226   /* unsigned long int Size; */
20227   field_decl = build_decl (FIELD_DECL, get_identifier ("Size"), long_unsigned_type_node);
20228   TREE_CHAIN (field_decl) = field_decl_chain;
20229   field_decl_chain = field_decl;
20230
20231   if (withCopyDispose)
20232   {
20233       /* void *CopyFuncPtr; */
20234       field_decl = build_decl (FIELD_DECL, get_identifier ("CopyFuncPtr"), ptr_type_node);
20235       TREE_CHAIN (field_decl) = field_decl_chain;
20236       field_decl_chain = field_decl;
20237       /* void *DestroyFuncPtr; */
20238       field_decl = build_decl (FIELD_DECL, get_identifier ("DestroyFuncPtr"), ptr_type_node);
20239       TREE_CHAIN (field_decl) = field_decl_chain;
20240       field_decl_chain = field_decl;
20241   }
20242
20243   /* Mark this struct as being a block struct rather than a 'normal'
20244    struct.  */
20245   TYPE_BLOCK_IMPL_STRUCT (main_type) = 1;
20246   if (withCopyDispose)
20247       finish_builtin_struct (main_type, "__block_descriptor_withcopydispose", field_decl_chain, NULL_TREE);
20248   else
20249       finish_builtin_struct (main_type, "__block_descriptor", field_decl_chain, NULL_TREE);
20250   CLASSTYPE_AS_BASE (main_type) = main_type;
20251
20252   main_type = build_pointer_type (main_type);
20253   if (withCopyDispose)
20254       descriptor_ptr_type_with_copydispose = main_type;
20255   else
20256       descriptor_ptr_type = main_type;
20257   return main_type;
20258 }
20259 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
20260
20261 cp_declarator *
20262 make_block_pointer_declarator (tree attributes,
20263                                 cp_cv_quals quals,
20264                                 cp_declarator *target)
20265 {
20266   struct cp_declarator *itarget = target;
20267   struct cp_declarator *ret = make_declarator (cdk_block_pointer);
20268
20269   /* APPLE LOCAL radar 5847213 */
20270   /* code removed */
20271
20272
20273   ret->attributes = attributes;
20274   ret->declarator = itarget;
20275   ret->u.block_pointer.qualifiers = quals;
20276   return ret;
20277 }
20278
20279 /* This routine returns 'true' if 'name' has a declaration inside the
20280  current block, 'false' otherwise.  If 'name' has no declaration in
20281  the current block, it returns in DECL the user declaration for
20282  'name' found in the enclosing scope.  Note that if it is declared
20283  in current declaration, it can be either a user declaration or a
20284  byref/copied-in declaration added in current block's scope by the
20285  compiler.  */
20286 bool
20287 lookup_name_in_block (tree name, tree *decl)
20288 {
20289   /* FIXME - Broken, should be found via objc runtime testcases.  */
20290   /* FIXME - Don't use DECL_CONTEXT on any helpers */
20291   cxx_binding *b = I_SYMBOL_BINDING (name);
20292   if (b && b->declared_in_block
20293       && DECL_CONTEXT (BINDING_VALUE (b)) == current_function_decl)
20294       return true;
20295
20296   /* Check for variables only, as we may have parameters, such as
20297      'self' */
20298   /* Note that if a copied-in variable (BLOCK_DECL_COPIED) in the
20299      enclosing block is found, it must be returned as this is
20300      where the variable in current (nested block) will have to get
20301      its value. */
20302   while (b
20303           && TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20304           && (BLOCK_DECL_BYREF (BINDING_VALUE (b))))
20305       b = b->previous;
20306   if (b)
20307       *decl = BINDING_VALUE (b);
20308   return false;
20309 }
20310
20311 /**
20312  build_helper_func_decl - This routine builds a FUNCTION_DECL for
20313  a block helper function.
20314  */
20315 tree
20316 build_helper_func_decl (tree ident, tree type)
20317 {
20318   tree func_decl = build_decl (FUNCTION_DECL, ident, type);
20319   DECL_EXTERNAL (func_decl) = 0;
20320   TREE_PUBLIC (func_decl) = 0;
20321   TREE_USED (func_decl) = 1;
20322   TREE_NOTHROW (func_decl) = 0;
20323   /* APPLE LOCAL radar 6172148 */
20324   BLOCK_SYNTHESIZED_FUNC (func_decl) = 1;
20325   retrofit_lang_decl (func_decl);
20326   if (current_function_decl)
20327       DECL_NO_STATIC_CHAIN (current_function_decl) = 0;
20328   return func_decl;
20329 }
20330
20331 /**
20332  declare_block_prologue_local_vars - utility routine to do the actual
20333  declaration and initialization for each referecned block variable.
20334  */
20335 /* APPLE LOCAL begin radar 6169527 */
20336 /* This routine is mostly rewritten for c++ because initialization of variables
20337  may involve copy construction. */
20338 static void
20339 declare_block_prologue_local_vars (tree self_parm, tree component,
20340                                     tree stmt)
20341 {
20342   tree decl, block_component;
20343   tree_stmt_iterator i;
20344   tree initialization_stmt;
20345   /* APPLE LOCAL radar 6163705  */
20346   int save_line = LOCATION_LINE (input_location);
20347   
20348   decl = component;
20349   block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
20350                                           DECL_NAME (component));
20351   gcc_assert (block_component);
20352   /* APPLE LOCAL radar 6163705  */
20353   LOCATION_LINE (input_location) = DECL_SOURCE_LINE (decl) - 1;
20354   DECL_EXTERNAL (decl) = 0;
20355   TREE_STATIC (decl) = 0;
20356   TREE_USED (decl) = 1;
20357   DECL_CONTEXT (decl) = current_function_decl;
20358   DECL_ARTIFICIAL (decl) = 1;
20359   initialization_stmt = push_stmt_list();
20360   cp_finish_decl (decl, block_component, 0, 0, LOOKUP_ONLYCONVERTING);
20361   initialization_stmt = pop_stmt_list (initialization_stmt);
20362   /* APPLE LOCAL radar 6163705  */
20363   LOCATION_LINE (input_location) = save_line;
20364   /* Prepend a initialization_stmt statement to the statement list. */
20365   i = tsi_start (stmt);
20366   tsi_link_before (&i, initialization_stmt, TSI_SAME_STMT);
20367 }
20368
20369 /**
20370  declare_block_prologue_local_byref_vars - utility routine to do the actual
20371  declaration and initialization for each __block referenced block variable.
20372  */
20373 static void
20374 declare_block_prologue_local_byref_vars (tree self_parm, tree component,
20375                                           tree stmt)
20376 {
20377   tree decl, block_component;
20378   tree_stmt_iterator i;
20379   tree decl_stmt;
20380   
20381   decl = component;
20382   block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
20383                                           DECL_NAME (component));
20384   gcc_assert (block_component);
20385   DECL_EXTERNAL (decl) = 0;
20386   TREE_STATIC (decl) = 0;
20387   TREE_USED (decl) = 1;
20388   DECL_CONTEXT (decl) = current_function_decl;
20389   DECL_ARTIFICIAL (decl) = 1;
20390   DECL_INITIAL (decl) = block_component;
20391   /* Prepend a DECL_EXPR statement to the statement list. */
20392   i = tsi_start (stmt);
20393   decl_stmt = build_stmt (DECL_EXPR, decl);
20394   SET_EXPR_LOCATION (decl_stmt, DECL_SOURCE_LOCATION (decl));
20395   /* APPLE LOCAL begin radar 6163705, Blocks prologues  */
20396   /* Give the prologue statements a line number of one before the beginning of
20397      the function, to make them easily identifiable later.  */
20398   EXPR_LINENO (decl_stmt) =  DECL_SOURCE_LINE (decl) - 1;
20399   /* APPLE LOCAL end radar 6163705, Blocks prologues  */
20400   decl_stmt = build3 (BIND_EXPR, void_type_node, decl, decl_stmt, NULL);
20401   TREE_SIDE_EFFECTS (decl_stmt) = 1;
20402
20403   tsi_link_before (&i, decl_stmt, TSI_SAME_STMT);  
20404 }
20405 /* APPLE LOCAL end radar 6169527 */
20406
20407 /**
20408  block_build_prologue
20409  - This routine builds the declarations for the
20410  variables referenced in the block; as in:
20411  int *y = .block_descriptor->y;
20412  int x = .block_descriptor->x;
20413
20414  The decl_expr declaration for each initialization is enterred at the
20415  beginning of the helper function's statement-list which is passed
20416  in block_impl->block_body.
20417  */
20418 void
20419 block_build_prologue (struct block_sema_info *block_impl)
20420 {
20421   tree chain;
20422   tree self_parm = lookup_name (get_identifier (".block_descriptor"));
20423   gcc_assert (self_parm);
20424   
20425   for (chain = block_impl->block_ref_decl_list; chain;
20426         chain = TREE_CHAIN (chain))
20427       declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
20428                                           block_impl->block_body);
20429   /* APPLE LOCAL begin radar 6169527 */
20430   for (chain = block_impl->block_byref_decl_list; chain;
20431         chain = TREE_CHAIN (chain))
20432       declare_block_prologue_local_byref_vars (self_parm, TREE_VALUE (chain),
20433                                                 block_impl->block_body);
20434   /* APPLE LOCAL end radar 6169527 */
20435 }
20436 /* APPLE LOCAL end blocks 6040305 (ch) */
20437
20438 /* OpenMP 2.5 parsing routines.  */
20439
20440 /* All OpenMP clauses.  OpenMP 2.5.  */
20441 typedef enum pragma_omp_clause {
20442   PRAGMA_OMP_CLAUSE_NONE = 0,
20443
20444   PRAGMA_OMP_CLAUSE_COPYIN,
20445   PRAGMA_OMP_CLAUSE_COPYPRIVATE,
20446   PRAGMA_OMP_CLAUSE_DEFAULT,
20447   PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
20448   PRAGMA_OMP_CLAUSE_IF,
20449   PRAGMA_OMP_CLAUSE_LASTPRIVATE,
20450   PRAGMA_OMP_CLAUSE_NOWAIT,
20451   PRAGMA_OMP_CLAUSE_NUM_THREADS,
20452   PRAGMA_OMP_CLAUSE_ORDERED,
20453   PRAGMA_OMP_CLAUSE_PRIVATE,
20454   PRAGMA_OMP_CLAUSE_REDUCTION,
20455   PRAGMA_OMP_CLAUSE_SCHEDULE,
20456   PRAGMA_OMP_CLAUSE_SHARED
20457 } pragma_omp_clause;
20458
20459 /* Returns name of the next clause.
20460    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20461    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20462    returned and the token is consumed.  */
20463
20464 static pragma_omp_clause
20465 cp_parser_omp_clause_name (cp_parser *parser)
20466 {
20467   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20468
20469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20470     result = PRAGMA_OMP_CLAUSE_IF;
20471   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20472     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20473   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20474     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20475   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20476     {
20477       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20478       const char *p = IDENTIFIER_POINTER (id);
20479
20480       switch (p[0])
20481         {
20482         case 'c':
20483           if (!strcmp ("copyin", p))
20484             result = PRAGMA_OMP_CLAUSE_COPYIN;
20485           else if (!strcmp ("copyprivate", p))
20486             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20487           break;
20488         case 'f':
20489           if (!strcmp ("firstprivate", p))
20490             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20491           break;
20492         case 'l':
20493           if (!strcmp ("lastprivate", p))
20494             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20495           break;
20496         case 'n':
20497           if (!strcmp ("nowait", p))
20498             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20499           else if (!strcmp ("num_threads", p))
20500             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20501           break;
20502         case 'o':
20503           if (!strcmp ("ordered", p))
20504             result = PRAGMA_OMP_CLAUSE_ORDERED;
20505           break;
20506         case 'r':
20507           if (!strcmp ("reduction", p))
20508             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20509           break;
20510         case 's':
20511           if (!strcmp ("schedule", p))
20512             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20513           else if (!strcmp ("shared", p))
20514             result = PRAGMA_OMP_CLAUSE_SHARED;
20515           break;
20516         }
20517     }
20518
20519   if (result != PRAGMA_OMP_CLAUSE_NONE)
20520     cp_lexer_consume_token (parser->lexer);
20521
20522   return result;
20523 }
20524
20525 /* Validate that a clause of the given type does not already exist.  */
20526
20527 static void
20528 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
20529 {
20530   tree c;
20531
20532   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20533     if (OMP_CLAUSE_CODE (c) == code)
20534       {
20535         error ("too many %qs clauses", name);
20536         break;
20537       }
20538 }
20539
20540 /* OpenMP 2.5:
20541    variable-list:
20542      identifier
20543      variable-list , identifier
20544
20545    In addition, we match a closing parenthesis.  An opening parenthesis
20546    will have been consumed by the caller.
20547
20548    If KIND is nonzero, create the appropriate node and install the decl
20549    in OMP_CLAUSE_DECL and add the node to the head of the list.
20550
20551    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20552    return the list created.  */
20553
20554 static tree
20555 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20556                                 tree list)
20557 {
20558   while (1)
20559     {
20560       tree name, decl;
20561
20562       name = cp_parser_id_expression (parser, /*template_p=*/false,
20563                                       /*check_dependency_p=*/true,
20564                                       /*template_p=*/NULL,
20565                                       /*declarator_p=*/false,
20566                                       /*optional_p=*/false);
20567       if (name == error_mark_node)
20568         goto skip_comma;
20569
20570       decl = cp_parser_lookup_name_simple (parser, name);
20571       if (decl == error_mark_node)
20572         cp_parser_name_lookup_error (parser, name, decl, NULL);
20573       else if (kind != 0)
20574         {
20575           tree u = build_omp_clause (kind);
20576           OMP_CLAUSE_DECL (u) = decl;
20577           OMP_CLAUSE_CHAIN (u) = list;
20578           list = u;
20579         }
20580       else
20581         list = tree_cons (decl, NULL_TREE, list);
20582
20583     get_comma:
20584       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20585         break;
20586       cp_lexer_consume_token (parser->lexer);
20587     }
20588
20589   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20590     {
20591       int ending;
20592
20593       /* Try to resync to an unnested comma.  Copied from
20594          cp_parser_parenthesized_expression_list.  */
20595     skip_comma:
20596       ending = cp_parser_skip_to_closing_parenthesis (parser,
20597                                                       /*recovering=*/true,
20598                                                       /*or_comma=*/true,
20599                                                       /*consume_paren=*/true);
20600       if (ending < 0)
20601         goto get_comma;
20602     }
20603
20604   return list;
20605 }
20606
20607 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20608    common case for omp clauses.  */
20609
20610 static tree
20611 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20612 {
20613   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20614     return cp_parser_omp_var_list_no_open (parser, kind, list);
20615   return list;
20616 }
20617
20618 /* OpenMP 2.5:
20619    default ( shared | none ) */
20620
20621 static tree
20622 cp_parser_omp_clause_default (cp_parser *parser, tree list)
20623 {
20624   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20625   tree c;
20626
20627   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20628     return list;
20629   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20630     {
20631       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20632       const char *p = IDENTIFIER_POINTER (id);
20633
20634       switch (p[0])
20635         {
20636         case 'n':
20637           if (strcmp ("none", p) != 0)
20638             goto invalid_kind;
20639           kind = OMP_CLAUSE_DEFAULT_NONE;
20640           break;
20641
20642         case 's':
20643           if (strcmp ("shared", p) != 0)
20644             goto invalid_kind;
20645           kind = OMP_CLAUSE_DEFAULT_SHARED;
20646           break;
20647
20648         default:
20649           goto invalid_kind;
20650         }
20651
20652       cp_lexer_consume_token (parser->lexer);
20653     }
20654   else
20655     {
20656     invalid_kind:
20657       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20658     }
20659
20660   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20661     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20662                                            /*or_comma=*/false,
20663                                            /*consume_paren=*/true);
20664
20665   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20666     return list;
20667
20668   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
20669   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20670   OMP_CLAUSE_CHAIN (c) = list;
20671   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20672
20673   return c;
20674 }
20675
20676 /* OpenMP 2.5:
20677    if ( expression ) */
20678
20679 static tree
20680 cp_parser_omp_clause_if (cp_parser *parser, tree list)
20681 {
20682   tree t, c;
20683
20684   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20685     return list;
20686
20687   t = cp_parser_condition (parser);
20688
20689   if (t == error_mark_node
20690       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20691     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20692                                            /*or_comma=*/false,
20693                                            /*consume_paren=*/true);
20694
20695   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
20696
20697   c = build_omp_clause (OMP_CLAUSE_IF);
20698   OMP_CLAUSE_IF_EXPR (c) = t;
20699   OMP_CLAUSE_CHAIN (c) = list;
20700
20701   return c;
20702 }
20703
20704 /* OpenMP 2.5:
20705    nowait */
20706
20707 static tree
20708 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
20709 {
20710   tree c;
20711
20712   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
20713
20714   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20715   OMP_CLAUSE_CHAIN (c) = list;
20716   return c;
20717 }
20718
20719 /* OpenMP 2.5:
20720    num_threads ( expression ) */
20721
20722 static tree
20723 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
20724 {
20725   tree t, c;
20726
20727   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20728     return list;
20729
20730   t = cp_parser_expression (parser, false);
20731
20732   if (t == error_mark_node
20733       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20734     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20735                                            /*or_comma=*/false,
20736                                            /*consume_paren=*/true);
20737
20738   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
20739
20740   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20741   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20742   OMP_CLAUSE_CHAIN (c) = list;
20743
20744   return c;
20745 }
20746
20747 /* OpenMP 2.5:
20748    ordered */
20749
20750 static tree
20751 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
20752 {
20753   tree c;
20754
20755   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
20756
20757   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20758   OMP_CLAUSE_CHAIN (c) = list;
20759   return c;
20760 }
20761
20762 /* OpenMP 2.5:
20763    reduction ( reduction-operator : variable-list )
20764
20765    reduction-operator:
20766      One of: + * - & ^ | && || */
20767
20768 static tree
20769 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20770 {
20771   enum tree_code code;
20772   tree nlist, c;
20773
20774   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20775     return list;
20776
20777   switch (cp_lexer_peek_token (parser->lexer)->type)
20778     {
20779     case CPP_PLUS:
20780       code = PLUS_EXPR;
20781       break;
20782     case CPP_MULT:
20783       code = MULT_EXPR;
20784       break;
20785     case CPP_MINUS:
20786       code = MINUS_EXPR;
20787       break;
20788     case CPP_AND:
20789       code = BIT_AND_EXPR;
20790       break;
20791     case CPP_XOR:
20792       code = BIT_XOR_EXPR;
20793       break;
20794     case CPP_OR:
20795       code = BIT_IOR_EXPR;
20796       break;
20797     case CPP_AND_AND:
20798       code = TRUTH_ANDIF_EXPR;
20799       break;
20800     case CPP_OR_OR:
20801       code = TRUTH_ORIF_EXPR;
20802       break;
20803     default:
20804       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
20805     resync_fail:
20806       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20807                                              /*or_comma=*/false,
20808                                              /*consume_paren=*/true);
20809       return list;
20810     }
20811   cp_lexer_consume_token (parser->lexer);
20812
20813   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
20814     goto resync_fail;
20815
20816   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20817   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20818     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20819
20820   return nlist;
20821 }
20822
20823 /* OpenMP 2.5:
20824    schedule ( schedule-kind )
20825    schedule ( schedule-kind , expression )
20826
20827    schedule-kind:
20828      static | dynamic | guided | runtime  */
20829
20830 static tree
20831 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
20832 {
20833   tree c, t;
20834
20835   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20836     return list;
20837
20838   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20839
20840   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20841     {
20842       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20843       const char *p = IDENTIFIER_POINTER (id);
20844
20845       switch (p[0])
20846         {
20847         case 'd':
20848           if (strcmp ("dynamic", p) != 0)
20849             goto invalid_kind;
20850           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20851           break;
20852
20853         case 'g':
20854           if (strcmp ("guided", p) != 0)
20855             goto invalid_kind;
20856           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20857           break;
20858
20859         case 'r':
20860           if (strcmp ("runtime", p) != 0)
20861             goto invalid_kind;
20862           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20863           break;
20864
20865         default:
20866           goto invalid_kind;
20867         }
20868     }
20869   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20870     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20871   else
20872     goto invalid_kind;
20873   cp_lexer_consume_token (parser->lexer);
20874
20875   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20876     {
20877       cp_lexer_consume_token (parser->lexer);
20878
20879       t = cp_parser_assignment_expression (parser, false);
20880
20881       if (t == error_mark_node)
20882         goto resync_fail;
20883       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20884         error ("schedule %<runtime%> does not take "
20885                "a %<chunk_size%> parameter");
20886       else
20887         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20888
20889       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20890         goto resync_fail;
20891     }
20892   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
20893     goto resync_fail;
20894
20895   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
20896   OMP_CLAUSE_CHAIN (c) = list;
20897   return c;
20898
20899  invalid_kind:
20900   cp_parser_error (parser, "invalid schedule kind");
20901  resync_fail:
20902   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20903                                          /*or_comma=*/false,
20904                                          /*consume_paren=*/true);
20905   return list;
20906 }
20907
20908 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20909    is a bitmask in MASK.  Return the list of clauses found; the result
20910    of clause default goes in *pdefault.  */
20911
20912 static tree
20913 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20914                            const char *where, cp_token *pragma_tok)
20915 {
20916   tree clauses = NULL;
20917
20918   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20919     {
20920       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
20921       const char *c_name;
20922       tree prev = clauses;
20923
20924       switch (c_kind)
20925         {
20926         case PRAGMA_OMP_CLAUSE_COPYIN:
20927           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20928           c_name = "copyin";
20929           break;
20930         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20931           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20932                                             clauses);
20933           c_name = "copyprivate";
20934           break;
20935         case PRAGMA_OMP_CLAUSE_DEFAULT:
20936           clauses = cp_parser_omp_clause_default (parser, clauses);
20937           c_name = "default";
20938           break;
20939         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20940           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20941                                             clauses);
20942           c_name = "firstprivate";
20943           break;
20944         case PRAGMA_OMP_CLAUSE_IF:
20945           clauses = cp_parser_omp_clause_if (parser, clauses);
20946           c_name = "if";
20947           break;
20948         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20949           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20950                                             clauses);
20951           c_name = "lastprivate";
20952           break;
20953         case PRAGMA_OMP_CLAUSE_NOWAIT:
20954           clauses = cp_parser_omp_clause_nowait (parser, clauses);
20955           c_name = "nowait";
20956           break;
20957         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20958           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
20959           c_name = "num_threads";
20960           break;
20961         case PRAGMA_OMP_CLAUSE_ORDERED:
20962           clauses = cp_parser_omp_clause_ordered (parser, clauses);
20963           c_name = "ordered";
20964           break;
20965         case PRAGMA_OMP_CLAUSE_PRIVATE:
20966           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20967                                             clauses);
20968           c_name = "private";
20969           break;
20970         case PRAGMA_OMP_CLAUSE_REDUCTION:
20971           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20972           c_name = "reduction";
20973           break;
20974         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20975           clauses = cp_parser_omp_clause_schedule (parser, clauses);
20976           c_name = "schedule";
20977           break;
20978         case PRAGMA_OMP_CLAUSE_SHARED:
20979           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20980                                             clauses);
20981           c_name = "shared";
20982           break;
20983         default:
20984           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20985           goto saw_error;
20986         }
20987
20988       if (((mask >> c_kind) & 1) == 0)
20989         {
20990           /* Remove the invalid clause(s) from the list to avoid
20991              confusing the rest of the compiler.  */
20992           clauses = prev;
20993           error ("%qs is not valid for %qs", c_name, where);
20994         }
20995     }
20996  saw_error:
20997   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20998   return finish_omp_clauses (clauses);
20999 }
21000
21001 /* OpenMP 2.5:
21002    structured-block:
21003      statement
21004
21005    In practice, we're also interested in adding the statement to an
21006    outer node.  So it is convenient if we work around the fact that
21007    cp_parser_statement calls add_stmt.  */
21008
21009 static unsigned
21010 cp_parser_begin_omp_structured_block (cp_parser *parser)
21011 {
21012   unsigned save = parser->in_statement;
21013
21014   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21015      This preserves the "not within loop or switch" style error messages
21016      for nonsense cases like
21017         void foo() {
21018         #pragma omp single
21019           break;
21020         }
21021   */
21022   if (parser->in_statement)
21023     parser->in_statement = IN_OMP_BLOCK;
21024
21025   return save;
21026 }
21027
21028 static void
21029 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21030 {
21031   parser->in_statement = save;
21032 }
21033
21034 static tree
21035 cp_parser_omp_structured_block (cp_parser *parser)
21036 {
21037   tree stmt = begin_omp_structured_block ();
21038   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21039
21040   cp_parser_statement (parser, NULL_TREE, false, NULL);
21041
21042   cp_parser_end_omp_structured_block (parser, save);
21043   return finish_omp_structured_block (stmt);
21044 }
21045
21046 /* OpenMP 2.5:
21047    # pragma omp atomic new-line
21048      expression-stmt
21049
21050    expression-stmt:
21051      x binop= expr | x++ | ++x | x-- | --x
21052    binop:
21053      +, *, -, /, &, ^, |, <<, >>
21054
21055   where x is an lvalue expression with scalar type.  */
21056
21057 static void
21058 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21059 {
21060   tree lhs, rhs;
21061   enum tree_code code;
21062
21063   cp_parser_require_pragma_eol (parser, pragma_tok);
21064
21065   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21066                                     /*cast_p=*/false);
21067   switch (TREE_CODE (lhs))
21068     {
21069     case ERROR_MARK:
21070       goto saw_error;
21071
21072     case PREINCREMENT_EXPR:
21073     case POSTINCREMENT_EXPR:
21074       lhs = TREE_OPERAND (lhs, 0);
21075       code = PLUS_EXPR;
21076       rhs = integer_one_node;
21077       break;
21078
21079     case PREDECREMENT_EXPR:
21080     case POSTDECREMENT_EXPR:
21081       lhs = TREE_OPERAND (lhs, 0);
21082       code = MINUS_EXPR;
21083       rhs = integer_one_node;
21084       break;
21085
21086     default:
21087       switch (cp_lexer_peek_token (parser->lexer)->type)
21088         {
21089         case CPP_MULT_EQ:
21090           code = MULT_EXPR;
21091           break;
21092         case CPP_DIV_EQ:
21093           code = TRUNC_DIV_EXPR;
21094           break;
21095         case CPP_PLUS_EQ:
21096           code = PLUS_EXPR;
21097           break;
21098         case CPP_MINUS_EQ:
21099           code = MINUS_EXPR;
21100           break;
21101         case CPP_LSHIFT_EQ:
21102           code = LSHIFT_EXPR;
21103           break;
21104         case CPP_RSHIFT_EQ:
21105           code = RSHIFT_EXPR;
21106           break;
21107         case CPP_AND_EQ:
21108           code = BIT_AND_EXPR;
21109           break;
21110         case CPP_OR_EQ:
21111           code = BIT_IOR_EXPR;
21112           break;
21113         case CPP_XOR_EQ:
21114           code = BIT_XOR_EXPR;
21115           break;
21116         default:
21117           cp_parser_error (parser,
21118                            "invalid operator for %<#pragma omp atomic%>");
21119           goto saw_error;
21120         }
21121       cp_lexer_consume_token (parser->lexer);
21122
21123       rhs = cp_parser_expression (parser, false);
21124       if (rhs == error_mark_node)
21125         goto saw_error;
21126       break;
21127     }
21128   finish_omp_atomic (code, lhs, rhs);
21129   cp_parser_consume_semicolon_at_end_of_statement (parser);
21130   return;
21131
21132  saw_error:
21133   cp_parser_skip_to_end_of_block_or_statement (parser);
21134 }
21135
21136
21137 /* OpenMP 2.5:
21138    # pragma omp barrier new-line  */
21139
21140 static void
21141 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21142 {
21143   cp_parser_require_pragma_eol (parser, pragma_tok);
21144   finish_omp_barrier ();
21145 }
21146
21147 /* OpenMP 2.5:
21148    # pragma omp critical [(name)] new-line
21149      structured-block  */
21150
21151 static tree
21152 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21153 {
21154   tree stmt, name = NULL;
21155
21156   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21157     {
21158       cp_lexer_consume_token (parser->lexer);
21159
21160       name = cp_parser_identifier (parser);
21161
21162       if (name == error_mark_node
21163           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
21164         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21165                                                /*or_comma=*/false,
21166                                                /*consume_paren=*/true);
21167       if (name == error_mark_node)
21168         name = NULL;
21169     }
21170   cp_parser_require_pragma_eol (parser, pragma_tok);
21171
21172   stmt = cp_parser_omp_structured_block (parser);
21173   return c_finish_omp_critical (stmt, name);
21174 }
21175
21176
21177 /* OpenMP 2.5:
21178    # pragma omp flush flush-vars[opt] new-line
21179
21180    flush-vars:
21181      ( variable-list ) */
21182
21183 static void
21184 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21185 {
21186   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21187     (void) cp_parser_omp_var_list (parser, 0, NULL);
21188   cp_parser_require_pragma_eol (parser, pragma_tok);
21189
21190   finish_omp_flush ();
21191 }
21192
21193 /* Parse the restricted form of the for statment allowed by OpenMP.  */
21194
21195 static tree
21196 cp_parser_omp_for_loop (cp_parser *parser)
21197 {
21198   tree init, cond, incr, body, decl, pre_body;
21199   location_t loc;
21200
21201   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21202     {
21203       cp_parser_error (parser, "for statement expected");
21204       return NULL;
21205     }
21206   loc = cp_lexer_consume_token (parser->lexer)->location;
21207   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
21208     return NULL;
21209
21210   init = decl = NULL;
21211   pre_body = push_stmt_list ();
21212   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21213     {
21214       cp_decl_specifier_seq type_specifiers;
21215
21216       /* First, try to parse as an initialized declaration.  See
21217          cp_parser_condition, from whence the bulk of this is copied.  */
21218
21219       cp_parser_parse_tentatively (parser);
21220       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21221                                     &type_specifiers);
21222       if (!cp_parser_error_occurred (parser))
21223         {
21224           tree asm_specification, attributes;
21225           cp_declarator *declarator;
21226
21227           declarator = cp_parser_declarator (parser,
21228                                              CP_PARSER_DECLARATOR_NAMED,
21229                                              /*ctor_dtor_or_conv_p=*/NULL,
21230                                              /*parenthesized_p=*/NULL,
21231                                              /*member_p=*/false);
21232           attributes = cp_parser_attributes_opt (parser);
21233           asm_specification = cp_parser_asm_specification_opt (parser);
21234
21235           cp_parser_require (parser, CPP_EQ, "`='");
21236           if (cp_parser_parse_definitely (parser))
21237             {
21238               tree pushed_scope;
21239
21240               decl = start_decl (declarator, &type_specifiers,
21241                                  /*initialized_p=*/false, attributes,
21242                                  /*prefix_attributes=*/NULL_TREE,
21243                                  &pushed_scope);
21244
21245               init = cp_parser_assignment_expression (parser, false);
21246
21247               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
21248                               asm_specification, LOOKUP_ONLYCONVERTING);
21249
21250               if (pushed_scope)
21251                 pop_scope (pushed_scope);
21252             }
21253         }
21254       else
21255         cp_parser_abort_tentative_parse (parser);
21256
21257       /* If parsing as an initialized declaration failed, try again as
21258          a simple expression.  */
21259       if (decl == NULL)
21260         init = cp_parser_expression (parser, false);
21261     }
21262   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
21263   pre_body = pop_stmt_list (pre_body);
21264
21265   cond = NULL;
21266   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21267     cond = cp_parser_condition (parser);
21268   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
21269
21270   incr = NULL;
21271   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21272     incr = cp_parser_expression (parser, false);
21273
21274   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
21275     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21276                                            /*or_comma=*/false,
21277                                            /*consume_paren=*/true);
21278
21279   /* Note that we saved the original contents of this flag when we entered
21280      the structured block, and so we don't need to re-save it here.  */
21281   parser->in_statement = IN_OMP_FOR;
21282
21283   /* Note that the grammar doesn't call for a structured block here,
21284      though the loop as a whole is a structured block.  */
21285   body = push_stmt_list ();
21286   cp_parser_statement (parser, NULL_TREE, false, NULL);
21287   body = pop_stmt_list (body);
21288
21289   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
21290 }
21291
21292 /* OpenMP 2.5:
21293    #pragma omp for for-clause[optseq] new-line
21294      for-loop  */
21295
21296 #define OMP_FOR_CLAUSE_MASK                             \
21297         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21298         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21299         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21300         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21301         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21302         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21303         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21304
21305 static tree
21306 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21307 {
21308   tree clauses, sb, ret;
21309   unsigned int save;
21310
21311   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21312                                        "#pragma omp for", pragma_tok);
21313
21314   sb = begin_omp_structured_block ();
21315   save = cp_parser_begin_omp_structured_block (parser);
21316
21317   ret = cp_parser_omp_for_loop (parser);
21318   if (ret)
21319     OMP_FOR_CLAUSES (ret) = clauses;
21320
21321   cp_parser_end_omp_structured_block (parser, save);
21322   add_stmt (finish_omp_structured_block (sb));
21323
21324   return ret;
21325 }
21326
21327 /* OpenMP 2.5:
21328    # pragma omp master new-line
21329      structured-block  */
21330
21331 static tree
21332 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21333 {
21334   cp_parser_require_pragma_eol (parser, pragma_tok);
21335   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21336 }
21337
21338 /* OpenMP 2.5:
21339    # pragma omp ordered new-line
21340      structured-block  */
21341
21342 static tree
21343 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21344 {
21345   cp_parser_require_pragma_eol (parser, pragma_tok);
21346   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21347 }
21348
21349 /* OpenMP 2.5:
21350
21351    section-scope:
21352      { section-sequence }
21353
21354    section-sequence:
21355      section-directive[opt] structured-block
21356      section-sequence section-directive structured-block  */
21357
21358 static tree
21359 cp_parser_omp_sections_scope (cp_parser *parser)
21360 {
21361   tree stmt, substmt;
21362   bool error_suppress = false;
21363   cp_token *tok;
21364
21365   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
21366     return NULL_TREE;
21367
21368   stmt = push_stmt_list ();
21369
21370   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21371     {
21372       unsigned save;
21373
21374       substmt = begin_omp_structured_block ();
21375       save = cp_parser_begin_omp_structured_block (parser);
21376
21377       while (1)
21378         {
21379           cp_parser_statement (parser, NULL_TREE, false, NULL);
21380
21381           tok = cp_lexer_peek_token (parser->lexer);
21382           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21383             break;
21384           if (tok->type == CPP_CLOSE_BRACE)
21385             break;
21386           if (tok->type == CPP_EOF)
21387             break;
21388         }
21389
21390       cp_parser_end_omp_structured_block (parser, save);
21391       substmt = finish_omp_structured_block (substmt);
21392       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21393       add_stmt (substmt);
21394     }
21395
21396   while (1)
21397     {
21398       tok = cp_lexer_peek_token (parser->lexer);
21399       if (tok->type == CPP_CLOSE_BRACE)
21400         break;
21401       if (tok->type == CPP_EOF)
21402         break;
21403
21404       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21405         {
21406           cp_lexer_consume_token (parser->lexer);
21407           cp_parser_require_pragma_eol (parser, tok);
21408           error_suppress = false;
21409         }
21410       else if (!error_suppress)
21411         {
21412           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21413           error_suppress = true;
21414         }
21415
21416       substmt = cp_parser_omp_structured_block (parser);
21417       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21418       add_stmt (substmt);
21419     }
21420   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21421
21422   substmt = pop_stmt_list (stmt);
21423
21424   stmt = make_node (OMP_SECTIONS);
21425   TREE_TYPE (stmt) = void_type_node;
21426   OMP_SECTIONS_BODY (stmt) = substmt;
21427
21428   add_stmt (stmt);
21429   return stmt;
21430 }
21431
21432 /* OpenMP 2.5:
21433    # pragma omp sections sections-clause[optseq] newline
21434      sections-scope  */
21435
21436 #define OMP_SECTIONS_CLAUSE_MASK                        \
21437         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21438         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21439         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21440         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21441         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21442
21443 static tree
21444 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21445 {
21446   tree clauses, ret;
21447
21448   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21449                                        "#pragma omp sections", pragma_tok);
21450
21451   ret = cp_parser_omp_sections_scope (parser);
21452   if (ret)
21453     OMP_SECTIONS_CLAUSES (ret) = clauses;
21454
21455   return ret;
21456 }
21457
21458 /* OpenMP 2.5:
21459    # pragma parallel parallel-clause new-line
21460    # pragma parallel for parallel-for-clause new-line
21461    # pragma parallel sections parallel-sections-clause new-line  */
21462
21463 #define OMP_PARALLEL_CLAUSE_MASK                        \
21464         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21465         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21466         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21467         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21468         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21469         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21470         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21471         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21472
21473 static tree
21474 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21475 {
21476   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21477   const char *p_name = "#pragma omp parallel";
21478   tree stmt, clauses, par_clause, ws_clause, block;
21479   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21480   unsigned int save;
21481
21482   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21483     {
21484       cp_lexer_consume_token (parser->lexer);
21485       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21486       p_name = "#pragma omp parallel for";
21487       mask |= OMP_FOR_CLAUSE_MASK;
21488       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21489     }
21490   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21491     {
21492       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21493       const char *p = IDENTIFIER_POINTER (id);
21494       if (strcmp (p, "sections") == 0)
21495         {
21496           cp_lexer_consume_token (parser->lexer);
21497           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21498           p_name = "#pragma omp parallel sections";
21499           mask |= OMP_SECTIONS_CLAUSE_MASK;
21500           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21501         }
21502     }
21503
21504   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21505   block = begin_omp_parallel ();
21506   save = cp_parser_begin_omp_structured_block (parser);
21507
21508   switch (p_kind)
21509     {
21510     case PRAGMA_OMP_PARALLEL:
21511       cp_parser_already_scoped_statement (parser);
21512       par_clause = clauses;
21513       break;
21514
21515     case PRAGMA_OMP_PARALLEL_FOR:
21516       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21517       stmt = cp_parser_omp_for_loop (parser);
21518       if (stmt)
21519         OMP_FOR_CLAUSES (stmt) = ws_clause;
21520       break;
21521
21522     case PRAGMA_OMP_PARALLEL_SECTIONS:
21523       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21524       stmt = cp_parser_omp_sections_scope (parser);
21525       if (stmt)
21526         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21527       break;
21528
21529     default:
21530       gcc_unreachable ();
21531     }
21532
21533   cp_parser_end_omp_structured_block (parser, save);
21534   stmt = finish_omp_parallel (par_clause, block);
21535   if (p_kind != PRAGMA_OMP_PARALLEL)
21536     OMP_PARALLEL_COMBINED (stmt) = 1;
21537   return stmt;
21538 }
21539
21540 /* OpenMP 2.5:
21541    # pragma omp single single-clause[optseq] new-line
21542      structured-block  */
21543
21544 #define OMP_SINGLE_CLAUSE_MASK                          \
21545         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21546         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21547         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21548         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21549
21550 static tree
21551 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21552 {
21553   tree stmt = make_node (OMP_SINGLE);
21554   TREE_TYPE (stmt) = void_type_node;
21555
21556   OMP_SINGLE_CLAUSES (stmt)
21557     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21558                                  "#pragma omp single", pragma_tok);
21559   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21560
21561   return add_stmt (stmt);
21562 }
21563
21564 /* OpenMP 2.5:
21565    # pragma omp threadprivate (variable-list) */
21566
21567 static void
21568 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21569 {
21570   tree vars;
21571
21572   vars = cp_parser_omp_var_list (parser, 0, NULL);
21573   cp_parser_require_pragma_eol (parser, pragma_tok);
21574
21575   if (!targetm.have_tls)
21576     sorry ("threadprivate variables not supported in this target");
21577
21578   finish_omp_threadprivate (vars);
21579 }
21580
21581 /* Main entry point to OpenMP statement pragmas.  */
21582
21583 static void
21584 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21585 {
21586   tree stmt;
21587
21588   switch (pragma_tok->pragma_kind)
21589     {
21590     case PRAGMA_OMP_ATOMIC:
21591       cp_parser_omp_atomic (parser, pragma_tok);
21592       return;
21593     case PRAGMA_OMP_CRITICAL:
21594       stmt = cp_parser_omp_critical (parser, pragma_tok);
21595       break;
21596     case PRAGMA_OMP_FOR:
21597       stmt = cp_parser_omp_for (parser, pragma_tok);
21598       break;
21599     case PRAGMA_OMP_MASTER:
21600       stmt = cp_parser_omp_master (parser, pragma_tok);
21601       break;
21602     case PRAGMA_OMP_ORDERED:
21603       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21604       break;
21605     case PRAGMA_OMP_PARALLEL:
21606       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21607       break;
21608     case PRAGMA_OMP_SECTIONS:
21609       stmt = cp_parser_omp_sections (parser, pragma_tok);
21610       break;
21611     case PRAGMA_OMP_SINGLE:
21612       stmt = cp_parser_omp_single (parser, pragma_tok);
21613       break;
21614     default:
21615       gcc_unreachable ();
21616     }
21617
21618   if (stmt)
21619     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21620 }
21621 \f
21622 /* The parser.  */
21623
21624 static GTY (()) cp_parser *the_parser;
21625
21626 \f
21627 /* Special handling for the first token or line in the file.  The first
21628    thing in the file might be #pragma GCC pch_preprocess, which loads a
21629    PCH file, which is a GC collection point.  So we need to handle this
21630    first pragma without benefit of an existing lexer structure.
21631
21632    Always returns one token to the caller in *FIRST_TOKEN.  This is
21633    either the true first token of the file, or the first token after
21634    the initial pragma.  */
21635
21636 static void
21637 cp_parser_initial_pragma (cp_token *first_token)
21638 {
21639   tree name = NULL;
21640
21641   cp_lexer_get_preprocessor_token (NULL, first_token);
21642   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21643     return;
21644
21645   cp_lexer_get_preprocessor_token (NULL, first_token);
21646   if (first_token->type == CPP_STRING)
21647     {
21648       name = first_token->u.value;
21649
21650       cp_lexer_get_preprocessor_token (NULL, first_token);
21651       if (first_token->type != CPP_PRAGMA_EOL)
21652         error ("junk at end of %<#pragma GCC pch_preprocess%>");
21653     }
21654   else
21655     error ("expected string literal");
21656
21657   /* Skip to the end of the pragma.  */
21658   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21659     cp_lexer_get_preprocessor_token (NULL, first_token);
21660
21661   /* Now actually load the PCH file.  */
21662   if (name)
21663     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21664
21665   /* Read one more token to return to our caller.  We have to do this
21666      after reading the PCH file in, since its pointers have to be
21667      live.  */
21668   cp_lexer_get_preprocessor_token (NULL, first_token);
21669 }
21670
21671 /* Normal parsing of a pragma token.  Here we can (and must) use the
21672    regular lexer.  */
21673
21674 static bool
21675 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21676 {
21677   cp_token *pragma_tok;
21678   unsigned int id;
21679
21680   pragma_tok = cp_lexer_consume_token (parser->lexer);
21681   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21682   parser->lexer->in_pragma = true;
21683
21684   id = pragma_tok->pragma_kind;
21685   switch (id)
21686     {
21687     case PRAGMA_GCC_PCH_PREPROCESS:
21688       error ("%<#pragma GCC pch_preprocess%> must be first");
21689       break;
21690
21691     case PRAGMA_OMP_BARRIER:
21692       switch (context)
21693         {
21694         case pragma_compound:
21695           cp_parser_omp_barrier (parser, pragma_tok);
21696           return false;
21697         case pragma_stmt:
21698           error ("%<#pragma omp barrier%> may only be "
21699                  "used in compound statements");
21700           break;
21701         default:
21702           goto bad_stmt;
21703         }
21704       break;
21705
21706     case PRAGMA_OMP_FLUSH:
21707       switch (context)
21708         {
21709         case pragma_compound:
21710           cp_parser_omp_flush (parser, pragma_tok);
21711           return false;
21712         case pragma_stmt:
21713           error ("%<#pragma omp flush%> may only be "
21714                  "used in compound statements");
21715           break;
21716         default:
21717           goto bad_stmt;
21718         }
21719       break;
21720
21721     case PRAGMA_OMP_THREADPRIVATE:
21722       cp_parser_omp_threadprivate (parser, pragma_tok);
21723       return false;
21724
21725     case PRAGMA_OMP_ATOMIC:
21726     case PRAGMA_OMP_CRITICAL:
21727     case PRAGMA_OMP_FOR:
21728     case PRAGMA_OMP_MASTER:
21729     case PRAGMA_OMP_ORDERED:
21730     case PRAGMA_OMP_PARALLEL:
21731     case PRAGMA_OMP_SECTIONS:
21732     case PRAGMA_OMP_SINGLE:
21733       if (context == pragma_external)
21734         goto bad_stmt;
21735       cp_parser_omp_construct (parser, pragma_tok);
21736       return true;
21737
21738     case PRAGMA_OMP_SECTION:
21739       error ("%<#pragma omp section%> may only be used in "
21740              "%<#pragma omp sections%> construct");
21741       break;
21742
21743     default:
21744       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21745       c_invoke_pragma_handler (id);
21746       break;
21747
21748     bad_stmt:
21749       cp_parser_error (parser, "expected declaration specifiers");
21750       break;
21751     }
21752
21753   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21754   return false;
21755 }
21756
21757 /* The interface the pragma parsers have to the lexer.  */
21758
21759 enum cpp_ttype
21760 pragma_lex (tree *value)
21761 {
21762   cp_token *tok;
21763   enum cpp_ttype ret;
21764
21765   tok = cp_lexer_peek_token (the_parser->lexer);
21766
21767   ret = tok->type;
21768   *value = tok->u.value;
21769
21770   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21771     ret = CPP_EOF;
21772   else if (ret == CPP_STRING)
21773     *value = cp_parser_string_literal (the_parser, false, false);
21774   else
21775     {
21776       cp_lexer_consume_token (the_parser->lexer);
21777       if (ret == CPP_KEYWORD)
21778         ret = CPP_NAME;
21779     }
21780
21781   return ret;
21782 }
21783
21784 \f
21785 /* External interface.  */
21786
21787 /* Parse one entire translation unit.  */
21788
21789 void
21790 c_parse_file (void)
21791 {
21792   bool error_occurred;
21793   static bool already_called = false;
21794
21795   if (already_called)
21796     {
21797       sorry ("inter-module optimizations not implemented for C++");
21798       return;
21799     }
21800   already_called = true;
21801
21802   the_parser = cp_parser_new ();
21803   push_deferring_access_checks (flag_access_control
21804                                 ? dk_no_deferred : dk_no_check);
21805   error_occurred = cp_parser_translation_unit (the_parser);
21806   the_parser = NULL;
21807 }
21808
21809 /* This variable must be provided by every front end.  */
21810
21811 int yydebug;
21812
21813 #include "gt-cp-parser.h"