OSDN Git Service

PR c++/22621
[pf3gnuchains/gcc-fork.git] / 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 "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* The value associated with this token, if any.  */
63   tree value;
64   /* The location at which this token was found.  */
65   location_t location;
66 } cp_token;
67
68 /* We use a stack of token pointer for saving token sets.  */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
72
73 static const cp_token eof_token =
74 {
75   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
77   0
78 #else
79   {0, 0}
80 #endif
81 };
82
83 /* The cp_lexer structure represents the C++ lexer.  It is responsible
84    for managing the token stream from the preprocessor and supplying
85    it to the parser.  Tokens are never added to the cp_lexer after
86    it is created.  */
87
88 typedef struct cp_lexer GTY (())
89 {
90   /* The memory allocated for the buffer.  NULL if this lexer does not
91      own the token buffer.  */
92   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93   /* If the lexer owns the buffer, this is the number of tokens in the
94      buffer.  */
95   size_t buffer_length;
96
97   /* A pointer just past the last available token.  The tokens
98      in this lexer are [buffer, last_token).  */
99   cp_token_position GTY ((skip)) last_token;
100
101   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
102      no more available tokens.  */
103   cp_token_position GTY ((skip)) next_token;
104
105   /* A stack indicating positions at which cp_lexer_save_tokens was
106      called.  The top entry is the most recent position at which we
107      began saving tokens.  If the stack is non-empty, we are saving
108      tokens.  */
109   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
110
111   /* True if we should output debugging information.  */
112   bool debugging_p;
113
114   /* The next lexer in a linked list of lexers.  */
115   struct cp_lexer *next;
116 } cp_lexer;
117
118 /* cp_token_cache is a range of tokens.  There is no need to represent
119    allocate heap memory for it, since tokens are never removed from the
120    lexer's array.  There is also no need for the GC to walk through
121    a cp_token_cache, since everything in here is referenced through
122    a lexer.  */
123
124 typedef struct cp_token_cache GTY(())
125 {
126   /* The beginning of the token range.  */
127   cp_token * GTY((skip)) first;
128
129   /* Points immediately after the last token in the range.  */
130   cp_token * GTY ((skip)) last;
131 } cp_token_cache;
132
133 /* Prototypes.  */
134
135 static cp_lexer *cp_lexer_new_main
136   (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138   (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140   (cp_lexer *);
141 static int cp_lexer_saving_tokens
142   (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144   (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146   (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148   (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150   (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152   (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156   (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158   (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160   (cp_lexer *);
161 static void cp_lexer_purge_token
162   (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164   (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166   (cp_lexer *);
167 static void cp_lexer_save_tokens
168   (cp_lexer *);
169 static void cp_lexer_commit_tokens
170   (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172   (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175   (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177   (cp_lexer *);
178 static void cp_lexer_start_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181   (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184    about passing NULL to functions that require non-NULL arguments
185    (fputs, fprintf).  It will never be used, so all we need is a value
186    of the right type that's guaranteed not to be NULL.  */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
191
192 static cp_token_cache *cp_token_cache_new
193   (cp_token *, cp_token *);
194
195 /* Manifest constants.  */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
198
199 /* A token type for keywords, as opposed to ordinary identifiers.  */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202 /* A token type for template-ids.  If a template-id is processed while
203    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204    the value of the CPP_TEMPLATE_ID is whatever was returned by
205    cp_parser_template_id.  */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208 /* A token type for nested-name-specifiers.  If a
209    nested-name-specifier is processed while parsing tentatively, it is
210    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212    cp_parser_nested_name_specifier_opt.  */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215 /* A token type for tokens that are not tokens at all; these are used
216    to represent slots in the array where there used to be a token
217    that has now been deleted.  */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220 /* The number of token types, including C++-specific ones.  */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222
223 /* Variables.  */
224
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written.  */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
229
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231    preprocessor.  */
232
233 static cp_lexer *
234 cp_lexer_new_main (void)
235 {
236   cp_token first_token;
237   cp_lexer *lexer;
238   cp_token *pos;
239   size_t alloc;
240   size_t space;
241   cp_token *buffer;
242
243   /* It's possible that lexing the first token will load a PCH file,
244      which is a GC collection point.  So we have to grab the first
245      token before allocating any memory.  Pragmas must not be deferred
246      as -fpch-preprocess can generate a pragma to load the PCH file in
247      the preprocessed output used by -save-temps.  */
248   cp_lexer_get_preprocessor_token (NULL, &first_token);
249
250   /* Tell cpplib we want CPP_PRAGMA tokens.  */
251   cpp_get_options (parse_in)->defer_pragmas = true;
252
253   /* Tell c_lex not to merge string constants.  */
254   c_lex_return_raw_strings = true;
255
256   c_common_no_more_pch ();
257
258   /* Allocate the memory.  */
259   lexer = GGC_CNEW (cp_lexer);
260
261 #ifdef ENABLE_CHECKING
262   /* Initially we are not debugging.  */
263   lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266                                    CP_SAVED_TOKEN_STACK);
267
268   /* Create the buffer.  */
269   alloc = CP_LEXER_BUFFER_SIZE;
270   buffer = ggc_alloc (alloc * sizeof (cp_token));
271
272   /* Put the first token in the buffer.  */
273   space = alloc;
274   pos = buffer;
275   *pos = first_token;
276
277   /* Get the remaining tokens from the preprocessor.  */
278   while (pos->type != CPP_EOF)
279     {
280       pos++;
281       if (!--space)
282         {
283           space = alloc;
284           alloc *= 2;
285           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286           pos = buffer + space;
287         }
288       cp_lexer_get_preprocessor_token (lexer, pos);
289     }
290   lexer->buffer = buffer;
291   lexer->buffer_length = alloc - space;
292   lexer->last_token = pos;
293   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
294
295   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296      direct calls to c_lex.  Those callers all expect c_lex to do
297      string constant concatenation.  */
298   c_lex_return_raw_strings = false;
299
300   gcc_assert (lexer->next_token->type != CPP_PURGED);
301   return lexer;
302 }
303
304 /* Create a new lexer whose token stream is primed with the tokens in
305    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
306
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
309 {
310   cp_token *first = cache->first;
311   cp_token *last = cache->last;
312   cp_lexer *lexer = GGC_CNEW (cp_lexer);
313
314   /* We do not own the buffer.  */
315   lexer->buffer = NULL;
316   lexer->buffer_length = 0;
317   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318   lexer->last_token = last;
319
320   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321                                    CP_SAVED_TOKEN_STACK);
322
323 #ifdef ENABLE_CHECKING
324   /* Initially we are not debugging.  */
325   lexer->debugging_p = false;
326 #endif
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Frees all resources associated with LEXER.  */
333
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
336 {
337   if (lexer->buffer)
338     ggc_free (lexer->buffer);
339   VEC_free (cp_token_position, heap, lexer->saved_tokens);
340   ggc_free (lexer);
341 }
342
343 /* Returns nonzero if debugging information should be output.  */
344
345 #ifdef ENABLE_CHECKING
346
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
349 {
350   return lexer->debugging_p;
351 }
352
353 #endif /* ENABLE_CHECKING */
354
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
357 {
358   gcc_assert (!previous_p || lexer->next_token != &eof_token);
359
360   return lexer->next_token - previous_p;
361 }
362
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 {
366   return pos;
367 }
368
369 /* nonzero if we are presently saving tokens.  */
370
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
373 {
374   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
375 }
376
377 /* Store the next token from the preprocessor in *TOKEN.  Return true
378    if we reach EOF.  */
379
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382                                  cp_token *token)
383 {
384   static int is_extern_c = 0;
385
386    /* Get a new token from the preprocessor.  */
387   token->type
388     = c_lex_with_flags (&token->value, &token->location, &token->flags);
389   token->in_system_header = in_system_header;
390
391   /* On some systems, some header files are surrounded by an
392      implicit extern "C" block.  Set a flag in the token if it
393      comes from such a header.  */
394   is_extern_c += pending_lang_change;
395   pending_lang_change = 0;
396   token->implicit_extern_c = is_extern_c > 0;
397
398   /* Check to see if this token is a keyword.  */
399   if (token->type == CPP_NAME
400       && C_IS_RESERVED_WORD (token->value))
401     {
402       /* Mark this token as a keyword.  */
403       token->type = CPP_KEYWORD;
404       /* Record which keyword.  */
405       token->keyword = C_RID_CODE (token->value);
406       /* Update the value.  Some keywords are mapped to particular
407          entities, rather than simply having the value of the
408          corresponding IDENTIFIER_NODE.  For example, `__const' is
409          mapped to `const'.  */
410       token->value = ridpointers[token->keyword];
411     }
412   /* Handle Objective-C++ keywords.  */
413   else if (token->type == CPP_AT_NAME)
414     {
415       token->type = CPP_KEYWORD;
416       switch (C_RID_CODE (token->value))
417         {
418         /* Map 'class' to '@class', 'private' to '@private', etc.  */
419         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423         case RID_THROW: token->keyword = RID_AT_THROW; break;
424         case RID_TRY: token->keyword = RID_AT_TRY; break;
425         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426         default: token->keyword = C_RID_CODE (token->value);
427         }
428     }
429   else
430     token->keyword = RID_MAX;
431 }
432
433 /* Update the globals input_location and in_system_header from TOKEN.  */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
436 {
437   if (token->type != CPP_EOF)
438     {
439       input_location = token->location;
440       in_system_header = token->in_system_header;
441     }
442 }
443
444 /* Return a pointer to the next token in the token stream, but do not
445    consume it.  */
446
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
449 {
450   if (cp_lexer_debugging_p (lexer))
451     {
452       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454       putc ('\n', cp_lexer_debug_stream);
455     }
456   return lexer->next_token;
457 }
458
459 /* Return true if the next token has the indicated TYPE.  */
460
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
463 {
464   return cp_lexer_peek_token (lexer)->type == type;
465 }
466
467 /* Return true if the next token does not have the indicated TYPE.  */
468
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
471 {
472   return !cp_lexer_next_token_is (lexer, type);
473 }
474
475 /* Return true if the next token is the indicated KEYWORD.  */
476
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
479 {
480   cp_token *token;
481
482   /* Peek at the next token.  */
483   token = cp_lexer_peek_token (lexer);
484   /* Check to see if it is the indicated keyword.  */
485   return token->keyword == keyword;
486 }
487
488 /* Return a pointer to the Nth token in the token stream.  If N is 1,
489    then this is precisely equivalent to cp_lexer_peek_token (except
490    that it is not inline).  One would like to disallow that case, but
491    there is one case (cp_parser_nth_token_starts_template_id) where
492    the caller passes a variable for N and it might be 1.  */
493
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
496 {
497   cp_token *token;
498
499   /* N is 1-based, not zero-based.  */
500   gcc_assert (n > 0);
501   
502   if (cp_lexer_debugging_p (lexer))
503     fprintf (cp_lexer_debug_stream,
504              "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
506   --n;
507   token = lexer->next_token;
508   gcc_assert (!n || token != &eof_token);
509   while (n != 0)
510     {
511       ++token;
512       if (token == lexer->last_token)
513         {
514           token = (cp_token *)&eof_token;
515           break;
516         }
517
518       if (token->type != CPP_PURGED)
519         --n;
520     }
521
522   if (cp_lexer_debugging_p (lexer))
523     {
524       cp_lexer_print_token (cp_lexer_debug_stream, token);
525       putc ('\n', cp_lexer_debug_stream);
526     }
527
528   return token;
529 }
530
531 /* Return the next token, and advance the lexer's next_token pointer
532    to point to the next non-purged token.  */
533
534 static cp_token *
535 cp_lexer_consume_token (cp_lexer* lexer)
536 {
537   cp_token *token = lexer->next_token;
538
539   gcc_assert (token != &eof_token);
540
541   do
542     {
543       lexer->next_token++;
544       if (lexer->next_token == lexer->last_token)
545         {
546           lexer->next_token = (cp_token *)&eof_token;
547           break;
548         }
549
550     }
551   while (lexer->next_token->type == CPP_PURGED);
552
553   cp_lexer_set_source_position_from_token (token);
554
555   /* Provide debugging output.  */
556   if (cp_lexer_debugging_p (lexer))
557     {
558       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
559       cp_lexer_print_token (cp_lexer_debug_stream, token);
560       putc ('\n', cp_lexer_debug_stream);
561     }
562
563   return token;
564 }
565
566 /* Permanently remove the next token from the token stream, and
567    advance the next_token pointer to refer to the next non-purged
568    token.  */
569
570 static void
571 cp_lexer_purge_token (cp_lexer *lexer)
572 {
573   cp_token *tok = lexer->next_token;
574
575   gcc_assert (tok != &eof_token);
576   tok->type = CPP_PURGED;
577   tok->location = UNKNOWN_LOCATION;
578   tok->value = NULL_TREE;
579   tok->keyword = RID_MAX;
580
581   do
582     {
583       tok++;
584       if (tok == lexer->last_token)
585         {
586           tok = (cp_token *)&eof_token;
587           break;
588         }
589     }
590   while (tok->type == CPP_PURGED);
591   lexer->next_token = tok;
592 }
593
594 /* Permanently remove all tokens after TOK, up to, but not
595    including, the token that will be returned next by
596    cp_lexer_peek_token.  */
597
598 static void
599 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
600 {
601   cp_token *peek = lexer->next_token;
602
603   if (peek == &eof_token)
604     peek = lexer->last_token;
605
606   gcc_assert (tok < peek);
607
608   for ( tok += 1; tok != peek; tok += 1)
609     {
610       tok->type = CPP_PURGED;
611       tok->location = UNKNOWN_LOCATION;
612       tok->value = NULL_TREE;
613       tok->keyword = RID_MAX;
614     }
615 }
616
617 /* Consume and handle a pragma token.  */
618 static void
619 cp_lexer_handle_pragma (cp_lexer *lexer)
620 {
621   cpp_string s;
622   cp_token *token = cp_lexer_consume_token (lexer);
623   gcc_assert (token->type == CPP_PRAGMA);
624   gcc_assert (token->value);
625
626   s.len = TREE_STRING_LENGTH (token->value);
627   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
628
629   cpp_handle_deferred_pragma (parse_in, &s);
630
631   /* Clearing token->value here means that we will get an ICE if we
632      try to process this #pragma again (which should be impossible).  */
633   token->value = NULL;
634 }
635
636 /* Begin saving tokens.  All tokens consumed after this point will be
637    preserved.  */
638
639 static void
640 cp_lexer_save_tokens (cp_lexer* lexer)
641 {
642   /* Provide debugging output.  */
643   if (cp_lexer_debugging_p (lexer))
644     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
645
646   VEC_safe_push (cp_token_position, heap,
647                  lexer->saved_tokens, lexer->next_token);
648 }
649
650 /* Commit to the portion of the token stream most recently saved.  */
651
652 static void
653 cp_lexer_commit_tokens (cp_lexer* lexer)
654 {
655   /* Provide debugging output.  */
656   if (cp_lexer_debugging_p (lexer))
657     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
658
659   VEC_pop (cp_token_position, lexer->saved_tokens);
660 }
661
662 /* Return all tokens saved since the last call to cp_lexer_save_tokens
663    to the token stream.  Stop saving tokens.  */
664
665 static void
666 cp_lexer_rollback_tokens (cp_lexer* lexer)
667 {
668   /* Provide debugging output.  */
669   if (cp_lexer_debugging_p (lexer))
670     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
671
672   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
673 }
674
675 /* Print a representation of the TOKEN on the STREAM.  */
676
677 #ifdef ENABLE_CHECKING
678
679 static void
680 cp_lexer_print_token (FILE * stream, cp_token *token)
681 {
682   /* We don't use cpp_type2name here because the parser defines
683      a few tokens of its own.  */
684   static const char *const token_names[] = {
685     /* cpplib-defined token types */
686 #define OP(e, s) #e,
687 #define TK(e, s) #e,
688     TTYPE_TABLE
689 #undef OP
690 #undef TK
691     /* C++ parser token types - see "Manifest constants", above.  */
692     "KEYWORD",
693     "TEMPLATE_ID",
694     "NESTED_NAME_SPECIFIER",
695     "PURGED"
696   };
697
698   /* If we have a name for the token, print it out.  Otherwise, we
699      simply give the numeric code.  */
700   gcc_assert (token->type < ARRAY_SIZE(token_names));
701   fputs (token_names[token->type], stream);
702
703   /* For some tokens, print the associated data.  */
704   switch (token->type)
705     {
706     case CPP_KEYWORD:
707       /* Some keywords have a value that is not an IDENTIFIER_NODE.
708          For example, `struct' is mapped to an INTEGER_CST.  */
709       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710         break;
711       /* else fall through */
712     case CPP_NAME:
713       fputs (IDENTIFIER_POINTER (token->value), stream);
714       break;
715
716     case CPP_STRING:
717     case CPP_WSTRING:
718     case CPP_PRAGMA:
719       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720       break;
721
722     default:
723       break;
724     }
725 }
726
727 /* Start emitting debugging information.  */
728
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
731 {
732   lexer->debugging_p = true;
733 }
734
735 /* Stop emitting debugging information.  */
736
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
739 {
740   lexer->debugging_p = false;
741 }
742
743 #endif /* ENABLE_CHECKING */
744
745 /* Create a new cp_token_cache, representing a range of tokens.  */
746
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
749 {
750   cp_token_cache *cache = GGC_NEW (cp_token_cache);
751   cache->first = first;
752   cache->last = last;
753   return cache;
754 }
755
756 \f
757 /* Decl-specifiers.  */
758
759 static void clear_decl_specs
760   (cp_decl_specifier_seq *);
761
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
763
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
766 {
767   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
768 }
769
770 /* Declarators.  */
771
772 /* Nothing other than the parser should be creating declarators;
773    declarators are a semi-syntactic representation of C++ entities.
774    Other parts of the front end that need to create entities (like
775    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
776
777 static cp_declarator *make_call_declarator
778   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780   (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782   (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784   (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786   (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788   (cp_cv_quals, tree, cp_declarator *);
789
790 cp_declarator *cp_error_declarator;
791
792 /* The obstack on which declarators and related data structures are
793    allocated.  */
794 static struct obstack declarator_obstack;
795
796 /* Alloc BYTES from the declarator memory pool.  */
797
798 static inline void *
799 alloc_declarator (size_t bytes)
800 {
801   return obstack_alloc (&declarator_obstack, bytes);
802 }
803
804 /* Allocate a declarator of the indicated KIND.  Clear fields that are
805    common to all declarators.  */
806
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
809 {
810   cp_declarator *declarator;
811
812   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813   declarator->kind = kind;
814   declarator->attributes = NULL_TREE;
815   declarator->declarator = NULL;
816
817   return declarator;
818 }
819
820 /* Make a declarator for a generalized identifier.  If non-NULL, the
821    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822    just UNQUALIFIED_NAME.  */
823
824 static cp_declarator *
825 make_id_declarator (tree qualifying_scope, tree unqualified_name)
826 {
827   cp_declarator *declarator;
828
829   /* It is valid to write:
830
831        class C { void f(); };
832        typedef C D;
833        void D::f();
834
835      The standard is not clear about whether `typedef const C D' is
836      legal; as of 2002-09-15 the committee is considering that
837      question.  EDG 3.0 allows that syntax.  Therefore, we do as
838      well.  */
839   if (qualifying_scope && TYPE_P (qualifying_scope))
840     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
841
842   declarator = make_declarator (cdk_id);
843   declarator->u.id.qualifying_scope = qualifying_scope;
844   declarator->u.id.unqualified_name = unqualified_name;
845   declarator->u.id.sfk = sfk_none;
846
847   return declarator;
848 }
849
850 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
851    of modifiers such as const or volatile to apply to the pointer
852    type, represented as identifiers.  */
853
854 cp_declarator *
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
856 {
857   cp_declarator *declarator;
858
859   declarator = make_declarator (cdk_pointer);
860   declarator->declarator = target;
861   declarator->u.pointer.qualifiers = cv_qualifiers;
862   declarator->u.pointer.class_type = NULL_TREE;
863
864   return declarator;
865 }
866
867 /* Like make_pointer_declarator -- but for references.  */
868
869 cp_declarator *
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
871 {
872   cp_declarator *declarator;
873
874   declarator = make_declarator (cdk_reference);
875   declarator->declarator = target;
876   declarator->u.pointer.qualifiers = cv_qualifiers;
877   declarator->u.pointer.class_type = NULL_TREE;
878
879   return declarator;
880 }
881
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883    member of CLASS_TYPE.  */
884
885 cp_declarator *
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887                         cp_declarator *pointee)
888 {
889   cp_declarator *declarator;
890
891   declarator = make_declarator (cdk_ptrmem);
892   declarator->declarator = pointee;
893   declarator->u.pointer.qualifiers = cv_qualifiers;
894   declarator->u.pointer.class_type = class_type;
895
896   return declarator;
897 }
898
899 /* Make a declarator for the function given by TARGET, with the
900    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
901    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
902    indicates what exceptions can be thrown.  */
903
904 cp_declarator *
905 make_call_declarator (cp_declarator *target,
906                       cp_parameter_declarator *parms,
907                       cp_cv_quals cv_qualifiers,
908                       tree exception_specification)
909 {
910   cp_declarator *declarator;
911
912   declarator = make_declarator (cdk_function);
913   declarator->declarator = target;
914   declarator->u.function.parameters = parms;
915   declarator->u.function.qualifiers = cv_qualifiers;
916   declarator->u.function.exception_specification = exception_specification;
917
918   return declarator;
919 }
920
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922    defined by ELEMENT.  */
923
924 cp_declarator *
925 make_array_declarator (cp_declarator *element, tree bounds)
926 {
927   cp_declarator *declarator;
928
929   declarator = make_declarator (cdk_array);
930   declarator->declarator = element;
931   declarator->u.array.bounds = bounds;
932
933   return declarator;
934 }
935
936 cp_parameter_declarator *no_parameters;
937
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939    DECLARATOR and DEFAULT_ARGUMENT.  */
940
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943                            cp_declarator *declarator,
944                            tree default_argument)
945 {
946   cp_parameter_declarator *parameter;
947
948   parameter = ((cp_parameter_declarator *)
949                alloc_declarator (sizeof (cp_parameter_declarator)));
950   parameter->next = NULL;
951   if (decl_specifiers)
952     parameter->decl_specifiers = *decl_specifiers;
953   else
954     clear_decl_specs (&parameter->decl_specifiers);
955   parameter->declarator = declarator;
956   parameter->default_argument = default_argument;
957   parameter->ellipsis_p = false;
958
959   return parameter;
960 }
961
962 /* The parser.  */
963
964 /* Overview
965    --------
966
967    A cp_parser parses the token stream as specified by the C++
968    grammar.  Its job is purely parsing, not semantic analysis.  For
969    example, the parser breaks the token stream into declarators,
970    expressions, statements, and other similar syntactic constructs.
971    It does not check that the types of the expressions on either side
972    of an assignment-statement are compatible, or that a function is
973    not declared with a parameter of type `void'.
974
975    The parser invokes routines elsewhere in the compiler to perform
976    semantic analysis and to build up the abstract syntax tree for the
977    code processed.
978
979    The parser (and the template instantiation code, which is, in a
980    way, a close relative of parsing) are the only parts of the
981    compiler that should be calling push_scope and pop_scope, or
982    related functions.  The parser (and template instantiation code)
983    keeps track of what scope is presently active; everything else
984    should simply honor that.  (The code that generates static
985    initializers may also need to set the scope, in order to check
986    access control correctly when emitting the initializers.)
987
988    Methodology
989    -----------
990
991    The parser is of the standard recursive-descent variety.  Upcoming
992    tokens in the token stream are examined in order to determine which
993    production to use when parsing a non-terminal.  Some C++ constructs
994    require arbitrary look ahead to disambiguate.  For example, it is
995    impossible, in the general case, to tell whether a statement is an
996    expression or declaration without scanning the entire statement.
997    Therefore, the parser is capable of "parsing tentatively."  When the
998    parser is not sure what construct comes next, it enters this mode.
999    Then, while we attempt to parse the construct, the parser queues up
1000    error messages, rather than issuing them immediately, and saves the
1001    tokens it consumes.  If the construct is parsed successfully, the
1002    parser "commits", i.e., it issues any queued error messages and
1003    the tokens that were being preserved are permanently discarded.
1004    If, however, the construct is not parsed successfully, the parser
1005    rolls back its state completely so that it can resume parsing using
1006    a different alternative.
1007
1008    Future Improvements
1009    -------------------
1010
1011    The performance of the parser could probably be improved substantially.
1012    We could often eliminate the need to parse tentatively by looking ahead
1013    a little bit.  In some places, this approach might not entirely eliminate
1014    the need to parse tentatively, but it might still speed up the average
1015    case.  */
1016
1017 /* Flags that are passed to some parsing functions.  These values can
1018    be bitwise-ored together.  */
1019
1020 typedef enum cp_parser_flags
1021 {
1022   /* No flags.  */
1023   CP_PARSER_FLAGS_NONE = 0x0,
1024   /* The construct is optional.  If it is not present, then no error
1025      should be issued.  */
1026   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027   /* When parsing a type-specifier, do not allow user-defined types.  */
1028   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029 } cp_parser_flags;
1030
1031 /* The different kinds of declarators we want to parse.  */
1032
1033 typedef enum cp_parser_declarator_kind
1034 {
1035   /* We want an abstract declarator.  */
1036   CP_PARSER_DECLARATOR_ABSTRACT,
1037   /* We want a named declarator.  */
1038   CP_PARSER_DECLARATOR_NAMED,
1039   /* We don't mind, but the name must be an unqualified-id.  */
1040   CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1042
1043 /* The precedence values used to parse binary expressions.  The minimum value
1044    of PREC must be 1, because zero is reserved to quickly discriminate
1045    binary operators from other tokens.  */
1046
1047 enum cp_parser_prec
1048 {
1049   PREC_NOT_OPERATOR,
1050   PREC_LOGICAL_OR_EXPRESSION,
1051   PREC_LOGICAL_AND_EXPRESSION,
1052   PREC_INCLUSIVE_OR_EXPRESSION,
1053   PREC_EXCLUSIVE_OR_EXPRESSION,
1054   PREC_AND_EXPRESSION,
1055   PREC_EQUALITY_EXPRESSION,
1056   PREC_RELATIONAL_EXPRESSION,
1057   PREC_SHIFT_EXPRESSION,
1058   PREC_ADDITIVE_EXPRESSION,
1059   PREC_MULTIPLICATIVE_EXPRESSION,
1060   PREC_PM_EXPRESSION,
1061   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1062 };
1063
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065    precedence value.  */
1066
1067 typedef struct cp_parser_binary_operations_map_node
1068 {
1069   /* The token type.  */
1070   enum cpp_ttype token_type;
1071   /* The corresponding tree code.  */
1072   enum tree_code tree_type;
1073   /* The precedence of this operator.  */
1074   enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1076
1077 /* The status of a tentative parse.  */
1078
1079 typedef enum cp_parser_status_kind
1080 {
1081   /* No errors have occurred.  */
1082   CP_PARSER_STATUS_KIND_NO_ERROR,
1083   /* An error has occurred.  */
1084   CP_PARSER_STATUS_KIND_ERROR,
1085   /* We are committed to this tentative parse, whether or not an error
1086      has occurred.  */
1087   CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1089
1090 typedef struct cp_parser_expression_stack_entry
1091 {
1092   tree lhs;
1093   enum tree_code tree_type;
1094   int prec;
1095 } cp_parser_expression_stack_entry;
1096
1097 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1098    entries because precedence levels on the stack are monotonically
1099    increasing.  */
1100 typedef struct cp_parser_expression_stack_entry
1101   cp_parser_expression_stack[NUM_PREC_VALUES];
1102
1103 /* Context that is saved and restored when parsing tentatively.  */
1104 typedef struct cp_parser_context GTY (())
1105 {
1106   /* If this is a tentative parsing context, the status of the
1107      tentative parse.  */
1108   enum cp_parser_status_kind status;
1109   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1110      that are looked up in this context must be looked up both in the
1111      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112      the context of the containing expression.  */
1113   tree object_type;
1114
1115   /* The next parsing context in the stack.  */
1116   struct cp_parser_context *next;
1117 } cp_parser_context;
1118
1119 /* Prototypes.  */
1120
1121 /* Constructors and destructors.  */
1122
1123 static cp_parser_context *cp_parser_context_new
1124   (cp_parser_context *);
1125
1126 /* Class variables.  */
1127
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1129
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131    Transformed into an associative array (binops_by_token) by
1132    cp_parser_new.  */
1133
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1137
1138   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141
1142   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144
1145   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147
1148   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1154
1155   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157
1158   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159
1160   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161
1162   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163
1164   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165
1166   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1167 };
1168
1169 /* The same as binops, but initialized by cp_parser_new so that
1170    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1171    for speed.  */
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173
1174 /* Constructors and destructors.  */
1175
1176 /* Construct a new context.  The context below this one on the stack
1177    is given by NEXT.  */
1178
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1181 {
1182   cp_parser_context *context;
1183
1184   /* Allocate the storage.  */
1185   if (cp_parser_context_free_list != NULL)
1186     {
1187       /* Pull the first entry from the free list.  */
1188       context = cp_parser_context_free_list;
1189       cp_parser_context_free_list = context->next;
1190       memset (context, 0, sizeof (*context));
1191     }
1192   else
1193     context = GGC_CNEW (cp_parser_context);
1194
1195   /* No errors have occurred yet in this context.  */
1196   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197   /* If this is not the bottomost context, copy information that we
1198      need from the previous context.  */
1199   if (next)
1200     {
1201       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202          expression, then we are parsing one in this context, too.  */
1203       context->object_type = next->object_type;
1204       /* Thread the stack.  */
1205       context->next = next;
1206     }
1207
1208   return context;
1209 }
1210
1211 /* The cp_parser structure represents the C++ parser.  */
1212
1213 typedef struct cp_parser GTY(())
1214 {
1215   /* The lexer from which we are obtaining tokens.  */
1216   cp_lexer *lexer;
1217
1218   /* The scope in which names should be looked up.  If NULL_TREE, then
1219      we look up names in the scope that is currently open in the
1220      source program.  If non-NULL, this is either a TYPE or
1221      NAMESPACE_DECL for the scope in which we should look.  It can
1222      also be ERROR_MARK, when we've parsed a bogus scope.
1223
1224      This value is not cleared automatically after a name is looked
1225      up, so we must be careful to clear it before starting a new look
1226      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1227      will look up `Z' in the scope of `X', rather than the current
1228      scope.)  Unfortunately, it is difficult to tell when name lookup
1229      is complete, because we sometimes peek at a token, look it up,
1230      and then decide not to consume it.   */
1231   tree scope;
1232
1233   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234      last lookup took place.  OBJECT_SCOPE is used if an expression
1235      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236      respectively.  QUALIFYING_SCOPE is used for an expression of the
1237      form "X::Y"; it refers to X.  */
1238   tree object_scope;
1239   tree qualifying_scope;
1240
1241   /* A stack of parsing contexts.  All but the bottom entry on the
1242      stack will be tentative contexts.
1243
1244      We parse tentatively in order to determine which construct is in
1245      use in some situations.  For example, in order to determine
1246      whether a statement is an expression-statement or a
1247      declaration-statement we parse it tentatively as a
1248      declaration-statement.  If that fails, we then reparse the same
1249      token stream as an expression-statement.  */
1250   cp_parser_context *context;
1251
1252   /* True if we are parsing GNU C++.  If this flag is not set, then
1253      GNU extensions are not recognized.  */
1254   bool allow_gnu_extensions_p;
1255
1256   /* TRUE if the `>' token should be interpreted as the greater-than
1257      operator.  FALSE if it is the end of a template-id or
1258      template-parameter-list.  */
1259   bool greater_than_is_operator_p;
1260
1261   /* TRUE if default arguments are allowed within a parameter list
1262      that starts at this point. FALSE if only a gnu extension makes
1263      them permissible.  */
1264   bool default_arg_ok_p;
1265
1266   /* TRUE if we are parsing an integral constant-expression.  See
1267      [expr.const] for a precise definition.  */
1268   bool integral_constant_expression_p;
1269
1270   /* TRUE if we are parsing an integral constant-expression -- but a
1271      non-constant expression should be permitted as well.  This flag
1272      is used when parsing an array bound so that GNU variable-length
1273      arrays are tolerated.  */
1274   bool allow_non_integral_constant_expression_p;
1275
1276   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277      been seen that makes the expression non-constant.  */
1278   bool non_integral_constant_expression_p;
1279
1280   /* TRUE if local variable names and `this' are forbidden in the
1281      current context.  */
1282   bool local_variables_forbidden_p;
1283
1284   /* TRUE if the declaration we are parsing is part of a
1285      linkage-specification of the form `extern string-literal
1286      declaration'.  */
1287   bool in_unbraced_linkage_specification_p;
1288
1289   /* TRUE if we are presently parsing a declarator, after the
1290      direct-declarator.  */
1291   bool in_declarator_p;
1292
1293   /* TRUE if we are presently parsing a template-argument-list.  */
1294   bool in_template_argument_list_p;
1295
1296   /* TRUE if we are presently parsing the body of an
1297      iteration-statement.  */
1298   bool in_iteration_statement_p;
1299
1300   /* TRUE if we are presently parsing the body of a switch
1301      statement.  */
1302   bool in_switch_statement_p;
1303
1304   /* TRUE if we are parsing a type-id in an expression context.  In
1305      such a situation, both "type (expr)" and "type (type)" are valid
1306      alternatives.  */
1307   bool in_type_id_in_expr_p;
1308
1309   /* TRUE if we are currently in a header file where declarations are
1310      implicitly extern "C".  */
1311   bool implicit_extern_c;
1312
1313   /* TRUE if strings in expressions should be translated to the execution
1314      character set.  */
1315   bool translate_strings_p;
1316
1317   /* If non-NULL, then we are parsing a construct where new type
1318      definitions are not permitted.  The string stored here will be
1319      issued as an error message if a type is defined.  */
1320   const char *type_definition_forbidden_message;
1321
1322   /* A list of lists. The outer list is a stack, used for member
1323      functions of local classes. At each level there are two sub-list,
1324      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326      TREE_VALUE's. The functions are chained in reverse declaration
1327      order.
1328
1329      The TREE_PURPOSE sublist contains those functions with default
1330      arguments that need post processing, and the TREE_VALUE sublist
1331      contains those functions with definitions that need post
1332      processing.
1333
1334      These lists can only be processed once the outermost class being
1335      defined is complete.  */
1336   tree unparsed_functions_queues;
1337
1338   /* The number of classes whose definitions are currently in
1339      progress.  */
1340   unsigned num_classes_being_defined;
1341
1342   /* The number of template parameter lists that apply directly to the
1343      current declaration.  */
1344   unsigned num_template_parameter_lists;
1345 } cp_parser;
1346
1347 /* The type of a function that parses some kind of expression.  */
1348 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1349
1350 /* Prototypes.  */
1351
1352 /* Constructors and destructors.  */
1353
1354 static cp_parser *cp_parser_new
1355   (void);
1356
1357 /* Routines to parse various constructs.
1358
1359    Those that return `tree' will return the error_mark_node (rather
1360    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361    Sometimes, they will return an ordinary node if error-recovery was
1362    attempted, even though a parse error occurred.  So, to check
1363    whether or not a parse error occurred, you should always use
1364    cp_parser_error_occurred.  If the construct is optional (indicated
1365    either by an `_opt' in the name of the function that does the
1366    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367    the construct is not present.  */
1368
1369 /* Lexical conventions [gram.lex]  */
1370
1371 static tree cp_parser_identifier
1372   (cp_parser *);
1373 static tree cp_parser_string_literal
1374   (cp_parser *, bool, bool);
1375
1376 /* Basic concepts [gram.basic]  */
1377
1378 static bool cp_parser_translation_unit
1379   (cp_parser *);
1380
1381 /* Expressions [gram.expr]  */
1382
1383 static tree cp_parser_primary_expression
1384   (cp_parser *, bool, cp_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386   (cp_parser *, bool, bool, bool *, bool);
1387 static tree cp_parser_unqualified_id
1388   (cp_parser *, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier_opt
1390   (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392   (cp_parser *, bool, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394   (cp_parser *, bool, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396   (cp_parser *, bool, bool);
1397 static tree cp_parser_postfix_open_square_expression
1398   (cp_parser *, tree, bool);
1399 static tree cp_parser_postfix_dot_deref_expression
1400   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1401 static tree cp_parser_parenthesized_expression_list
1402   (cp_parser *, bool, bool, bool *);
1403 static void cp_parser_pseudo_destructor_name
1404   (cp_parser *, tree *, tree *);
1405 static tree cp_parser_unary_expression
1406   (cp_parser *, bool, bool);
1407 static enum tree_code cp_parser_unary_operator
1408   (cp_token *);
1409 static tree cp_parser_new_expression
1410   (cp_parser *);
1411 static tree cp_parser_new_placement
1412   (cp_parser *);
1413 static tree cp_parser_new_type_id
1414   (cp_parser *, tree *);
1415 static cp_declarator *cp_parser_new_declarator_opt
1416   (cp_parser *);
1417 static cp_declarator *cp_parser_direct_new_declarator
1418   (cp_parser *);
1419 static tree cp_parser_new_initializer
1420   (cp_parser *);
1421 static tree cp_parser_delete_expression
1422   (cp_parser *);
1423 static tree cp_parser_cast_expression
1424   (cp_parser *, bool, bool);
1425 static tree cp_parser_binary_expression
1426   (cp_parser *, bool);
1427 static tree cp_parser_question_colon_clause
1428   (cp_parser *, tree);
1429 static tree cp_parser_assignment_expression
1430   (cp_parser *, bool);
1431 static enum tree_code cp_parser_assignment_operator_opt
1432   (cp_parser *);
1433 static tree cp_parser_expression
1434   (cp_parser *, bool);
1435 static tree cp_parser_constant_expression
1436   (cp_parser *, bool, bool *);
1437 static tree cp_parser_builtin_offsetof
1438   (cp_parser *);
1439
1440 /* Statements [gram.stmt.stmt]  */
1441
1442 static void cp_parser_statement
1443   (cp_parser *, tree);
1444 static tree cp_parser_labeled_statement
1445   (cp_parser *, tree);
1446 static tree cp_parser_expression_statement
1447   (cp_parser *, tree);
1448 static tree cp_parser_compound_statement
1449   (cp_parser *, tree, bool);
1450 static void cp_parser_statement_seq_opt
1451   (cp_parser *, tree);
1452 static tree cp_parser_selection_statement
1453   (cp_parser *);
1454 static tree cp_parser_condition
1455   (cp_parser *);
1456 static tree cp_parser_iteration_statement
1457   (cp_parser *);
1458 static void cp_parser_for_init_statement
1459   (cp_parser *);
1460 static tree cp_parser_jump_statement
1461   (cp_parser *);
1462 static void cp_parser_declaration_statement
1463   (cp_parser *);
1464
1465 static tree cp_parser_implicitly_scoped_statement
1466   (cp_parser *);
1467 static void cp_parser_already_scoped_statement
1468   (cp_parser *);
1469
1470 /* Declarations [gram.dcl.dcl] */
1471
1472 static void cp_parser_declaration_seq_opt
1473   (cp_parser *);
1474 static void cp_parser_declaration
1475   (cp_parser *);
1476 static void cp_parser_block_declaration
1477   (cp_parser *, bool);
1478 static void cp_parser_simple_declaration
1479   (cp_parser *, bool);
1480 static void cp_parser_decl_specifier_seq
1481   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1482 static tree cp_parser_storage_class_specifier_opt
1483   (cp_parser *);
1484 static tree cp_parser_function_specifier_opt
1485   (cp_parser *, cp_decl_specifier_seq *);
1486 static tree cp_parser_type_specifier
1487   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1488    int *, bool *);
1489 static tree cp_parser_simple_type_specifier
1490   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1491 static tree cp_parser_type_name
1492   (cp_parser *);
1493 static tree cp_parser_elaborated_type_specifier
1494   (cp_parser *, bool, bool);
1495 static tree cp_parser_enum_specifier
1496   (cp_parser *);
1497 static void cp_parser_enumerator_list
1498   (cp_parser *, tree);
1499 static void cp_parser_enumerator_definition
1500   (cp_parser *, tree);
1501 static tree cp_parser_namespace_name
1502   (cp_parser *);
1503 static void cp_parser_namespace_definition
1504   (cp_parser *);
1505 static void cp_parser_namespace_body
1506   (cp_parser *);
1507 static tree cp_parser_qualified_namespace_specifier
1508   (cp_parser *);
1509 static void cp_parser_namespace_alias_definition
1510   (cp_parser *);
1511 static void cp_parser_using_declaration
1512   (cp_parser *);
1513 static void cp_parser_using_directive
1514   (cp_parser *);
1515 static void cp_parser_asm_definition
1516   (cp_parser *);
1517 static void cp_parser_linkage_specification
1518   (cp_parser *);
1519
1520 /* Declarators [gram.dcl.decl] */
1521
1522 static tree cp_parser_init_declarator
1523   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1524 static cp_declarator *cp_parser_declarator
1525   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1526 static cp_declarator *cp_parser_direct_declarator
1527   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1528 static enum tree_code cp_parser_ptr_operator
1529   (cp_parser *, tree *, cp_cv_quals *);
1530 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1531   (cp_parser *);
1532 static tree cp_parser_declarator_id
1533   (cp_parser *);
1534 static tree cp_parser_type_id
1535   (cp_parser *);
1536 static void cp_parser_type_specifier_seq
1537   (cp_parser *, bool, cp_decl_specifier_seq *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1539   (cp_parser *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1541   (cp_parser *, bool *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration
1543   (cp_parser *, bool, bool *);
1544 static void cp_parser_function_body
1545   (cp_parser *);
1546 static tree cp_parser_initializer
1547   (cp_parser *, bool *, bool *);
1548 static tree cp_parser_initializer_clause
1549   (cp_parser *, bool *);
1550 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1551   (cp_parser *, bool *);
1552
1553 static bool cp_parser_ctor_initializer_opt_and_function_body
1554   (cp_parser *);
1555
1556 /* Classes [gram.class] */
1557
1558 static tree cp_parser_class_name
1559   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1560 static tree cp_parser_class_specifier
1561   (cp_parser *);
1562 static tree cp_parser_class_head
1563   (cp_parser *, bool *, tree *);
1564 static enum tag_types cp_parser_class_key
1565   (cp_parser *);
1566 static void cp_parser_member_specification_opt
1567   (cp_parser *);
1568 static void cp_parser_member_declaration
1569   (cp_parser *);
1570 static tree cp_parser_pure_specifier
1571   (cp_parser *);
1572 static tree cp_parser_constant_initializer
1573   (cp_parser *);
1574
1575 /* Derived classes [gram.class.derived] */
1576
1577 static tree cp_parser_base_clause
1578   (cp_parser *);
1579 static tree cp_parser_base_specifier
1580   (cp_parser *);
1581
1582 /* Special member functions [gram.special] */
1583
1584 static tree cp_parser_conversion_function_id
1585   (cp_parser *);
1586 static tree cp_parser_conversion_type_id
1587   (cp_parser *);
1588 static cp_declarator *cp_parser_conversion_declarator_opt
1589   (cp_parser *);
1590 static bool cp_parser_ctor_initializer_opt
1591   (cp_parser *);
1592 static void cp_parser_mem_initializer_list
1593   (cp_parser *);
1594 static tree cp_parser_mem_initializer
1595   (cp_parser *);
1596 static tree cp_parser_mem_initializer_id
1597   (cp_parser *);
1598
1599 /* Overloading [gram.over] */
1600
1601 static tree cp_parser_operator_function_id
1602   (cp_parser *);
1603 static tree cp_parser_operator
1604   (cp_parser *);
1605
1606 /* Templates [gram.temp] */
1607
1608 static void cp_parser_template_declaration
1609   (cp_parser *, bool);
1610 static tree cp_parser_template_parameter_list
1611   (cp_parser *);
1612 static tree cp_parser_template_parameter
1613   (cp_parser *, bool *);
1614 static tree cp_parser_type_parameter
1615   (cp_parser *);
1616 static tree cp_parser_template_id
1617   (cp_parser *, bool, bool, bool);
1618 static tree cp_parser_template_name
1619   (cp_parser *, bool, bool, bool, bool *);
1620 static tree cp_parser_template_argument_list
1621   (cp_parser *);
1622 static tree cp_parser_template_argument
1623   (cp_parser *);
1624 static void cp_parser_explicit_instantiation
1625   (cp_parser *);
1626 static void cp_parser_explicit_specialization
1627   (cp_parser *);
1628
1629 /* Exception handling [gram.exception] */
1630
1631 static tree cp_parser_try_block
1632   (cp_parser *);
1633 static bool cp_parser_function_try_block
1634   (cp_parser *);
1635 static void cp_parser_handler_seq
1636   (cp_parser *);
1637 static void cp_parser_handler
1638   (cp_parser *);
1639 static tree cp_parser_exception_declaration
1640   (cp_parser *);
1641 static tree cp_parser_throw_expression
1642   (cp_parser *);
1643 static tree cp_parser_exception_specification_opt
1644   (cp_parser *);
1645 static tree cp_parser_type_id_list
1646   (cp_parser *);
1647
1648 /* GNU Extensions */
1649
1650 static tree cp_parser_asm_specification_opt
1651   (cp_parser *);
1652 static tree cp_parser_asm_operand_list
1653   (cp_parser *);
1654 static tree cp_parser_asm_clobber_list
1655   (cp_parser *);
1656 static tree cp_parser_attributes_opt
1657   (cp_parser *);
1658 static tree cp_parser_attribute_list
1659   (cp_parser *);
1660 static bool cp_parser_extension_opt
1661   (cp_parser *, int *);
1662 static void cp_parser_label_declaration
1663   (cp_parser *);
1664
1665 /* Objective-C++ Productions */
1666
1667 static tree cp_parser_objc_message_receiver
1668   (cp_parser *);
1669 static tree cp_parser_objc_message_args
1670   (cp_parser *);
1671 static tree cp_parser_objc_message_expression
1672   (cp_parser *);
1673 static tree cp_parser_objc_encode_expression
1674   (cp_parser *);
1675 static tree cp_parser_objc_defs_expression
1676   (cp_parser *);
1677 static tree cp_parser_objc_protocol_expression
1678   (cp_parser *);
1679 static tree cp_parser_objc_selector_expression
1680   (cp_parser *);
1681 static tree cp_parser_objc_expression
1682   (cp_parser *);
1683 static bool cp_parser_objc_selector_p
1684   (enum cpp_ttype);
1685 static tree cp_parser_objc_selector
1686   (cp_parser *);
1687 static tree cp_parser_objc_protocol_refs_opt
1688   (cp_parser *);
1689 static void cp_parser_objc_declaration
1690   (cp_parser *);
1691 static tree cp_parser_objc_statement
1692   (cp_parser *);
1693
1694 /* Utility Routines */
1695
1696 static tree cp_parser_lookup_name
1697   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1698 static tree cp_parser_lookup_name_simple
1699   (cp_parser *, tree);
1700 static tree cp_parser_maybe_treat_template_as_class
1701   (tree, bool);
1702 static bool cp_parser_check_declarator_template_parameters
1703   (cp_parser *, cp_declarator *);
1704 static bool cp_parser_check_template_parameters
1705   (cp_parser *, unsigned);
1706 static tree cp_parser_simple_cast_expression
1707   (cp_parser *);
1708 static tree cp_parser_global_scope_opt
1709   (cp_parser *, bool);
1710 static bool cp_parser_constructor_declarator_p
1711   (cp_parser *, bool);
1712 static tree cp_parser_function_definition_from_specifiers_and_declarator
1713   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1714 static tree cp_parser_function_definition_after_declarator
1715   (cp_parser *, bool);
1716 static void cp_parser_template_declaration_after_export
1717   (cp_parser *, bool);
1718 static tree cp_parser_single_declaration
1719   (cp_parser *, bool, bool *);
1720 static tree cp_parser_functional_cast
1721   (cp_parser *, tree);
1722 static tree cp_parser_save_member_function_body
1723   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1724 static tree cp_parser_enclosed_template_argument_list
1725   (cp_parser *);
1726 static void cp_parser_save_default_args
1727   (cp_parser *, tree);
1728 static void cp_parser_late_parsing_for_member
1729   (cp_parser *, tree);
1730 static void cp_parser_late_parsing_default_args
1731   (cp_parser *, tree);
1732 static tree cp_parser_sizeof_operand
1733   (cp_parser *, enum rid);
1734 static bool cp_parser_declares_only_class_p
1735   (cp_parser *);
1736 static void cp_parser_set_storage_class
1737   (cp_decl_specifier_seq *, cp_storage_class);
1738 static void cp_parser_set_decl_spec_type
1739   (cp_decl_specifier_seq *, tree, bool);
1740 static bool cp_parser_friend_p
1741   (const cp_decl_specifier_seq *);
1742 static cp_token *cp_parser_require
1743   (cp_parser *, enum cpp_ttype, const char *);
1744 static cp_token *cp_parser_require_keyword
1745   (cp_parser *, enum rid, const char *);
1746 static bool cp_parser_token_starts_function_definition_p
1747   (cp_token *);
1748 static bool cp_parser_next_token_starts_class_definition_p
1749   (cp_parser *);
1750 static bool cp_parser_next_token_ends_template_argument_p
1751   (cp_parser *);
1752 static bool cp_parser_nth_token_starts_template_argument_list_p
1753   (cp_parser *, size_t);
1754 static enum tag_types cp_parser_token_is_class_key
1755   (cp_token *);
1756 static void cp_parser_check_class_key
1757   (enum tag_types, tree type);
1758 static void cp_parser_check_access_in_redeclaration
1759   (tree type);
1760 static bool cp_parser_optional_template_keyword
1761   (cp_parser *);
1762 static void cp_parser_pre_parsed_nested_name_specifier
1763   (cp_parser *);
1764 static void cp_parser_cache_group
1765   (cp_parser *, enum cpp_ttype, unsigned);
1766 static void cp_parser_parse_tentatively
1767   (cp_parser *);
1768 static void cp_parser_commit_to_tentative_parse
1769   (cp_parser *);
1770 static void cp_parser_abort_tentative_parse
1771   (cp_parser *);
1772 static bool cp_parser_parse_definitely
1773   (cp_parser *);
1774 static inline bool cp_parser_parsing_tentatively
1775   (cp_parser *);
1776 static bool cp_parser_uncommitted_to_tentative_parse_p
1777   (cp_parser *);
1778 static void cp_parser_error
1779   (cp_parser *, const char *);
1780 static void cp_parser_name_lookup_error
1781   (cp_parser *, tree, tree, const char *);
1782 static bool cp_parser_simulate_error
1783   (cp_parser *);
1784 static void cp_parser_check_type_definition
1785   (cp_parser *);
1786 static void cp_parser_check_for_definition_in_return_type
1787   (cp_declarator *, tree);
1788 static void cp_parser_check_for_invalid_template_id
1789   (cp_parser *, tree);
1790 static bool cp_parser_non_integral_constant_expression
1791   (cp_parser *, const char *);
1792 static void cp_parser_diagnose_invalid_type_name
1793   (cp_parser *, tree, tree);
1794 static bool cp_parser_parse_and_diagnose_invalid_type_name
1795   (cp_parser *);
1796 static int cp_parser_skip_to_closing_parenthesis
1797   (cp_parser *, bool, bool, bool);
1798 static void cp_parser_skip_to_end_of_statement
1799   (cp_parser *);
1800 static void cp_parser_consume_semicolon_at_end_of_statement
1801   (cp_parser *);
1802 static void cp_parser_skip_to_end_of_block_or_statement
1803   (cp_parser *);
1804 static void cp_parser_skip_to_closing_brace
1805   (cp_parser *);
1806 static void cp_parser_skip_until_found
1807   (cp_parser *, enum cpp_ttype, const char *);
1808 static bool cp_parser_error_occurred
1809   (cp_parser *);
1810 static bool cp_parser_allow_gnu_extensions_p
1811   (cp_parser *);
1812 static bool cp_parser_is_string_literal
1813   (cp_token *);
1814 static bool cp_parser_is_keyword
1815   (cp_token *, enum rid);
1816 static tree cp_parser_make_typename_type
1817   (cp_parser *, tree, tree);
1818
1819 /* Returns nonzero if we are parsing tentatively.  */
1820
1821 static inline bool
1822 cp_parser_parsing_tentatively (cp_parser* parser)
1823 {
1824   return parser->context->next != NULL;
1825 }
1826
1827 /* Returns nonzero if TOKEN is a string literal.  */
1828
1829 static bool
1830 cp_parser_is_string_literal (cp_token* token)
1831 {
1832   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1833 }
1834
1835 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1836
1837 static bool
1838 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1839 {
1840   return token->keyword == keyword;
1841 }
1842
1843 /* A minimum or maximum operator has been seen.  As these are
1844    deprecated, issue a warning.  */
1845
1846 static inline void
1847 cp_parser_warn_min_max (void)
1848 {
1849   if (warn_deprecated && !in_system_header)
1850     warning (0, "minimum/maximum operators are deprecated");
1851 }
1852
1853 /* If not parsing tentatively, issue a diagnostic of the form
1854       FILE:LINE: MESSAGE before TOKEN
1855    where TOKEN is the next token in the input stream.  MESSAGE
1856    (specified by the caller) is usually of the form "expected
1857    OTHER-TOKEN".  */
1858
1859 static void
1860 cp_parser_error (cp_parser* parser, const char* message)
1861 {
1862   if (!cp_parser_simulate_error (parser))
1863     {
1864       cp_token *token = cp_lexer_peek_token (parser->lexer);
1865       /* This diagnostic makes more sense if it is tagged to the line
1866          of the token we just peeked at.  */
1867       cp_lexer_set_source_position_from_token (token);
1868       if (token->type == CPP_PRAGMA)
1869         {
1870           error ("%<#pragma%> is not allowed here");
1871           cp_lexer_purge_token (parser->lexer);
1872           return;
1873         }
1874       c_parse_error (message,
1875                      /* Because c_parser_error does not understand
1876                         CPP_KEYWORD, keywords are treated like
1877                         identifiers.  */
1878                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1879                      token->value);
1880     }
1881 }
1882
1883 /* Issue an error about name-lookup failing.  NAME is the
1884    IDENTIFIER_NODE DECL is the result of
1885    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1886    the thing that we hoped to find.  */
1887
1888 static void
1889 cp_parser_name_lookup_error (cp_parser* parser,
1890                              tree name,
1891                              tree decl,
1892                              const char* desired)
1893 {
1894   /* If name lookup completely failed, tell the user that NAME was not
1895      declared.  */
1896   if (decl == error_mark_node)
1897     {
1898       if (parser->scope && parser->scope != global_namespace)
1899         error ("%<%D::%D%> has not been declared",
1900                parser->scope, name);
1901       else if (parser->scope == global_namespace)
1902         error ("%<::%D%> has not been declared", name);
1903       else if (parser->object_scope
1904                && !CLASS_TYPE_P (parser->object_scope))
1905         error ("request for member %qD in non-class type %qT",
1906                name, parser->object_scope);
1907       else if (parser->object_scope)
1908         error ("%<%T::%D%> has not been declared",
1909                parser->object_scope, name);
1910       else
1911         error ("%qD has not been declared", name);
1912     }
1913   else if (parser->scope && parser->scope != global_namespace)
1914     error ("%<%D::%D%> %s", parser->scope, name, desired);
1915   else if (parser->scope == global_namespace)
1916     error ("%<::%D%> %s", name, desired);
1917   else
1918     error ("%qD %s", name, desired);
1919 }
1920
1921 /* If we are parsing tentatively, remember that an error has occurred
1922    during this tentative parse.  Returns true if the error was
1923    simulated; false if a message should be issued by the caller.  */
1924
1925 static bool
1926 cp_parser_simulate_error (cp_parser* parser)
1927 {
1928   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1929     {
1930       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1931       return true;
1932     }
1933   return false;
1934 }
1935
1936 /* This function is called when a type is defined.  If type
1937    definitions are forbidden at this point, an error message is
1938    issued.  */
1939
1940 static void
1941 cp_parser_check_type_definition (cp_parser* parser)
1942 {
1943   /* If types are forbidden here, issue a message.  */
1944   if (parser->type_definition_forbidden_message)
1945     /* Use `%s' to print the string in case there are any escape
1946        characters in the message.  */
1947     error ("%s", parser->type_definition_forbidden_message);
1948 }
1949
1950 /* This function is called when the DECLARATOR is processed.  The TYPE
1951    was a type defined in the decl-specifiers.  If it is invalid to
1952    define a type in the decl-specifiers for DECLARATOR, an error is
1953    issued.  */
1954
1955 static void
1956 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1957                                                tree type)
1958 {
1959   /* [dcl.fct] forbids type definitions in return types.
1960      Unfortunately, it's not easy to know whether or not we are
1961      processing a return type until after the fact.  */
1962   while (declarator
1963          && (declarator->kind == cdk_pointer
1964              || declarator->kind == cdk_reference
1965              || declarator->kind == cdk_ptrmem))
1966     declarator = declarator->declarator;
1967   if (declarator
1968       && declarator->kind == cdk_function)
1969     {
1970       error ("new types may not be defined in a return type");
1971       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1972               type);
1973     }
1974 }
1975
1976 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1977    "<" in any valid C++ program.  If the next token is indeed "<",
1978    issue a message warning the user about what appears to be an
1979    invalid attempt to form a template-id.  */
1980
1981 static void
1982 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1983                                          tree type)
1984 {
1985   cp_token_position start = 0;
1986
1987   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1988     {
1989       if (TYPE_P (type))
1990         error ("%qT is not a template", type);
1991       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1992         error ("%qE is not a template", type);
1993       else
1994         error ("invalid template-id");
1995       /* Remember the location of the invalid "<".  */
1996       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1997         start = cp_lexer_token_position (parser->lexer, true);
1998       /* Consume the "<".  */
1999       cp_lexer_consume_token (parser->lexer);
2000       /* Parse the template arguments.  */
2001       cp_parser_enclosed_template_argument_list (parser);
2002       /* Permanently remove the invalid template arguments so that
2003          this error message is not issued again.  */
2004       if (start)
2005         cp_lexer_purge_tokens_after (parser->lexer, start);
2006     }
2007 }
2008
2009 /* If parsing an integral constant-expression, issue an error message
2010    about the fact that THING appeared and return true.  Otherwise,
2011    return false.  In either case, set
2012    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2013
2014 static bool
2015 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2016                                             const char *thing)
2017 {
2018   parser->non_integral_constant_expression_p = true;
2019   if (parser->integral_constant_expression_p)
2020     {
2021       if (!parser->allow_non_integral_constant_expression_p)
2022         {
2023           error ("%s cannot appear in a constant-expression", thing);
2024           return true;
2025         }
2026     }
2027   return false;
2028 }
2029
2030 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2031    qualifying scope (or NULL, if none) for ID.  This function commits
2032    to the current active tentative parse, if any.  (Otherwise, the
2033    problematic construct might be encountered again later, resulting
2034    in duplicate error messages.)  */
2035
2036 static void
2037 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2038 {
2039   tree decl, old_scope;
2040   /* Try to lookup the identifier.  */
2041   old_scope = parser->scope;
2042   parser->scope = scope;
2043   decl = cp_parser_lookup_name_simple (parser, id);
2044   parser->scope = old_scope;
2045   /* If the lookup found a template-name, it means that the user forgot
2046   to specify an argument list. Emit a useful error message.  */
2047   if (TREE_CODE (decl) == TEMPLATE_DECL)
2048     error ("invalid use of template-name %qE without an argument list",
2049       decl);
2050   else if (!parser->scope || parser->scope == error_mark_node)
2051     {
2052       /* Issue an error message.  */
2053       error ("%qE does not name a type", id);
2054       /* If we're in a template class, it's possible that the user was
2055          referring to a type from a base class.  For example:
2056
2057            template <typename T> struct A { typedef T X; };
2058            template <typename T> struct B : public A<T> { X x; };
2059
2060          The user should have said "typename A<T>::X".  */
2061       if (processing_template_decl && current_class_type
2062           && TYPE_BINFO (current_class_type))
2063         {
2064           tree b;
2065
2066           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2067                b;
2068                b = TREE_CHAIN (b))
2069             {
2070               tree base_type = BINFO_TYPE (b);
2071               if (CLASS_TYPE_P (base_type)
2072                   && dependent_type_p (base_type))
2073                 {
2074                   tree field;
2075                   /* Go from a particular instantiation of the
2076                      template (which will have an empty TYPE_FIELDs),
2077                      to the main version.  */
2078                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2079                   for (field = TYPE_FIELDS (base_type);
2080                        field;
2081                        field = TREE_CHAIN (field))
2082                     if (TREE_CODE (field) == TYPE_DECL
2083                         && DECL_NAME (field) == id)
2084                       {
2085                         inform ("(perhaps %<typename %T::%E%> was intended)",
2086                                 BINFO_TYPE (b), id);
2087                         break;
2088                       }
2089                   if (field)
2090                     break;
2091                 }
2092             }
2093         }
2094     }
2095   /* Here we diagnose qualified-ids where the scope is actually correct,
2096      but the identifier does not resolve to a valid type name.  */
2097   else
2098     {
2099       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2100         error ("%qE in namespace %qE does not name a type",
2101                id, parser->scope);
2102       else if (TYPE_P (parser->scope))
2103         error ("%qE in class %qT does not name a type", id, parser->scope);
2104       else
2105         gcc_unreachable ();
2106     }
2107   cp_parser_commit_to_tentative_parse (parser);
2108 }
2109
2110 /* Check for a common situation where a type-name should be present,
2111    but is not, and issue a sensible error message.  Returns true if an
2112    invalid type-name was detected.
2113
2114    The situation handled by this function are variable declarations of the
2115    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116    Usually, `ID' should name a type, but if we got here it means that it
2117    does not. We try to emit the best possible error message depending on
2118    how exactly the id-expression looks like.
2119 */
2120
2121 static bool
2122 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2123 {
2124   tree id;
2125
2126   cp_parser_parse_tentatively (parser);
2127   id = cp_parser_id_expression (parser,
2128                                 /*template_keyword_p=*/false,
2129                                 /*check_dependency_p=*/true,
2130                                 /*template_p=*/NULL,
2131                                 /*declarator_p=*/true);
2132   /* After the id-expression, there should be a plain identifier,
2133      otherwise this is not a simple variable declaration. Also, if
2134      the scope is dependent, we cannot do much.  */
2135   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2136       || (parser->scope && TYPE_P (parser->scope)
2137           && dependent_type_p (parser->scope)))
2138     {
2139       cp_parser_abort_tentative_parse (parser);
2140       return false;
2141     }
2142   if (!cp_parser_parse_definitely (parser)
2143       || TREE_CODE (id) != IDENTIFIER_NODE)
2144     return false;
2145
2146   /* Emit a diagnostic for the invalid type.  */
2147   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2148   /* Skip to the end of the declaration; there's no point in
2149      trying to process it.  */
2150   cp_parser_skip_to_end_of_block_or_statement (parser);
2151   return true;
2152 }
2153
2154 /* Consume tokens up to, and including, the next non-nested closing `)'.
2155    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2156    are doing error recovery. Returns -1 if OR_COMMA is true and we
2157    found an unnested comma.  */
2158
2159 static int
2160 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2161                                        bool recovering,
2162                                        bool or_comma,
2163                                        bool consume_paren)
2164 {
2165   unsigned paren_depth = 0;
2166   unsigned brace_depth = 0;
2167   int result;
2168
2169   if (recovering && !or_comma
2170       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2171     return 0;
2172
2173   while (true)
2174     {
2175       cp_token *token;
2176
2177       /* If we've run out of tokens, then there is no closing `)'.  */
2178       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2179         {
2180           result = 0;
2181           break;
2182         }
2183
2184       token = cp_lexer_peek_token (parser->lexer);
2185
2186       /* This matches the processing in skip_to_end_of_statement.  */
2187       if (token->type == CPP_SEMICOLON && !brace_depth)
2188         {
2189           result = 0;
2190           break;
2191         }
2192       if (token->type == CPP_OPEN_BRACE)
2193         ++brace_depth;
2194       if (token->type == CPP_CLOSE_BRACE)
2195         {
2196           if (!brace_depth--)
2197             {
2198               result = 0;
2199               break;
2200             }
2201         }
2202       if (recovering && or_comma && token->type == CPP_COMMA
2203           && !brace_depth && !paren_depth)
2204         {
2205           result = -1;
2206           break;
2207         }
2208
2209       if (!brace_depth)
2210         {
2211           /* If it is an `(', we have entered another level of nesting.  */
2212           if (token->type == CPP_OPEN_PAREN)
2213             ++paren_depth;
2214           /* If it is a `)', then we might be done.  */
2215           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2216             {
2217               if (consume_paren)
2218                 cp_lexer_consume_token (parser->lexer);
2219               {
2220                 result = 1;
2221                 break;
2222               }
2223             }
2224         }
2225
2226       /* Consume the token.  */
2227       cp_lexer_consume_token (parser->lexer);
2228     }
2229
2230   return result;
2231 }
2232
2233 /* Consume tokens until we reach the end of the current statement.
2234    Normally, that will be just before consuming a `;'.  However, if a
2235    non-nested `}' comes first, then we stop before consuming that.  */
2236
2237 static void
2238 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2239 {
2240   unsigned nesting_depth = 0;
2241
2242   while (true)
2243     {
2244       cp_token *token;
2245
2246       /* Peek at the next token.  */
2247       token = cp_lexer_peek_token (parser->lexer);
2248       /* If we've run out of tokens, stop.  */
2249       if (token->type == CPP_EOF)
2250         break;
2251       /* If the next token is a `;', we have reached the end of the
2252          statement.  */
2253       if (token->type == CPP_SEMICOLON && !nesting_depth)
2254         break;
2255       /* If the next token is a non-nested `}', then we have reached
2256          the end of the current block.  */
2257       if (token->type == CPP_CLOSE_BRACE)
2258         {
2259           /* If this is a non-nested `}', stop before consuming it.
2260              That way, when confronted with something like:
2261
2262                { 3 + }
2263
2264              we stop before consuming the closing `}', even though we
2265              have not yet reached a `;'.  */
2266           if (nesting_depth == 0)
2267             break;
2268           /* If it is the closing `}' for a block that we have
2269              scanned, stop -- but only after consuming the token.
2270              That way given:
2271
2272                 void f g () { ... }
2273                 typedef int I;
2274
2275              we will stop after the body of the erroneously declared
2276              function, but before consuming the following `typedef'
2277              declaration.  */
2278           if (--nesting_depth == 0)
2279             {
2280               cp_lexer_consume_token (parser->lexer);
2281               break;
2282             }
2283         }
2284       /* If it the next token is a `{', then we are entering a new
2285          block.  Consume the entire block.  */
2286       else if (token->type == CPP_OPEN_BRACE)
2287         ++nesting_depth;
2288       /* Consume the token.  */
2289       cp_lexer_consume_token (parser->lexer);
2290     }
2291 }
2292
2293 /* This function is called at the end of a statement or declaration.
2294    If the next token is a semicolon, it is consumed; otherwise, error
2295    recovery is attempted.  */
2296
2297 static void
2298 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2299 {
2300   /* Look for the trailing `;'.  */
2301   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2302     {
2303       /* If there is additional (erroneous) input, skip to the end of
2304          the statement.  */
2305       cp_parser_skip_to_end_of_statement (parser);
2306       /* If the next token is now a `;', consume it.  */
2307       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2308         cp_lexer_consume_token (parser->lexer);
2309     }
2310 }
2311
2312 /* Skip tokens until we have consumed an entire block, or until we
2313    have consumed a non-nested `;'.  */
2314
2315 static void
2316 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2317 {
2318   int nesting_depth = 0;
2319
2320   while (nesting_depth >= 0)
2321     {
2322       cp_token *token = cp_lexer_peek_token (parser->lexer);
2323
2324       if (token->type == CPP_EOF)
2325         break;
2326
2327       switch (token->type)
2328         {
2329         case CPP_EOF:
2330           /* If we've run out of tokens, stop.  */
2331           nesting_depth = -1;
2332           continue;
2333
2334         case CPP_SEMICOLON:
2335           /* Stop if this is an unnested ';'. */
2336           if (!nesting_depth)
2337             nesting_depth = -1;
2338           break;
2339
2340         case CPP_CLOSE_BRACE:
2341           /* Stop if this is an unnested '}', or closes the outermost
2342              nesting level.  */
2343           nesting_depth--;
2344           if (!nesting_depth)
2345             nesting_depth = -1;
2346           break;
2347
2348         case CPP_OPEN_BRACE:
2349           /* Nest. */
2350           nesting_depth++;
2351           break;
2352
2353         default:
2354           break;
2355         }
2356
2357       /* Consume the token.  */
2358       cp_lexer_consume_token (parser->lexer);
2359
2360     }
2361 }
2362
2363 /* Skip tokens until a non-nested closing curly brace is the next
2364    token.  */
2365
2366 static void
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2368 {
2369   unsigned nesting_depth = 0;
2370
2371   while (true)
2372     {
2373       cp_token *token;
2374
2375       /* Peek at the next token.  */
2376       token = cp_lexer_peek_token (parser->lexer);
2377       /* If we've run out of tokens, stop.  */
2378       if (token->type == CPP_EOF)
2379         break;
2380       /* If the next token is a non-nested `}', then we have reached
2381          the end of the current block.  */
2382       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2383         break;
2384       /* If it the next token is a `{', then we are entering a new
2385          block.  Consume the entire block.  */
2386       else if (token->type == CPP_OPEN_BRACE)
2387         ++nesting_depth;
2388       /* Consume the token.  */
2389       cp_lexer_consume_token (parser->lexer);
2390     }
2391 }
2392
2393 /* This is a simple wrapper around make_typename_type. When the id is
2394    an unresolved identifier node, we can provide a superior diagnostic
2395    using cp_parser_diagnose_invalid_type_name.  */
2396
2397 static tree
2398 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2399 {
2400   tree result;
2401   if (TREE_CODE (id) == IDENTIFIER_NODE)
2402     {
2403       result = make_typename_type (scope, id, typename_type,
2404                                    /*complain=*/0);
2405       if (result == error_mark_node)
2406         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2407       return result;
2408     }
2409   return make_typename_type (scope, id, typename_type, tf_error);
2410 }
2411
2412
2413 /* Create a new C++ parser.  */
2414
2415 static cp_parser *
2416 cp_parser_new (void)
2417 {
2418   cp_parser *parser;
2419   cp_lexer *lexer;
2420   unsigned i;
2421
2422   /* cp_lexer_new_main is called before calling ggc_alloc because
2423      cp_lexer_new_main might load a PCH file.  */
2424   lexer = cp_lexer_new_main ();
2425
2426   /* Initialize the binops_by_token so that we can get the tree
2427      directly from the token.  */
2428   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2429     binops_by_token[binops[i].token_type] = binops[i];
2430
2431   parser = GGC_CNEW (cp_parser);
2432   parser->lexer = lexer;
2433   parser->context = cp_parser_context_new (NULL);
2434
2435   /* For now, we always accept GNU extensions.  */
2436   parser->allow_gnu_extensions_p = 1;
2437
2438   /* The `>' token is a greater-than operator, not the end of a
2439      template-id.  */
2440   parser->greater_than_is_operator_p = true;
2441
2442   parser->default_arg_ok_p = true;
2443
2444   /* We are not parsing a constant-expression.  */
2445   parser->integral_constant_expression_p = false;
2446   parser->allow_non_integral_constant_expression_p = false;
2447   parser->non_integral_constant_expression_p = false;
2448
2449   /* Local variable names are not forbidden.  */
2450   parser->local_variables_forbidden_p = false;
2451
2452   /* We are not processing an `extern "C"' declaration.  */
2453   parser->in_unbraced_linkage_specification_p = false;
2454
2455   /* We are not processing a declarator.  */
2456   parser->in_declarator_p = false;
2457
2458   /* We are not processing a template-argument-list.  */
2459   parser->in_template_argument_list_p = false;
2460
2461   /* We are not in an iteration statement.  */
2462   parser->in_iteration_statement_p = false;
2463
2464   /* We are not in a switch statement.  */
2465   parser->in_switch_statement_p = false;
2466
2467   /* We are not parsing a type-id inside an expression.  */
2468   parser->in_type_id_in_expr_p = false;
2469
2470   /* Declarations aren't implicitly extern "C".  */
2471   parser->implicit_extern_c = false;
2472
2473   /* String literals should be translated to the execution character set.  */
2474   parser->translate_strings_p = true;
2475
2476   /* The unparsed function queue is empty.  */
2477   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2478
2479   /* There are no classes being defined.  */
2480   parser->num_classes_being_defined = 0;
2481
2482   /* No template parameters apply.  */
2483   parser->num_template_parameter_lists = 0;
2484
2485   return parser;
2486 }
2487
2488 /* Create a cp_lexer structure which will emit the tokens in CACHE
2489    and push it onto the parser's lexer stack.  This is used for delayed
2490    parsing of in-class method bodies and default arguments, and should
2491    not be confused with tentative parsing.  */
2492 static void
2493 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2494 {
2495   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2496   lexer->next = parser->lexer;
2497   parser->lexer = lexer;
2498
2499   /* Move the current source position to that of the first token in the
2500      new lexer.  */
2501   cp_lexer_set_source_position_from_token (lexer->next_token);
2502 }
2503
2504 /* Pop the top lexer off the parser stack.  This is never used for the
2505    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2506 static void
2507 cp_parser_pop_lexer (cp_parser *parser)
2508 {
2509   cp_lexer *lexer = parser->lexer;
2510   parser->lexer = lexer->next;
2511   cp_lexer_destroy (lexer);
2512
2513   /* Put the current source position back where it was before this
2514      lexer was pushed.  */
2515   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2516 }
2517
2518 /* Lexical conventions [gram.lex]  */
2519
2520 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2521    identifier.  */
2522
2523 static tree
2524 cp_parser_identifier (cp_parser* parser)
2525 {
2526   cp_token *token;
2527
2528   /* Look for the identifier.  */
2529   token = cp_parser_require (parser, CPP_NAME, "identifier");
2530   /* Return the value.  */
2531   return token ? token->value : error_mark_node;
2532 }
2533
2534 /* Parse a sequence of adjacent string constants.  Returns a
2535    TREE_STRING representing the combined, nul-terminated string
2536    constant.  If TRANSLATE is true, translate the string to the
2537    execution character set.  If WIDE_OK is true, a wide string is
2538    invalid here.
2539
2540    C++98 [lex.string] says that if a narrow string literal token is
2541    adjacent to a wide string literal token, the behavior is undefined.
2542    However, C99 6.4.5p4 says that this results in a wide string literal.
2543    We follow C99 here, for consistency with the C front end.
2544
2545    This code is largely lifted from lex_string() in c-lex.c.
2546
2547    FUTURE: ObjC++ will need to handle @-strings here.  */
2548 static tree
2549 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2550 {
2551   tree value;
2552   bool wide = false;
2553   size_t count;
2554   struct obstack str_ob;
2555   cpp_string str, istr, *strs;
2556   cp_token *tok;
2557
2558   tok = cp_lexer_peek_token (parser->lexer);
2559   if (!cp_parser_is_string_literal (tok))
2560     {
2561       cp_parser_error (parser, "expected string-literal");
2562       return error_mark_node;
2563     }
2564
2565   /* Try to avoid the overhead of creating and destroying an obstack
2566      for the common case of just one string.  */
2567   if (!cp_parser_is_string_literal
2568       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2569     {
2570       cp_lexer_consume_token (parser->lexer);
2571
2572       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2573       str.len = TREE_STRING_LENGTH (tok->value);
2574       count = 1;
2575       if (tok->type == CPP_WSTRING)
2576         wide = true;
2577
2578       strs = &str;
2579     }
2580   else
2581     {
2582       gcc_obstack_init (&str_ob);
2583       count = 0;
2584
2585       do
2586         {
2587           cp_lexer_consume_token (parser->lexer);
2588           count++;
2589           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2590           str.len = TREE_STRING_LENGTH (tok->value);
2591           if (tok->type == CPP_WSTRING)
2592             wide = true;
2593
2594           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2595
2596           tok = cp_lexer_peek_token (parser->lexer);
2597         }
2598       while (cp_parser_is_string_literal (tok));
2599
2600       strs = (cpp_string *) obstack_finish (&str_ob);
2601     }
2602
2603   if (wide && !wide_ok)
2604     {
2605       cp_parser_error (parser, "a wide string is invalid in this context");
2606       wide = false;
2607     }
2608
2609   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2610       (parse_in, strs, count, &istr, wide))
2611     {
2612       value = build_string (istr.len, (char *)istr.text);
2613       free ((void *)istr.text);
2614
2615       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2616       value = fix_string_type (value);
2617     }
2618   else
2619     /* cpp_interpret_string has issued an error.  */
2620     value = error_mark_node;
2621
2622   if (count > 1)
2623     obstack_free (&str_ob, 0);
2624
2625   return value;
2626 }
2627
2628
2629 /* Basic concepts [gram.basic]  */
2630
2631 /* Parse a translation-unit.
2632
2633    translation-unit:
2634      declaration-seq [opt]
2635
2636    Returns TRUE if all went well.  */
2637
2638 static bool
2639 cp_parser_translation_unit (cp_parser* parser)
2640 {
2641   /* The address of the first non-permanent object on the declarator
2642      obstack.  */
2643   static void *declarator_obstack_base;
2644
2645   bool success;
2646
2647   /* Create the declarator obstack, if necessary.  */
2648   if (!cp_error_declarator)
2649     {
2650       gcc_obstack_init (&declarator_obstack);
2651       /* Create the error declarator.  */
2652       cp_error_declarator = make_declarator (cdk_error);
2653       /* Create the empty parameter list.  */
2654       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2655       /* Remember where the base of the declarator obstack lies.  */
2656       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2657     }
2658
2659   cp_parser_declaration_seq_opt (parser);
2660   
2661   /* If there are no tokens left then all went well.  */
2662   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2663     {
2664       /* Get rid of the token array; we don't need it any more.  */
2665       cp_lexer_destroy (parser->lexer);
2666       parser->lexer = NULL;
2667       
2668       /* This file might have been a context that's implicitly extern
2669          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2670       if (parser->implicit_extern_c)
2671         {
2672           pop_lang_context ();
2673           parser->implicit_extern_c = false;
2674         }
2675       
2676       /* Finish up.  */
2677       finish_translation_unit ();
2678       
2679       success = true;
2680     }
2681   else
2682     {
2683       cp_parser_error (parser, "expected declaration");
2684       success = false;
2685     }
2686   
2687   /* Make sure the declarator obstack was fully cleaned up.  */
2688   gcc_assert (obstack_next_free (&declarator_obstack)
2689               == declarator_obstack_base);
2690
2691   /* All went well.  */
2692   return success;
2693 }
2694
2695 /* Expressions [gram.expr] */
2696
2697 /* Parse a primary-expression.
2698
2699    primary-expression:
2700      literal
2701      this
2702      ( expression )
2703      id-expression
2704
2705    GNU Extensions:
2706
2707    primary-expression:
2708      ( compound-statement )
2709      __builtin_va_arg ( assignment-expression , type-id )
2710
2711    Objective-C++ Extension:
2712
2713    primary-expression:
2714      objc-expression
2715
2716    literal:
2717      __null
2718
2719    CAST_P is true if this primary expression is the target of a cast.
2720
2721    Returns a representation of the expression.
2722
2723    *IDK indicates what kind of id-expression (if any) was present.
2724
2725    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2726    used as the operand of a pointer-to-member.  In that case,
2727    *QUALIFYING_CLASS gives the class that is used as the qualifying
2728    class in the pointer-to-member.  */
2729
2730 static tree
2731 cp_parser_primary_expression (cp_parser *parser,
2732                               bool cast_p,
2733                               cp_id_kind *idk,
2734                               tree *qualifying_class)
2735 {
2736   cp_token *token;
2737
2738   /* Assume the primary expression is not an id-expression.  */
2739   *idk = CP_ID_KIND_NONE;
2740   /* And that it cannot be used as pointer-to-member.  */
2741   *qualifying_class = NULL_TREE;
2742
2743   /* Peek at the next token.  */
2744   token = cp_lexer_peek_token (parser->lexer);
2745   switch (token->type)
2746     {
2747       /* literal:
2748            integer-literal
2749            character-literal
2750            floating-literal
2751            string-literal
2752            boolean-literal  */
2753     case CPP_CHAR:
2754     case CPP_WCHAR:
2755     case CPP_NUMBER:
2756       token = cp_lexer_consume_token (parser->lexer);
2757       /* Floating-point literals are only allowed in an integral
2758          constant expression if they are cast to an integral or
2759          enumeration type.  */
2760       if (TREE_CODE (token->value) == REAL_CST
2761           && parser->integral_constant_expression_p
2762           && pedantic)
2763         {
2764           /* CAST_P will be set even in invalid code like "int(2.7 +
2765              ...)".   Therefore, we have to check that the next token
2766              is sure to end the cast.  */
2767           if (cast_p)
2768             {
2769               cp_token *next_token;
2770
2771               next_token = cp_lexer_peek_token (parser->lexer);
2772               if (/* The comma at the end of an
2773                      enumerator-definition.  */
2774                   next_token->type != CPP_COMMA
2775                   /* The curly brace at the end of an enum-specifier.  */
2776                   && next_token->type != CPP_CLOSE_BRACE
2777                   /* The end of a statement.  */
2778                   && next_token->type != CPP_SEMICOLON
2779                   /* The end of the cast-expression.  */
2780                   && next_token->type != CPP_CLOSE_PAREN
2781                   /* The end of an array bound.  */
2782                   && next_token->type != CPP_CLOSE_SQUARE
2783                   /* The closing ">" in a template-argument-list.  */
2784                   && (next_token->type != CPP_GREATER
2785                       || parser->greater_than_is_operator_p))
2786                 cast_p = false;
2787             }
2788
2789           /* If we are within a cast, then the constraint that the
2790              cast is to an integral or enumeration type will be
2791              checked at that point.  If we are not within a cast, then
2792              this code is invalid.  */
2793           if (!cast_p)
2794             cp_parser_non_integral_constant_expression
2795               (parser, "floating-point literal");
2796         }
2797       return token->value;
2798
2799     case CPP_STRING:
2800     case CPP_WSTRING:
2801       /* ??? Should wide strings be allowed when parser->translate_strings_p
2802          is false (i.e. in attributes)?  If not, we can kill the third
2803          argument to cp_parser_string_literal.  */
2804       return cp_parser_string_literal (parser,
2805                                        parser->translate_strings_p,
2806                                        true);
2807
2808     case CPP_OPEN_PAREN:
2809       {
2810         tree expr;
2811         bool saved_greater_than_is_operator_p;
2812
2813         /* Consume the `('.  */
2814         cp_lexer_consume_token (parser->lexer);
2815         /* Within a parenthesized expression, a `>' token is always
2816            the greater-than operator.  */
2817         saved_greater_than_is_operator_p
2818           = parser->greater_than_is_operator_p;
2819         parser->greater_than_is_operator_p = true;
2820         /* If we see `( { ' then we are looking at the beginning of
2821            a GNU statement-expression.  */
2822         if (cp_parser_allow_gnu_extensions_p (parser)
2823             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2824           {
2825             /* Statement-expressions are not allowed by the standard.  */
2826             if (pedantic)
2827               pedwarn ("ISO C++ forbids braced-groups within expressions");
2828
2829             /* And they're not allowed outside of a function-body; you
2830                cannot, for example, write:
2831
2832                  int i = ({ int j = 3; j + 1; });
2833
2834                at class or namespace scope.  */
2835             if (!at_function_scope_p ())
2836               error ("statement-expressions are allowed only inside functions");
2837             /* Start the statement-expression.  */
2838             expr = begin_stmt_expr ();
2839             /* Parse the compound-statement.  */
2840             cp_parser_compound_statement (parser, expr, false);
2841             /* Finish up.  */
2842             expr = finish_stmt_expr (expr, false);
2843           }
2844         else
2845           {
2846             /* Parse the parenthesized expression.  */
2847             expr = cp_parser_expression (parser, cast_p);
2848             /* Let the front end know that this expression was
2849                enclosed in parentheses. This matters in case, for
2850                example, the expression is of the form `A::B', since
2851                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2852                not.  */
2853             finish_parenthesized_expr (expr);
2854           }
2855         /* The `>' token might be the end of a template-id or
2856            template-parameter-list now.  */
2857         parser->greater_than_is_operator_p
2858           = saved_greater_than_is_operator_p;
2859         /* Consume the `)'.  */
2860         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2861           cp_parser_skip_to_end_of_statement (parser);
2862
2863         return expr;
2864       }
2865
2866     case CPP_KEYWORD:
2867       switch (token->keyword)
2868         {
2869           /* These two are the boolean literals.  */
2870         case RID_TRUE:
2871           cp_lexer_consume_token (parser->lexer);
2872           return boolean_true_node;
2873         case RID_FALSE:
2874           cp_lexer_consume_token (parser->lexer);
2875           return boolean_false_node;
2876
2877           /* The `__null' literal.  */
2878         case RID_NULL:
2879           cp_lexer_consume_token (parser->lexer);
2880           return null_node;
2881
2882           /* Recognize the `this' keyword.  */
2883         case RID_THIS:
2884           cp_lexer_consume_token (parser->lexer);
2885           if (parser->local_variables_forbidden_p)
2886             {
2887               error ("%<this%> may not be used in this context");
2888               return error_mark_node;
2889             }
2890           /* Pointers cannot appear in constant-expressions.  */
2891           if (cp_parser_non_integral_constant_expression (parser,
2892                                                           "`this'"))
2893             return error_mark_node;
2894           return finish_this_expr ();
2895
2896           /* The `operator' keyword can be the beginning of an
2897              id-expression.  */
2898         case RID_OPERATOR:
2899           goto id_expression;
2900
2901         case RID_FUNCTION_NAME:
2902         case RID_PRETTY_FUNCTION_NAME:
2903         case RID_C99_FUNCTION_NAME:
2904           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2905              __func__ are the names of variables -- but they are
2906              treated specially.  Therefore, they are handled here,
2907              rather than relying on the generic id-expression logic
2908              below.  Grammatically, these names are id-expressions.
2909
2910              Consume the token.  */
2911           token = cp_lexer_consume_token (parser->lexer);
2912           /* Look up the name.  */
2913           return finish_fname (token->value);
2914
2915         case RID_VA_ARG:
2916           {
2917             tree expression;
2918             tree type;
2919
2920             /* The `__builtin_va_arg' construct is used to handle
2921                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2922             cp_lexer_consume_token (parser->lexer);
2923             /* Look for the opening `('.  */
2924             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2925             /* Now, parse the assignment-expression.  */
2926             expression = cp_parser_assignment_expression (parser,
2927                                                           /*cast_p=*/false);
2928             /* Look for the `,'.  */
2929             cp_parser_require (parser, CPP_COMMA, "`,'");
2930             /* Parse the type-id.  */
2931             type = cp_parser_type_id (parser);
2932             /* Look for the closing `)'.  */
2933             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2934             /* Using `va_arg' in a constant-expression is not
2935                allowed.  */
2936             if (cp_parser_non_integral_constant_expression (parser,
2937                                                             "`va_arg'"))
2938               return error_mark_node;
2939             return build_x_va_arg (expression, type);
2940           }
2941
2942         case RID_OFFSETOF:
2943           return cp_parser_builtin_offsetof (parser);
2944
2945           /* Objective-C++ expressions.  */
2946         case RID_AT_ENCODE:
2947         case RID_AT_PROTOCOL:
2948         case RID_AT_SELECTOR:
2949           return cp_parser_objc_expression (parser);
2950
2951         default:
2952           cp_parser_error (parser, "expected primary-expression");
2953           return error_mark_node;
2954         }
2955
2956       /* An id-expression can start with either an identifier, a
2957          `::' as the beginning of a qualified-id, or the "operator"
2958          keyword.  */
2959     case CPP_NAME:
2960     case CPP_SCOPE:
2961     case CPP_TEMPLATE_ID:
2962     case CPP_NESTED_NAME_SPECIFIER:
2963       {
2964         tree id_expression;
2965         tree decl;
2966         const char *error_msg;
2967
2968       id_expression:
2969         /* Parse the id-expression.  */
2970         id_expression
2971           = cp_parser_id_expression (parser,
2972                                      /*template_keyword_p=*/false,
2973                                      /*check_dependency_p=*/true,
2974                                      /*template_p=*/NULL,
2975                                      /*declarator_p=*/false);
2976         if (id_expression == error_mark_node)
2977           return error_mark_node;
2978         /* If we have a template-id, then no further lookup is
2979            required.  If the template-id was for a template-class, we
2980            will sometimes have a TYPE_DECL at this point.  */
2981         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2982             || TREE_CODE (id_expression) == TYPE_DECL)
2983           decl = id_expression;
2984         /* Look up the name.  */
2985         else
2986           {
2987             bool ambiguous_p;
2988
2989             decl = cp_parser_lookup_name (parser, id_expression,
2990                                           none_type,
2991                                           /*is_template=*/false,
2992                                           /*is_namespace=*/false,
2993                                           /*check_dependency=*/true,
2994                                           &ambiguous_p);
2995             /* If the lookup was ambiguous, an error will already have
2996                been issued.  */
2997             if (ambiguous_p)
2998               return error_mark_node;
2999
3000             /* In Objective-C++, an instance variable (ivar) may be preferred
3001                to whatever cp_parser_lookup_name() found.  */
3002             decl = objc_lookup_ivar (decl, id_expression);
3003
3004             /* If name lookup gives us a SCOPE_REF, then the
3005                qualifying scope was dependent.  Just propagate the
3006                name.  */
3007             if (TREE_CODE (decl) == SCOPE_REF)
3008               {
3009                 if (TYPE_P (TREE_OPERAND (decl, 0)))
3010                   *qualifying_class = TREE_OPERAND (decl, 0);
3011                 return decl;
3012               }
3013             /* Check to see if DECL is a local variable in a context
3014                where that is forbidden.  */
3015             if (parser->local_variables_forbidden_p
3016                 && local_variable_p (decl))
3017               {
3018                 /* It might be that we only found DECL because we are
3019                    trying to be generous with pre-ISO scoping rules.
3020                    For example, consider:
3021
3022                      int i;
3023                      void g() {
3024                        for (int i = 0; i < 10; ++i) {}
3025                        extern void f(int j = i);
3026                      }
3027
3028                    Here, name look up will originally find the out
3029                    of scope `i'.  We need to issue a warning message,
3030                    but then use the global `i'.  */
3031                 decl = check_for_out_of_scope_variable (decl);
3032                 if (local_variable_p (decl))
3033                   {
3034                     error ("local variable %qD may not appear in this context",
3035                            decl);
3036                     return error_mark_node;
3037                   }
3038               }
3039           }
3040
3041         decl = finish_id_expression (id_expression, decl, parser->scope,
3042                                      idk, qualifying_class,
3043                                      parser->integral_constant_expression_p,
3044                                      parser->allow_non_integral_constant_expression_p,
3045                                      &parser->non_integral_constant_expression_p,
3046                                      &error_msg);
3047         if (error_msg)
3048           cp_parser_error (parser, error_msg);
3049         return decl;
3050       }
3051
3052       /* Anything else is an error.  */
3053     default:
3054       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3055       if (c_dialect_objc ()
3056           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3057         return cp_parser_objc_expression (parser);
3058
3059       cp_parser_error (parser, "expected primary-expression");
3060       return error_mark_node;
3061     }
3062 }
3063
3064 /* Parse an id-expression.
3065
3066    id-expression:
3067      unqualified-id
3068      qualified-id
3069
3070    qualified-id:
3071      :: [opt] nested-name-specifier template [opt] unqualified-id
3072      :: identifier
3073      :: operator-function-id
3074      :: template-id
3075
3076    Return a representation of the unqualified portion of the
3077    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3078    a `::' or nested-name-specifier.
3079
3080    Often, if the id-expression was a qualified-id, the caller will
3081    want to make a SCOPE_REF to represent the qualified-id.  This
3082    function does not do this in order to avoid wastefully creating
3083    SCOPE_REFs when they are not required.
3084
3085    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3086    `template' keyword.
3087
3088    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3089    uninstantiated templates.
3090
3091    If *TEMPLATE_P is non-NULL, it is set to true iff the
3092    `template' keyword is used to explicitly indicate that the entity
3093    named is a template.
3094
3095    If DECLARATOR_P is true, the id-expression is appearing as part of
3096    a declarator, rather than as part of an expression.  */
3097
3098 static tree
3099 cp_parser_id_expression (cp_parser *parser,
3100                          bool template_keyword_p,
3101                          bool check_dependency_p,
3102                          bool *template_p,
3103                          bool declarator_p)
3104 {
3105   bool global_scope_p;
3106   bool nested_name_specifier_p;
3107
3108   /* Assume the `template' keyword was not used.  */
3109   if (template_p)
3110     *template_p = false;
3111
3112   /* Look for the optional `::' operator.  */
3113   global_scope_p
3114     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3115        != NULL_TREE);
3116   /* Look for the optional nested-name-specifier.  */
3117   nested_name_specifier_p
3118     = (cp_parser_nested_name_specifier_opt (parser,
3119                                             /*typename_keyword_p=*/false,
3120                                             check_dependency_p,
3121                                             /*type_p=*/false,
3122                                             declarator_p)
3123        != NULL_TREE);
3124   /* If there is a nested-name-specifier, then we are looking at
3125      the first qualified-id production.  */
3126   if (nested_name_specifier_p)
3127     {
3128       tree saved_scope;
3129       tree saved_object_scope;
3130       tree saved_qualifying_scope;
3131       tree unqualified_id;
3132       bool is_template;
3133
3134       /* See if the next token is the `template' keyword.  */
3135       if (!template_p)
3136         template_p = &is_template;
3137       *template_p = cp_parser_optional_template_keyword (parser);
3138       /* Name lookup we do during the processing of the
3139          unqualified-id might obliterate SCOPE.  */
3140       saved_scope = parser->scope;
3141       saved_object_scope = parser->object_scope;
3142       saved_qualifying_scope = parser->qualifying_scope;
3143       /* Process the final unqualified-id.  */
3144       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3145                                                  check_dependency_p,
3146                                                  declarator_p);
3147       /* Restore the SAVED_SCOPE for our caller.  */
3148       parser->scope = saved_scope;
3149       parser->object_scope = saved_object_scope;
3150       parser->qualifying_scope = saved_qualifying_scope;
3151
3152       return unqualified_id;
3153     }
3154   /* Otherwise, if we are in global scope, then we are looking at one
3155      of the other qualified-id productions.  */
3156   else if (global_scope_p)
3157     {
3158       cp_token *token;
3159       tree id;
3160
3161       /* Peek at the next token.  */
3162       token = cp_lexer_peek_token (parser->lexer);
3163
3164       /* If it's an identifier, and the next token is not a "<", then
3165          we can avoid the template-id case.  This is an optimization
3166          for this common case.  */
3167       if (token->type == CPP_NAME
3168           && !cp_parser_nth_token_starts_template_argument_list_p
3169                (parser, 2))
3170         return cp_parser_identifier (parser);
3171
3172       cp_parser_parse_tentatively (parser);
3173       /* Try a template-id.  */
3174       id = cp_parser_template_id (parser,
3175                                   /*template_keyword_p=*/false,
3176                                   /*check_dependency_p=*/true,
3177                                   declarator_p);
3178       /* If that worked, we're done.  */
3179       if (cp_parser_parse_definitely (parser))
3180         return id;
3181
3182       /* Peek at the next token.  (Changes in the token buffer may
3183          have invalidated the pointer obtained above.)  */
3184       token = cp_lexer_peek_token (parser->lexer);
3185
3186       switch (token->type)
3187         {
3188         case CPP_NAME:
3189           return cp_parser_identifier (parser);
3190
3191         case CPP_KEYWORD:
3192           if (token->keyword == RID_OPERATOR)
3193             return cp_parser_operator_function_id (parser);
3194           /* Fall through.  */
3195
3196         default:
3197           cp_parser_error (parser, "expected id-expression");
3198           return error_mark_node;
3199         }
3200     }
3201   else
3202     return cp_parser_unqualified_id (parser, template_keyword_p,
3203                                      /*check_dependency_p=*/true,
3204                                      declarator_p);
3205 }
3206
3207 /* Parse an unqualified-id.
3208
3209    unqualified-id:
3210      identifier
3211      operator-function-id
3212      conversion-function-id
3213      ~ class-name
3214      template-id
3215
3216    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3217    keyword, in a construct like `A::template ...'.
3218
3219    Returns a representation of unqualified-id.  For the `identifier'
3220    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3221    production a BIT_NOT_EXPR is returned; the operand of the
3222    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3223    other productions, see the documentation accompanying the
3224    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3225    names are looked up in uninstantiated templates.  If DECLARATOR_P
3226    is true, the unqualified-id is appearing as part of a declarator,
3227    rather than as part of an expression.  */
3228
3229 static tree
3230 cp_parser_unqualified_id (cp_parser* parser,
3231                           bool template_keyword_p,
3232                           bool check_dependency_p,
3233                           bool declarator_p)
3234 {
3235   cp_token *token;
3236
3237   /* Peek at the next token.  */
3238   token = cp_lexer_peek_token (parser->lexer);
3239
3240   switch (token->type)
3241     {
3242     case CPP_NAME:
3243       {
3244         tree id;
3245
3246         /* We don't know yet whether or not this will be a
3247            template-id.  */
3248         cp_parser_parse_tentatively (parser);
3249         /* Try a template-id.  */
3250         id = cp_parser_template_id (parser, template_keyword_p,
3251                                     check_dependency_p,
3252                                     declarator_p);
3253         /* If it worked, we're done.  */
3254         if (cp_parser_parse_definitely (parser))
3255           return id;
3256         /* Otherwise, it's an ordinary identifier.  */
3257         return cp_parser_identifier (parser);
3258       }
3259
3260     case CPP_TEMPLATE_ID:
3261       return cp_parser_template_id (parser, template_keyword_p,
3262                                     check_dependency_p,
3263                                     declarator_p);
3264
3265     case CPP_COMPL:
3266       {
3267         tree type_decl;
3268         tree qualifying_scope;
3269         tree object_scope;
3270         tree scope;
3271         bool done;
3272
3273         /* Consume the `~' token.  */
3274         cp_lexer_consume_token (parser->lexer);
3275         /* Parse the class-name.  The standard, as written, seems to
3276            say that:
3277
3278              template <typename T> struct S { ~S (); };
3279              template <typename T> S<T>::~S() {}
3280
3281            is invalid, since `~' must be followed by a class-name, but
3282            `S<T>' is dependent, and so not known to be a class.
3283            That's not right; we need to look in uninstantiated
3284            templates.  A further complication arises from:
3285
3286              template <typename T> void f(T t) {
3287                t.T::~T();
3288              }
3289
3290            Here, it is not possible to look up `T' in the scope of `T'
3291            itself.  We must look in both the current scope, and the
3292            scope of the containing complete expression.
3293
3294            Yet another issue is:
3295
3296              struct S {
3297                int S;
3298                ~S();
3299              };
3300
3301              S::~S() {}
3302
3303            The standard does not seem to say that the `S' in `~S'
3304            should refer to the type `S' and not the data member
3305            `S::S'.  */
3306
3307         /* DR 244 says that we look up the name after the "~" in the
3308            same scope as we looked up the qualifying name.  That idea
3309            isn't fully worked out; it's more complicated than that.  */
3310         scope = parser->scope;
3311         object_scope = parser->object_scope;
3312         qualifying_scope = parser->qualifying_scope;
3313
3314         /* If the name is of the form "X::~X" it's OK.  */
3315         if (scope && TYPE_P (scope)
3316             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3317             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3318                 == CPP_OPEN_PAREN)
3319             && (cp_lexer_peek_token (parser->lexer)->value
3320                 == TYPE_IDENTIFIER (scope)))
3321           {
3322             cp_lexer_consume_token (parser->lexer);
3323             return build_nt (BIT_NOT_EXPR, scope);
3324           }
3325
3326         /* If there was an explicit qualification (S::~T), first look
3327            in the scope given by the qualification (i.e., S).  */
3328         done = false;
3329         type_decl = NULL_TREE;
3330         if (scope)
3331           {
3332             cp_parser_parse_tentatively (parser);
3333             type_decl = cp_parser_class_name (parser,
3334                                               /*typename_keyword_p=*/false,
3335                                               /*template_keyword_p=*/false,
3336                                               none_type,
3337                                               /*check_dependency=*/false,
3338                                               /*class_head_p=*/false,
3339                                               declarator_p);
3340             if (cp_parser_parse_definitely (parser))
3341               done = true;
3342           }
3343         /* In "N::S::~S", look in "N" as well.  */
3344         if (!done && scope && qualifying_scope)
3345           {
3346             cp_parser_parse_tentatively (parser);
3347             parser->scope = qualifying_scope;
3348             parser->object_scope = NULL_TREE;
3349             parser->qualifying_scope = NULL_TREE;
3350             type_decl
3351               = cp_parser_class_name (parser,
3352                                       /*typename_keyword_p=*/false,
3353                                       /*template_keyword_p=*/false,
3354                                       none_type,
3355                                       /*check_dependency=*/false,
3356                                       /*class_head_p=*/false,
3357                                       declarator_p);
3358             if (cp_parser_parse_definitely (parser))
3359               done = true;
3360           }
3361         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3362         else if (!done && object_scope)
3363           {
3364             cp_parser_parse_tentatively (parser);
3365             parser->scope = object_scope;
3366             parser->object_scope = NULL_TREE;
3367             parser->qualifying_scope = NULL_TREE;
3368             type_decl
3369               = cp_parser_class_name (parser,
3370                                       /*typename_keyword_p=*/false,
3371                                       /*template_keyword_p=*/false,
3372                                       none_type,
3373                                       /*check_dependency=*/false,
3374                                       /*class_head_p=*/false,
3375                                       declarator_p);
3376             if (cp_parser_parse_definitely (parser))
3377               done = true;
3378           }
3379         /* Look in the surrounding context.  */
3380         if (!done)
3381           {
3382             parser->scope = NULL_TREE;
3383             parser->object_scope = NULL_TREE;
3384             parser->qualifying_scope = NULL_TREE;
3385             type_decl
3386               = cp_parser_class_name (parser,
3387                                       /*typename_keyword_p=*/false,
3388                                       /*template_keyword_p=*/false,
3389                                       none_type,
3390                                       /*check_dependency=*/false,
3391                                       /*class_head_p=*/false,
3392                                       declarator_p);
3393           }
3394         /* If an error occurred, assume that the name of the
3395            destructor is the same as the name of the qualifying
3396            class.  That allows us to keep parsing after running
3397            into ill-formed destructor names.  */
3398         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3399           return build_nt (BIT_NOT_EXPR, scope);
3400         else if (type_decl == error_mark_node)
3401           return error_mark_node;
3402
3403         /* [class.dtor]
3404
3405            A typedef-name that names a class shall not be used as the
3406            identifier in the declarator for a destructor declaration.  */
3407         if (declarator_p
3408             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3409             && !DECL_SELF_REFERENCE_P (type_decl)
3410             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3411           error ("typedef-name %qD used as destructor declarator",
3412                  type_decl);
3413
3414         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3415       }
3416
3417     case CPP_KEYWORD:
3418       if (token->keyword == RID_OPERATOR)
3419         {
3420           tree id;
3421
3422           /* This could be a template-id, so we try that first.  */
3423           cp_parser_parse_tentatively (parser);
3424           /* Try a template-id.  */
3425           id = cp_parser_template_id (parser, template_keyword_p,
3426                                       /*check_dependency_p=*/true,
3427                                       declarator_p);
3428           /* If that worked, we're done.  */
3429           if (cp_parser_parse_definitely (parser))
3430             return id;
3431           /* We still don't know whether we're looking at an
3432              operator-function-id or a conversion-function-id.  */
3433           cp_parser_parse_tentatively (parser);
3434           /* Try an operator-function-id.  */
3435           id = cp_parser_operator_function_id (parser);
3436           /* If that didn't work, try a conversion-function-id.  */
3437           if (!cp_parser_parse_definitely (parser))
3438             id = cp_parser_conversion_function_id (parser);
3439
3440           return id;
3441         }
3442       /* Fall through.  */
3443
3444     default:
3445       cp_parser_error (parser, "expected unqualified-id");
3446       return error_mark_node;
3447     }
3448 }
3449
3450 /* Parse an (optional) nested-name-specifier.
3451
3452    nested-name-specifier:
3453      class-or-namespace-name :: nested-name-specifier [opt]
3454      class-or-namespace-name :: template nested-name-specifier [opt]
3455
3456    PARSER->SCOPE should be set appropriately before this function is
3457    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3458    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3459    in name lookups.
3460
3461    Sets PARSER->SCOPE to the class (TYPE) or namespace
3462    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3463    it unchanged if there is no nested-name-specifier.  Returns the new
3464    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3465
3466    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3467    part of a declaration and/or decl-specifier.  */
3468
3469 static tree
3470 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3471                                      bool typename_keyword_p,
3472                                      bool check_dependency_p,
3473                                      bool type_p,
3474                                      bool is_declaration)
3475 {
3476   bool success = false;
3477   tree access_check = NULL_TREE;
3478   cp_token_position start = 0;
3479   cp_token *token;
3480
3481   /* If the next token corresponds to a nested name specifier, there
3482      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3483      false, it may have been true before, in which case something
3484      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3485      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3486      CHECK_DEPENDENCY_P is false, we have to fall through into the
3487      main loop.  */
3488   if (check_dependency_p
3489       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3490     {
3491       cp_parser_pre_parsed_nested_name_specifier (parser);
3492       return parser->scope;
3493     }
3494
3495   /* Remember where the nested-name-specifier starts.  */
3496   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3497     start = cp_lexer_token_position (parser->lexer, false);
3498
3499   push_deferring_access_checks (dk_deferred);
3500
3501   while (true)
3502     {
3503       tree new_scope;
3504       tree old_scope;
3505       tree saved_qualifying_scope;
3506       bool template_keyword_p;
3507
3508       /* Spot cases that cannot be the beginning of a
3509          nested-name-specifier.  */
3510       token = cp_lexer_peek_token (parser->lexer);
3511
3512       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3513          the already parsed nested-name-specifier.  */
3514       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3515         {
3516           /* Grab the nested-name-specifier and continue the loop.  */
3517           cp_parser_pre_parsed_nested_name_specifier (parser);
3518           success = true;
3519           continue;
3520         }
3521
3522       /* Spot cases that cannot be the beginning of a
3523          nested-name-specifier.  On the second and subsequent times
3524          through the loop, we look for the `template' keyword.  */
3525       if (success && token->keyword == RID_TEMPLATE)
3526         ;
3527       /* A template-id can start a nested-name-specifier.  */
3528       else if (token->type == CPP_TEMPLATE_ID)
3529         ;
3530       else
3531         {
3532           /* If the next token is not an identifier, then it is
3533              definitely not a class-or-namespace-name.  */
3534           if (token->type != CPP_NAME)
3535             break;
3536           /* If the following token is neither a `<' (to begin a
3537              template-id), nor a `::', then we are not looking at a
3538              nested-name-specifier.  */
3539           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3540           if (token->type != CPP_SCOPE
3541               && !cp_parser_nth_token_starts_template_argument_list_p
3542                   (parser, 2))
3543             break;
3544         }
3545
3546       /* The nested-name-specifier is optional, so we parse
3547          tentatively.  */
3548       cp_parser_parse_tentatively (parser);
3549
3550       /* Look for the optional `template' keyword, if this isn't the
3551          first time through the loop.  */
3552       if (success)
3553         template_keyword_p = cp_parser_optional_template_keyword (parser);
3554       else
3555         template_keyword_p = false;
3556
3557       /* Save the old scope since the name lookup we are about to do
3558          might destroy it.  */
3559       old_scope = parser->scope;
3560       saved_qualifying_scope = parser->qualifying_scope;
3561       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3562          look up names in "X<T>::I" in order to determine that "Y" is
3563          a template.  So, if we have a typename at this point, we make
3564          an effort to look through it.  */
3565       if (is_declaration
3566           && !typename_keyword_p
3567           && parser->scope
3568           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3569         parser->scope = resolve_typename_type (parser->scope,
3570                                                /*only_current_p=*/false);
3571       /* Parse the qualifying entity.  */
3572       new_scope
3573         = cp_parser_class_or_namespace_name (parser,
3574                                              typename_keyword_p,
3575                                              template_keyword_p,
3576                                              check_dependency_p,
3577                                              type_p,
3578                                              is_declaration);
3579       /* Look for the `::' token.  */
3580       cp_parser_require (parser, CPP_SCOPE, "`::'");
3581
3582       /* If we found what we wanted, we keep going; otherwise, we're
3583          done.  */
3584       if (!cp_parser_parse_definitely (parser))
3585         {
3586           bool error_p = false;
3587
3588           /* Restore the OLD_SCOPE since it was valid before the
3589              failed attempt at finding the last
3590              class-or-namespace-name.  */
3591           parser->scope = old_scope;
3592           parser->qualifying_scope = saved_qualifying_scope;
3593           /* If the next token is an identifier, and the one after
3594              that is a `::', then any valid interpretation would have
3595              found a class-or-namespace-name.  */
3596           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3597                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3598                      == CPP_SCOPE)
3599                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3600                      != CPP_COMPL))
3601             {
3602               token = cp_lexer_consume_token (parser->lexer);
3603               if (!error_p)
3604                 {
3605                   tree decl;
3606
3607                   decl = cp_parser_lookup_name_simple (parser, token->value);
3608                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3609                     error ("%qD used without template parameters", decl);
3610                   else
3611                     cp_parser_name_lookup_error
3612                       (parser, token->value, decl,
3613                        "is not a class or namespace");
3614                   parser->scope = NULL_TREE;
3615                   error_p = true;
3616                   /* Treat this as a successful nested-name-specifier
3617                      due to:
3618
3619                      [basic.lookup.qual]
3620
3621                      If the name found is not a class-name (clause
3622                      _class_) or namespace-name (_namespace.def_), the
3623                      program is ill-formed.  */
3624                   success = true;
3625                 }
3626               cp_lexer_consume_token (parser->lexer);
3627             }
3628           break;
3629         }
3630
3631       /* We've found one valid nested-name-specifier.  */
3632       success = true;
3633       /* Make sure we look in the right scope the next time through
3634          the loop.  */
3635       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3636                        ? TREE_TYPE (new_scope)
3637                        : new_scope);
3638       /* If it is a class scope, try to complete it; we are about to
3639          be looking up names inside the class.  */
3640       if (TYPE_P (parser->scope)
3641           /* Since checking types for dependency can be expensive,
3642              avoid doing it if the type is already complete.  */
3643           && !COMPLETE_TYPE_P (parser->scope)
3644           /* Do not try to complete dependent types.  */
3645           && !dependent_type_p (parser->scope))
3646         complete_type (parser->scope);
3647     }
3648
3649   /* Retrieve any deferred checks.  Do not pop this access checks yet
3650      so the memory will not be reclaimed during token replacing below.  */
3651   access_check = get_deferred_access_checks ();
3652
3653   /* If parsing tentatively, replace the sequence of tokens that makes
3654      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3655      token.  That way, should we re-parse the token stream, we will
3656      not have to repeat the effort required to do the parse, nor will
3657      we issue duplicate error messages.  */
3658   if (success && start)
3659     {
3660       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3661
3662       /* Reset the contents of the START token.  */
3663       token->type = CPP_NESTED_NAME_SPECIFIER;
3664       token->value = build_tree_list (access_check, parser->scope);
3665       TREE_TYPE (token->value) = parser->qualifying_scope;
3666       token->keyword = RID_MAX;
3667
3668       /* Purge all subsequent tokens.  */
3669       cp_lexer_purge_tokens_after (parser->lexer, start);
3670     }
3671
3672   pop_deferring_access_checks ();
3673   return success ? parser->scope : NULL_TREE;
3674 }
3675
3676 /* Parse a nested-name-specifier.  See
3677    cp_parser_nested_name_specifier_opt for details.  This function
3678    behaves identically, except that it will an issue an error if no
3679    nested-name-specifier is present.  */
3680
3681 static tree
3682 cp_parser_nested_name_specifier (cp_parser *parser,
3683                                  bool typename_keyword_p,
3684                                  bool check_dependency_p,
3685                                  bool type_p,
3686                                  bool is_declaration)
3687 {
3688   tree scope;
3689
3690   /* Look for the nested-name-specifier.  */
3691   scope = cp_parser_nested_name_specifier_opt (parser,
3692                                                typename_keyword_p,
3693                                                check_dependency_p,
3694                                                type_p,
3695                                                is_declaration);
3696   /* If it was not present, issue an error message.  */
3697   if (!scope)
3698     {
3699       cp_parser_error (parser, "expected nested-name-specifier");
3700       parser->scope = NULL_TREE;
3701     }
3702
3703   return scope;
3704 }
3705
3706 /* Parse a class-or-namespace-name.
3707
3708    class-or-namespace-name:
3709      class-name
3710      namespace-name
3711
3712    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3713    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3714    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3715    TYPE_P is TRUE iff the next name should be taken as a class-name,
3716    even the same name is declared to be another entity in the same
3717    scope.
3718
3719    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3720    specified by the class-or-namespace-name.  If neither is found the
3721    ERROR_MARK_NODE is returned.  */
3722
3723 static tree
3724 cp_parser_class_or_namespace_name (cp_parser *parser,
3725                                    bool typename_keyword_p,
3726                                    bool template_keyword_p,
3727                                    bool check_dependency_p,
3728                                    bool type_p,
3729                                    bool is_declaration)
3730 {
3731   tree saved_scope;
3732   tree saved_qualifying_scope;
3733   tree saved_object_scope;
3734   tree scope;
3735   bool only_class_p;
3736
3737   /* Before we try to parse the class-name, we must save away the
3738      current PARSER->SCOPE since cp_parser_class_name will destroy
3739      it.  */
3740   saved_scope = parser->scope;
3741   saved_qualifying_scope = parser->qualifying_scope;
3742   saved_object_scope = parser->object_scope;
3743   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3744      there is no need to look for a namespace-name.  */
3745   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3746   if (!only_class_p)
3747     cp_parser_parse_tentatively (parser);
3748   scope = cp_parser_class_name (parser,
3749                                 typename_keyword_p,
3750                                 template_keyword_p,
3751                                 type_p ? class_type : none_type,
3752                                 check_dependency_p,
3753                                 /*class_head_p=*/false,
3754                                 is_declaration);
3755   /* If that didn't work, try for a namespace-name.  */
3756   if (!only_class_p && !cp_parser_parse_definitely (parser))
3757     {
3758       /* Restore the saved scope.  */
3759       parser->scope = saved_scope;
3760       parser->qualifying_scope = saved_qualifying_scope;
3761       parser->object_scope = saved_object_scope;
3762       /* If we are not looking at an identifier followed by the scope
3763          resolution operator, then this is not part of a
3764          nested-name-specifier.  (Note that this function is only used
3765          to parse the components of a nested-name-specifier.)  */
3766       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3767           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3768         return error_mark_node;
3769       scope = cp_parser_namespace_name (parser);
3770     }
3771
3772   return scope;
3773 }
3774
3775 /* Parse a postfix-expression.
3776
3777    postfix-expression:
3778      primary-expression
3779      postfix-expression [ expression ]
3780      postfix-expression ( expression-list [opt] )
3781      simple-type-specifier ( expression-list [opt] )
3782      typename :: [opt] nested-name-specifier identifier
3783        ( expression-list [opt] )
3784      typename :: [opt] nested-name-specifier template [opt] template-id
3785        ( expression-list [opt] )
3786      postfix-expression . template [opt] id-expression
3787      postfix-expression -> template [opt] id-expression
3788      postfix-expression . pseudo-destructor-name
3789      postfix-expression -> pseudo-destructor-name
3790      postfix-expression ++
3791      postfix-expression --
3792      dynamic_cast < type-id > ( expression )
3793      static_cast < type-id > ( expression )
3794      reinterpret_cast < type-id > ( expression )
3795      const_cast < type-id > ( expression )
3796      typeid ( expression )
3797      typeid ( type-id )
3798
3799    GNU Extension:
3800
3801    postfix-expression:
3802      ( type-id ) { initializer-list , [opt] }
3803
3804    This extension is a GNU version of the C99 compound-literal
3805    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3806    but they are essentially the same concept.)
3807
3808    If ADDRESS_P is true, the postfix expression is the operand of the
3809    `&' operator.  CAST_P is true if this expression is the target of a
3810    cast.
3811
3812    Returns a representation of the expression.  */
3813
3814 static tree
3815 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3816 {
3817   cp_token *token;
3818   enum rid keyword;
3819   cp_id_kind idk = CP_ID_KIND_NONE;
3820   tree postfix_expression = NULL_TREE;
3821   /* Non-NULL only if the current postfix-expression can be used to
3822      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3823      class used to qualify the member.  */
3824   tree qualifying_class = NULL_TREE;
3825
3826   /* Peek at the next token.  */
3827   token = cp_lexer_peek_token (parser->lexer);
3828   /* Some of the productions are determined by keywords.  */
3829   keyword = token->keyword;
3830   switch (keyword)
3831     {
3832     case RID_DYNCAST:
3833     case RID_STATCAST:
3834     case RID_REINTCAST:
3835     case RID_CONSTCAST:
3836       {
3837         tree type;
3838         tree expression;
3839         const char *saved_message;
3840
3841         /* All of these can be handled in the same way from the point
3842            of view of parsing.  Begin by consuming the token
3843            identifying the cast.  */
3844         cp_lexer_consume_token (parser->lexer);
3845
3846         /* New types cannot be defined in the cast.  */
3847         saved_message = parser->type_definition_forbidden_message;
3848         parser->type_definition_forbidden_message
3849           = "types may not be defined in casts";
3850
3851         /* Look for the opening `<'.  */
3852         cp_parser_require (parser, CPP_LESS, "`<'");
3853         /* Parse the type to which we are casting.  */
3854         type = cp_parser_type_id (parser);
3855         /* Look for the closing `>'.  */
3856         cp_parser_require (parser, CPP_GREATER, "`>'");
3857         /* Restore the old message.  */
3858         parser->type_definition_forbidden_message = saved_message;
3859
3860         /* And the expression which is being cast.  */
3861         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862         expression = cp_parser_expression (parser, /*cast_p=*/true);
3863         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864
3865         /* Only type conversions to integral or enumeration types
3866            can be used in constant-expressions.  */
3867         if (parser->integral_constant_expression_p
3868             && !dependent_type_p (type)
3869             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3870             && (cp_parser_non_integral_constant_expression
3871                 (parser,
3872                  "a cast to a type other than an integral or "
3873                  "enumeration type")))
3874           return error_mark_node;
3875
3876         switch (keyword)
3877           {
3878           case RID_DYNCAST:
3879             postfix_expression
3880               = build_dynamic_cast (type, expression);
3881             break;
3882           case RID_STATCAST:
3883             postfix_expression
3884               = build_static_cast (type, expression);
3885             break;
3886           case RID_REINTCAST:
3887             postfix_expression
3888               = build_reinterpret_cast (type, expression);
3889             break;
3890           case RID_CONSTCAST:
3891             postfix_expression
3892               = build_const_cast (type, expression);
3893             break;
3894           default:
3895             gcc_unreachable ();
3896           }
3897       }
3898       break;
3899
3900     case RID_TYPEID:
3901       {
3902         tree type;
3903         const char *saved_message;
3904         bool saved_in_type_id_in_expr_p;
3905
3906         /* Consume the `typeid' token.  */
3907         cp_lexer_consume_token (parser->lexer);
3908         /* Look for the `(' token.  */
3909         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3910         /* Types cannot be defined in a `typeid' expression.  */
3911         saved_message = parser->type_definition_forbidden_message;
3912         parser->type_definition_forbidden_message
3913           = "types may not be defined in a `typeid\' expression";
3914         /* We can't be sure yet whether we're looking at a type-id or an
3915            expression.  */
3916         cp_parser_parse_tentatively (parser);
3917         /* Try a type-id first.  */
3918         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3919         parser->in_type_id_in_expr_p = true;
3920         type = cp_parser_type_id (parser);
3921         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3922         /* Look for the `)' token.  Otherwise, we can't be sure that
3923            we're not looking at an expression: consider `typeid (int
3924            (3))', for example.  */
3925         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3926         /* If all went well, simply lookup the type-id.  */
3927         if (cp_parser_parse_definitely (parser))
3928           postfix_expression = get_typeid (type);
3929         /* Otherwise, fall back to the expression variant.  */
3930         else
3931           {
3932             tree expression;
3933
3934             /* Look for an expression.  */
3935             expression = cp_parser_expression (parser, /*cast_p=*/false);
3936             /* Compute its typeid.  */
3937             postfix_expression = build_typeid (expression);
3938             /* Look for the `)' token.  */
3939             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3940           }
3941         /* `typeid' may not appear in an integral constant expression.  */
3942         if (cp_parser_non_integral_constant_expression(parser,
3943                                                        "`typeid' operator"))
3944           return error_mark_node;
3945         /* Restore the saved message.  */
3946         parser->type_definition_forbidden_message = saved_message;
3947       }
3948       break;
3949
3950     case RID_TYPENAME:
3951       {
3952         bool template_p = false;
3953         tree id;
3954         tree type;
3955         tree scope;
3956
3957         /* Consume the `typename' token.  */
3958         cp_lexer_consume_token (parser->lexer);
3959         /* Look for the optional `::' operator.  */
3960         cp_parser_global_scope_opt (parser,
3961                                     /*current_scope_valid_p=*/false);
3962         /* Look for the nested-name-specifier.  In case of error here,
3963            consume the trailing id to avoid subsequent error messages
3964            for usual cases.  */
3965         scope = cp_parser_nested_name_specifier (parser,
3966                                                  /*typename_keyword_p=*/true,
3967                                                  /*check_dependency_p=*/true,
3968                                                  /*type_p=*/true,
3969                                                  /*is_declaration=*/true);
3970
3971         /* Look for the optional `template' keyword.  */
3972         template_p = cp_parser_optional_template_keyword (parser);
3973         /* We don't know whether we're looking at a template-id or an
3974            identifier.  */
3975         cp_parser_parse_tentatively (parser);
3976         /* Try a template-id.  */
3977         id = cp_parser_template_id (parser, template_p,
3978                                     /*check_dependency_p=*/true,
3979                                     /*is_declaration=*/true);
3980         /* If that didn't work, try an identifier.  */
3981         if (!cp_parser_parse_definitely (parser))
3982           id = cp_parser_identifier (parser);
3983
3984         /* Don't process id if nested name specifier is invalid.  */
3985         if (!scope || scope == error_mark_node)
3986           return error_mark_node;
3987         /* If we look up a template-id in a non-dependent qualifying
3988            scope, there's no need to create a dependent type.  */
3989         else if (TREE_CODE (id) == TYPE_DECL
3990             && !dependent_type_p (parser->scope))
3991           type = TREE_TYPE (id);
3992         /* Create a TYPENAME_TYPE to represent the type to which the
3993            functional cast is being performed.  */
3994         else
3995           type = make_typename_type (parser->scope, id,
3996                                      typename_type,
3997                                      /*complain=*/1);
3998
3999         postfix_expression = cp_parser_functional_cast (parser, type);
4000       }
4001       break;
4002
4003     default:
4004       {
4005         tree type;
4006
4007         /* If the next thing is a simple-type-specifier, we may be
4008            looking at a functional cast.  We could also be looking at
4009            an id-expression.  So, we try the functional cast, and if
4010            that doesn't work we fall back to the primary-expression.  */
4011         cp_parser_parse_tentatively (parser);
4012         /* Look for the simple-type-specifier.  */
4013         type = cp_parser_simple_type_specifier (parser,
4014                                                 /*decl_specs=*/NULL,
4015                                                 CP_PARSER_FLAGS_NONE);
4016         /* Parse the cast itself.  */
4017         if (!cp_parser_error_occurred (parser))
4018           postfix_expression
4019             = cp_parser_functional_cast (parser, type);
4020         /* If that worked, we're done.  */
4021         if (cp_parser_parse_definitely (parser))
4022           break;
4023
4024         /* If the functional-cast didn't work out, try a
4025            compound-literal.  */
4026         if (cp_parser_allow_gnu_extensions_p (parser)
4027             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4028           {
4029             VEC(constructor_elt,gc) *initializer_list = NULL;
4030             bool saved_in_type_id_in_expr_p;
4031
4032             cp_parser_parse_tentatively (parser);
4033             /* Consume the `('.  */
4034             cp_lexer_consume_token (parser->lexer);
4035             /* Parse the type.  */
4036             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4037             parser->in_type_id_in_expr_p = true;
4038             type = cp_parser_type_id (parser);
4039             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4040             /* Look for the `)'.  */
4041             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4042             /* Look for the `{'.  */
4043             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4044             /* If things aren't going well, there's no need to
4045                keep going.  */
4046             if (!cp_parser_error_occurred (parser))
4047               {
4048                 bool non_constant_p;
4049                 /* Parse the initializer-list.  */
4050                 initializer_list
4051                   = cp_parser_initializer_list (parser, &non_constant_p);
4052                 /* Allow a trailing `,'.  */
4053                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4054                   cp_lexer_consume_token (parser->lexer);
4055                 /* Look for the final `}'.  */
4056                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4057               }
4058             /* If that worked, we're definitely looking at a
4059                compound-literal expression.  */
4060             if (cp_parser_parse_definitely (parser))
4061               {
4062                 /* Warn the user that a compound literal is not
4063                    allowed in standard C++.  */
4064                 if (pedantic)
4065                   pedwarn ("ISO C++ forbids compound-literals");
4066                 /* Form the representation of the compound-literal.  */
4067                 postfix_expression
4068                   = finish_compound_literal (type, initializer_list);
4069                 break;
4070               }
4071           }
4072
4073         /* It must be a primary-expression.  */
4074         postfix_expression = cp_parser_primary_expression (parser,
4075                                                            cast_p,
4076                                                            &idk,
4077                                                            &qualifying_class);
4078       }
4079       break;
4080     }
4081
4082   /* If we were avoiding committing to the processing of a
4083      qualified-id until we knew whether or not we had a
4084      pointer-to-member, we now know.  */
4085   if (qualifying_class)
4086     {
4087       bool done;
4088
4089       /* Peek at the next token.  */
4090       token = cp_lexer_peek_token (parser->lexer);
4091       done = (token->type != CPP_OPEN_SQUARE
4092               && token->type != CPP_OPEN_PAREN
4093               && token->type != CPP_DOT
4094               && token->type != CPP_DEREF
4095               && token->type != CPP_PLUS_PLUS
4096               && token->type != CPP_MINUS_MINUS);
4097
4098       postfix_expression = finish_qualified_id_expr (qualifying_class,
4099                                                      postfix_expression,
4100                                                      done,
4101                                                      address_p);
4102       if (done)
4103         return postfix_expression;
4104     }
4105
4106   /* Keep looping until the postfix-expression is complete.  */
4107   while (true)
4108     {
4109       if (idk == CP_ID_KIND_UNQUALIFIED
4110           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4111           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4112         /* It is not a Koenig lookup function call.  */
4113         postfix_expression
4114           = unqualified_name_lookup_error (postfix_expression);
4115
4116       /* Peek at the next token.  */
4117       token = cp_lexer_peek_token (parser->lexer);
4118
4119       switch (token->type)
4120         {
4121         case CPP_OPEN_SQUARE:
4122           postfix_expression
4123             = cp_parser_postfix_open_square_expression (parser,
4124                                                         postfix_expression,
4125                                                         false);
4126           idk = CP_ID_KIND_NONE;
4127           break;
4128
4129         case CPP_OPEN_PAREN:
4130           /* postfix-expression ( expression-list [opt] ) */
4131           {
4132             bool koenig_p;
4133             bool is_builtin_constant_p;
4134             bool saved_integral_constant_expression_p = false;
4135             bool saved_non_integral_constant_expression_p = false;
4136             tree args;
4137
4138             is_builtin_constant_p
4139               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4140             if (is_builtin_constant_p)
4141               {
4142                 /* The whole point of __builtin_constant_p is to allow
4143                    non-constant expressions to appear as arguments.  */
4144                 saved_integral_constant_expression_p
4145                   = parser->integral_constant_expression_p;
4146                 saved_non_integral_constant_expression_p
4147                   = parser->non_integral_constant_expression_p;
4148                 parser->integral_constant_expression_p = false;
4149               }
4150             args = (cp_parser_parenthesized_expression_list
4151                     (parser, /*is_attribute_list=*/false,
4152                      /*cast_p=*/false,
4153                      /*non_constant_p=*/NULL));
4154             if (is_builtin_constant_p)
4155               {
4156                 parser->integral_constant_expression_p
4157                   = saved_integral_constant_expression_p;
4158                 parser->non_integral_constant_expression_p
4159                   = saved_non_integral_constant_expression_p;
4160               }
4161
4162             if (args == error_mark_node)
4163               {
4164                 postfix_expression = error_mark_node;
4165                 break;
4166               }
4167
4168             /* Function calls are not permitted in
4169                constant-expressions.  */
4170             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4171                 && cp_parser_non_integral_constant_expression (parser,
4172                                                                "a function call"))
4173               {
4174                 postfix_expression = error_mark_node;
4175                 break;
4176               }
4177
4178             koenig_p = false;
4179             if (idk == CP_ID_KIND_UNQUALIFIED)
4180               {
4181                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4182                   {
4183                     if (args)
4184                       {
4185                         koenig_p = true;
4186                         postfix_expression
4187                           = perform_koenig_lookup (postfix_expression, args);
4188                       }
4189                     else
4190                       postfix_expression
4191                         = unqualified_fn_lookup_error (postfix_expression);
4192                   }
4193                 /* We do not perform argument-dependent lookup if
4194                    normal lookup finds a non-function, in accordance
4195                    with the expected resolution of DR 218.  */
4196                 else if (args && is_overloaded_fn (postfix_expression))
4197                   {
4198                     tree fn = get_first_fn (postfix_expression);
4199
4200                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4201                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4202
4203                     /* Only do argument dependent lookup if regular
4204                        lookup does not find a set of member functions.
4205                        [basic.lookup.koenig]/2a  */
4206                     if (!DECL_FUNCTION_MEMBER_P (fn))
4207                       {
4208                         koenig_p = true;
4209                         postfix_expression
4210                           = perform_koenig_lookup (postfix_expression, args);
4211                       }
4212                   }
4213               }
4214
4215             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4216               {
4217                 tree instance = TREE_OPERAND (postfix_expression, 0);
4218                 tree fn = TREE_OPERAND (postfix_expression, 1);
4219
4220                 if (processing_template_decl
4221                     && (type_dependent_expression_p (instance)
4222                         || (!BASELINK_P (fn)
4223                             && TREE_CODE (fn) != FIELD_DECL)
4224                         || type_dependent_expression_p (fn)
4225                         || any_type_dependent_arguments_p (args)))
4226                   {
4227                     postfix_expression
4228                       = build_min_nt (CALL_EXPR, postfix_expression,
4229                                       args, NULL_TREE);
4230                     break;
4231                   }
4232
4233                 if (BASELINK_P (fn))
4234                   postfix_expression
4235                     = (build_new_method_call
4236                        (instance, fn, args, NULL_TREE,
4237                         (idk == CP_ID_KIND_QUALIFIED
4238                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4239                 else
4240                   postfix_expression
4241                     = finish_call_expr (postfix_expression, args,
4242                                         /*disallow_virtual=*/false,
4243                                         /*koenig_p=*/false);
4244               }
4245             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4246                      || TREE_CODE (postfix_expression) == MEMBER_REF
4247                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4248               postfix_expression = (build_offset_ref_call_from_tree
4249                                     (postfix_expression, args));
4250             else if (idk == CP_ID_KIND_QUALIFIED)
4251               /* A call to a static class member, or a namespace-scope
4252                  function.  */
4253               postfix_expression
4254                 = finish_call_expr (postfix_expression, args,
4255                                     /*disallow_virtual=*/true,
4256                                     koenig_p);
4257             else
4258               /* All other function calls.  */
4259               postfix_expression
4260                 = finish_call_expr (postfix_expression, args,
4261                                     /*disallow_virtual=*/false,
4262                                     koenig_p);
4263
4264             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4265             idk = CP_ID_KIND_NONE;
4266           }
4267           break;
4268
4269         case CPP_DOT:
4270         case CPP_DEREF:
4271           /* postfix-expression . template [opt] id-expression
4272              postfix-expression . pseudo-destructor-name
4273              postfix-expression -> template [opt] id-expression
4274              postfix-expression -> pseudo-destructor-name */
4275
4276           /* Consume the `.' or `->' operator.  */
4277           cp_lexer_consume_token (parser->lexer);
4278
4279           postfix_expression
4280             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4281                                                       postfix_expression,
4282                                                       false, &idk);
4283           break;
4284
4285         case CPP_PLUS_PLUS:
4286           /* postfix-expression ++  */
4287           /* Consume the `++' token.  */
4288           cp_lexer_consume_token (parser->lexer);
4289           /* Generate a representation for the complete expression.  */
4290           postfix_expression
4291             = finish_increment_expr (postfix_expression,
4292                                      POSTINCREMENT_EXPR);
4293           /* Increments may not appear in constant-expressions.  */
4294           if (cp_parser_non_integral_constant_expression (parser,
4295                                                           "an increment"))
4296             postfix_expression = error_mark_node;
4297           idk = CP_ID_KIND_NONE;
4298           break;
4299
4300         case CPP_MINUS_MINUS:
4301           /* postfix-expression -- */
4302           /* Consume the `--' token.  */
4303           cp_lexer_consume_token (parser->lexer);
4304           /* Generate a representation for the complete expression.  */
4305           postfix_expression
4306             = finish_increment_expr (postfix_expression,
4307                                      POSTDECREMENT_EXPR);
4308           /* Decrements may not appear in constant-expressions.  */
4309           if (cp_parser_non_integral_constant_expression (parser,
4310                                                           "a decrement"))
4311             postfix_expression = error_mark_node;
4312           idk = CP_ID_KIND_NONE;
4313           break;
4314
4315         default:
4316           return postfix_expression;
4317         }
4318     }
4319
4320   /* We should never get here.  */
4321   gcc_unreachable ();
4322   return error_mark_node;
4323 }
4324
4325 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4326    by cp_parser_builtin_offsetof.  We're looking for
4327
4328      postfix-expression [ expression ]
4329
4330    FOR_OFFSETOF is set if we're being called in that context, which
4331    changes how we deal with integer constant expressions.  */
4332
4333 static tree
4334 cp_parser_postfix_open_square_expression (cp_parser *parser,
4335                                           tree postfix_expression,
4336                                           bool for_offsetof)
4337 {
4338   tree index;
4339
4340   /* Consume the `[' token.  */
4341   cp_lexer_consume_token (parser->lexer);
4342
4343   /* Parse the index expression.  */
4344   /* ??? For offsetof, there is a question of what to allow here.  If
4345      offsetof is not being used in an integral constant expression context,
4346      then we *could* get the right answer by computing the value at runtime.
4347      If we are in an integral constant expression context, then we might
4348      could accept any constant expression; hard to say without analysis.
4349      Rather than open the barn door too wide right away, allow only integer
4350      constant expressions here.  */
4351   if (for_offsetof)
4352     index = cp_parser_constant_expression (parser, false, NULL);
4353   else
4354     index = cp_parser_expression (parser, /*cast_p=*/false);
4355
4356   /* Look for the closing `]'.  */
4357   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4358
4359   /* Build the ARRAY_REF.  */
4360   postfix_expression = grok_array_decl (postfix_expression, index);
4361
4362   /* When not doing offsetof, array references are not permitted in
4363      constant-expressions.  */
4364   if (!for_offsetof
4365       && (cp_parser_non_integral_constant_expression
4366           (parser, "an array reference")))
4367     postfix_expression = error_mark_node;
4368
4369   return postfix_expression;
4370 }
4371
4372 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4373    by cp_parser_builtin_offsetof.  We're looking for
4374
4375      postfix-expression . template [opt] id-expression
4376      postfix-expression . pseudo-destructor-name
4377      postfix-expression -> template [opt] id-expression
4378      postfix-expression -> pseudo-destructor-name
4379
4380    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4381    limits what of the above we'll actually accept, but nevermind.
4382    TOKEN_TYPE is the "." or "->" token, which will already have been
4383    removed from the stream.  */
4384
4385 static tree
4386 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4387                                         enum cpp_ttype token_type,
4388                                         tree postfix_expression,
4389                                         bool for_offsetof, cp_id_kind *idk)
4390 {
4391   tree name;
4392   bool dependent_p;
4393   bool template_p;
4394   bool pseudo_destructor_p;
4395   tree scope = NULL_TREE;
4396
4397   /* If this is a `->' operator, dereference the pointer.  */
4398   if (token_type == CPP_DEREF)
4399     postfix_expression = build_x_arrow (postfix_expression);
4400   /* Check to see whether or not the expression is type-dependent.  */
4401   dependent_p = type_dependent_expression_p (postfix_expression);
4402   /* The identifier following the `->' or `.' is not qualified.  */
4403   parser->scope = NULL_TREE;
4404   parser->qualifying_scope = NULL_TREE;
4405   parser->object_scope = NULL_TREE;
4406   *idk = CP_ID_KIND_NONE;
4407   /* Enter the scope corresponding to the type of the object
4408      given by the POSTFIX_EXPRESSION.  */
4409   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4410     {
4411       scope = TREE_TYPE (postfix_expression);
4412       /* According to the standard, no expression should ever have
4413          reference type.  Unfortunately, we do not currently match
4414          the standard in this respect in that our internal representation
4415          of an expression may have reference type even when the standard
4416          says it does not.  Therefore, we have to manually obtain the
4417          underlying type here.  */
4418       scope = non_reference (scope);
4419       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4420       scope = complete_type_or_else (scope, NULL_TREE);
4421       /* Let the name lookup machinery know that we are processing a
4422          class member access expression.  */
4423       parser->context->object_type = scope;
4424       /* If something went wrong, we want to be able to discern that case,
4425          as opposed to the case where there was no SCOPE due to the type
4426          of expression being dependent.  */
4427       if (!scope)
4428         scope = error_mark_node;
4429       /* If the SCOPE was erroneous, make the various semantic analysis
4430          functions exit quickly -- and without issuing additional error
4431          messages.  */
4432       if (scope == error_mark_node)
4433         postfix_expression = error_mark_node;
4434     }
4435
4436   /* Assume this expression is not a pseudo-destructor access.  */
4437   pseudo_destructor_p = false;
4438
4439   /* If the SCOPE is a scalar type, then, if this is a valid program,
4440      we must be looking at a pseudo-destructor-name.  */
4441   if (scope && SCALAR_TYPE_P (scope))
4442     {
4443       tree s;
4444       tree type;
4445
4446       cp_parser_parse_tentatively (parser);
4447       /* Parse the pseudo-destructor-name.  */
4448       s = NULL_TREE;
4449       cp_parser_pseudo_destructor_name (parser, &s, &type);
4450       if (cp_parser_parse_definitely (parser))
4451         {
4452           pseudo_destructor_p = true;
4453           postfix_expression
4454             = finish_pseudo_destructor_expr (postfix_expression,
4455                                              s, TREE_TYPE (type));
4456         }
4457     }
4458
4459   if (!pseudo_destructor_p)
4460     {
4461       /* If the SCOPE is not a scalar type, we are looking at an
4462          ordinary class member access expression, rather than a
4463          pseudo-destructor-name.  */
4464       template_p = cp_parser_optional_template_keyword (parser);
4465       /* Parse the id-expression.  */
4466       name = cp_parser_id_expression (parser, template_p,
4467                                       /*check_dependency_p=*/true,
4468                                       /*template_p=*/NULL,
4469                                       /*declarator_p=*/false);
4470       /* In general, build a SCOPE_REF if the member name is qualified.
4471          However, if the name was not dependent and has already been
4472          resolved; there is no need to build the SCOPE_REF.  For example;
4473
4474              struct X { void f(); };
4475              template <typename T> void f(T* t) { t->X::f(); }
4476
4477          Even though "t" is dependent, "X::f" is not and has been resolved
4478          to a BASELINK; there is no need to include scope information.  */
4479
4480       /* But we do need to remember that there was an explicit scope for
4481          virtual function calls.  */
4482       if (parser->scope)
4483         *idk = CP_ID_KIND_QUALIFIED;
4484
4485       /* If the name is a template-id that names a type, we will get a
4486          TYPE_DECL here.  That is invalid code.  */
4487       if (TREE_CODE (name) == TYPE_DECL)
4488         {
4489           error ("invalid use of %qD", name);
4490           postfix_expression = error_mark_node;
4491         }
4492       else
4493         {
4494           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4495             {
4496               name = build_nt (SCOPE_REF, parser->scope, name);
4497               parser->scope = NULL_TREE;
4498               parser->qualifying_scope = NULL_TREE;
4499               parser->object_scope = NULL_TREE;
4500             }
4501           if (scope && name && BASELINK_P (name))
4502             adjust_result_of_qualified_name_lookup
4503               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4504           postfix_expression
4505             = finish_class_member_access_expr (postfix_expression, name);
4506         }
4507     }
4508
4509   /* We no longer need to look up names in the scope of the object on
4510      the left-hand side of the `.' or `->' operator.  */
4511   parser->context->object_type = NULL_TREE;
4512
4513   /* Outside of offsetof, these operators may not appear in
4514      constant-expressions.  */
4515   if (!for_offsetof
4516       && (cp_parser_non_integral_constant_expression
4517           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4518     postfix_expression = error_mark_node;
4519
4520   return postfix_expression;
4521 }
4522
4523 /* Parse a parenthesized expression-list.
4524
4525    expression-list:
4526      assignment-expression
4527      expression-list, assignment-expression
4528
4529    attribute-list:
4530      expression-list
4531      identifier
4532      identifier, expression-list
4533
4534    CAST_P is true if this expression is the target of a cast.
4535
4536    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4537    representation of an assignment-expression.  Note that a TREE_LIST
4538    is returned even if there is only a single expression in the list.
4539    error_mark_node is returned if the ( and or ) are
4540    missing. NULL_TREE is returned on no expressions. The parentheses
4541    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4542    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4543    indicates whether or not all of the expressions in the list were
4544    constant.  */
4545
4546 static tree
4547 cp_parser_parenthesized_expression_list (cp_parser* parser,
4548                                          bool is_attribute_list,
4549                                          bool cast_p,
4550                                          bool *non_constant_p)
4551 {
4552   tree expression_list = NULL_TREE;
4553   bool fold_expr_p = is_attribute_list;
4554   tree identifier = NULL_TREE;
4555
4556   /* Assume all the expressions will be constant.  */
4557   if (non_constant_p)
4558     *non_constant_p = false;
4559
4560   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4561     return error_mark_node;
4562
4563   /* Consume expressions until there are no more.  */
4564   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4565     while (true)
4566       {
4567         tree expr;
4568
4569         /* At the beginning of attribute lists, check to see if the
4570            next token is an identifier.  */
4571         if (is_attribute_list
4572             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4573           {
4574             cp_token *token;
4575
4576             /* Consume the identifier.  */
4577             token = cp_lexer_consume_token (parser->lexer);
4578             /* Save the identifier.  */
4579             identifier = token->value;
4580           }
4581         else
4582           {
4583             /* Parse the next assignment-expression.  */
4584             if (non_constant_p)
4585               {
4586                 bool expr_non_constant_p;
4587                 expr = (cp_parser_constant_expression
4588                         (parser, /*allow_non_constant_p=*/true,
4589                          &expr_non_constant_p));
4590                 if (expr_non_constant_p)
4591                   *non_constant_p = true;
4592               }
4593             else
4594               expr = cp_parser_assignment_expression (parser, cast_p);
4595
4596             if (fold_expr_p)
4597               expr = fold_non_dependent_expr (expr);
4598
4599              /* Add it to the list.  We add error_mark_node
4600                 expressions to the list, so that we can still tell if
4601                 the correct form for a parenthesized expression-list
4602                 is found. That gives better errors.  */
4603             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4604
4605             if (expr == error_mark_node)
4606               goto skip_comma;
4607           }
4608
4609         /* After the first item, attribute lists look the same as
4610            expression lists.  */
4611         is_attribute_list = false;
4612
4613       get_comma:;
4614         /* If the next token isn't a `,', then we are done.  */
4615         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4616           break;
4617
4618         /* Otherwise, consume the `,' and keep going.  */
4619         cp_lexer_consume_token (parser->lexer);
4620       }
4621
4622   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4623     {
4624       int ending;
4625
4626     skip_comma:;
4627       /* We try and resync to an unnested comma, as that will give the
4628          user better diagnostics.  */
4629       ending = cp_parser_skip_to_closing_parenthesis (parser,
4630                                                       /*recovering=*/true,
4631                                                       /*or_comma=*/true,
4632                                                       /*consume_paren=*/true);
4633       if (ending < 0)
4634         goto get_comma;
4635       if (!ending)
4636         return error_mark_node;
4637     }
4638
4639   /* We built up the list in reverse order so we must reverse it now.  */
4640   expression_list = nreverse (expression_list);
4641   if (identifier)
4642     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4643
4644   return expression_list;
4645 }
4646
4647 /* Parse a pseudo-destructor-name.
4648
4649    pseudo-destructor-name:
4650      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4651      :: [opt] nested-name-specifier template template-id :: ~ type-name
4652      :: [opt] nested-name-specifier [opt] ~ type-name
4653
4654    If either of the first two productions is used, sets *SCOPE to the
4655    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4656    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4657    or ERROR_MARK_NODE if the parse fails.  */
4658
4659 static void
4660 cp_parser_pseudo_destructor_name (cp_parser* parser,
4661                                   tree* scope,
4662                                   tree* type)
4663 {
4664   bool nested_name_specifier_p;
4665
4666   /* Assume that things will not work out.  */
4667   *type = error_mark_node;
4668
4669   /* Look for the optional `::' operator.  */
4670   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4671   /* Look for the optional nested-name-specifier.  */
4672   nested_name_specifier_p
4673     = (cp_parser_nested_name_specifier_opt (parser,
4674                                             /*typename_keyword_p=*/false,
4675                                             /*check_dependency_p=*/true,
4676                                             /*type_p=*/false,
4677                                             /*is_declaration=*/true)
4678        != NULL_TREE);
4679   /* Now, if we saw a nested-name-specifier, we might be doing the
4680      second production.  */
4681   if (nested_name_specifier_p
4682       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4683     {
4684       /* Consume the `template' keyword.  */
4685       cp_lexer_consume_token (parser->lexer);
4686       /* Parse the template-id.  */
4687       cp_parser_template_id (parser,
4688                              /*template_keyword_p=*/true,
4689                              /*check_dependency_p=*/false,
4690                              /*is_declaration=*/true);
4691       /* Look for the `::' token.  */
4692       cp_parser_require (parser, CPP_SCOPE, "`::'");
4693     }
4694   /* If the next token is not a `~', then there might be some
4695      additional qualification.  */
4696   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4697     {
4698       /* Look for the type-name.  */
4699       *scope = TREE_TYPE (cp_parser_type_name (parser));
4700
4701       if (*scope == error_mark_node)
4702         return;
4703
4704       /* If we don't have ::~, then something has gone wrong.  Since
4705          the only caller of this function is looking for something
4706          after `.' or `->' after a scalar type, most likely the
4707          program is trying to get a member of a non-aggregate
4708          type.  */
4709       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4710           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4711         {
4712           cp_parser_error (parser, "request for member of non-aggregate type");
4713           return;
4714         }
4715
4716       /* Look for the `::' token.  */
4717       cp_parser_require (parser, CPP_SCOPE, "`::'");
4718     }
4719   else
4720     *scope = NULL_TREE;
4721
4722   /* Look for the `~'.  */
4723   cp_parser_require (parser, CPP_COMPL, "`~'");
4724   /* Look for the type-name again.  We are not responsible for
4725      checking that it matches the first type-name.  */
4726   *type = cp_parser_type_name (parser);
4727 }
4728
4729 /* Parse a unary-expression.
4730
4731    unary-expression:
4732      postfix-expression
4733      ++ cast-expression
4734      -- cast-expression
4735      unary-operator cast-expression
4736      sizeof unary-expression
4737      sizeof ( type-id )
4738      new-expression
4739      delete-expression
4740
4741    GNU Extensions:
4742
4743    unary-expression:
4744      __extension__ cast-expression
4745      __alignof__ unary-expression
4746      __alignof__ ( type-id )
4747      __real__ cast-expression
4748      __imag__ cast-expression
4749      && identifier
4750
4751    ADDRESS_P is true iff the unary-expression is appearing as the
4752    operand of the `&' operator.   CAST_P is true if this expression is
4753    the target of a cast.
4754
4755    Returns a representation of the expression.  */
4756
4757 static tree
4758 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4759 {
4760   cp_token *token;
4761   enum tree_code unary_operator;
4762
4763   /* Peek at the next token.  */
4764   token = cp_lexer_peek_token (parser->lexer);
4765   /* Some keywords give away the kind of expression.  */
4766   if (token->type == CPP_KEYWORD)
4767     {
4768       enum rid keyword = token->keyword;
4769
4770       switch (keyword)
4771         {
4772         case RID_ALIGNOF:
4773         case RID_SIZEOF:
4774           {
4775             tree operand;
4776             enum tree_code op;
4777
4778             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4779             /* Consume the token.  */
4780             cp_lexer_consume_token (parser->lexer);
4781             /* Parse the operand.  */
4782             operand = cp_parser_sizeof_operand (parser, keyword);
4783
4784             if (TYPE_P (operand))
4785               return cxx_sizeof_or_alignof_type (operand, op, true);
4786             else
4787               return cxx_sizeof_or_alignof_expr (operand, op);
4788           }
4789
4790         case RID_NEW:
4791           return cp_parser_new_expression (parser);
4792
4793         case RID_DELETE:
4794           return cp_parser_delete_expression (parser);
4795
4796         case RID_EXTENSION:
4797           {
4798             /* The saved value of the PEDANTIC flag.  */
4799             int saved_pedantic;
4800             tree expr;
4801
4802             /* Save away the PEDANTIC flag.  */
4803             cp_parser_extension_opt (parser, &saved_pedantic);
4804             /* Parse the cast-expression.  */
4805             expr = cp_parser_simple_cast_expression (parser);
4806             /* Restore the PEDANTIC flag.  */
4807             pedantic = saved_pedantic;
4808
4809             return expr;
4810           }
4811
4812         case RID_REALPART:
4813         case RID_IMAGPART:
4814           {
4815             tree expression;
4816
4817             /* Consume the `__real__' or `__imag__' token.  */
4818             cp_lexer_consume_token (parser->lexer);
4819             /* Parse the cast-expression.  */
4820             expression = cp_parser_simple_cast_expression (parser);
4821             /* Create the complete representation.  */
4822             return build_x_unary_op ((keyword == RID_REALPART
4823                                       ? REALPART_EXPR : IMAGPART_EXPR),
4824                                      expression);
4825           }
4826           break;
4827
4828         default:
4829           break;
4830         }
4831     }
4832
4833   /* Look for the `:: new' and `:: delete', which also signal the
4834      beginning of a new-expression, or delete-expression,
4835      respectively.  If the next token is `::', then it might be one of
4836      these.  */
4837   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4838     {
4839       enum rid keyword;
4840
4841       /* See if the token after the `::' is one of the keywords in
4842          which we're interested.  */
4843       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4844       /* If it's `new', we have a new-expression.  */
4845       if (keyword == RID_NEW)
4846         return cp_parser_new_expression (parser);
4847       /* Similarly, for `delete'.  */
4848       else if (keyword == RID_DELETE)
4849         return cp_parser_delete_expression (parser);
4850     }
4851
4852   /* Look for a unary operator.  */
4853   unary_operator = cp_parser_unary_operator (token);
4854   /* The `++' and `--' operators can be handled similarly, even though
4855      they are not technically unary-operators in the grammar.  */
4856   if (unary_operator == ERROR_MARK)
4857     {
4858       if (token->type == CPP_PLUS_PLUS)
4859         unary_operator = PREINCREMENT_EXPR;
4860       else if (token->type == CPP_MINUS_MINUS)
4861         unary_operator = PREDECREMENT_EXPR;
4862       /* Handle the GNU address-of-label extension.  */
4863       else if (cp_parser_allow_gnu_extensions_p (parser)
4864                && token->type == CPP_AND_AND)
4865         {
4866           tree identifier;
4867
4868           /* Consume the '&&' token.  */
4869           cp_lexer_consume_token (parser->lexer);
4870           /* Look for the identifier.  */
4871           identifier = cp_parser_identifier (parser);
4872           /* Create an expression representing the address.  */
4873           return finish_label_address_expr (identifier);
4874         }
4875     }
4876   if (unary_operator != ERROR_MARK)
4877     {
4878       tree cast_expression;
4879       tree expression = error_mark_node;
4880       const char *non_constant_p = NULL;
4881
4882       /* Consume the operator token.  */
4883       token = cp_lexer_consume_token (parser->lexer);
4884       /* Parse the cast-expression.  */
4885       cast_expression
4886         = cp_parser_cast_expression (parser,
4887                                      unary_operator == ADDR_EXPR,
4888                                      /*cast_p=*/false);
4889       /* Now, build an appropriate representation.  */
4890       switch (unary_operator)
4891         {
4892         case INDIRECT_REF:
4893           non_constant_p = "`*'";
4894           expression = build_x_indirect_ref (cast_expression, "unary *");
4895           break;
4896
4897         case ADDR_EXPR:
4898           non_constant_p = "`&'";
4899           /* Fall through.  */
4900         case BIT_NOT_EXPR:
4901           expression = build_x_unary_op (unary_operator, cast_expression);
4902           break;
4903
4904         case PREINCREMENT_EXPR:
4905         case PREDECREMENT_EXPR:
4906           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4907                             ? "`++'" : "`--'");
4908           /* Fall through.  */
4909         case UNARY_PLUS_EXPR:
4910         case NEGATE_EXPR:
4911         case TRUTH_NOT_EXPR:
4912           expression = finish_unary_op_expr (unary_operator, cast_expression);
4913           break;
4914
4915         default:
4916           gcc_unreachable ();
4917         }
4918
4919       if (non_constant_p
4920           && cp_parser_non_integral_constant_expression (parser,
4921                                                          non_constant_p))
4922         expression = error_mark_node;
4923
4924       return expression;
4925     }
4926
4927   return cp_parser_postfix_expression (parser, address_p, cast_p);
4928 }
4929
4930 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4931    unary-operator, the corresponding tree code is returned.  */
4932
4933 static enum tree_code
4934 cp_parser_unary_operator (cp_token* token)
4935 {
4936   switch (token->type)
4937     {
4938     case CPP_MULT:
4939       return INDIRECT_REF;
4940
4941     case CPP_AND:
4942       return ADDR_EXPR;
4943
4944     case CPP_PLUS:
4945       return UNARY_PLUS_EXPR;
4946
4947     case CPP_MINUS:
4948       return NEGATE_EXPR;
4949
4950     case CPP_NOT:
4951       return TRUTH_NOT_EXPR;
4952
4953     case CPP_COMPL:
4954       return BIT_NOT_EXPR;
4955
4956     default:
4957       return ERROR_MARK;
4958     }
4959 }
4960
4961 /* Parse a new-expression.
4962
4963    new-expression:
4964      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4965      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4966
4967    Returns a representation of the expression.  */
4968
4969 static tree
4970 cp_parser_new_expression (cp_parser* parser)
4971 {
4972   bool global_scope_p;
4973   tree placement;
4974   tree type;
4975   tree initializer;
4976   tree nelts;
4977
4978   /* Look for the optional `::' operator.  */
4979   global_scope_p
4980     = (cp_parser_global_scope_opt (parser,
4981                                    /*current_scope_valid_p=*/false)
4982        != NULL_TREE);
4983   /* Look for the `new' operator.  */
4984   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4985   /* There's no easy way to tell a new-placement from the
4986      `( type-id )' construct.  */
4987   cp_parser_parse_tentatively (parser);
4988   /* Look for a new-placement.  */
4989   placement = cp_parser_new_placement (parser);
4990   /* If that didn't work out, there's no new-placement.  */
4991   if (!cp_parser_parse_definitely (parser))
4992     placement = NULL_TREE;
4993
4994   /* If the next token is a `(', then we have a parenthesized
4995      type-id.  */
4996   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4997     {
4998       /* Consume the `('.  */
4999       cp_lexer_consume_token (parser->lexer);
5000       /* Parse the type-id.  */
5001       type = cp_parser_type_id (parser);
5002       /* Look for the closing `)'.  */
5003       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5004       /* There should not be a direct-new-declarator in this production,
5005          but GCC used to allowed this, so we check and emit a sensible error
5006          message for this case.  */
5007       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5008         {
5009           error ("array bound forbidden after parenthesized type-id");
5010           inform ("try removing the parentheses around the type-id");
5011           cp_parser_direct_new_declarator (parser);
5012         }
5013       nelts = NULL_TREE;
5014     }
5015   /* Otherwise, there must be a new-type-id.  */
5016   else
5017     type = cp_parser_new_type_id (parser, &nelts);
5018
5019   /* If the next token is a `(', then we have a new-initializer.  */
5020   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5021     initializer = cp_parser_new_initializer (parser);
5022   else
5023     initializer = NULL_TREE;
5024
5025   /* A new-expression may not appear in an integral constant
5026      expression.  */
5027   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5028     return error_mark_node;
5029
5030   /* Create a representation of the new-expression.  */
5031   return build_new (placement, type, nelts, initializer, global_scope_p);
5032 }
5033
5034 /* Parse a new-placement.
5035
5036    new-placement:
5037      ( expression-list )
5038
5039    Returns the same representation as for an expression-list.  */
5040
5041 static tree
5042 cp_parser_new_placement (cp_parser* parser)
5043 {
5044   tree expression_list;
5045
5046   /* Parse the expression-list.  */
5047   expression_list = (cp_parser_parenthesized_expression_list
5048                      (parser, false, /*cast_p=*/false,
5049                       /*non_constant_p=*/NULL));
5050
5051   return expression_list;
5052 }
5053
5054 /* Parse a new-type-id.
5055
5056    new-type-id:
5057      type-specifier-seq new-declarator [opt]
5058
5059    Returns the TYPE allocated.  If the new-type-id indicates an array
5060    type, *NELTS is set to the number of elements in the last array
5061    bound; the TYPE will not include the last array bound.  */
5062
5063 static tree
5064 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5065 {
5066   cp_decl_specifier_seq type_specifier_seq;
5067   cp_declarator *new_declarator;
5068   cp_declarator *declarator;
5069   cp_declarator *outer_declarator;
5070   const char *saved_message;
5071   tree type;
5072
5073   /* The type-specifier sequence must not contain type definitions.
5074      (It cannot contain declarations of new types either, but if they
5075      are not definitions we will catch that because they are not
5076      complete.)  */
5077   saved_message = parser->type_definition_forbidden_message;
5078   parser->type_definition_forbidden_message
5079     = "types may not be defined in a new-type-id";
5080   /* Parse the type-specifier-seq.  */
5081   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5082                                 &type_specifier_seq);
5083   /* Restore the old message.  */
5084   parser->type_definition_forbidden_message = saved_message;
5085   /* Parse the new-declarator.  */
5086   new_declarator = cp_parser_new_declarator_opt (parser);
5087
5088   /* Determine the number of elements in the last array dimension, if
5089      any.  */
5090   *nelts = NULL_TREE;
5091   /* Skip down to the last array dimension.  */
5092   declarator = new_declarator;
5093   outer_declarator = NULL;
5094   while (declarator && (declarator->kind == cdk_pointer
5095                         || declarator->kind == cdk_ptrmem))
5096     {
5097       outer_declarator = declarator;
5098       declarator = declarator->declarator;
5099     }
5100   while (declarator
5101          && declarator->kind == cdk_array
5102          && declarator->declarator
5103          && declarator->declarator->kind == cdk_array)
5104     {
5105       outer_declarator = declarator;
5106       declarator = declarator->declarator;
5107     }
5108
5109   if (declarator && declarator->kind == cdk_array)
5110     {
5111       *nelts = declarator->u.array.bounds;
5112       if (*nelts == error_mark_node)
5113         *nelts = integer_one_node;
5114
5115       if (outer_declarator)
5116         outer_declarator->declarator = declarator->declarator;
5117       else
5118         new_declarator = NULL;
5119     }
5120
5121   type = groktypename (&type_specifier_seq, new_declarator);
5122   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5123     {
5124       *nelts = array_type_nelts_top (type);
5125       type = TREE_TYPE (type);
5126     }
5127   return type;
5128 }
5129
5130 /* Parse an (optional) new-declarator.
5131
5132    new-declarator:
5133      ptr-operator new-declarator [opt]
5134      direct-new-declarator
5135
5136    Returns the declarator.  */
5137
5138 static cp_declarator *
5139 cp_parser_new_declarator_opt (cp_parser* parser)
5140 {
5141   enum tree_code code;
5142   tree type;
5143   cp_cv_quals cv_quals;
5144
5145   /* We don't know if there's a ptr-operator next, or not.  */
5146   cp_parser_parse_tentatively (parser);
5147   /* Look for a ptr-operator.  */
5148   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5149   /* If that worked, look for more new-declarators.  */
5150   if (cp_parser_parse_definitely (parser))
5151     {
5152       cp_declarator *declarator;
5153
5154       /* Parse another optional declarator.  */
5155       declarator = cp_parser_new_declarator_opt (parser);
5156
5157       /* Create the representation of the declarator.  */
5158       if (type)
5159         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5160       else if (code == INDIRECT_REF)
5161         declarator = make_pointer_declarator (cv_quals, declarator);
5162       else
5163         declarator = make_reference_declarator (cv_quals, declarator);
5164
5165       return declarator;
5166     }
5167
5168   /* If the next token is a `[', there is a direct-new-declarator.  */
5169   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5170     return cp_parser_direct_new_declarator (parser);
5171
5172   return NULL;
5173 }
5174
5175 /* Parse a direct-new-declarator.
5176
5177    direct-new-declarator:
5178      [ expression ]
5179      direct-new-declarator [constant-expression]
5180
5181    */
5182
5183 static cp_declarator *
5184 cp_parser_direct_new_declarator (cp_parser* parser)
5185 {
5186   cp_declarator *declarator = NULL;
5187
5188   while (true)
5189     {
5190       tree expression;
5191
5192       /* Look for the opening `['.  */
5193       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5194       /* The first expression is not required to be constant.  */
5195       if (!declarator)
5196         {
5197           expression = cp_parser_expression (parser, /*cast_p=*/false);
5198           /* The standard requires that the expression have integral
5199              type.  DR 74 adds enumeration types.  We believe that the
5200              real intent is that these expressions be handled like the
5201              expression in a `switch' condition, which also allows
5202              classes with a single conversion to integral or
5203              enumeration type.  */
5204           if (!processing_template_decl)
5205             {
5206               expression
5207                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5208                                               expression,
5209                                               /*complain=*/true);
5210               if (!expression)
5211                 {
5212                   error ("expression in new-declarator must have integral "
5213                          "or enumeration type");
5214                   expression = error_mark_node;
5215                 }
5216             }
5217         }
5218       /* But all the other expressions must be.  */
5219       else
5220         expression
5221           = cp_parser_constant_expression (parser,
5222                                            /*allow_non_constant=*/false,
5223                                            NULL);
5224       /* Look for the closing `]'.  */
5225       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5226
5227       /* Add this bound to the declarator.  */
5228       declarator = make_array_declarator (declarator, expression);
5229
5230       /* If the next token is not a `[', then there are no more
5231          bounds.  */
5232       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5233         break;
5234     }
5235
5236   return declarator;
5237 }
5238
5239 /* Parse a new-initializer.
5240
5241    new-initializer:
5242      ( expression-list [opt] )
5243
5244    Returns a representation of the expression-list.  If there is no
5245    expression-list, VOID_ZERO_NODE is returned.  */
5246
5247 static tree
5248 cp_parser_new_initializer (cp_parser* parser)
5249 {
5250   tree expression_list;
5251
5252   expression_list = (cp_parser_parenthesized_expression_list
5253                      (parser, false, /*cast_p=*/false,
5254                       /*non_constant_p=*/NULL));
5255   if (!expression_list)
5256     expression_list = void_zero_node;
5257
5258   return expression_list;
5259 }
5260
5261 /* Parse a delete-expression.
5262
5263    delete-expression:
5264      :: [opt] delete cast-expression
5265      :: [opt] delete [ ] cast-expression
5266
5267    Returns a representation of the expression.  */
5268
5269 static tree
5270 cp_parser_delete_expression (cp_parser* parser)
5271 {
5272   bool global_scope_p;
5273   bool array_p;
5274   tree expression;
5275
5276   /* Look for the optional `::' operator.  */
5277   global_scope_p
5278     = (cp_parser_global_scope_opt (parser,
5279                                    /*current_scope_valid_p=*/false)
5280        != NULL_TREE);
5281   /* Look for the `delete' keyword.  */
5282   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5283   /* See if the array syntax is in use.  */
5284   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5285     {
5286       /* Consume the `[' token.  */
5287       cp_lexer_consume_token (parser->lexer);
5288       /* Look for the `]' token.  */
5289       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5290       /* Remember that this is the `[]' construct.  */
5291       array_p = true;
5292     }
5293   else
5294     array_p = false;
5295
5296   /* Parse the cast-expression.  */
5297   expression = cp_parser_simple_cast_expression (parser);
5298
5299   /* A delete-expression may not appear in an integral constant
5300      expression.  */
5301   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5302     return error_mark_node;
5303
5304   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5305 }
5306
5307 /* Parse a cast-expression.
5308
5309    cast-expression:
5310      unary-expression
5311      ( type-id ) cast-expression
5312
5313    ADDRESS_P is true iff the unary-expression is appearing as the
5314    operand of the `&' operator.   CAST_P is true if this expression is
5315    the target of a cast.
5316
5317    Returns a representation of the expression.  */
5318
5319 static tree
5320 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5321 {
5322   /* If it's a `(', then we might be looking at a cast.  */
5323   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5324     {
5325       tree type = NULL_TREE;
5326       tree expr = NULL_TREE;
5327       bool compound_literal_p;
5328       const char *saved_message;
5329
5330       /* There's no way to know yet whether or not this is a cast.
5331          For example, `(int (3))' is a unary-expression, while `(int)
5332          3' is a cast.  So, we resort to parsing tentatively.  */
5333       cp_parser_parse_tentatively (parser);
5334       /* Types may not be defined in a cast.  */
5335       saved_message = parser->type_definition_forbidden_message;
5336       parser->type_definition_forbidden_message
5337         = "types may not be defined in casts";
5338       /* Consume the `('.  */
5339       cp_lexer_consume_token (parser->lexer);
5340       /* A very tricky bit is that `(struct S) { 3 }' is a
5341          compound-literal (which we permit in C++ as an extension).
5342          But, that construct is not a cast-expression -- it is a
5343          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5344          is legal; if the compound-literal were a cast-expression,
5345          you'd need an extra set of parentheses.)  But, if we parse
5346          the type-id, and it happens to be a class-specifier, then we
5347          will commit to the parse at that point, because we cannot
5348          undo the action that is done when creating a new class.  So,
5349          then we cannot back up and do a postfix-expression.
5350
5351          Therefore, we scan ahead to the closing `)', and check to see
5352          if the token after the `)' is a `{'.  If so, we are not
5353          looking at a cast-expression.
5354
5355          Save tokens so that we can put them back.  */
5356       cp_lexer_save_tokens (parser->lexer);
5357       /* Skip tokens until the next token is a closing parenthesis.
5358          If we find the closing `)', and the next token is a `{', then
5359          we are looking at a compound-literal.  */
5360       compound_literal_p
5361         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5362                                                   /*consume_paren=*/true)
5363            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5364       /* Roll back the tokens we skipped.  */
5365       cp_lexer_rollback_tokens (parser->lexer);
5366       /* If we were looking at a compound-literal, simulate an error
5367          so that the call to cp_parser_parse_definitely below will
5368          fail.  */
5369       if (compound_literal_p)
5370         cp_parser_simulate_error (parser);
5371       else
5372         {
5373           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5374           parser->in_type_id_in_expr_p = true;
5375           /* Look for the type-id.  */
5376           type = cp_parser_type_id (parser);
5377           /* Look for the closing `)'.  */
5378           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5379           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5380         }
5381
5382       /* Restore the saved message.  */
5383       parser->type_definition_forbidden_message = saved_message;
5384
5385       /* If ok so far, parse the dependent expression. We cannot be
5386          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5387          ctor of T, but looks like a cast to function returning T
5388          without a dependent expression.  */
5389       if (!cp_parser_error_occurred (parser))
5390         expr = cp_parser_cast_expression (parser,
5391                                           /*address_p=*/false,
5392                                           /*cast_p=*/true);
5393
5394       if (cp_parser_parse_definitely (parser))
5395         {
5396           /* Warn about old-style casts, if so requested.  */
5397           if (warn_old_style_cast
5398               && !in_system_header
5399               && !VOID_TYPE_P (type)
5400               && current_lang_name != lang_name_c)
5401             warning (0, "use of old-style cast");
5402
5403           /* Only type conversions to integral or enumeration types
5404              can be used in constant-expressions.  */
5405           if (parser->integral_constant_expression_p
5406               && !dependent_type_p (type)
5407               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5408               && (cp_parser_non_integral_constant_expression
5409                   (parser,
5410                    "a cast to a type other than an integral or "
5411                    "enumeration type")))
5412             return error_mark_node;
5413
5414           /* Perform the cast.  */
5415           expr = build_c_cast (type, expr);
5416           return expr;
5417         }
5418     }
5419
5420   /* If we get here, then it's not a cast, so it must be a
5421      unary-expression.  */
5422   return cp_parser_unary_expression (parser, address_p, cast_p);
5423 }
5424
5425 /* Parse a binary expression of the general form:
5426
5427    pm-expression:
5428      cast-expression
5429      pm-expression .* cast-expression
5430      pm-expression ->* cast-expression
5431
5432    multiplicative-expression:
5433      pm-expression
5434      multiplicative-expression * pm-expression
5435      multiplicative-expression / pm-expression
5436      multiplicative-expression % pm-expression
5437
5438    additive-expression:
5439      multiplicative-expression
5440      additive-expression + multiplicative-expression
5441      additive-expression - multiplicative-expression
5442
5443    shift-expression:
5444      additive-expression
5445      shift-expression << additive-expression
5446      shift-expression >> additive-expression
5447
5448    relational-expression:
5449      shift-expression
5450      relational-expression < shift-expression
5451      relational-expression > shift-expression
5452      relational-expression <= shift-expression
5453      relational-expression >= shift-expression
5454
5455   GNU Extension:
5456
5457    relational-expression:
5458      relational-expression <? shift-expression
5459      relational-expression >? shift-expression
5460
5461    equality-expression:
5462      relational-expression
5463      equality-expression == relational-expression
5464      equality-expression != relational-expression
5465
5466    and-expression:
5467      equality-expression
5468      and-expression & equality-expression
5469
5470    exclusive-or-expression:
5471      and-expression
5472      exclusive-or-expression ^ and-expression
5473
5474    inclusive-or-expression:
5475      exclusive-or-expression
5476      inclusive-or-expression | exclusive-or-expression
5477
5478    logical-and-expression:
5479      inclusive-or-expression
5480      logical-and-expression && inclusive-or-expression
5481
5482    logical-or-expression:
5483      logical-and-expression
5484      logical-or-expression || logical-and-expression
5485
5486    All these are implemented with a single function like:
5487
5488    binary-expression:
5489      simple-cast-expression
5490      binary-expression <token> binary-expression
5491
5492    CAST_P is true if this expression is the target of a cast.
5493
5494    The binops_by_token map is used to get the tree codes for each <token> type.
5495    binary-expressions are associated according to a precedence table.  */
5496
5497 #define TOKEN_PRECEDENCE(token) \
5498   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5499    ? PREC_NOT_OPERATOR \
5500    : binops_by_token[token->type].prec)
5501
5502 static tree
5503 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5504 {
5505   cp_parser_expression_stack stack;
5506   cp_parser_expression_stack_entry *sp = &stack[0];
5507   tree lhs, rhs;
5508   cp_token *token;
5509   enum tree_code tree_type;
5510   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5511   bool overloaded_p;
5512
5513   /* Parse the first expression.  */
5514   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5515
5516   for (;;)
5517     {
5518       /* Get an operator token.  */
5519       token = cp_lexer_peek_token (parser->lexer);
5520       if (token->type == CPP_MIN || token->type == CPP_MAX)
5521         cp_parser_warn_min_max ();
5522
5523       new_prec = TOKEN_PRECEDENCE (token);
5524
5525       /* Popping an entry off the stack means we completed a subexpression:
5526          - either we found a token which is not an operator (`>' where it is not
5527            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5528            will happen repeatedly;
5529          - or, we found an operator which has lower priority.  This is the case
5530            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5531            parsing `3 * 4'.  */
5532       if (new_prec <= prec)
5533         {
5534           if (sp == stack)
5535             break;
5536           else
5537             goto pop;
5538         }
5539
5540      get_rhs:
5541       tree_type = binops_by_token[token->type].tree_type;
5542
5543       /* We used the operator token.  */
5544       cp_lexer_consume_token (parser->lexer);
5545
5546       /* Extract another operand.  It may be the RHS of this expression
5547          or the LHS of a new, higher priority expression.  */
5548       rhs = cp_parser_simple_cast_expression (parser);
5549
5550       /* Get another operator token.  Look up its precedence to avoid
5551          building a useless (immediately popped) stack entry for common
5552          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5553       token = cp_lexer_peek_token (parser->lexer);
5554       lookahead_prec = TOKEN_PRECEDENCE (token);
5555       if (lookahead_prec > new_prec)
5556         {
5557           /* ... and prepare to parse the RHS of the new, higher priority
5558              expression.  Since precedence levels on the stack are
5559              monotonically increasing, we do not have to care about
5560              stack overflows.  */
5561           sp->prec = prec;
5562           sp->tree_type = tree_type;
5563           sp->lhs = lhs;
5564           sp++;
5565           lhs = rhs;
5566           prec = new_prec;
5567           new_prec = lookahead_prec;
5568           goto get_rhs;
5569
5570          pop:
5571           /* If the stack is not empty, we have parsed into LHS the right side
5572              (`4' in the example above) of an expression we had suspended.
5573              We can use the information on the stack to recover the LHS (`3')
5574              from the stack together with the tree code (`MULT_EXPR'), and
5575              the precedence of the higher level subexpression
5576              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5577              which will be used to actually build the additive expression.  */
5578           --sp;
5579           prec = sp->prec;
5580           tree_type = sp->tree_type;
5581           rhs = lhs;
5582           lhs = sp->lhs;
5583         }
5584
5585       overloaded_p = false;
5586       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5587
5588       /* If the binary operator required the use of an overloaded operator,
5589          then this expression cannot be an integral constant-expression.
5590          An overloaded operator can be used even if both operands are
5591          otherwise permissible in an integral constant-expression if at
5592          least one of the operands is of enumeration type.  */
5593
5594       if (overloaded_p
5595           && (cp_parser_non_integral_constant_expression
5596               (parser, "calls to overloaded operators")))
5597         return error_mark_node;
5598     }
5599
5600   return lhs;
5601 }
5602
5603
5604 /* Parse the `? expression : assignment-expression' part of a
5605    conditional-expression.  The LOGICAL_OR_EXPR is the
5606    logical-or-expression that started the conditional-expression.
5607    Returns a representation of the entire conditional-expression.
5608
5609    This routine is used by cp_parser_assignment_expression.
5610
5611      ? expression : assignment-expression
5612
5613    GNU Extensions:
5614
5615      ? : assignment-expression */
5616
5617 static tree
5618 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5619 {
5620   tree expr;
5621   tree assignment_expr;
5622
5623   /* Consume the `?' token.  */
5624   cp_lexer_consume_token (parser->lexer);
5625   if (cp_parser_allow_gnu_extensions_p (parser)
5626       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5627     /* Implicit true clause.  */
5628     expr = NULL_TREE;
5629   else
5630     /* Parse the expression.  */
5631     expr = cp_parser_expression (parser, /*cast_p=*/false);
5632
5633   /* The next token should be a `:'.  */
5634   cp_parser_require (parser, CPP_COLON, "`:'");
5635   /* Parse the assignment-expression.  */
5636   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5637
5638   /* Build the conditional-expression.  */
5639   return build_x_conditional_expr (logical_or_expr,
5640                                    expr,
5641                                    assignment_expr);
5642 }
5643
5644 /* Parse an assignment-expression.
5645
5646    assignment-expression:
5647      conditional-expression
5648      logical-or-expression assignment-operator assignment_expression
5649      throw-expression
5650
5651    CAST_P is true if this expression is the target of a cast.
5652
5653    Returns a representation for the expression.  */
5654
5655 static tree
5656 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5657 {
5658   tree expr;
5659
5660   /* If the next token is the `throw' keyword, then we're looking at
5661      a throw-expression.  */
5662   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5663     expr = cp_parser_throw_expression (parser);
5664   /* Otherwise, it must be that we are looking at a
5665      logical-or-expression.  */
5666   else
5667     {
5668       /* Parse the binary expressions (logical-or-expression).  */
5669       expr = cp_parser_binary_expression (parser, cast_p);
5670       /* If the next token is a `?' then we're actually looking at a
5671          conditional-expression.  */
5672       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5673         return cp_parser_question_colon_clause (parser, expr);
5674       else
5675         {
5676           enum tree_code assignment_operator;
5677
5678           /* If it's an assignment-operator, we're using the second
5679              production.  */
5680           assignment_operator
5681             = cp_parser_assignment_operator_opt (parser);
5682           if (assignment_operator != ERROR_MARK)
5683             {
5684               tree rhs;
5685
5686               /* Parse the right-hand side of the assignment.  */
5687               rhs = cp_parser_assignment_expression (parser, cast_p);
5688               /* An assignment may not appear in a
5689                  constant-expression.  */
5690               if (cp_parser_non_integral_constant_expression (parser,
5691                                                               "an assignment"))
5692                 return error_mark_node;
5693               /* Build the assignment expression.  */
5694               expr = build_x_modify_expr (expr,
5695                                           assignment_operator,
5696                                           rhs);
5697             }
5698         }
5699     }
5700
5701   return expr;
5702 }
5703
5704 /* Parse an (optional) assignment-operator.
5705
5706    assignment-operator: one of
5707      = *= /= %= += -= >>= <<= &= ^= |=
5708
5709    GNU Extension:
5710
5711    assignment-operator: one of
5712      <?= >?=
5713
5714    If the next token is an assignment operator, the corresponding tree
5715    code is returned, and the token is consumed.  For example, for
5716    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5717    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5718    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5719    operator, ERROR_MARK is returned.  */
5720
5721 static enum tree_code
5722 cp_parser_assignment_operator_opt (cp_parser* parser)
5723 {
5724   enum tree_code op;
5725   cp_token *token;
5726
5727   /* Peek at the next toen.  */
5728   token = cp_lexer_peek_token (parser->lexer);
5729
5730   switch (token->type)
5731     {
5732     case CPP_EQ:
5733       op = NOP_EXPR;
5734       break;
5735
5736     case CPP_MULT_EQ:
5737       op = MULT_EXPR;
5738       break;
5739
5740     case CPP_DIV_EQ:
5741       op = TRUNC_DIV_EXPR;
5742       break;
5743
5744     case CPP_MOD_EQ:
5745       op = TRUNC_MOD_EXPR;
5746       break;
5747
5748     case CPP_PLUS_EQ:
5749       op = PLUS_EXPR;
5750       break;
5751
5752     case CPP_MINUS_EQ:
5753       op = MINUS_EXPR;
5754       break;
5755
5756     case CPP_RSHIFT_EQ:
5757       op = RSHIFT_EXPR;
5758       break;
5759
5760     case CPP_LSHIFT_EQ:
5761       op = LSHIFT_EXPR;
5762       break;
5763
5764     case CPP_AND_EQ:
5765       op = BIT_AND_EXPR;
5766       break;
5767
5768     case CPP_XOR_EQ:
5769       op = BIT_XOR_EXPR;
5770       break;
5771
5772     case CPP_OR_EQ:
5773       op = BIT_IOR_EXPR;
5774       break;
5775
5776     case CPP_MIN_EQ:
5777       op = MIN_EXPR;
5778       cp_parser_warn_min_max ();
5779       break;
5780
5781     case CPP_MAX_EQ:
5782       op = MAX_EXPR;
5783       cp_parser_warn_min_max ();
5784       break;
5785
5786     default:
5787       /* Nothing else is an assignment operator.  */
5788       op = ERROR_MARK;
5789     }
5790
5791   /* If it was an assignment operator, consume it.  */
5792   if (op != ERROR_MARK)
5793     cp_lexer_consume_token (parser->lexer);
5794
5795   return op;
5796 }
5797
5798 /* Parse an expression.
5799
5800    expression:
5801      assignment-expression
5802      expression , assignment-expression
5803
5804    CAST_P is true if this expression is the target of a cast.
5805
5806    Returns a representation of the expression.  */
5807
5808 static tree
5809 cp_parser_expression (cp_parser* parser, bool cast_p)
5810 {
5811   tree expression = NULL_TREE;
5812
5813   while (true)
5814     {
5815       tree assignment_expression;
5816
5817       /* Parse the next assignment-expression.  */
5818       assignment_expression
5819         = cp_parser_assignment_expression (parser, cast_p);
5820       /* If this is the first assignment-expression, we can just
5821          save it away.  */
5822       if (!expression)
5823         expression = assignment_expression;
5824       else
5825         expression = build_x_compound_expr (expression,
5826                                             assignment_expression);
5827       /* If the next token is not a comma, then we are done with the
5828          expression.  */
5829       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5830         break;
5831       /* Consume the `,'.  */
5832       cp_lexer_consume_token (parser->lexer);
5833       /* A comma operator cannot appear in a constant-expression.  */
5834       if (cp_parser_non_integral_constant_expression (parser,
5835                                                       "a comma operator"))
5836         expression = error_mark_node;
5837     }
5838
5839   return expression;
5840 }
5841
5842 /* Parse a constant-expression.
5843
5844    constant-expression:
5845      conditional-expression
5846
5847   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5848   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5849   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5850   is false, NON_CONSTANT_P should be NULL.  */
5851
5852 static tree
5853 cp_parser_constant_expression (cp_parser* parser,
5854                                bool allow_non_constant_p,
5855                                bool *non_constant_p)
5856 {
5857   bool saved_integral_constant_expression_p;
5858   bool saved_allow_non_integral_constant_expression_p;
5859   bool saved_non_integral_constant_expression_p;
5860   tree expression;
5861
5862   /* It might seem that we could simply parse the
5863      conditional-expression, and then check to see if it were
5864      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5865      one that the compiler can figure out is constant, possibly after
5866      doing some simplifications or optimizations.  The standard has a
5867      precise definition of constant-expression, and we must honor
5868      that, even though it is somewhat more restrictive.
5869
5870      For example:
5871
5872        int i[(2, 3)];
5873
5874      is not a legal declaration, because `(2, 3)' is not a
5875      constant-expression.  The `,' operator is forbidden in a
5876      constant-expression.  However, GCC's constant-folding machinery
5877      will fold this operation to an INTEGER_CST for `3'.  */
5878
5879   /* Save the old settings.  */
5880   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5881   saved_allow_non_integral_constant_expression_p
5882     = parser->allow_non_integral_constant_expression_p;
5883   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5884   /* We are now parsing a constant-expression.  */
5885   parser->integral_constant_expression_p = true;
5886   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5887   parser->non_integral_constant_expression_p = false;
5888   /* Although the grammar says "conditional-expression", we parse an
5889      "assignment-expression", which also permits "throw-expression"
5890      and the use of assignment operators.  In the case that
5891      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5892      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5893      actually essential that we look for an assignment-expression.
5894      For example, cp_parser_initializer_clauses uses this function to
5895      determine whether a particular assignment-expression is in fact
5896      constant.  */
5897   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5898   /* Restore the old settings.  */
5899   parser->integral_constant_expression_p
5900     = saved_integral_constant_expression_p;
5901   parser->allow_non_integral_constant_expression_p
5902     = saved_allow_non_integral_constant_expression_p;
5903   if (allow_non_constant_p)
5904     *non_constant_p = parser->non_integral_constant_expression_p;
5905   else if (parser->non_integral_constant_expression_p)
5906     expression = error_mark_node;
5907   parser->non_integral_constant_expression_p
5908     = saved_non_integral_constant_expression_p;
5909
5910   return expression;
5911 }
5912
5913 /* Parse __builtin_offsetof.
5914
5915    offsetof-expression:
5916      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5917
5918    offsetof-member-designator:
5919      id-expression
5920      | offsetof-member-designator "." id-expression
5921      | offsetof-member-designator "[" expression "]"
5922 */
5923
5924 static tree
5925 cp_parser_builtin_offsetof (cp_parser *parser)
5926 {
5927   int save_ice_p, save_non_ice_p;
5928   tree type, expr;
5929   cp_id_kind dummy;
5930
5931   /* We're about to accept non-integral-constant things, but will
5932      definitely yield an integral constant expression.  Save and
5933      restore these values around our local parsing.  */
5934   save_ice_p = parser->integral_constant_expression_p;
5935   save_non_ice_p = parser->non_integral_constant_expression_p;
5936
5937   /* Consume the "__builtin_offsetof" token.  */
5938   cp_lexer_consume_token (parser->lexer);
5939   /* Consume the opening `('.  */
5940   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5941   /* Parse the type-id.  */
5942   type = cp_parser_type_id (parser);
5943   /* Look for the `,'.  */
5944   cp_parser_require (parser, CPP_COMMA, "`,'");
5945
5946   /* Build the (type *)null that begins the traditional offsetof macro.  */
5947   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5948
5949   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5950   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5951                                                  true, &dummy);
5952   while (true)
5953     {
5954       cp_token *token = cp_lexer_peek_token (parser->lexer);
5955       switch (token->type)
5956         {
5957         case CPP_OPEN_SQUARE:
5958           /* offsetof-member-designator "[" expression "]" */
5959           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5960           break;
5961
5962         case CPP_DOT:
5963           /* offsetof-member-designator "." identifier */
5964           cp_lexer_consume_token (parser->lexer);
5965           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5966                                                          true, &dummy);
5967           break;
5968
5969         case CPP_CLOSE_PAREN:
5970           /* Consume the ")" token.  */
5971           cp_lexer_consume_token (parser->lexer);
5972           goto success;
5973
5974         default:
5975           /* Error.  We know the following require will fail, but
5976              that gives the proper error message.  */
5977           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5978           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5979           expr = error_mark_node;
5980           goto failure;
5981         }
5982     }
5983
5984  success:
5985   /* If we're processing a template, we can't finish the semantics yet.
5986      Otherwise we can fold the entire expression now.  */
5987   if (processing_template_decl)
5988     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5989   else
5990     expr = fold_offsetof (expr);
5991
5992  failure:
5993   parser->integral_constant_expression_p = save_ice_p;
5994   parser->non_integral_constant_expression_p = save_non_ice_p;
5995
5996   return expr;
5997 }
5998
5999 /* Statements [gram.stmt.stmt]  */
6000
6001 /* Parse a statement.
6002
6003    statement:
6004      labeled-statement
6005      expression-statement
6006      compound-statement
6007      selection-statement
6008      iteration-statement
6009      jump-statement
6010      declaration-statement
6011      try-block  */
6012
6013 static void
6014 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6015 {
6016   tree statement;
6017   cp_token *token;
6018   location_t statement_location;
6019
6020   /* There is no statement yet.  */
6021   statement = NULL_TREE;
6022   /* Peek at the next token.  */
6023   token = cp_lexer_peek_token (parser->lexer);
6024   /* Remember the location of the first token in the statement.  */
6025   statement_location = token->location;
6026   /* If this is a keyword, then that will often determine what kind of
6027      statement we have.  */
6028   if (token->type == CPP_KEYWORD)
6029     {
6030       enum rid keyword = token->keyword;
6031
6032       switch (keyword)
6033         {
6034         case RID_CASE:
6035         case RID_DEFAULT:
6036           statement = cp_parser_labeled_statement (parser,
6037                                                    in_statement_expr);
6038           break;
6039
6040         case RID_IF:
6041         case RID_SWITCH:
6042           statement = cp_parser_selection_statement (parser);
6043           break;
6044
6045         case RID_WHILE:
6046         case RID_DO:
6047         case RID_FOR:
6048           statement = cp_parser_iteration_statement (parser);
6049           break;
6050
6051         case RID_BREAK:
6052         case RID_CONTINUE:
6053         case RID_RETURN:
6054         case RID_GOTO:
6055           statement = cp_parser_jump_statement (parser);
6056           break;
6057
6058           /* Objective-C++ exception-handling constructs.  */
6059         case RID_AT_TRY:
6060         case RID_AT_CATCH:
6061         case RID_AT_FINALLY:
6062         case RID_AT_SYNCHRONIZED:
6063         case RID_AT_THROW:
6064           statement = cp_parser_objc_statement (parser);
6065           break;
6066
6067         case RID_TRY:
6068           statement = cp_parser_try_block (parser);
6069           break;
6070
6071         default:
6072           /* It might be a keyword like `int' that can start a
6073              declaration-statement.  */
6074           break;
6075         }
6076     }
6077   else if (token->type == CPP_NAME)
6078     {
6079       /* If the next token is a `:', then we are looking at a
6080          labeled-statement.  */
6081       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6082       if (token->type == CPP_COLON)
6083         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6084     }
6085   /* Anything that starts with a `{' must be a compound-statement.  */
6086   else if (token->type == CPP_OPEN_BRACE)
6087     statement = cp_parser_compound_statement (parser, NULL, false);
6088   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6089      a statement all its own.  */
6090   else if (token->type == CPP_PRAGMA)
6091     {
6092       cp_lexer_handle_pragma (parser->lexer);
6093       return;
6094     }
6095
6096   /* Everything else must be a declaration-statement or an
6097      expression-statement.  Try for the declaration-statement
6098      first, unless we are looking at a `;', in which case we know that
6099      we have an expression-statement.  */
6100   if (!statement)
6101     {
6102       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6103         {
6104           cp_parser_parse_tentatively (parser);
6105           /* Try to parse the declaration-statement.  */
6106           cp_parser_declaration_statement (parser);
6107           /* If that worked, we're done.  */
6108           if (cp_parser_parse_definitely (parser))
6109             return;
6110         }
6111       /* Look for an expression-statement instead.  */
6112       statement = cp_parser_expression_statement (parser, in_statement_expr);
6113     }
6114
6115   /* Set the line number for the statement.  */
6116   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6117     SET_EXPR_LOCATION (statement, statement_location);
6118 }
6119
6120 /* Parse a labeled-statement.
6121
6122    labeled-statement:
6123      identifier : statement
6124      case constant-expression : statement
6125      default : statement
6126
6127    GNU Extension:
6128
6129    labeled-statement:
6130      case constant-expression ... constant-expression : statement
6131
6132    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6133    For an ordinary label, returns a LABEL_EXPR.  */
6134
6135 static tree
6136 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6137 {
6138   cp_token *token;
6139   tree statement = error_mark_node;
6140
6141   /* The next token should be an identifier.  */
6142   token = cp_lexer_peek_token (parser->lexer);
6143   if (token->type != CPP_NAME
6144       && token->type != CPP_KEYWORD)
6145     {
6146       cp_parser_error (parser, "expected labeled-statement");
6147       return error_mark_node;
6148     }
6149
6150   switch (token->keyword)
6151     {
6152     case RID_CASE:
6153       {
6154         tree expr, expr_hi;
6155         cp_token *ellipsis;
6156
6157         /* Consume the `case' token.  */
6158         cp_lexer_consume_token (parser->lexer);
6159         /* Parse the constant-expression.  */
6160         expr = cp_parser_constant_expression (parser,
6161                                               /*allow_non_constant_p=*/false,
6162                                               NULL);
6163
6164         ellipsis = cp_lexer_peek_token (parser->lexer);
6165         if (ellipsis->type == CPP_ELLIPSIS)
6166           {
6167             /* Consume the `...' token.  */
6168             cp_lexer_consume_token (parser->lexer);
6169             expr_hi =
6170               cp_parser_constant_expression (parser,
6171                                              /*allow_non_constant_p=*/false,
6172                                              NULL);
6173             /* We don't need to emit warnings here, as the common code
6174                will do this for us.  */
6175           }
6176         else
6177           expr_hi = NULL_TREE;
6178
6179         if (!parser->in_switch_statement_p)
6180           error ("case label %qE not within a switch statement", expr);
6181         else
6182           statement = finish_case_label (expr, expr_hi);
6183       }
6184       break;
6185
6186     case RID_DEFAULT:
6187       /* Consume the `default' token.  */
6188       cp_lexer_consume_token (parser->lexer);
6189       if (!parser->in_switch_statement_p)
6190         error ("case label not within a switch statement");
6191       else
6192         statement = finish_case_label (NULL_TREE, NULL_TREE);
6193       break;
6194
6195     default:
6196       /* Anything else must be an ordinary label.  */
6197       statement = finish_label_stmt (cp_parser_identifier (parser));
6198       break;
6199     }
6200
6201   /* Require the `:' token.  */
6202   cp_parser_require (parser, CPP_COLON, "`:'");
6203   /* Parse the labeled statement.  */
6204   cp_parser_statement (parser, in_statement_expr);
6205
6206   /* Return the label, in the case of a `case' or `default' label.  */
6207   return statement;
6208 }
6209
6210 /* Parse an expression-statement.
6211
6212    expression-statement:
6213      expression [opt] ;
6214
6215    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6216    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6217    indicates whether this expression-statement is part of an
6218    expression statement.  */
6219
6220 static tree
6221 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6222 {
6223   tree statement = NULL_TREE;
6224
6225   /* If the next token is a ';', then there is no expression
6226      statement.  */
6227   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6228     statement = cp_parser_expression (parser, /*cast_p=*/false);
6229
6230   /* Consume the final `;'.  */
6231   cp_parser_consume_semicolon_at_end_of_statement (parser);
6232
6233   if (in_statement_expr
6234       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6235     /* This is the final expression statement of a statement
6236        expression.  */
6237     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6238   else if (statement)
6239     statement = finish_expr_stmt (statement);
6240   else
6241     finish_stmt ();
6242
6243   return statement;
6244 }
6245
6246 /* Parse a compound-statement.
6247
6248    compound-statement:
6249      { statement-seq [opt] }
6250
6251    Returns a tree representing the statement.  */
6252
6253 static tree
6254 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6255                               bool in_try)
6256 {
6257   tree compound_stmt;
6258
6259   /* Consume the `{'.  */
6260   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6261     return error_mark_node;
6262   /* Begin the compound-statement.  */
6263   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6264   /* Parse an (optional) statement-seq.  */
6265   cp_parser_statement_seq_opt (parser, in_statement_expr);
6266   /* Finish the compound-statement.  */
6267   finish_compound_stmt (compound_stmt);
6268   /* Consume the `}'.  */
6269   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6270
6271   return compound_stmt;
6272 }
6273
6274 /* Parse an (optional) statement-seq.
6275
6276    statement-seq:
6277      statement
6278      statement-seq [opt] statement  */
6279
6280 static void
6281 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6282 {
6283   /* Scan statements until there aren't any more.  */
6284   while (true)
6285     {
6286       /* If we're looking at a `}', then we've run out of statements.  */
6287       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6288           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6289         break;
6290
6291       /* Parse the statement.  */
6292       cp_parser_statement (parser, in_statement_expr);
6293     }
6294 }
6295
6296 /* Parse a selection-statement.
6297
6298    selection-statement:
6299      if ( condition ) statement
6300      if ( condition ) statement else statement
6301      switch ( condition ) statement
6302
6303    Returns the new IF_STMT or SWITCH_STMT.  */
6304
6305 static tree
6306 cp_parser_selection_statement (cp_parser* parser)
6307 {
6308   cp_token *token;
6309   enum rid keyword;
6310
6311   /* Peek at the next token.  */
6312   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6313
6314   /* See what kind of keyword it is.  */
6315   keyword = token->keyword;
6316   switch (keyword)
6317     {
6318     case RID_IF:
6319     case RID_SWITCH:
6320       {
6321         tree statement;
6322         tree condition;
6323
6324         /* Look for the `('.  */
6325         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6326           {
6327             cp_parser_skip_to_end_of_statement (parser);
6328             return error_mark_node;
6329           }
6330
6331         /* Begin the selection-statement.  */
6332         if (keyword == RID_IF)
6333           statement = begin_if_stmt ();
6334         else
6335           statement = begin_switch_stmt ();
6336
6337         /* Parse the condition.  */
6338         condition = cp_parser_condition (parser);
6339         /* Look for the `)'.  */
6340         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6341           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6342                                                  /*consume_paren=*/true);
6343
6344         if (keyword == RID_IF)
6345           {
6346             /* Add the condition.  */
6347             finish_if_stmt_cond (condition, statement);
6348
6349             /* Parse the then-clause.  */
6350             cp_parser_implicitly_scoped_statement (parser);
6351             finish_then_clause (statement);
6352
6353             /* If the next token is `else', parse the else-clause.  */
6354             if (cp_lexer_next_token_is_keyword (parser->lexer,
6355                                                 RID_ELSE))
6356               {
6357                 /* Consume the `else' keyword.  */
6358                 cp_lexer_consume_token (parser->lexer);
6359                 begin_else_clause (statement);
6360                 /* Parse the else-clause.  */
6361                 cp_parser_implicitly_scoped_statement (parser);
6362                 finish_else_clause (statement);
6363               }
6364
6365             /* Now we're all done with the if-statement.  */
6366             finish_if_stmt (statement);
6367           }
6368         else
6369           {
6370             bool in_switch_statement_p;
6371
6372             /* Add the condition.  */
6373             finish_switch_cond (condition, statement);
6374
6375             /* Parse the body of the switch-statement.  */
6376             in_switch_statement_p = parser->in_switch_statement_p;
6377             parser->in_switch_statement_p = true;
6378             cp_parser_implicitly_scoped_statement (parser);
6379             parser->in_switch_statement_p = in_switch_statement_p;
6380
6381             /* Now we're all done with the switch-statement.  */
6382             finish_switch_stmt (statement);
6383           }
6384
6385         return statement;
6386       }
6387       break;
6388
6389     default:
6390       cp_parser_error (parser, "expected selection-statement");
6391       return error_mark_node;
6392     }
6393 }
6394
6395 /* Parse a condition.
6396
6397    condition:
6398      expression
6399      type-specifier-seq declarator = assignment-expression
6400
6401    GNU Extension:
6402
6403    condition:
6404      type-specifier-seq declarator asm-specification [opt]
6405        attributes [opt] = assignment-expression
6406
6407    Returns the expression that should be tested.  */
6408
6409 static tree
6410 cp_parser_condition (cp_parser* parser)
6411 {
6412   cp_decl_specifier_seq type_specifiers;
6413   const char *saved_message;
6414
6415   /* Try the declaration first.  */
6416   cp_parser_parse_tentatively (parser);
6417   /* New types are not allowed in the type-specifier-seq for a
6418      condition.  */
6419   saved_message = parser->type_definition_forbidden_message;
6420   parser->type_definition_forbidden_message
6421     = "types may not be defined in conditions";
6422   /* Parse the type-specifier-seq.  */
6423   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6424                                 &type_specifiers);
6425   /* Restore the saved message.  */
6426   parser->type_definition_forbidden_message = saved_message;
6427   /* If all is well, we might be looking at a declaration.  */
6428   if (!cp_parser_error_occurred (parser))
6429     {
6430       tree decl;
6431       tree asm_specification;
6432       tree attributes;
6433       cp_declarator *declarator;
6434       tree initializer = NULL_TREE;
6435
6436       /* Parse the declarator.  */
6437       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6438                                          /*ctor_dtor_or_conv_p=*/NULL,
6439                                          /*parenthesized_p=*/NULL,
6440                                          /*member_p=*/false);
6441       /* Parse the attributes.  */
6442       attributes = cp_parser_attributes_opt (parser);
6443       /* Parse the asm-specification.  */
6444       asm_specification = cp_parser_asm_specification_opt (parser);
6445       /* If the next token is not an `=', then we might still be
6446          looking at an expression.  For example:
6447
6448            if (A(a).x)
6449
6450          looks like a decl-specifier-seq and a declarator -- but then
6451          there is no `=', so this is an expression.  */
6452       cp_parser_require (parser, CPP_EQ, "`='");
6453       /* If we did see an `=', then we are looking at a declaration
6454          for sure.  */
6455       if (cp_parser_parse_definitely (parser))
6456         {
6457           tree pushed_scope;
6458
6459           /* Create the declaration.  */
6460           decl = start_decl (declarator, &type_specifiers,
6461                              /*initialized_p=*/true,
6462                              attributes, /*prefix_attributes=*/NULL_TREE,
6463                              &pushed_scope);
6464           /* Parse the assignment-expression.  */
6465           initializer = cp_parser_assignment_expression (parser,
6466                                                          /*cast_p=*/false);
6467
6468           /* Process the initializer.  */
6469           cp_finish_decl (decl,
6470                           initializer,
6471                           asm_specification,
6472                           LOOKUP_ONLYCONVERTING);
6473
6474           if (pushed_scope)
6475             pop_scope (pushed_scope);
6476
6477           return convert_from_reference (decl);
6478         }
6479     }
6480   /* If we didn't even get past the declarator successfully, we are
6481      definitely not looking at a declaration.  */
6482   else
6483     cp_parser_abort_tentative_parse (parser);
6484
6485   /* Otherwise, we are looking at an expression.  */
6486   return cp_parser_expression (parser, /*cast_p=*/false);
6487 }
6488
6489 /* Parse an iteration-statement.
6490
6491    iteration-statement:
6492      while ( condition ) statement
6493      do statement while ( expression ) ;
6494      for ( for-init-statement condition [opt] ; expression [opt] )
6495        statement
6496
6497    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6498
6499 static tree
6500 cp_parser_iteration_statement (cp_parser* parser)
6501 {
6502   cp_token *token;
6503   enum rid keyword;
6504   tree statement;
6505   bool in_iteration_statement_p;
6506
6507
6508   /* Peek at the next token.  */
6509   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6510   if (!token)
6511     return error_mark_node;
6512
6513   /* Remember whether or not we are already within an iteration
6514      statement.  */
6515   in_iteration_statement_p = parser->in_iteration_statement_p;
6516
6517   /* See what kind of keyword it is.  */
6518   keyword = token->keyword;
6519   switch (keyword)
6520     {
6521     case RID_WHILE:
6522       {
6523         tree condition;
6524
6525         /* Begin the while-statement.  */
6526         statement = begin_while_stmt ();
6527         /* Look for the `('.  */
6528         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6529         /* Parse the condition.  */
6530         condition = cp_parser_condition (parser);
6531         finish_while_stmt_cond (condition, statement);
6532         /* Look for the `)'.  */
6533         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6534         /* Parse the dependent statement.  */
6535         parser->in_iteration_statement_p = true;
6536         cp_parser_already_scoped_statement (parser);
6537         parser->in_iteration_statement_p = in_iteration_statement_p;
6538         /* We're done with the while-statement.  */
6539         finish_while_stmt (statement);
6540       }
6541       break;
6542
6543     case RID_DO:
6544       {
6545         tree expression;
6546
6547         /* Begin the do-statement.  */
6548         statement = begin_do_stmt ();
6549         /* Parse the body of the do-statement.  */
6550         parser->in_iteration_statement_p = true;
6551         cp_parser_implicitly_scoped_statement (parser);
6552         parser->in_iteration_statement_p = in_iteration_statement_p;
6553         finish_do_body (statement);
6554         /* Look for the `while' keyword.  */
6555         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6556         /* Look for the `('.  */
6557         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6558         /* Parse the expression.  */
6559         expression = cp_parser_expression (parser, /*cast_p=*/false);
6560         /* We're done with the do-statement.  */
6561         finish_do_stmt (expression, statement);
6562         /* Look for the `)'.  */
6563         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6564         /* Look for the `;'.  */
6565         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6566       }
6567       break;
6568
6569     case RID_FOR:
6570       {
6571         tree condition = NULL_TREE;
6572         tree expression = NULL_TREE;
6573
6574         /* Begin the for-statement.  */
6575         statement = begin_for_stmt ();
6576         /* Look for the `('.  */
6577         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6578         /* Parse the initialization.  */
6579         cp_parser_for_init_statement (parser);
6580         finish_for_init_stmt (statement);
6581
6582         /* If there's a condition, process it.  */
6583         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6584           condition = cp_parser_condition (parser);
6585         finish_for_cond (condition, statement);
6586         /* Look for the `;'.  */
6587         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6588
6589         /* If there's an expression, process it.  */
6590         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6591           expression = cp_parser_expression (parser, /*cast_p=*/false);
6592         finish_for_expr (expression, statement);
6593         /* Look for the `)'.  */
6594         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6595
6596         /* Parse the body of the for-statement.  */
6597         parser->in_iteration_statement_p = true;
6598         cp_parser_already_scoped_statement (parser);
6599         parser->in_iteration_statement_p = in_iteration_statement_p;
6600
6601         /* We're done with the for-statement.  */
6602         finish_for_stmt (statement);
6603       }
6604       break;
6605
6606     default:
6607       cp_parser_error (parser, "expected iteration-statement");
6608       statement = error_mark_node;
6609       break;
6610     }
6611
6612   return statement;
6613 }
6614
6615 /* Parse a for-init-statement.
6616
6617    for-init-statement:
6618      expression-statement
6619      simple-declaration  */
6620
6621 static void
6622 cp_parser_for_init_statement (cp_parser* parser)
6623 {
6624   /* If the next token is a `;', then we have an empty
6625      expression-statement.  Grammatically, this is also a
6626      simple-declaration, but an invalid one, because it does not
6627      declare anything.  Therefore, if we did not handle this case
6628      specially, we would issue an error message about an invalid
6629      declaration.  */
6630   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6631     {
6632       /* We're going to speculatively look for a declaration, falling back
6633          to an expression, if necessary.  */
6634       cp_parser_parse_tentatively (parser);
6635       /* Parse the declaration.  */
6636       cp_parser_simple_declaration (parser,
6637                                     /*function_definition_allowed_p=*/false);
6638       /* If the tentative parse failed, then we shall need to look for an
6639          expression-statement.  */
6640       if (cp_parser_parse_definitely (parser))
6641         return;
6642     }
6643
6644   cp_parser_expression_statement (parser, false);
6645 }
6646
6647 /* Parse a jump-statement.
6648
6649    jump-statement:
6650      break ;
6651      continue ;
6652      return expression [opt] ;
6653      goto identifier ;
6654
6655    GNU extension:
6656
6657    jump-statement:
6658      goto * expression ;
6659
6660    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6661
6662 static tree
6663 cp_parser_jump_statement (cp_parser* parser)
6664 {
6665   tree statement = error_mark_node;
6666   cp_token *token;
6667   enum rid keyword;
6668
6669   /* Peek at the next token.  */
6670   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6671   if (!token)
6672     return error_mark_node;
6673
6674   /* See what kind of keyword it is.  */
6675   keyword = token->keyword;
6676   switch (keyword)
6677     {
6678     case RID_BREAK:
6679       if (!parser->in_switch_statement_p
6680           && !parser->in_iteration_statement_p)
6681         {
6682           error ("break statement not within loop or switch");
6683           statement = error_mark_node;
6684         }
6685       else
6686         statement = finish_break_stmt ();
6687       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6688       break;
6689
6690     case RID_CONTINUE:
6691       if (!parser->in_iteration_statement_p)
6692         {
6693           error ("continue statement not within a loop");
6694           statement = error_mark_node;
6695         }
6696       else
6697         statement = finish_continue_stmt ();
6698       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6699       break;
6700
6701     case RID_RETURN:
6702       {
6703         tree expr;
6704
6705         /* If the next token is a `;', then there is no
6706            expression.  */
6707         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6708           expr = cp_parser_expression (parser, /*cast_p=*/false);
6709         else
6710           expr = NULL_TREE;
6711         /* Build the return-statement.  */
6712         statement = finish_return_stmt (expr);
6713         /* Look for the final `;'.  */
6714         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6715       }
6716       break;
6717
6718     case RID_GOTO:
6719       /* Create the goto-statement.  */
6720       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6721         {
6722           /* Issue a warning about this use of a GNU extension.  */
6723           if (pedantic)
6724             pedwarn ("ISO C++ forbids computed gotos");
6725           /* Consume the '*' token.  */
6726           cp_lexer_consume_token (parser->lexer);
6727           /* Parse the dependent expression.  */
6728           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6729         }
6730       else
6731         finish_goto_stmt (cp_parser_identifier (parser));
6732       /* Look for the final `;'.  */
6733       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6734       break;
6735
6736     default:
6737       cp_parser_error (parser, "expected jump-statement");
6738       break;
6739     }
6740
6741   return statement;
6742 }
6743
6744 /* Parse a declaration-statement.
6745
6746    declaration-statement:
6747      block-declaration  */
6748
6749 static void
6750 cp_parser_declaration_statement (cp_parser* parser)
6751 {
6752   void *p;
6753
6754   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6755   p = obstack_alloc (&declarator_obstack, 0);
6756
6757  /* Parse the block-declaration.  */
6758   cp_parser_block_declaration (parser, /*statement_p=*/true);
6759
6760   /* Free any declarators allocated.  */
6761   obstack_free (&declarator_obstack, p);
6762
6763   /* Finish off the statement.  */
6764   finish_stmt ();
6765 }
6766
6767 /* Some dependent statements (like `if (cond) statement'), are
6768    implicitly in their own scope.  In other words, if the statement is
6769    a single statement (as opposed to a compound-statement), it is
6770    none-the-less treated as if it were enclosed in braces.  Any
6771    declarations appearing in the dependent statement are out of scope
6772    after control passes that point.  This function parses a statement,
6773    but ensures that is in its own scope, even if it is not a
6774    compound-statement.
6775
6776    Returns the new statement.  */
6777
6778 static tree
6779 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6780 {
6781   tree statement;
6782
6783   /* If the token is not a `{', then we must take special action.  */
6784   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6785     {
6786       /* Create a compound-statement.  */
6787       statement = begin_compound_stmt (0);
6788       /* Parse the dependent-statement.  */
6789       cp_parser_statement (parser, false);
6790       /* Finish the dummy compound-statement.  */
6791       finish_compound_stmt (statement);
6792     }
6793   /* Otherwise, we simply parse the statement directly.  */
6794   else
6795     statement = cp_parser_compound_statement (parser, NULL, false);
6796
6797   /* Return the statement.  */
6798   return statement;
6799 }
6800
6801 /* For some dependent statements (like `while (cond) statement'), we
6802    have already created a scope.  Therefore, even if the dependent
6803    statement is a compound-statement, we do not want to create another
6804    scope.  */
6805
6806 static void
6807 cp_parser_already_scoped_statement (cp_parser* parser)
6808 {
6809   /* If the token is a `{', then we must take special action.  */
6810   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6811     cp_parser_statement (parser, false);
6812   else
6813     {
6814       /* Avoid calling cp_parser_compound_statement, so that we
6815          don't create a new scope.  Do everything else by hand.  */
6816       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6817       cp_parser_statement_seq_opt (parser, false);
6818       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6819     }
6820 }
6821
6822 /* Declarations [gram.dcl.dcl] */
6823
6824 /* Parse an optional declaration-sequence.
6825
6826    declaration-seq:
6827      declaration
6828      declaration-seq declaration  */
6829
6830 static void
6831 cp_parser_declaration_seq_opt (cp_parser* parser)
6832 {
6833   while (true)
6834     {
6835       cp_token *token;
6836
6837       token = cp_lexer_peek_token (parser->lexer);
6838
6839       if (token->type == CPP_CLOSE_BRACE
6840           || token->type == CPP_EOF)
6841         break;
6842
6843       if (token->type == CPP_SEMICOLON)
6844         {
6845           /* A declaration consisting of a single semicolon is
6846              invalid.  Allow it unless we're being pedantic.  */
6847           cp_lexer_consume_token (parser->lexer);
6848           if (pedantic && !in_system_header)
6849             pedwarn ("extra %<;%>");
6850           continue;
6851         }
6852
6853       /* If we're entering or exiting a region that's implicitly
6854          extern "C", modify the lang context appropriately.  */
6855       if (!parser->implicit_extern_c && token->implicit_extern_c)
6856         {
6857           push_lang_context (lang_name_c);
6858           parser->implicit_extern_c = true;
6859         }
6860       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6861         {
6862           pop_lang_context ();
6863           parser->implicit_extern_c = false;
6864         }
6865
6866       if (token->type == CPP_PRAGMA)
6867         {
6868           /* A top-level declaration can consist solely of a #pragma.
6869              A nested declaration cannot, so this is done here and not
6870              in cp_parser_declaration.  (A #pragma at block scope is
6871              handled in cp_parser_statement.)  */
6872           cp_lexer_handle_pragma (parser->lexer);
6873           continue;
6874         }
6875
6876       /* Parse the declaration itself.  */
6877       cp_parser_declaration (parser);
6878     }
6879 }
6880
6881 /* Parse a declaration.
6882
6883    declaration:
6884      block-declaration
6885      function-definition
6886      template-declaration
6887      explicit-instantiation
6888      explicit-specialization
6889      linkage-specification
6890      namespace-definition
6891
6892    GNU extension:
6893
6894    declaration:
6895       __extension__ declaration */
6896
6897 static void
6898 cp_parser_declaration (cp_parser* parser)
6899 {
6900   cp_token token1;
6901   cp_token token2;
6902   int saved_pedantic;
6903   void *p;
6904
6905   /* Check for the `__extension__' keyword.  */
6906   if (cp_parser_extension_opt (parser, &saved_pedantic))
6907     {
6908       /* Parse the qualified declaration.  */
6909       cp_parser_declaration (parser);
6910       /* Restore the PEDANTIC flag.  */
6911       pedantic = saved_pedantic;
6912
6913       return;
6914     }
6915
6916   /* Try to figure out what kind of declaration is present.  */
6917   token1 = *cp_lexer_peek_token (parser->lexer);
6918
6919   if (token1.type != CPP_EOF)
6920     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6921   else
6922     token2.type = token2.keyword = RID_MAX;
6923
6924   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6925   p = obstack_alloc (&declarator_obstack, 0);
6926
6927   /* If the next token is `extern' and the following token is a string
6928      literal, then we have a linkage specification.  */
6929   if (token1.keyword == RID_EXTERN
6930       && cp_parser_is_string_literal (&token2))
6931     cp_parser_linkage_specification (parser);
6932   /* If the next token is `template', then we have either a template
6933      declaration, an explicit instantiation, or an explicit
6934      specialization.  */
6935   else if (token1.keyword == RID_TEMPLATE)
6936     {
6937       /* `template <>' indicates a template specialization.  */
6938       if (token2.type == CPP_LESS
6939           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6940         cp_parser_explicit_specialization (parser);
6941       /* `template <' indicates a template declaration.  */
6942       else if (token2.type == CPP_LESS)
6943         cp_parser_template_declaration (parser, /*member_p=*/false);
6944       /* Anything else must be an explicit instantiation.  */
6945       else
6946         cp_parser_explicit_instantiation (parser);
6947     }
6948   /* If the next token is `export', then we have a template
6949      declaration.  */
6950   else if (token1.keyword == RID_EXPORT)
6951     cp_parser_template_declaration (parser, /*member_p=*/false);
6952   /* If the next token is `extern', 'static' or 'inline' and the one
6953      after that is `template', we have a GNU extended explicit
6954      instantiation directive.  */
6955   else if (cp_parser_allow_gnu_extensions_p (parser)
6956            && (token1.keyword == RID_EXTERN
6957                || token1.keyword == RID_STATIC
6958                || token1.keyword == RID_INLINE)
6959            && token2.keyword == RID_TEMPLATE)
6960     cp_parser_explicit_instantiation (parser);
6961   /* If the next token is `namespace', check for a named or unnamed
6962      namespace definition.  */
6963   else if (token1.keyword == RID_NAMESPACE
6964            && (/* A named namespace definition.  */
6965                (token2.type == CPP_NAME
6966                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6967                     == CPP_OPEN_BRACE))
6968                /* An unnamed namespace definition.  */
6969                || token2.type == CPP_OPEN_BRACE))
6970     cp_parser_namespace_definition (parser);
6971   /* Objective-C++ declaration/definition.  */
6972   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6973     cp_parser_objc_declaration (parser);
6974   /* We must have either a block declaration or a function
6975      definition.  */
6976   else
6977     /* Try to parse a block-declaration, or a function-definition.  */
6978     cp_parser_block_declaration (parser, /*statement_p=*/false);
6979
6980   /* Free any declarators allocated.  */
6981   obstack_free (&declarator_obstack, p);
6982 }
6983
6984 /* Parse a block-declaration.
6985
6986    block-declaration:
6987      simple-declaration
6988      asm-definition
6989      namespace-alias-definition
6990      using-declaration
6991      using-directive
6992
6993    GNU Extension:
6994
6995    block-declaration:
6996      __extension__ block-declaration
6997      label-declaration
6998
6999    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7000    part of a declaration-statement.  */
7001
7002 static void
7003 cp_parser_block_declaration (cp_parser *parser,
7004                              bool      statement_p)
7005 {
7006   cp_token *token1;
7007   int saved_pedantic;
7008
7009   /* Check for the `__extension__' keyword.  */
7010   if (cp_parser_extension_opt (parser, &saved_pedantic))
7011     {
7012       /* Parse the qualified declaration.  */
7013       cp_parser_block_declaration (parser, statement_p);
7014       /* Restore the PEDANTIC flag.  */
7015       pedantic = saved_pedantic;
7016
7017       return;
7018     }
7019
7020   /* Peek at the next token to figure out which kind of declaration is
7021      present.  */
7022   token1 = cp_lexer_peek_token (parser->lexer);
7023
7024   /* If the next keyword is `asm', we have an asm-definition.  */
7025   if (token1->keyword == RID_ASM)
7026     {
7027       if (statement_p)
7028         cp_parser_commit_to_tentative_parse (parser);
7029       cp_parser_asm_definition (parser);
7030     }
7031   /* If the next keyword is `namespace', we have a
7032      namespace-alias-definition.  */
7033   else if (token1->keyword == RID_NAMESPACE)
7034     cp_parser_namespace_alias_definition (parser);
7035   /* If the next keyword is `using', we have either a
7036      using-declaration or a using-directive.  */
7037   else if (token1->keyword == RID_USING)
7038     {
7039       cp_token *token2;
7040
7041       if (statement_p)
7042         cp_parser_commit_to_tentative_parse (parser);
7043       /* If the token after `using' is `namespace', then we have a
7044          using-directive.  */
7045       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7046       if (token2->keyword == RID_NAMESPACE)
7047         cp_parser_using_directive (parser);
7048       /* Otherwise, it's a using-declaration.  */
7049       else
7050         cp_parser_using_declaration (parser);
7051     }
7052   /* If the next keyword is `__label__' we have a label declaration.  */
7053   else if (token1->keyword == RID_LABEL)
7054     {
7055       if (statement_p)
7056         cp_parser_commit_to_tentative_parse (parser);
7057       cp_parser_label_declaration (parser);
7058     }
7059   /* Anything else must be a simple-declaration.  */
7060   else
7061     cp_parser_simple_declaration (parser, !statement_p);
7062 }
7063
7064 /* Parse a simple-declaration.
7065
7066    simple-declaration:
7067      decl-specifier-seq [opt] init-declarator-list [opt] ;
7068
7069    init-declarator-list:
7070      init-declarator
7071      init-declarator-list , init-declarator
7072
7073    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7074    function-definition as a simple-declaration.  */
7075
7076 static void
7077 cp_parser_simple_declaration (cp_parser* parser,
7078                               bool function_definition_allowed_p)
7079 {
7080   cp_decl_specifier_seq decl_specifiers;
7081   int declares_class_or_enum;
7082   bool saw_declarator;
7083
7084   /* Defer access checks until we know what is being declared; the
7085      checks for names appearing in the decl-specifier-seq should be
7086      done as if we were in the scope of the thing being declared.  */
7087   push_deferring_access_checks (dk_deferred);
7088
7089   /* Parse the decl-specifier-seq.  We have to keep track of whether
7090      or not the decl-specifier-seq declares a named class or
7091      enumeration type, since that is the only case in which the
7092      init-declarator-list is allowed to be empty.
7093
7094      [dcl.dcl]
7095
7096      In a simple-declaration, the optional init-declarator-list can be
7097      omitted only when declaring a class or enumeration, that is when
7098      the decl-specifier-seq contains either a class-specifier, an
7099      elaborated-type-specifier, or an enum-specifier.  */
7100   cp_parser_decl_specifier_seq (parser,
7101                                 CP_PARSER_FLAGS_OPTIONAL,
7102                                 &decl_specifiers,
7103                                 &declares_class_or_enum);
7104   /* We no longer need to defer access checks.  */
7105   stop_deferring_access_checks ();
7106
7107   /* In a block scope, a valid declaration must always have a
7108      decl-specifier-seq.  By not trying to parse declarators, we can
7109      resolve the declaration/expression ambiguity more quickly.  */
7110   if (!function_definition_allowed_p
7111       && !decl_specifiers.any_specifiers_p)
7112     {
7113       cp_parser_error (parser, "expected declaration");
7114       goto done;
7115     }
7116
7117   /* If the next two tokens are both identifiers, the code is
7118      erroneous. The usual cause of this situation is code like:
7119
7120        T t;
7121
7122      where "T" should name a type -- but does not.  */
7123   if (!decl_specifiers.type
7124       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7125     {
7126       /* If parsing tentatively, we should commit; we really are
7127          looking at a declaration.  */
7128       cp_parser_commit_to_tentative_parse (parser);
7129       /* Give up.  */
7130       goto done;
7131     }
7132
7133   /* If we have seen at least one decl-specifier, and the next token
7134      is not a parenthesis, then we must be looking at a declaration.
7135      (After "int (" we might be looking at a functional cast.)  */
7136   if (decl_specifiers.any_specifiers_p
7137       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7138     cp_parser_commit_to_tentative_parse (parser);
7139
7140   /* Keep going until we hit the `;' at the end of the simple
7141      declaration.  */
7142   saw_declarator = false;
7143   while (cp_lexer_next_token_is_not (parser->lexer,
7144                                      CPP_SEMICOLON))
7145     {
7146       cp_token *token;
7147       bool function_definition_p;
7148       tree decl;
7149
7150       saw_declarator = true;
7151       /* Parse the init-declarator.  */
7152       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7153                                         function_definition_allowed_p,
7154                                         /*member_p=*/false,
7155                                         declares_class_or_enum,
7156                                         &function_definition_p);
7157       /* If an error occurred while parsing tentatively, exit quickly.
7158          (That usually happens when in the body of a function; each
7159          statement is treated as a declaration-statement until proven
7160          otherwise.)  */
7161       if (cp_parser_error_occurred (parser))
7162         goto done;
7163       /* Handle function definitions specially.  */
7164       if (function_definition_p)
7165         {
7166           /* If the next token is a `,', then we are probably
7167              processing something like:
7168
7169                void f() {}, *p;
7170
7171              which is erroneous.  */
7172           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7173             error ("mixing declarations and function-definitions is forbidden");
7174           /* Otherwise, we're done with the list of declarators.  */
7175           else
7176             {
7177               pop_deferring_access_checks ();
7178               return;
7179             }
7180         }
7181       /* The next token should be either a `,' or a `;'.  */
7182       token = cp_lexer_peek_token (parser->lexer);
7183       /* If it's a `,', there are more declarators to come.  */
7184       if (token->type == CPP_COMMA)
7185         cp_lexer_consume_token (parser->lexer);
7186       /* If it's a `;', we are done.  */
7187       else if (token->type == CPP_SEMICOLON)
7188         break;
7189       /* Anything else is an error.  */
7190       else
7191         {
7192           /* If we have already issued an error message we don't need
7193              to issue another one.  */
7194           if (decl != error_mark_node
7195               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7196             cp_parser_error (parser, "expected %<,%> or %<;%>");
7197           /* Skip tokens until we reach the end of the statement.  */
7198           cp_parser_skip_to_end_of_statement (parser);
7199           /* If the next token is now a `;', consume it.  */
7200           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7201             cp_lexer_consume_token (parser->lexer);
7202           goto done;
7203         }
7204       /* After the first time around, a function-definition is not
7205          allowed -- even if it was OK at first.  For example:
7206
7207            int i, f() {}
7208
7209          is not valid.  */
7210       function_definition_allowed_p = false;
7211     }
7212
7213   /* Issue an error message if no declarators are present, and the
7214      decl-specifier-seq does not itself declare a class or
7215      enumeration.  */
7216   if (!saw_declarator)
7217     {
7218       if (cp_parser_declares_only_class_p (parser))
7219         shadow_tag (&decl_specifiers);
7220       /* Perform any deferred access checks.  */
7221       perform_deferred_access_checks ();
7222     }
7223
7224   /* Consume the `;'.  */
7225   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7226
7227  done:
7228   pop_deferring_access_checks ();
7229 }
7230
7231 /* Parse a decl-specifier-seq.
7232
7233    decl-specifier-seq:
7234      decl-specifier-seq [opt] decl-specifier
7235
7236    decl-specifier:
7237      storage-class-specifier
7238      type-specifier
7239      function-specifier
7240      friend
7241      typedef
7242
7243    GNU Extension:
7244
7245    decl-specifier:
7246      attributes
7247
7248    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7249
7250    The parser flags FLAGS is used to control type-specifier parsing.
7251
7252    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7253    flags:
7254
7255      1: one of the decl-specifiers is an elaborated-type-specifier
7256         (i.e., a type declaration)
7257      2: one of the decl-specifiers is an enum-specifier or a
7258         class-specifier (i.e., a type definition)
7259
7260    */
7261
7262 static void
7263 cp_parser_decl_specifier_seq (cp_parser* parser,
7264                               cp_parser_flags flags,
7265                               cp_decl_specifier_seq *decl_specs,
7266                               int* declares_class_or_enum)
7267 {
7268   bool constructor_possible_p = !parser->in_declarator_p;
7269
7270   /* Clear DECL_SPECS.  */
7271   clear_decl_specs (decl_specs);
7272
7273   /* Assume no class or enumeration type is declared.  */
7274   *declares_class_or_enum = 0;
7275
7276   /* Keep reading specifiers until there are no more to read.  */
7277   while (true)
7278     {
7279       bool constructor_p;
7280       bool found_decl_spec;
7281       cp_token *token;
7282
7283       /* Peek at the next token.  */
7284       token = cp_lexer_peek_token (parser->lexer);
7285       /* Handle attributes.  */
7286       if (token->keyword == RID_ATTRIBUTE)
7287         {
7288           /* Parse the attributes.  */
7289           decl_specs->attributes
7290             = chainon (decl_specs->attributes,
7291                        cp_parser_attributes_opt (parser));
7292           continue;
7293         }
7294       /* Assume we will find a decl-specifier keyword.  */
7295       found_decl_spec = true;
7296       /* If the next token is an appropriate keyword, we can simply
7297          add it to the list.  */
7298       switch (token->keyword)
7299         {
7300           /* decl-specifier:
7301                friend  */
7302         case RID_FRIEND:
7303           if (decl_specs->specs[(int) ds_friend]++)
7304             error ("duplicate %<friend%>");
7305           /* Consume the token.  */
7306           cp_lexer_consume_token (parser->lexer);
7307           break;
7308
7309           /* function-specifier:
7310                inline
7311                virtual
7312                explicit  */
7313         case RID_INLINE:
7314         case RID_VIRTUAL:
7315         case RID_EXPLICIT:
7316           cp_parser_function_specifier_opt (parser, decl_specs);
7317           break;
7318
7319           /* decl-specifier:
7320                typedef  */
7321         case RID_TYPEDEF:
7322           ++decl_specs->specs[(int) ds_typedef];
7323           /* Consume the token.  */
7324           cp_lexer_consume_token (parser->lexer);
7325           /* A constructor declarator cannot appear in a typedef.  */
7326           constructor_possible_p = false;
7327           /* The "typedef" keyword can only occur in a declaration; we
7328              may as well commit at this point.  */
7329           cp_parser_commit_to_tentative_parse (parser);
7330           break;
7331
7332           /* storage-class-specifier:
7333                auto
7334                register
7335                static
7336                extern
7337                mutable
7338
7339              GNU Extension:
7340                thread  */
7341         case RID_AUTO:
7342           /* Consume the token.  */
7343           cp_lexer_consume_token (parser->lexer);
7344           cp_parser_set_storage_class (decl_specs, sc_auto);
7345           break;
7346         case RID_REGISTER:
7347           /* Consume the token.  */
7348           cp_lexer_consume_token (parser->lexer);
7349           cp_parser_set_storage_class (decl_specs, sc_register);
7350           break;
7351         case RID_STATIC:
7352           /* Consume the token.  */
7353           cp_lexer_consume_token (parser->lexer);
7354           if (decl_specs->specs[(int) ds_thread])
7355             {
7356               error ("%<__thread%> before %<static%>");
7357               decl_specs->specs[(int) ds_thread] = 0;
7358             }
7359           cp_parser_set_storage_class (decl_specs, sc_static);
7360           break;
7361         case RID_EXTERN:
7362           /* Consume the token.  */
7363           cp_lexer_consume_token (parser->lexer);
7364           if (decl_specs->specs[(int) ds_thread])
7365             {
7366               error ("%<__thread%> before %<extern%>");
7367               decl_specs->specs[(int) ds_thread] = 0;
7368             }
7369           cp_parser_set_storage_class (decl_specs, sc_extern);
7370           break;
7371         case RID_MUTABLE:
7372           /* Consume the token.  */
7373           cp_lexer_consume_token (parser->lexer);
7374           cp_parser_set_storage_class (decl_specs, sc_mutable);
7375           break;
7376         case RID_THREAD:
7377           /* Consume the token.  */
7378           cp_lexer_consume_token (parser->lexer);
7379           ++decl_specs->specs[(int) ds_thread];
7380           break;
7381
7382         default:
7383           /* We did not yet find a decl-specifier yet.  */
7384           found_decl_spec = false;
7385           break;
7386         }
7387
7388       /* Constructors are a special case.  The `S' in `S()' is not a
7389          decl-specifier; it is the beginning of the declarator.  */
7390       constructor_p
7391         = (!found_decl_spec
7392            && constructor_possible_p
7393            && (cp_parser_constructor_declarator_p
7394                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7395
7396       /* If we don't have a DECL_SPEC yet, then we must be looking at
7397          a type-specifier.  */
7398       if (!found_decl_spec && !constructor_p)
7399         {
7400           int decl_spec_declares_class_or_enum;
7401           bool is_cv_qualifier;
7402           tree type_spec;
7403
7404           type_spec
7405             = cp_parser_type_specifier (parser, flags,
7406                                         decl_specs,
7407                                         /*is_declaration=*/true,
7408                                         &decl_spec_declares_class_or_enum,
7409                                         &is_cv_qualifier);
7410
7411           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7412
7413           /* If this type-specifier referenced a user-defined type
7414              (a typedef, class-name, etc.), then we can't allow any
7415              more such type-specifiers henceforth.
7416
7417              [dcl.spec]
7418
7419              The longest sequence of decl-specifiers that could
7420              possibly be a type name is taken as the
7421              decl-specifier-seq of a declaration.  The sequence shall
7422              be self-consistent as described below.
7423
7424              [dcl.type]
7425
7426              As a general rule, at most one type-specifier is allowed
7427              in the complete decl-specifier-seq of a declaration.  The
7428              only exceptions are the following:
7429
7430              -- const or volatile can be combined with any other
7431                 type-specifier.
7432
7433              -- signed or unsigned can be combined with char, long,
7434                 short, or int.
7435
7436              -- ..
7437
7438              Example:
7439
7440                typedef char* Pc;
7441                void g (const int Pc);
7442
7443              Here, Pc is *not* part of the decl-specifier seq; it's
7444              the declarator.  Therefore, once we see a type-specifier
7445              (other than a cv-qualifier), we forbid any additional
7446              user-defined types.  We *do* still allow things like `int
7447              int' to be considered a decl-specifier-seq, and issue the
7448              error message later.  */
7449           if (type_spec && !is_cv_qualifier)
7450             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7451           /* A constructor declarator cannot follow a type-specifier.  */
7452           if (type_spec)
7453             {
7454               constructor_possible_p = false;
7455               found_decl_spec = true;
7456             }
7457         }
7458
7459       /* If we still do not have a DECL_SPEC, then there are no more
7460          decl-specifiers.  */
7461       if (!found_decl_spec)
7462         break;
7463
7464       decl_specs->any_specifiers_p = true;
7465       /* After we see one decl-specifier, further decl-specifiers are
7466          always optional.  */
7467       flags |= CP_PARSER_FLAGS_OPTIONAL;
7468     }
7469
7470   /* Don't allow a friend specifier with a class definition.  */
7471   if (decl_specs->specs[(int) ds_friend] != 0
7472       && (*declares_class_or_enum & 2))
7473     error ("class definition may not be declared a friend");
7474 }
7475
7476 /* Parse an (optional) storage-class-specifier.
7477
7478    storage-class-specifier:
7479      auto
7480      register
7481      static
7482      extern
7483      mutable
7484
7485    GNU Extension:
7486
7487    storage-class-specifier:
7488      thread
7489
7490    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7491
7492 static tree
7493 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7494 {
7495   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7496     {
7497     case RID_AUTO:
7498     case RID_REGISTER:
7499     case RID_STATIC:
7500     case RID_EXTERN:
7501     case RID_MUTABLE:
7502     case RID_THREAD:
7503       /* Consume the token.  */
7504       return cp_lexer_consume_token (parser->lexer)->value;
7505
7506     default:
7507       return NULL_TREE;
7508     }
7509 }
7510
7511 /* Parse an (optional) function-specifier.
7512
7513    function-specifier:
7514      inline
7515      virtual
7516      explicit
7517
7518    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7519    Updates DECL_SPECS, if it is non-NULL.  */
7520
7521 static tree
7522 cp_parser_function_specifier_opt (cp_parser* parser,
7523                                   cp_decl_specifier_seq *decl_specs)
7524 {
7525   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7526     {
7527     case RID_INLINE:
7528       if (decl_specs)
7529         ++decl_specs->specs[(int) ds_inline];
7530       break;
7531
7532     case RID_VIRTUAL:
7533       if (decl_specs)
7534         ++decl_specs->specs[(int) ds_virtual];
7535       break;
7536
7537     case RID_EXPLICIT:
7538       if (decl_specs)
7539         ++decl_specs->specs[(int) ds_explicit];
7540       break;
7541
7542     default:
7543       return NULL_TREE;
7544     }
7545
7546   /* Consume the token.  */
7547   return cp_lexer_consume_token (parser->lexer)->value;
7548 }
7549
7550 /* Parse a linkage-specification.
7551
7552    linkage-specification:
7553      extern string-literal { declaration-seq [opt] }
7554      extern string-literal declaration  */
7555
7556 static void
7557 cp_parser_linkage_specification (cp_parser* parser)
7558 {
7559   tree linkage;
7560
7561   /* Look for the `extern' keyword.  */
7562   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7563
7564   /* Look for the string-literal.  */
7565   linkage = cp_parser_string_literal (parser, false, false);
7566
7567   /* Transform the literal into an identifier.  If the literal is a
7568      wide-character string, or contains embedded NULs, then we can't
7569      handle it as the user wants.  */
7570   if (strlen (TREE_STRING_POINTER (linkage))
7571       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7572     {
7573       cp_parser_error (parser, "invalid linkage-specification");
7574       /* Assume C++ linkage.  */
7575       linkage = lang_name_cplusplus;
7576     }
7577   else
7578     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7579
7580   /* We're now using the new linkage.  */
7581   push_lang_context (linkage);
7582
7583   /* If the next token is a `{', then we're using the first
7584      production.  */
7585   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7586     {
7587       /* Consume the `{' token.  */
7588       cp_lexer_consume_token (parser->lexer);
7589       /* Parse the declarations.  */
7590       cp_parser_declaration_seq_opt (parser);
7591       /* Look for the closing `}'.  */
7592       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7593     }
7594   /* Otherwise, there's just one declaration.  */
7595   else
7596     {
7597       bool saved_in_unbraced_linkage_specification_p;
7598
7599       saved_in_unbraced_linkage_specification_p
7600         = parser->in_unbraced_linkage_specification_p;
7601       parser->in_unbraced_linkage_specification_p = true;
7602       have_extern_spec = true;
7603       cp_parser_declaration (parser);
7604       have_extern_spec = false;
7605       parser->in_unbraced_linkage_specification_p
7606         = saved_in_unbraced_linkage_specification_p;
7607     }
7608
7609   /* We're done with the linkage-specification.  */
7610   pop_lang_context ();
7611 }
7612
7613 /* Special member functions [gram.special] */
7614
7615 /* Parse a conversion-function-id.
7616
7617    conversion-function-id:
7618      operator conversion-type-id
7619
7620    Returns an IDENTIFIER_NODE representing the operator.  */
7621
7622 static tree
7623 cp_parser_conversion_function_id (cp_parser* parser)
7624 {
7625   tree type;
7626   tree saved_scope;
7627   tree saved_qualifying_scope;
7628   tree saved_object_scope;
7629   tree pushed_scope = NULL_TREE;
7630
7631   /* Look for the `operator' token.  */
7632   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7633     return error_mark_node;
7634   /* When we parse the conversion-type-id, the current scope will be
7635      reset.  However, we need that information in able to look up the
7636      conversion function later, so we save it here.  */
7637   saved_scope = parser->scope;
7638   saved_qualifying_scope = parser->qualifying_scope;
7639   saved_object_scope = parser->object_scope;
7640   /* We must enter the scope of the class so that the names of
7641      entities declared within the class are available in the
7642      conversion-type-id.  For example, consider:
7643
7644        struct S {
7645          typedef int I;
7646          operator I();
7647        };
7648
7649        S::operator I() { ... }
7650
7651      In order to see that `I' is a type-name in the definition, we
7652      must be in the scope of `S'.  */
7653   if (saved_scope)
7654     pushed_scope = push_scope (saved_scope);
7655   /* Parse the conversion-type-id.  */
7656   type = cp_parser_conversion_type_id (parser);
7657   /* Leave the scope of the class, if any.  */
7658   if (pushed_scope)
7659     pop_scope (pushed_scope);
7660   /* Restore the saved scope.  */
7661   parser->scope = saved_scope;
7662   parser->qualifying_scope = saved_qualifying_scope;
7663   parser->object_scope = saved_object_scope;
7664   /* If the TYPE is invalid, indicate failure.  */
7665   if (type == error_mark_node)
7666     return error_mark_node;
7667   return mangle_conv_op_name_for_type (type);
7668 }
7669
7670 /* Parse a conversion-type-id:
7671
7672    conversion-type-id:
7673      type-specifier-seq conversion-declarator [opt]
7674
7675    Returns the TYPE specified.  */
7676
7677 static tree
7678 cp_parser_conversion_type_id (cp_parser* parser)
7679 {
7680   tree attributes;
7681   cp_decl_specifier_seq type_specifiers;
7682   cp_declarator *declarator;
7683   tree type_specified;
7684
7685   /* Parse the attributes.  */
7686   attributes = cp_parser_attributes_opt (parser);
7687   /* Parse the type-specifiers.  */
7688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7689                                 &type_specifiers);
7690   /* If that didn't work, stop.  */
7691   if (type_specifiers.type == error_mark_node)
7692     return error_mark_node;
7693   /* Parse the conversion-declarator.  */
7694   declarator = cp_parser_conversion_declarator_opt (parser);
7695
7696   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7697                                     /*initialized=*/0, &attributes);
7698   if (attributes)
7699     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7700   return type_specified;
7701 }
7702
7703 /* Parse an (optional) conversion-declarator.
7704
7705    conversion-declarator:
7706      ptr-operator conversion-declarator [opt]
7707
7708    */
7709
7710 static cp_declarator *
7711 cp_parser_conversion_declarator_opt (cp_parser* parser)
7712 {
7713   enum tree_code code;
7714   tree class_type;
7715   cp_cv_quals cv_quals;
7716
7717   /* We don't know if there's a ptr-operator next, or not.  */
7718   cp_parser_parse_tentatively (parser);
7719   /* Try the ptr-operator.  */
7720   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7721   /* If it worked, look for more conversion-declarators.  */
7722   if (cp_parser_parse_definitely (parser))
7723     {
7724       cp_declarator *declarator;
7725
7726       /* Parse another optional declarator.  */
7727       declarator = cp_parser_conversion_declarator_opt (parser);
7728
7729       /* Create the representation of the declarator.  */
7730       if (class_type)
7731         declarator = make_ptrmem_declarator (cv_quals, class_type,
7732                                              declarator);
7733       else if (code == INDIRECT_REF)
7734         declarator = make_pointer_declarator (cv_quals, declarator);
7735       else
7736         declarator = make_reference_declarator (cv_quals, declarator);
7737
7738       return declarator;
7739    }
7740
7741   return NULL;
7742 }
7743
7744 /* Parse an (optional) ctor-initializer.
7745
7746    ctor-initializer:
7747      : mem-initializer-list
7748
7749    Returns TRUE iff the ctor-initializer was actually present.  */
7750
7751 static bool
7752 cp_parser_ctor_initializer_opt (cp_parser* parser)
7753 {
7754   /* If the next token is not a `:', then there is no
7755      ctor-initializer.  */
7756   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7757     {
7758       /* Do default initialization of any bases and members.  */
7759       if (DECL_CONSTRUCTOR_P (current_function_decl))
7760         finish_mem_initializers (NULL_TREE);
7761
7762       return false;
7763     }
7764
7765   /* Consume the `:' token.  */
7766   cp_lexer_consume_token (parser->lexer);
7767   /* And the mem-initializer-list.  */
7768   cp_parser_mem_initializer_list (parser);
7769
7770   return true;
7771 }
7772
7773 /* Parse a mem-initializer-list.
7774
7775    mem-initializer-list:
7776      mem-initializer
7777      mem-initializer , mem-initializer-list  */
7778
7779 static void
7780 cp_parser_mem_initializer_list (cp_parser* parser)
7781 {
7782   tree mem_initializer_list = NULL_TREE;
7783
7784   /* Let the semantic analysis code know that we are starting the
7785      mem-initializer-list.  */
7786   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7787     error ("only constructors take base initializers");
7788
7789   /* Loop through the list.  */
7790   while (true)
7791     {
7792       tree mem_initializer;
7793
7794       /* Parse the mem-initializer.  */
7795       mem_initializer = cp_parser_mem_initializer (parser);
7796       /* Add it to the list, unless it was erroneous.  */
7797       if (mem_initializer)
7798         {
7799           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7800           mem_initializer_list = mem_initializer;
7801         }
7802       /* If the next token is not a `,', we're done.  */
7803       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7804         break;
7805       /* Consume the `,' token.  */
7806       cp_lexer_consume_token (parser->lexer);
7807     }
7808
7809   /* Perform semantic analysis.  */
7810   if (DECL_CONSTRUCTOR_P (current_function_decl))
7811     finish_mem_initializers (mem_initializer_list);
7812 }
7813
7814 /* Parse a mem-initializer.
7815
7816    mem-initializer:
7817      mem-initializer-id ( expression-list [opt] )
7818
7819    GNU extension:
7820
7821    mem-initializer:
7822      ( expression-list [opt] )
7823
7824    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7825    class) or FIELD_DECL (for a non-static data member) to initialize;
7826    the TREE_VALUE is the expression-list.  */
7827
7828 static tree
7829 cp_parser_mem_initializer (cp_parser* parser)
7830 {
7831   tree mem_initializer_id;
7832   tree expression_list;
7833   tree member;
7834
7835   /* Find out what is being initialized.  */
7836   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7837     {
7838       pedwarn ("anachronistic old-style base class initializer");
7839       mem_initializer_id = NULL_TREE;
7840     }
7841   else
7842     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7843   member = expand_member_init (mem_initializer_id);
7844   if (member && !DECL_P (member))
7845     in_base_initializer = 1;
7846
7847   expression_list
7848     = cp_parser_parenthesized_expression_list (parser, false,
7849                                                /*cast_p=*/false,
7850                                                /*non_constant_p=*/NULL);
7851   if (!expression_list)
7852     expression_list = void_type_node;
7853
7854   in_base_initializer = 0;
7855
7856   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7857 }
7858
7859 /* Parse a mem-initializer-id.
7860
7861    mem-initializer-id:
7862      :: [opt] nested-name-specifier [opt] class-name
7863      identifier
7864
7865    Returns a TYPE indicating the class to be initializer for the first
7866    production.  Returns an IDENTIFIER_NODE indicating the data member
7867    to be initialized for the second production.  */
7868
7869 static tree
7870 cp_parser_mem_initializer_id (cp_parser* parser)
7871 {
7872   bool global_scope_p;
7873   bool nested_name_specifier_p;
7874   bool template_p = false;
7875   tree id;
7876
7877   /* `typename' is not allowed in this context ([temp.res]).  */
7878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7879     {
7880       error ("keyword %<typename%> not allowed in this context (a qualified "
7881              "member initializer is implicitly a type)");
7882       cp_lexer_consume_token (parser->lexer);
7883     }
7884   /* Look for the optional `::' operator.  */
7885   global_scope_p
7886     = (cp_parser_global_scope_opt (parser,
7887                                    /*current_scope_valid_p=*/false)
7888        != NULL_TREE);
7889   /* Look for the optional nested-name-specifier.  The simplest way to
7890      implement:
7891
7892        [temp.res]
7893
7894        The keyword `typename' is not permitted in a base-specifier or
7895        mem-initializer; in these contexts a qualified name that
7896        depends on a template-parameter is implicitly assumed to be a
7897        type name.
7898
7899      is to assume that we have seen the `typename' keyword at this
7900      point.  */
7901   nested_name_specifier_p
7902     = (cp_parser_nested_name_specifier_opt (parser,
7903                                             /*typename_keyword_p=*/true,
7904                                             /*check_dependency_p=*/true,
7905                                             /*type_p=*/true,
7906                                             /*is_declaration=*/true)
7907        != NULL_TREE);
7908   if (nested_name_specifier_p)
7909     template_p = cp_parser_optional_template_keyword (parser);
7910   /* If there is a `::' operator or a nested-name-specifier, then we
7911      are definitely looking for a class-name.  */
7912   if (global_scope_p || nested_name_specifier_p)
7913     return cp_parser_class_name (parser,
7914                                  /*typename_keyword_p=*/true,
7915                                  /*template_keyword_p=*/template_p,
7916                                  none_type,
7917                                  /*check_dependency_p=*/true,
7918                                  /*class_head_p=*/false,
7919                                  /*is_declaration=*/true);
7920   /* Otherwise, we could also be looking for an ordinary identifier.  */
7921   cp_parser_parse_tentatively (parser);
7922   /* Try a class-name.  */
7923   id = cp_parser_class_name (parser,
7924                              /*typename_keyword_p=*/true,
7925                              /*template_keyword_p=*/false,
7926                              none_type,
7927                              /*check_dependency_p=*/true,
7928                              /*class_head_p=*/false,
7929                              /*is_declaration=*/true);
7930   /* If we found one, we're done.  */
7931   if (cp_parser_parse_definitely (parser))
7932     return id;
7933   /* Otherwise, look for an ordinary identifier.  */
7934   return cp_parser_identifier (parser);
7935 }
7936
7937 /* Overloading [gram.over] */
7938
7939 /* Parse an operator-function-id.
7940
7941    operator-function-id:
7942      operator operator
7943
7944    Returns an IDENTIFIER_NODE for the operator which is a
7945    human-readable spelling of the identifier, e.g., `operator +'.  */
7946
7947 static tree
7948 cp_parser_operator_function_id (cp_parser* parser)
7949 {
7950   /* Look for the `operator' keyword.  */
7951   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7952     return error_mark_node;
7953   /* And then the name of the operator itself.  */
7954   return cp_parser_operator (parser);
7955 }
7956
7957 /* Parse an operator.
7958
7959    operator:
7960      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7961      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7962      || ++ -- , ->* -> () []
7963
7964    GNU Extensions:
7965
7966    operator:
7967      <? >? <?= >?=
7968
7969    Returns an IDENTIFIER_NODE for the operator which is a
7970    human-readable spelling of the identifier, e.g., `operator +'.  */
7971
7972 static tree
7973 cp_parser_operator (cp_parser* parser)
7974 {
7975   tree id = NULL_TREE;
7976   cp_token *token;
7977
7978   /* Peek at the next token.  */
7979   token = cp_lexer_peek_token (parser->lexer);
7980   /* Figure out which operator we have.  */
7981   switch (token->type)
7982     {
7983     case CPP_KEYWORD:
7984       {
7985         enum tree_code op;
7986
7987         /* The keyword should be either `new' or `delete'.  */
7988         if (token->keyword == RID_NEW)
7989           op = NEW_EXPR;
7990         else if (token->keyword == RID_DELETE)
7991           op = DELETE_EXPR;
7992         else
7993           break;
7994
7995         /* Consume the `new' or `delete' token.  */
7996         cp_lexer_consume_token (parser->lexer);
7997
7998         /* Peek at the next token.  */
7999         token = cp_lexer_peek_token (parser->lexer);
8000         /* If it's a `[' token then this is the array variant of the
8001            operator.  */
8002         if (token->type == CPP_OPEN_SQUARE)
8003           {
8004             /* Consume the `[' token.  */
8005             cp_lexer_consume_token (parser->lexer);
8006             /* Look for the `]' token.  */
8007             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8008             id = ansi_opname (op == NEW_EXPR
8009                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8010           }
8011         /* Otherwise, we have the non-array variant.  */
8012         else
8013           id = ansi_opname (op);
8014
8015         return id;
8016       }
8017
8018     case CPP_PLUS:
8019       id = ansi_opname (PLUS_EXPR);
8020       break;
8021
8022     case CPP_MINUS:
8023       id = ansi_opname (MINUS_EXPR);
8024       break;
8025
8026     case CPP_MULT:
8027       id = ansi_opname (MULT_EXPR);
8028       break;
8029
8030     case CPP_DIV:
8031       id = ansi_opname (TRUNC_DIV_EXPR);
8032       break;
8033
8034     case CPP_MOD:
8035       id = ansi_opname (TRUNC_MOD_EXPR);
8036       break;
8037
8038     case CPP_XOR:
8039       id = ansi_opname (BIT_XOR_EXPR);
8040       break;
8041
8042     case CPP_AND:
8043       id = ansi_opname (BIT_AND_EXPR);
8044       break;
8045
8046     case CPP_OR:
8047       id = ansi_opname (BIT_IOR_EXPR);
8048       break;
8049
8050     case CPP_COMPL:
8051       id = ansi_opname (BIT_NOT_EXPR);
8052       break;
8053
8054     case CPP_NOT:
8055       id = ansi_opname (TRUTH_NOT_EXPR);
8056       break;
8057
8058     case CPP_EQ:
8059       id = ansi_assopname (NOP_EXPR);
8060       break;
8061
8062     case CPP_LESS:
8063       id = ansi_opname (LT_EXPR);
8064       break;
8065
8066     case CPP_GREATER:
8067       id = ansi_opname (GT_EXPR);
8068       break;
8069
8070     case CPP_PLUS_EQ:
8071       id = ansi_assopname (PLUS_EXPR);
8072       break;
8073
8074     case CPP_MINUS_EQ:
8075       id = ansi_assopname (MINUS_EXPR);
8076       break;
8077
8078     case CPP_MULT_EQ:
8079       id = ansi_assopname (MULT_EXPR);
8080       break;
8081
8082     case CPP_DIV_EQ:
8083       id = ansi_assopname (TRUNC_DIV_EXPR);
8084       break;
8085
8086     case CPP_MOD_EQ:
8087       id = ansi_assopname (TRUNC_MOD_EXPR);
8088       break;
8089
8090     case CPP_XOR_EQ:
8091       id = ansi_assopname (BIT_XOR_EXPR);
8092       break;
8093
8094     case CPP_AND_EQ:
8095       id = ansi_assopname (BIT_AND_EXPR);
8096       break;
8097
8098     case CPP_OR_EQ:
8099       id = ansi_assopname (BIT_IOR_EXPR);
8100       break;
8101
8102     case CPP_LSHIFT:
8103       id = ansi_opname (LSHIFT_EXPR);
8104       break;
8105
8106     case CPP_RSHIFT:
8107       id = ansi_opname (RSHIFT_EXPR);
8108       break;
8109
8110     case CPP_LSHIFT_EQ:
8111       id = ansi_assopname (LSHIFT_EXPR);
8112       break;
8113
8114     case CPP_RSHIFT_EQ:
8115       id = ansi_assopname (RSHIFT_EXPR);
8116       break;
8117
8118     case CPP_EQ_EQ:
8119       id = ansi_opname (EQ_EXPR);
8120       break;
8121
8122     case CPP_NOT_EQ:
8123       id = ansi_opname (NE_EXPR);
8124       break;
8125
8126     case CPP_LESS_EQ:
8127       id = ansi_opname (LE_EXPR);
8128       break;
8129
8130     case CPP_GREATER_EQ:
8131       id = ansi_opname (GE_EXPR);
8132       break;
8133
8134     case CPP_AND_AND:
8135       id = ansi_opname (TRUTH_ANDIF_EXPR);
8136       break;
8137
8138     case CPP_OR_OR:
8139       id = ansi_opname (TRUTH_ORIF_EXPR);
8140       break;
8141
8142     case CPP_PLUS_PLUS:
8143       id = ansi_opname (POSTINCREMENT_EXPR);
8144       break;
8145
8146     case CPP_MINUS_MINUS:
8147       id = ansi_opname (PREDECREMENT_EXPR);
8148       break;
8149
8150     case CPP_COMMA:
8151       id = ansi_opname (COMPOUND_EXPR);
8152       break;
8153
8154     case CPP_DEREF_STAR:
8155       id = ansi_opname (MEMBER_REF);
8156       break;
8157
8158     case CPP_DEREF:
8159       id = ansi_opname (COMPONENT_REF);
8160       break;
8161
8162     case CPP_OPEN_PAREN:
8163       /* Consume the `('.  */
8164       cp_lexer_consume_token (parser->lexer);
8165       /* Look for the matching `)'.  */
8166       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8167       return ansi_opname (CALL_EXPR);
8168
8169     case CPP_OPEN_SQUARE:
8170       /* Consume the `['.  */
8171       cp_lexer_consume_token (parser->lexer);
8172       /* Look for the matching `]'.  */
8173       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8174       return ansi_opname (ARRAY_REF);
8175
8176       /* Extensions.  */
8177     case CPP_MIN:
8178       id = ansi_opname (MIN_EXPR);
8179       cp_parser_warn_min_max ();
8180       break;
8181
8182     case CPP_MAX:
8183       id = ansi_opname (MAX_EXPR);
8184       cp_parser_warn_min_max ();
8185       break;
8186
8187     case CPP_MIN_EQ:
8188       id = ansi_assopname (MIN_EXPR);
8189       cp_parser_warn_min_max ();
8190       break;
8191
8192     case CPP_MAX_EQ:
8193       id = ansi_assopname (MAX_EXPR);
8194       cp_parser_warn_min_max ();
8195       break;
8196
8197     default:
8198       /* Anything else is an error.  */
8199       break;
8200     }
8201
8202   /* If we have selected an identifier, we need to consume the
8203      operator token.  */
8204   if (id)
8205     cp_lexer_consume_token (parser->lexer);
8206   /* Otherwise, no valid operator name was present.  */
8207   else
8208     {
8209       cp_parser_error (parser, "expected operator");
8210       id = error_mark_node;
8211     }
8212
8213   return id;
8214 }
8215
8216 /* Parse a template-declaration.
8217
8218    template-declaration:
8219      export [opt] template < template-parameter-list > declaration
8220
8221    If MEMBER_P is TRUE, this template-declaration occurs within a
8222    class-specifier.
8223
8224    The grammar rule given by the standard isn't correct.  What
8225    is really meant is:
8226
8227    template-declaration:
8228      export [opt] template-parameter-list-seq
8229        decl-specifier-seq [opt] init-declarator [opt] ;
8230      export [opt] template-parameter-list-seq
8231        function-definition
8232
8233    template-parameter-list-seq:
8234      template-parameter-list-seq [opt]
8235      template < template-parameter-list >  */
8236
8237 static void
8238 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8239 {
8240   /* Check for `export'.  */
8241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8242     {
8243       /* Consume the `export' token.  */
8244       cp_lexer_consume_token (parser->lexer);
8245       /* Warn that we do not support `export'.  */
8246       warning (0, "keyword %<export%> not implemented, and will be ignored");
8247     }
8248
8249   cp_parser_template_declaration_after_export (parser, member_p);
8250 }
8251
8252 /* Parse a template-parameter-list.
8253
8254    template-parameter-list:
8255      template-parameter
8256      template-parameter-list , template-parameter
8257
8258    Returns a TREE_LIST.  Each node represents a template parameter.
8259    The nodes are connected via their TREE_CHAINs.  */
8260
8261 static tree
8262 cp_parser_template_parameter_list (cp_parser* parser)
8263 {
8264   tree parameter_list = NULL_TREE;
8265
8266   while (true)
8267     {
8268       tree parameter;
8269       cp_token *token;
8270       bool is_non_type;
8271
8272       /* Parse the template-parameter.  */
8273       parameter = cp_parser_template_parameter (parser, &is_non_type);
8274       /* Add it to the list.  */
8275       if (parameter != error_mark_node)
8276         parameter_list = process_template_parm (parameter_list,
8277                                                 parameter,
8278                                                 is_non_type);
8279       /* Peek at the next token.  */
8280       token = cp_lexer_peek_token (parser->lexer);
8281       /* If it's not a `,', we're done.  */
8282       if (token->type != CPP_COMMA)
8283         break;
8284       /* Otherwise, consume the `,' token.  */
8285       cp_lexer_consume_token (parser->lexer);
8286     }
8287
8288   return parameter_list;
8289 }
8290
8291 /* Parse a template-parameter.
8292
8293    template-parameter:
8294      type-parameter
8295      parameter-declaration
8296
8297    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8298    the parameter.  The TREE_PURPOSE is the default value, if any.
8299    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8300    iff this parameter is a non-type parameter.  */
8301
8302 static tree
8303 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8304 {
8305   cp_token *token;
8306   cp_parameter_declarator *parameter_declarator;
8307   tree parm;
8308
8309   /* Assume it is a type parameter or a template parameter.  */
8310   *is_non_type = false;
8311   /* Peek at the next token.  */
8312   token = cp_lexer_peek_token (parser->lexer);
8313   /* If it is `class' or `template', we have a type-parameter.  */
8314   if (token->keyword == RID_TEMPLATE)
8315     return cp_parser_type_parameter (parser);
8316   /* If it is `class' or `typename' we do not know yet whether it is a
8317      type parameter or a non-type parameter.  Consider:
8318
8319        template <typename T, typename T::X X> ...
8320
8321      or:
8322
8323        template <class C, class D*> ...
8324
8325      Here, the first parameter is a type parameter, and the second is
8326      a non-type parameter.  We can tell by looking at the token after
8327      the identifier -- if it is a `,', `=', or `>' then we have a type
8328      parameter.  */
8329   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8330     {
8331       /* Peek at the token after `class' or `typename'.  */
8332       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8333       /* If it's an identifier, skip it.  */
8334       if (token->type == CPP_NAME)
8335         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8336       /* Now, see if the token looks like the end of a template
8337          parameter.  */
8338       if (token->type == CPP_COMMA
8339           || token->type == CPP_EQ
8340           || token->type == CPP_GREATER)
8341         return cp_parser_type_parameter (parser);
8342     }
8343
8344   /* Otherwise, it is a non-type parameter.
8345
8346      [temp.param]
8347
8348      When parsing a default template-argument for a non-type
8349      template-parameter, the first non-nested `>' is taken as the end
8350      of the template parameter-list rather than a greater-than
8351      operator.  */
8352   *is_non_type = true;
8353   parameter_declarator
8354      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8355                                         /*parenthesized_p=*/NULL);
8356   parm = grokdeclarator (parameter_declarator->declarator,
8357                          &parameter_declarator->decl_specifiers,
8358                          PARM, /*initialized=*/0,
8359                          /*attrlist=*/NULL);
8360   if (parm == error_mark_node)
8361     return error_mark_node;
8362   return build_tree_list (parameter_declarator->default_argument, parm);
8363 }
8364
8365 /* Parse a type-parameter.
8366
8367    type-parameter:
8368      class identifier [opt]
8369      class identifier [opt] = type-id
8370      typename identifier [opt]
8371      typename identifier [opt] = type-id
8372      template < template-parameter-list > class identifier [opt]
8373      template < template-parameter-list > class identifier [opt]
8374        = id-expression
8375
8376    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8377    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8378    the declaration of the parameter.  */
8379
8380 static tree
8381 cp_parser_type_parameter (cp_parser* parser)
8382 {
8383   cp_token *token;
8384   tree parameter;
8385
8386   /* Look for a keyword to tell us what kind of parameter this is.  */
8387   token = cp_parser_require (parser, CPP_KEYWORD,
8388                              "`class', `typename', or `template'");
8389   if (!token)
8390     return error_mark_node;
8391
8392   switch (token->keyword)
8393     {
8394     case RID_CLASS:
8395     case RID_TYPENAME:
8396       {
8397         tree identifier;
8398         tree default_argument;
8399
8400         /* If the next token is an identifier, then it names the
8401            parameter.  */
8402         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8403           identifier = cp_parser_identifier (parser);
8404         else
8405           identifier = NULL_TREE;
8406
8407         /* Create the parameter.  */
8408         parameter = finish_template_type_parm (class_type_node, identifier);
8409
8410         /* If the next token is an `=', we have a default argument.  */
8411         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8412           {
8413             /* Consume the `=' token.  */
8414             cp_lexer_consume_token (parser->lexer);
8415             /* Parse the default-argument.  */
8416             default_argument = cp_parser_type_id (parser);
8417           }
8418         else
8419           default_argument = NULL_TREE;
8420
8421         /* Create the combined representation of the parameter and the
8422            default argument.  */
8423         parameter = build_tree_list (default_argument, parameter);
8424       }
8425       break;
8426
8427     case RID_TEMPLATE:
8428       {
8429         tree parameter_list;
8430         tree identifier;
8431         tree default_argument;
8432
8433         /* Look for the `<'.  */
8434         cp_parser_require (parser, CPP_LESS, "`<'");
8435         /* Parse the template-parameter-list.  */
8436         begin_template_parm_list ();
8437         parameter_list
8438           = cp_parser_template_parameter_list (parser);
8439         parameter_list = end_template_parm_list (parameter_list);
8440         /* Look for the `>'.  */
8441         cp_parser_require (parser, CPP_GREATER, "`>'");
8442         /* Look for the `class' keyword.  */
8443         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8444         /* If the next token is an `=', then there is a
8445            default-argument.  If the next token is a `>', we are at
8446            the end of the parameter-list.  If the next token is a `,',
8447            then we are at the end of this parameter.  */
8448         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8449             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8450             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8451           {
8452             identifier = cp_parser_identifier (parser);
8453             /* Treat invalid names as if the parameter were nameless.  */
8454             if (identifier == error_mark_node)
8455               identifier = NULL_TREE;
8456           }
8457         else
8458           identifier = NULL_TREE;
8459
8460         /* Create the template parameter.  */
8461         parameter = finish_template_template_parm (class_type_node,
8462                                                    identifier);
8463
8464         /* If the next token is an `=', then there is a
8465            default-argument.  */
8466         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8467           {
8468             bool is_template;
8469
8470             /* Consume the `='.  */
8471             cp_lexer_consume_token (parser->lexer);
8472             /* Parse the id-expression.  */
8473             default_argument
8474               = cp_parser_id_expression (parser,
8475                                          /*template_keyword_p=*/false,
8476                                          /*check_dependency_p=*/true,
8477                                          /*template_p=*/&is_template,
8478                                          /*declarator_p=*/false);
8479             if (TREE_CODE (default_argument) == TYPE_DECL)
8480               /* If the id-expression was a template-id that refers to
8481                  a template-class, we already have the declaration here,
8482                  so no further lookup is needed.  */
8483                  ;
8484             else
8485               /* Look up the name.  */
8486               default_argument
8487                 = cp_parser_lookup_name (parser, default_argument,
8488                                          none_type,
8489                                          /*is_template=*/is_template,
8490                                          /*is_namespace=*/false,
8491                                          /*check_dependency=*/true,
8492                                          /*ambiguous_p=*/NULL);
8493             /* See if the default argument is valid.  */
8494             default_argument
8495               = check_template_template_default_arg (default_argument);
8496           }
8497         else
8498           default_argument = NULL_TREE;
8499
8500         /* Create the combined representation of the parameter and the
8501            default argument.  */
8502         parameter = build_tree_list (default_argument, parameter);
8503       }
8504       break;
8505
8506     default:
8507       gcc_unreachable ();
8508       break;
8509     }
8510
8511   return parameter;
8512 }
8513
8514 /* Parse a template-id.
8515
8516    template-id:
8517      template-name < template-argument-list [opt] >
8518
8519    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8520    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8521    returned.  Otherwise, if the template-name names a function, or set
8522    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8523    names a class, returns a TYPE_DECL for the specialization.
8524
8525    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8526    uninstantiated templates.  */
8527
8528 static tree
8529 cp_parser_template_id (cp_parser *parser,
8530                        bool template_keyword_p,
8531                        bool check_dependency_p,
8532                        bool is_declaration)
8533 {
8534   tree template;
8535   tree arguments;
8536   tree template_id;
8537   cp_token_position start_of_id = 0;
8538   tree access_check = NULL_TREE;
8539   cp_token *next_token, *next_token_2;
8540   bool is_identifier;
8541
8542   /* If the next token corresponds to a template-id, there is no need
8543      to reparse it.  */
8544   next_token = cp_lexer_peek_token (parser->lexer);
8545   if (next_token->type == CPP_TEMPLATE_ID)
8546     {
8547       tree value;
8548       tree check;
8549
8550       /* Get the stored value.  */
8551       value = cp_lexer_consume_token (parser->lexer)->value;
8552       /* Perform any access checks that were deferred.  */
8553       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8554         perform_or_defer_access_check (TREE_PURPOSE (check),
8555                                        TREE_VALUE (check));
8556       /* Return the stored value.  */
8557       return TREE_VALUE (value);
8558     }
8559
8560   /* Avoid performing name lookup if there is no possibility of
8561      finding a template-id.  */
8562   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8563       || (next_token->type == CPP_NAME
8564           && !cp_parser_nth_token_starts_template_argument_list_p
8565                (parser, 2)))
8566     {
8567       cp_parser_error (parser, "expected template-id");
8568       return error_mark_node;
8569     }
8570
8571   /* Remember where the template-id starts.  */
8572   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8573     start_of_id = cp_lexer_token_position (parser->lexer, false);
8574
8575   push_deferring_access_checks (dk_deferred);
8576
8577   /* Parse the template-name.  */
8578   is_identifier = false;
8579   template = cp_parser_template_name (parser, template_keyword_p,
8580                                       check_dependency_p,
8581                                       is_declaration,
8582                                       &is_identifier);
8583   if (template == error_mark_node || is_identifier)
8584     {
8585       pop_deferring_access_checks ();
8586       return template;
8587     }
8588
8589   /* If we find the sequence `[:' after a template-name, it's probably
8590      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8591      parse correctly the argument list.  */
8592   next_token = cp_lexer_peek_token (parser->lexer);
8593   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8594   if (next_token->type == CPP_OPEN_SQUARE
8595       && next_token->flags & DIGRAPH
8596       && next_token_2->type == CPP_COLON
8597       && !(next_token_2->flags & PREV_WHITE))
8598     {
8599       cp_parser_parse_tentatively (parser);
8600       /* Change `:' into `::'.  */
8601       next_token_2->type = CPP_SCOPE;
8602       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8603          CPP_LESS.  */
8604       cp_lexer_consume_token (parser->lexer);
8605       /* Parse the arguments.  */
8606       arguments = cp_parser_enclosed_template_argument_list (parser);
8607       if (!cp_parser_parse_definitely (parser))
8608         {
8609           /* If we couldn't parse an argument list, then we revert our changes
8610              and return simply an error. Maybe this is not a template-id
8611              after all.  */
8612           next_token_2->type = CPP_COLON;
8613           cp_parser_error (parser, "expected %<<%>");
8614           pop_deferring_access_checks ();
8615           return error_mark_node;
8616         }
8617       /* Otherwise, emit an error about the invalid digraph, but continue
8618          parsing because we got our argument list.  */
8619       pedwarn ("%<<::%> cannot begin a template-argument list");
8620       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8621               "between %<<%> and %<::%>");
8622       if (!flag_permissive)
8623         {
8624           static bool hint;
8625           if (!hint)
8626             {
8627               inform ("(if you use -fpermissive G++ will accept your code)");
8628               hint = true;
8629             }
8630         }
8631     }
8632   else
8633     {
8634       /* Look for the `<' that starts the template-argument-list.  */
8635       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8636         {
8637           pop_deferring_access_checks ();
8638           return error_mark_node;
8639         }
8640       /* Parse the arguments.  */
8641       arguments = cp_parser_enclosed_template_argument_list (parser);
8642     }
8643
8644   /* Build a representation of the specialization.  */
8645   if (TREE_CODE (template) == IDENTIFIER_NODE)
8646     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8647   else if (DECL_CLASS_TEMPLATE_P (template)
8648            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8649     template_id
8650       = finish_template_type (template, arguments,
8651                               cp_lexer_next_token_is (parser->lexer,
8652                                                       CPP_SCOPE));
8653   else
8654     {
8655       /* If it's not a class-template or a template-template, it should be
8656          a function-template.  */
8657       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8658                    || TREE_CODE (template) == OVERLOAD
8659                    || BASELINK_P (template)));
8660
8661       template_id = lookup_template_function (template, arguments);
8662     }
8663
8664   /* Retrieve any deferred checks.  Do not pop this access checks yet
8665      so the memory will not be reclaimed during token replacing below.  */
8666   access_check = get_deferred_access_checks ();
8667
8668   /* If parsing tentatively, replace the sequence of tokens that makes
8669      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8670      should we re-parse the token stream, we will not have to repeat
8671      the effort required to do the parse, nor will we issue duplicate
8672      error messages about problems during instantiation of the
8673      template.  */
8674   if (start_of_id)
8675     {
8676       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8677
8678       /* Reset the contents of the START_OF_ID token.  */
8679       token->type = CPP_TEMPLATE_ID;
8680       token->value = build_tree_list (access_check, template_id);
8681       token->keyword = RID_MAX;
8682
8683       /* Purge all subsequent tokens.  */
8684       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8685
8686       /* ??? Can we actually assume that, if template_id ==
8687          error_mark_node, we will have issued a diagnostic to the
8688          user, as opposed to simply marking the tentative parse as
8689          failed?  */
8690       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8691         error ("parse error in template argument list");
8692     }
8693
8694   pop_deferring_access_checks ();
8695   return template_id;
8696 }
8697
8698 /* Parse a template-name.
8699
8700    template-name:
8701      identifier
8702
8703    The standard should actually say:
8704
8705    template-name:
8706      identifier
8707      operator-function-id
8708
8709    A defect report has been filed about this issue.
8710
8711    A conversion-function-id cannot be a template name because they cannot
8712    be part of a template-id. In fact, looking at this code:
8713
8714    a.operator K<int>()
8715
8716    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8717    It is impossible to call a templated conversion-function-id with an
8718    explicit argument list, since the only allowed template parameter is
8719    the type to which it is converting.
8720
8721    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8722    `template' keyword, in a construction like:
8723
8724      T::template f<3>()
8725
8726    In that case `f' is taken to be a template-name, even though there
8727    is no way of knowing for sure.
8728
8729    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8730    name refers to a set of overloaded functions, at least one of which
8731    is a template, or an IDENTIFIER_NODE with the name of the template,
8732    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8733    names are looked up inside uninstantiated templates.  */
8734
8735 static tree
8736 cp_parser_template_name (cp_parser* parser,
8737                          bool template_keyword_p,
8738                          bool check_dependency_p,
8739                          bool is_declaration,
8740                          bool *is_identifier)
8741 {
8742   tree identifier;
8743   tree decl;
8744   tree fns;
8745
8746   /* If the next token is `operator', then we have either an
8747      operator-function-id or a conversion-function-id.  */
8748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8749     {
8750       /* We don't know whether we're looking at an
8751          operator-function-id or a conversion-function-id.  */
8752       cp_parser_parse_tentatively (parser);
8753       /* Try an operator-function-id.  */
8754       identifier = cp_parser_operator_function_id (parser);
8755       /* If that didn't work, try a conversion-function-id.  */
8756       if (!cp_parser_parse_definitely (parser))
8757         {
8758           cp_parser_error (parser, "expected template-name");
8759           return error_mark_node;
8760         }
8761     }
8762   /* Look for the identifier.  */
8763   else
8764     identifier = cp_parser_identifier (parser);
8765
8766   /* If we didn't find an identifier, we don't have a template-id.  */
8767   if (identifier == error_mark_node)
8768     return error_mark_node;
8769
8770   /* If the name immediately followed the `template' keyword, then it
8771      is a template-name.  However, if the next token is not `<', then
8772      we do not treat it as a template-name, since it is not being used
8773      as part of a template-id.  This enables us to handle constructs
8774      like:
8775
8776        template <typename T> struct S { S(); };
8777        template <typename T> S<T>::S();
8778
8779      correctly.  We would treat `S' as a template -- if it were `S<T>'
8780      -- but we do not if there is no `<'.  */
8781
8782   if (processing_template_decl
8783       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8784     {
8785       /* In a declaration, in a dependent context, we pretend that the
8786          "template" keyword was present in order to improve error
8787          recovery.  For example, given:
8788
8789            template <typename T> void f(T::X<int>);
8790
8791          we want to treat "X<int>" as a template-id.  */
8792       if (is_declaration
8793           && !template_keyword_p
8794           && parser->scope && TYPE_P (parser->scope)
8795           && check_dependency_p
8796           && dependent_type_p (parser->scope)
8797           /* Do not do this for dtors (or ctors), since they never
8798              need the template keyword before their name.  */
8799           && !constructor_name_p (identifier, parser->scope))
8800         {
8801           cp_token_position start = 0;
8802
8803           /* Explain what went wrong.  */
8804           error ("non-template %qD used as template", identifier);
8805           inform ("use %<%T::template %D%> to indicate that it is a template",
8806                   parser->scope, identifier);
8807           /* If parsing tentatively, find the location of the "<" token.  */
8808           if (cp_parser_simulate_error (parser))
8809             start = cp_lexer_token_position (parser->lexer, true);
8810           /* Parse the template arguments so that we can issue error
8811              messages about them.  */
8812           cp_lexer_consume_token (parser->lexer);
8813           cp_parser_enclosed_template_argument_list (parser);
8814           /* Skip tokens until we find a good place from which to
8815              continue parsing.  */
8816           cp_parser_skip_to_closing_parenthesis (parser,
8817                                                  /*recovering=*/true,
8818                                                  /*or_comma=*/true,
8819                                                  /*consume_paren=*/false);
8820           /* If parsing tentatively, permanently remove the
8821              template argument list.  That will prevent duplicate
8822              error messages from being issued about the missing
8823              "template" keyword.  */
8824           if (start)
8825             cp_lexer_purge_tokens_after (parser->lexer, start);
8826           if (is_identifier)
8827             *is_identifier = true;
8828           return identifier;
8829         }
8830
8831       /* If the "template" keyword is present, then there is generally
8832          no point in doing name-lookup, so we just return IDENTIFIER.
8833          But, if the qualifying scope is non-dependent then we can
8834          (and must) do name-lookup normally.  */
8835       if (template_keyword_p
8836           && (!parser->scope
8837               || (TYPE_P (parser->scope)
8838                   && dependent_type_p (parser->scope))))
8839         return identifier;
8840     }
8841
8842   /* Look up the name.  */
8843   decl = cp_parser_lookup_name (parser, identifier,
8844                                 none_type,
8845                                 /*is_template=*/false,
8846                                 /*is_namespace=*/false,
8847                                 check_dependency_p,
8848                                 /*ambiguous_p=*/NULL);
8849   decl = maybe_get_template_decl_from_type_decl (decl);
8850
8851   /* If DECL is a template, then the name was a template-name.  */
8852   if (TREE_CODE (decl) == TEMPLATE_DECL)
8853     ;
8854   else
8855     {
8856       tree fn = NULL_TREE;
8857
8858       /* The standard does not explicitly indicate whether a name that
8859          names a set of overloaded declarations, some of which are
8860          templates, is a template-name.  However, such a name should
8861          be a template-name; otherwise, there is no way to form a
8862          template-id for the overloaded templates.  */
8863       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8864       if (TREE_CODE (fns) == OVERLOAD)
8865         for (fn = fns; fn; fn = OVL_NEXT (fn))
8866           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8867             break;
8868
8869       if (!fn)
8870         {
8871           /* The name does not name a template.  */
8872           cp_parser_error (parser, "expected template-name");
8873           return error_mark_node;
8874         }
8875     }
8876
8877   /* If DECL is dependent, and refers to a function, then just return
8878      its name; we will look it up again during template instantiation.  */
8879   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8880     {
8881       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8882       if (TYPE_P (scope) && dependent_type_p (scope))
8883         return identifier;
8884     }
8885
8886   return decl;
8887 }
8888
8889 /* Parse a template-argument-list.
8890
8891    template-argument-list:
8892      template-argument
8893      template-argument-list , template-argument
8894
8895    Returns a TREE_VEC containing the arguments.  */
8896
8897 static tree
8898 cp_parser_template_argument_list (cp_parser* parser)
8899 {
8900   tree fixed_args[10];
8901   unsigned n_args = 0;
8902   unsigned alloced = 10;
8903   tree *arg_ary = fixed_args;
8904   tree vec;
8905   bool saved_in_template_argument_list_p;
8906
8907   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8908   parser->in_template_argument_list_p = true;
8909   do
8910     {
8911       tree argument;
8912
8913       if (n_args)
8914         /* Consume the comma.  */
8915         cp_lexer_consume_token (parser->lexer);
8916
8917       /* Parse the template-argument.  */
8918       argument = cp_parser_template_argument (parser);
8919       if (n_args == alloced)
8920         {
8921           alloced *= 2;
8922
8923           if (arg_ary == fixed_args)
8924             {
8925               arg_ary = xmalloc (sizeof (tree) * alloced);
8926               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8927             }
8928           else
8929             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8930         }
8931       arg_ary[n_args++] = argument;
8932     }
8933   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8934
8935   vec = make_tree_vec (n_args);
8936
8937   while (n_args--)
8938     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8939
8940   if (arg_ary != fixed_args)
8941     free (arg_ary);
8942   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8943   return vec;
8944 }
8945
8946 /* Parse a template-argument.
8947
8948    template-argument:
8949      assignment-expression
8950      type-id
8951      id-expression
8952
8953    The representation is that of an assignment-expression, type-id, or
8954    id-expression -- except that the qualified id-expression is
8955    evaluated, so that the value returned is either a DECL or an
8956    OVERLOAD.
8957
8958    Although the standard says "assignment-expression", it forbids
8959    throw-expressions or assignments in the template argument.
8960    Therefore, we use "conditional-expression" instead.  */
8961
8962 static tree
8963 cp_parser_template_argument (cp_parser* parser)
8964 {
8965   tree argument;
8966   bool template_p;
8967   bool address_p;
8968   bool maybe_type_id = false;
8969   cp_token *token;
8970   cp_id_kind idk;
8971   tree qualifying_class;
8972
8973   /* There's really no way to know what we're looking at, so we just
8974      try each alternative in order.
8975
8976        [temp.arg]
8977
8978        In a template-argument, an ambiguity between a type-id and an
8979        expression is resolved to a type-id, regardless of the form of
8980        the corresponding template-parameter.
8981
8982      Therefore, we try a type-id first.  */
8983   cp_parser_parse_tentatively (parser);
8984   argument = cp_parser_type_id (parser);
8985   /* If there was no error parsing the type-id but the next token is a '>>',
8986      we probably found a typo for '> >'. But there are type-id which are
8987      also valid expressions. For instance:
8988
8989      struct X { int operator >> (int); };
8990      template <int V> struct Foo {};
8991      Foo<X () >> 5> r;
8992
8993      Here 'X()' is a valid type-id of a function type, but the user just
8994      wanted to write the expression "X() >> 5". Thus, we remember that we
8995      found a valid type-id, but we still try to parse the argument as an
8996      expression to see what happens.  */
8997   if (!cp_parser_error_occurred (parser)
8998       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8999     {
9000       maybe_type_id = true;
9001       cp_parser_abort_tentative_parse (parser);
9002     }
9003   else
9004     {
9005       /* If the next token isn't a `,' or a `>', then this argument wasn't
9006       really finished. This means that the argument is not a valid
9007       type-id.  */
9008       if (!cp_parser_next_token_ends_template_argument_p (parser))
9009         cp_parser_error (parser, "expected template-argument");
9010       /* If that worked, we're done.  */
9011       if (cp_parser_parse_definitely (parser))
9012         return argument;
9013     }
9014   /* We're still not sure what the argument will be.  */
9015   cp_parser_parse_tentatively (parser);
9016   /* Try a template.  */
9017   argument = cp_parser_id_expression (parser,
9018                                       /*template_keyword_p=*/false,
9019                                       /*check_dependency_p=*/true,
9020                                       &template_p,
9021                                       /*declarator_p=*/false);
9022   /* If the next token isn't a `,' or a `>', then this argument wasn't
9023      really finished.  */
9024   if (!cp_parser_next_token_ends_template_argument_p (parser))
9025     cp_parser_error (parser, "expected template-argument");
9026   if (!cp_parser_error_occurred (parser))
9027     {
9028       /* Figure out what is being referred to.  If the id-expression
9029          was for a class template specialization, then we will have a
9030          TYPE_DECL at this point.  There is no need to do name lookup
9031          at this point in that case.  */
9032       if (TREE_CODE (argument) != TYPE_DECL)
9033         argument = cp_parser_lookup_name (parser, argument,
9034                                           none_type,
9035                                           /*is_template=*/template_p,
9036                                           /*is_namespace=*/false,
9037                                           /*check_dependency=*/true,
9038                                           /*ambiguous_p=*/NULL);
9039       if (TREE_CODE (argument) != TEMPLATE_DECL
9040           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9041         cp_parser_error (parser, "expected template-name");
9042     }
9043   if (cp_parser_parse_definitely (parser))
9044     return argument;
9045   /* It must be a non-type argument.  There permitted cases are given
9046      in [temp.arg.nontype]:
9047
9048      -- an integral constant-expression of integral or enumeration
9049         type; or
9050
9051      -- the name of a non-type template-parameter; or
9052
9053      -- the name of an object or function with external linkage...
9054
9055      -- the address of an object or function with external linkage...
9056
9057      -- a pointer to member...  */
9058   /* Look for a non-type template parameter.  */
9059   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9060     {
9061       cp_parser_parse_tentatively (parser);
9062       argument = cp_parser_primary_expression (parser,
9063                                                /*cast_p=*/false,
9064                                                &idk,
9065                                                &qualifying_class);
9066       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9067           || !cp_parser_next_token_ends_template_argument_p (parser))
9068         cp_parser_simulate_error (parser);
9069       if (cp_parser_parse_definitely (parser))
9070         return argument;
9071     }
9072
9073   /* If the next token is "&", the argument must be the address of an
9074      object or function with external linkage.  */
9075   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9076   if (address_p)
9077     cp_lexer_consume_token (parser->lexer);
9078   /* See if we might have an id-expression.  */
9079   token = cp_lexer_peek_token (parser->lexer);
9080   if (token->type == CPP_NAME
9081       || token->keyword == RID_OPERATOR
9082       || token->type == CPP_SCOPE
9083       || token->type == CPP_TEMPLATE_ID
9084       || token->type == CPP_NESTED_NAME_SPECIFIER)
9085     {
9086       cp_parser_parse_tentatively (parser);
9087       argument = cp_parser_primary_expression (parser,
9088                                                /*cast_p=*/false,
9089                                                &idk,
9090                                                &qualifying_class);
9091       if (cp_parser_error_occurred (parser)
9092           || !cp_parser_next_token_ends_template_argument_p (parser))
9093         cp_parser_abort_tentative_parse (parser);
9094       else
9095         {
9096           if (TREE_CODE (argument) == INDIRECT_REF)
9097             {
9098               gcc_assert (REFERENCE_REF_P (argument));
9099               argument = TREE_OPERAND (argument, 0);
9100             }
9101
9102           /* If ADDRESS_P, then we use finish_qualified_id_expr so
9103              that we get a pointer-to-member, if appropriate.
9104              However, if ADDRESS_P is false, we don't want to turn
9105              "T::f" into "(*this).T::f".  */
9106           if (qualifying_class && address_p)
9107             argument = finish_qualified_id_expr (qualifying_class,
9108                                                  argument,
9109                                                  /*done=*/true,
9110                                                  /*address_p=*/true);
9111           else if (TREE_CODE (argument) == BASELINK)
9112             /* We don't need the information about what class was used
9113                to name the overloaded functions.  */  
9114             argument = BASELINK_FUNCTIONS (argument);
9115
9116           if (TREE_CODE (argument) == VAR_DECL)
9117             {
9118               /* A variable without external linkage might still be a
9119                  valid constant-expression, so no error is issued here
9120                  if the external-linkage check fails.  */
9121               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9122                 cp_parser_simulate_error (parser);
9123             }
9124           else if (is_overloaded_fn (argument))
9125             /* All overloaded functions are allowed; if the external
9126                linkage test does not pass, an error will be issued
9127                later.  */
9128             ;
9129           else if (address_p
9130                    && (TREE_CODE (argument) == OFFSET_REF
9131                        || TREE_CODE (argument) == SCOPE_REF))
9132             /* A pointer-to-member.  */
9133             ;
9134           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9135             ;
9136           else
9137             cp_parser_simulate_error (parser);
9138
9139           if (cp_parser_parse_definitely (parser))
9140             {
9141               if (address_p)
9142                 argument = build_x_unary_op (ADDR_EXPR, argument);
9143               return argument;
9144             }
9145         }
9146     }
9147   /* If the argument started with "&", there are no other valid
9148      alternatives at this point.  */
9149   if (address_p)
9150     {
9151       cp_parser_error (parser, "invalid non-type template argument");
9152       return error_mark_node;
9153     }
9154
9155   /* If the argument wasn't successfully parsed as a type-id followed
9156      by '>>', the argument can only be a constant expression now.
9157      Otherwise, we try parsing the constant-expression tentatively,
9158      because the argument could really be a type-id.  */
9159   if (maybe_type_id)
9160     cp_parser_parse_tentatively (parser);
9161   argument = cp_parser_constant_expression (parser,
9162                                             /*allow_non_constant_p=*/false,
9163                                             /*non_constant_p=*/NULL);
9164   argument = fold_non_dependent_expr (argument);
9165   if (!maybe_type_id)
9166     return argument;
9167   if (!cp_parser_next_token_ends_template_argument_p (parser))
9168     cp_parser_error (parser, "expected template-argument");
9169   if (cp_parser_parse_definitely (parser))
9170     return argument;
9171   /* We did our best to parse the argument as a non type-id, but that
9172      was the only alternative that matched (albeit with a '>' after
9173      it). We can assume it's just a typo from the user, and a
9174      diagnostic will then be issued.  */
9175   return cp_parser_type_id (parser);
9176 }
9177
9178 /* Parse an explicit-instantiation.
9179
9180    explicit-instantiation:
9181      template declaration
9182
9183    Although the standard says `declaration', what it really means is:
9184
9185    explicit-instantiation:
9186      template decl-specifier-seq [opt] declarator [opt] ;
9187
9188    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9189    supposed to be allowed.  A defect report has been filed about this
9190    issue.
9191
9192    GNU Extension:
9193
9194    explicit-instantiation:
9195      storage-class-specifier template
9196        decl-specifier-seq [opt] declarator [opt] ;
9197      function-specifier template
9198        decl-specifier-seq [opt] declarator [opt] ;  */
9199
9200 static void
9201 cp_parser_explicit_instantiation (cp_parser* parser)
9202 {
9203   int declares_class_or_enum;
9204   cp_decl_specifier_seq decl_specifiers;
9205   tree extension_specifier = NULL_TREE;
9206
9207   /* Look for an (optional) storage-class-specifier or
9208      function-specifier.  */
9209   if (cp_parser_allow_gnu_extensions_p (parser))
9210     {
9211       extension_specifier
9212         = cp_parser_storage_class_specifier_opt (parser);
9213       if (!extension_specifier)
9214         extension_specifier
9215           = cp_parser_function_specifier_opt (parser,
9216                                               /*decl_specs=*/NULL);
9217     }
9218
9219   /* Look for the `template' keyword.  */
9220   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9221   /* Let the front end know that we are processing an explicit
9222      instantiation.  */
9223   begin_explicit_instantiation ();
9224   /* [temp.explicit] says that we are supposed to ignore access
9225      control while processing explicit instantiation directives.  */
9226   push_deferring_access_checks (dk_no_check);
9227   /* Parse a decl-specifier-seq.  */
9228   cp_parser_decl_specifier_seq (parser,
9229                                 CP_PARSER_FLAGS_OPTIONAL,
9230                                 &decl_specifiers,
9231                                 &declares_class_or_enum);
9232   /* If there was exactly one decl-specifier, and it declared a class,
9233      and there's no declarator, then we have an explicit type
9234      instantiation.  */
9235   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9236     {
9237       tree type;
9238
9239       type = check_tag_decl (&decl_specifiers);
9240       /* Turn access control back on for names used during
9241          template instantiation.  */
9242       pop_deferring_access_checks ();
9243       if (type)
9244         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9245     }
9246   else
9247     {
9248       cp_declarator *declarator;
9249       tree decl;
9250
9251       /* Parse the declarator.  */
9252       declarator
9253         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9254                                 /*ctor_dtor_or_conv_p=*/NULL,
9255                                 /*parenthesized_p=*/NULL,
9256                                 /*member_p=*/false);
9257       if (declares_class_or_enum & 2)
9258         cp_parser_check_for_definition_in_return_type (declarator,
9259                                                        decl_specifiers.type);
9260       if (declarator != cp_error_declarator)
9261         {
9262           decl = grokdeclarator (declarator, &decl_specifiers,
9263                                  NORMAL, 0, NULL);
9264           /* Turn access control back on for names used during
9265              template instantiation.  */
9266           pop_deferring_access_checks ();
9267           /* Do the explicit instantiation.  */
9268           do_decl_instantiation (decl, extension_specifier);
9269         }
9270       else
9271         {
9272           pop_deferring_access_checks ();
9273           /* Skip the body of the explicit instantiation.  */
9274           cp_parser_skip_to_end_of_statement (parser);
9275         }
9276     }
9277   /* We're done with the instantiation.  */
9278   end_explicit_instantiation ();
9279
9280   cp_parser_consume_semicolon_at_end_of_statement (parser);
9281 }
9282
9283 /* Parse an explicit-specialization.
9284
9285    explicit-specialization:
9286      template < > declaration
9287
9288    Although the standard says `declaration', what it really means is:
9289
9290    explicit-specialization:
9291      template <> decl-specifier [opt] init-declarator [opt] ;
9292      template <> function-definition
9293      template <> explicit-specialization
9294      template <> template-declaration  */
9295
9296 static void
9297 cp_parser_explicit_specialization (cp_parser* parser)
9298 {
9299   /* Look for the `template' keyword.  */
9300   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9301   /* Look for the `<'.  */
9302   cp_parser_require (parser, CPP_LESS, "`<'");
9303   /* Look for the `>'.  */
9304   cp_parser_require (parser, CPP_GREATER, "`>'");
9305   /* We have processed another parameter list.  */
9306   ++parser->num_template_parameter_lists;
9307   /* Let the front end know that we are beginning a specialization.  */
9308   begin_specialization ();
9309
9310   /* If the next keyword is `template', we need to figure out whether
9311      or not we're looking a template-declaration.  */
9312   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9313     {
9314       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9315           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9316         cp_parser_template_declaration_after_export (parser,
9317                                                      /*member_p=*/false);
9318       else
9319         cp_parser_explicit_specialization (parser);
9320     }
9321   else
9322     /* Parse the dependent declaration.  */
9323     cp_parser_single_declaration (parser,
9324                                   /*member_p=*/false,
9325                                   /*friend_p=*/NULL);
9326
9327   /* We're done with the specialization.  */
9328   end_specialization ();
9329   /* We're done with this parameter list.  */
9330   --parser->num_template_parameter_lists;
9331 }
9332
9333 /* Parse a type-specifier.
9334
9335    type-specifier:
9336      simple-type-specifier
9337      class-specifier
9338      enum-specifier
9339      elaborated-type-specifier
9340      cv-qualifier
9341
9342    GNU Extension:
9343
9344    type-specifier:
9345      __complex__
9346
9347    Returns a representation of the type-specifier.  For a
9348    class-specifier, enum-specifier, or elaborated-type-specifier, a
9349    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9350
9351    The parser flags FLAGS is used to control type-specifier parsing.
9352
9353    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9354    in a decl-specifier-seq.
9355
9356    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9357    class-specifier, enum-specifier, or elaborated-type-specifier, then
9358    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9359    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9360    zero.
9361
9362    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9363    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9364    is set to FALSE.  */
9365
9366 static tree
9367 cp_parser_type_specifier (cp_parser* parser,
9368                           cp_parser_flags flags,
9369                           cp_decl_specifier_seq *decl_specs,
9370                           bool is_declaration,
9371                           int* declares_class_or_enum,
9372                           bool* is_cv_qualifier)
9373 {
9374   tree type_spec = NULL_TREE;
9375   cp_token *token;
9376   enum rid keyword;
9377   cp_decl_spec ds = ds_last;
9378
9379   /* Assume this type-specifier does not declare a new type.  */
9380   if (declares_class_or_enum)
9381     *declares_class_or_enum = 0;
9382   /* And that it does not specify a cv-qualifier.  */
9383   if (is_cv_qualifier)
9384     *is_cv_qualifier = false;
9385   /* Peek at the next token.  */
9386   token = cp_lexer_peek_token (parser->lexer);
9387
9388   /* If we're looking at a keyword, we can use that to guide the
9389      production we choose.  */
9390   keyword = token->keyword;
9391   switch (keyword)
9392     {
9393     case RID_ENUM:
9394       /* 'enum' [identifier] '{' introduces an enum-specifier;
9395          'enum' <anything else> introduces an elaborated-type-specifier.  */
9396       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9397           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9398               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9399                  == CPP_OPEN_BRACE))
9400         {
9401           if (parser->num_template_parameter_lists)
9402             {
9403               error ("template declaration of %qs", "enum");
9404               cp_parser_skip_to_end_of_block_or_statement (parser);
9405               type_spec = error_mark_node;
9406             }
9407           else
9408             type_spec = cp_parser_enum_specifier (parser);
9409
9410           if (declares_class_or_enum)
9411             *declares_class_or_enum = 2;
9412           if (decl_specs)
9413             cp_parser_set_decl_spec_type (decl_specs,
9414                                           type_spec,
9415                                           /*user_defined_p=*/true);
9416           return type_spec;
9417         }
9418       else
9419         goto elaborated_type_specifier;
9420
9421       /* Any of these indicate either a class-specifier, or an
9422          elaborated-type-specifier.  */
9423     case RID_CLASS:
9424     case RID_STRUCT:
9425     case RID_UNION:
9426       /* Parse tentatively so that we can back up if we don't find a
9427          class-specifier.  */
9428       cp_parser_parse_tentatively (parser);
9429       /* Look for the class-specifier.  */
9430       type_spec = cp_parser_class_specifier (parser);
9431       /* If that worked, we're done.  */
9432       if (cp_parser_parse_definitely (parser))
9433         {
9434           if (declares_class_or_enum)
9435             *declares_class_or_enum = 2;
9436           if (decl_specs)
9437             cp_parser_set_decl_spec_type (decl_specs,
9438                                           type_spec,
9439                                           /*user_defined_p=*/true);
9440           return type_spec;
9441         }
9442
9443       /* Fall through.  */
9444     elaborated_type_specifier:
9445       /* We're declaring (not defining) a class or enum.  */
9446       if (declares_class_or_enum)
9447         *declares_class_or_enum = 1;
9448
9449       /* Fall through.  */
9450     case RID_TYPENAME:
9451       /* Look for an elaborated-type-specifier.  */
9452       type_spec
9453         = (cp_parser_elaborated_type_specifier
9454            (parser,
9455             decl_specs && decl_specs->specs[(int) ds_friend],
9456             is_declaration));
9457       if (decl_specs)
9458         cp_parser_set_decl_spec_type (decl_specs,
9459                                       type_spec,
9460                                       /*user_defined_p=*/true);
9461       return type_spec;
9462
9463     case RID_CONST:
9464       ds = ds_const;
9465       if (is_cv_qualifier)
9466         *is_cv_qualifier = true;
9467       break;
9468
9469     case RID_VOLATILE:
9470       ds = ds_volatile;
9471       if (is_cv_qualifier)
9472         *is_cv_qualifier = true;
9473       break;
9474
9475     case RID_RESTRICT:
9476       ds = ds_restrict;
9477       if (is_cv_qualifier)
9478         *is_cv_qualifier = true;
9479       break;
9480
9481     case RID_COMPLEX:
9482       /* The `__complex__' keyword is a GNU extension.  */
9483       ds = ds_complex;
9484       break;
9485
9486     default:
9487       break;
9488     }
9489
9490   /* Handle simple keywords.  */
9491   if (ds != ds_last)
9492     {
9493       if (decl_specs)
9494         {
9495           ++decl_specs->specs[(int)ds];
9496           decl_specs->any_specifiers_p = true;
9497         }
9498       return cp_lexer_consume_token (parser->lexer)->value;
9499     }
9500
9501   /* If we do not already have a type-specifier, assume we are looking
9502      at a simple-type-specifier.  */
9503   type_spec = cp_parser_simple_type_specifier (parser,
9504                                                decl_specs,
9505                                                flags);
9506
9507   /* If we didn't find a type-specifier, and a type-specifier was not
9508      optional in this context, issue an error message.  */
9509   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9510     {
9511       cp_parser_error (parser, "expected type specifier");
9512       return error_mark_node;
9513     }
9514
9515   return type_spec;
9516 }
9517
9518 /* Parse a simple-type-specifier.
9519
9520    simple-type-specifier:
9521      :: [opt] nested-name-specifier [opt] type-name
9522      :: [opt] nested-name-specifier template template-id
9523      char
9524      wchar_t
9525      bool
9526      short
9527      int
9528      long
9529      signed
9530      unsigned
9531      float
9532      double
9533      void
9534
9535    GNU Extension:
9536
9537    simple-type-specifier:
9538      __typeof__ unary-expression
9539      __typeof__ ( type-id )
9540
9541    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9542    appropriately updated.  */
9543
9544 static tree
9545 cp_parser_simple_type_specifier (cp_parser* parser,
9546                                  cp_decl_specifier_seq *decl_specs,
9547                                  cp_parser_flags flags)
9548 {
9549   tree type = NULL_TREE;
9550   cp_token *token;
9551
9552   /* Peek at the next token.  */
9553   token = cp_lexer_peek_token (parser->lexer);
9554
9555   /* If we're looking at a keyword, things are easy.  */
9556   switch (token->keyword)
9557     {
9558     case RID_CHAR:
9559       if (decl_specs)
9560         decl_specs->explicit_char_p = true;
9561       type = char_type_node;
9562       break;
9563     case RID_WCHAR:
9564       type = wchar_type_node;
9565       break;
9566     case RID_BOOL:
9567       type = boolean_type_node;
9568       break;
9569     case RID_SHORT:
9570       if (decl_specs)
9571         ++decl_specs->specs[(int) ds_short];
9572       type = short_integer_type_node;
9573       break;
9574     case RID_INT:
9575       if (decl_specs)
9576         decl_specs->explicit_int_p = true;
9577       type = integer_type_node;
9578       break;
9579     case RID_LONG:
9580       if (decl_specs)
9581         ++decl_specs->specs[(int) ds_long];
9582       type = long_integer_type_node;
9583       break;
9584     case RID_SIGNED:
9585       if (decl_specs)
9586         ++decl_specs->specs[(int) ds_signed];
9587       type = integer_type_node;
9588       break;
9589     case RID_UNSIGNED:
9590       if (decl_specs)
9591         ++decl_specs->specs[(int) ds_unsigned];
9592       type = unsigned_type_node;
9593       break;
9594     case RID_FLOAT:
9595       type = float_type_node;
9596       break;
9597     case RID_DOUBLE:
9598       type = double_type_node;
9599       break;
9600     case RID_VOID:
9601       type = void_type_node;
9602       break;
9603
9604     case RID_TYPEOF:
9605       /* Consume the `typeof' token.  */
9606       cp_lexer_consume_token (parser->lexer);
9607       /* Parse the operand to `typeof'.  */
9608       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9609       /* If it is not already a TYPE, take its type.  */
9610       if (!TYPE_P (type))
9611         type = finish_typeof (type);
9612
9613       if (decl_specs)
9614         cp_parser_set_decl_spec_type (decl_specs, type,
9615                                       /*user_defined_p=*/true);
9616
9617       return type;
9618
9619     default:
9620       break;
9621     }
9622
9623   /* If the type-specifier was for a built-in type, we're done.  */
9624   if (type)
9625     {
9626       tree id;
9627
9628       /* Record the type.  */
9629       if (decl_specs
9630           && (token->keyword != RID_SIGNED
9631               && token->keyword != RID_UNSIGNED
9632               && token->keyword != RID_SHORT
9633               && token->keyword != RID_LONG))
9634         cp_parser_set_decl_spec_type (decl_specs,
9635                                       type,
9636                                       /*user_defined=*/false);
9637       if (decl_specs)
9638         decl_specs->any_specifiers_p = true;
9639
9640       /* Consume the token.  */
9641       id = cp_lexer_consume_token (parser->lexer)->value;
9642
9643       /* There is no valid C++ program where a non-template type is
9644          followed by a "<".  That usually indicates that the user thought
9645          that the type was a template.  */
9646       cp_parser_check_for_invalid_template_id (parser, type);
9647
9648       return TYPE_NAME (type);
9649     }
9650
9651   /* The type-specifier must be a user-defined type.  */
9652   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9653     {
9654       bool qualified_p;
9655       bool global_p;
9656
9657       /* Don't gobble tokens or issue error messages if this is an
9658          optional type-specifier.  */
9659       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9660         cp_parser_parse_tentatively (parser);
9661
9662       /* Look for the optional `::' operator.  */
9663       global_p
9664         = (cp_parser_global_scope_opt (parser,
9665                                        /*current_scope_valid_p=*/false)
9666            != NULL_TREE);
9667       /* Look for the nested-name specifier.  */
9668       qualified_p
9669         = (cp_parser_nested_name_specifier_opt (parser,
9670                                                 /*typename_keyword_p=*/false,
9671                                                 /*check_dependency_p=*/true,
9672                                                 /*type_p=*/false,
9673                                                 /*is_declaration=*/false)
9674            != NULL_TREE);
9675       /* If we have seen a nested-name-specifier, and the next token
9676          is `template', then we are using the template-id production.  */
9677       if (parser->scope
9678           && cp_parser_optional_template_keyword (parser))
9679         {
9680           /* Look for the template-id.  */
9681           type = cp_parser_template_id (parser,
9682                                         /*template_keyword_p=*/true,
9683                                         /*check_dependency_p=*/true,
9684                                         /*is_declaration=*/false);
9685           /* If the template-id did not name a type, we are out of
9686              luck.  */
9687           if (TREE_CODE (type) != TYPE_DECL)
9688             {
9689               cp_parser_error (parser, "expected template-id for type");
9690               type = NULL_TREE;
9691             }
9692         }
9693       /* Otherwise, look for a type-name.  */
9694       else
9695         type = cp_parser_type_name (parser);
9696       /* Keep track of all name-lookups performed in class scopes.  */
9697       if (type
9698           && !global_p
9699           && !qualified_p
9700           && TREE_CODE (type) == TYPE_DECL
9701           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9702         maybe_note_name_used_in_class (DECL_NAME (type), type);
9703       /* If it didn't work out, we don't have a TYPE.  */
9704       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9705           && !cp_parser_parse_definitely (parser))
9706         type = NULL_TREE;
9707       if (type && decl_specs)
9708         cp_parser_set_decl_spec_type (decl_specs, type,
9709                                       /*user_defined=*/true);
9710     }
9711
9712   /* If we didn't get a type-name, issue an error message.  */
9713   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9714     {
9715       cp_parser_error (parser, "expected type-name");
9716       return error_mark_node;
9717     }
9718
9719   /* There is no valid C++ program where a non-template type is
9720      followed by a "<".  That usually indicates that the user thought
9721      that the type was a template.  */
9722   if (type && type != error_mark_node)
9723     {
9724       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9725          If it is, then the '<'...'>' enclose protocol names rather than
9726          template arguments, and so everything is fine.  */
9727       if (c_dialect_objc ()
9728           && (objc_is_id (type) || objc_is_class_name (type)))
9729         {
9730           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9731           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9732
9733           /* Clobber the "unqualified" type previously entered into
9734              DECL_SPECS with the new, improved protocol-qualified version.  */
9735           if (decl_specs)
9736             decl_specs->type = qual_type;
9737
9738           return qual_type;
9739         }
9740
9741       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9742     }
9743
9744   return type;
9745 }
9746
9747 /* Parse a type-name.
9748
9749    type-name:
9750      class-name
9751      enum-name
9752      typedef-name
9753
9754    enum-name:
9755      identifier
9756
9757    typedef-name:
9758      identifier
9759
9760    Returns a TYPE_DECL for the type.  */
9761
9762 static tree
9763 cp_parser_type_name (cp_parser* parser)
9764 {
9765   tree type_decl;
9766   tree identifier;
9767
9768   /* We can't know yet whether it is a class-name or not.  */
9769   cp_parser_parse_tentatively (parser);
9770   /* Try a class-name.  */
9771   type_decl = cp_parser_class_name (parser,
9772                                     /*typename_keyword_p=*/false,
9773                                     /*template_keyword_p=*/false,
9774                                     none_type,
9775                                     /*check_dependency_p=*/true,
9776                                     /*class_head_p=*/false,
9777                                     /*is_declaration=*/false);
9778   /* If it's not a class-name, keep looking.  */
9779   if (!cp_parser_parse_definitely (parser))
9780     {
9781       /* It must be a typedef-name or an enum-name.  */
9782       identifier = cp_parser_identifier (parser);
9783       if (identifier == error_mark_node)
9784         return error_mark_node;
9785
9786       /* Look up the type-name.  */
9787       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9788
9789       if (TREE_CODE (type_decl) != TYPE_DECL
9790           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9791         {
9792           /* See if this is an Objective-C type.  */
9793           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9794           tree type = objc_get_protocol_qualified_type (identifier, protos);
9795           if (type)
9796             type_decl = TYPE_NAME (type);
9797         }
9798
9799       /* Issue an error if we did not find a type-name.  */
9800       if (TREE_CODE (type_decl) != TYPE_DECL)
9801         {
9802           if (!cp_parser_simulate_error (parser))
9803             cp_parser_name_lookup_error (parser, identifier, type_decl,
9804                                          "is not a type");
9805           type_decl = error_mark_node;
9806         }
9807       /* Remember that the name was used in the definition of the
9808          current class so that we can check later to see if the
9809          meaning would have been different after the class was
9810          entirely defined.  */
9811       else if (type_decl != error_mark_node
9812                && !parser->scope)
9813         maybe_note_name_used_in_class (identifier, type_decl);
9814     }
9815
9816   return type_decl;
9817 }
9818
9819
9820 /* Parse an elaborated-type-specifier.  Note that the grammar given
9821    here incorporates the resolution to DR68.
9822
9823    elaborated-type-specifier:
9824      class-key :: [opt] nested-name-specifier [opt] identifier
9825      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9826      enum :: [opt] nested-name-specifier [opt] identifier
9827      typename :: [opt] nested-name-specifier identifier
9828      typename :: [opt] nested-name-specifier template [opt]
9829        template-id
9830
9831    GNU extension:
9832
9833    elaborated-type-specifier:
9834      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9835      class-key attributes :: [opt] nested-name-specifier [opt]
9836                template [opt] template-id
9837      enum attributes :: [opt] nested-name-specifier [opt] identifier
9838
9839    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9840    declared `friend'.  If IS_DECLARATION is TRUE, then this
9841    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9842    something is being declared.
9843
9844    Returns the TYPE specified.  */
9845
9846 static tree
9847 cp_parser_elaborated_type_specifier (cp_parser* parser,
9848                                      bool is_friend,
9849                                      bool is_declaration)
9850 {
9851   enum tag_types tag_type;
9852   tree identifier;
9853   tree type = NULL_TREE;
9854   tree attributes = NULL_TREE;
9855
9856   /* See if we're looking at the `enum' keyword.  */
9857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9858     {
9859       /* Consume the `enum' token.  */
9860       cp_lexer_consume_token (parser->lexer);
9861       /* Remember that it's an enumeration type.  */
9862       tag_type = enum_type;
9863       /* Parse the attributes.  */
9864       attributes = cp_parser_attributes_opt (parser);
9865     }
9866   /* Or, it might be `typename'.  */
9867   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9868                                            RID_TYPENAME))
9869     {
9870       /* Consume the `typename' token.  */
9871       cp_lexer_consume_token (parser->lexer);
9872       /* Remember that it's a `typename' type.  */
9873       tag_type = typename_type;
9874       /* The `typename' keyword is only allowed in templates.  */
9875       if (!processing_template_decl)
9876         pedwarn ("using %<typename%> outside of template");
9877     }
9878   /* Otherwise it must be a class-key.  */
9879   else
9880     {
9881       tag_type = cp_parser_class_key (parser);
9882       if (tag_type == none_type)
9883         return error_mark_node;
9884       /* Parse the attributes.  */
9885       attributes = cp_parser_attributes_opt (parser);
9886     }
9887
9888   /* Look for the `::' operator.  */
9889   cp_parser_global_scope_opt (parser,
9890                               /*current_scope_valid_p=*/false);
9891   /* Look for the nested-name-specifier.  */
9892   if (tag_type == typename_type)
9893     {
9894       if (!cp_parser_nested_name_specifier (parser,
9895                                            /*typename_keyword_p=*/true,
9896                                            /*check_dependency_p=*/true,
9897                                            /*type_p=*/true,
9898                                             is_declaration))
9899         return error_mark_node;
9900     }
9901   else
9902     /* Even though `typename' is not present, the proposed resolution
9903        to Core Issue 180 says that in `class A<T>::B', `B' should be
9904        considered a type-name, even if `A<T>' is dependent.  */
9905     cp_parser_nested_name_specifier_opt (parser,
9906                                          /*typename_keyword_p=*/true,
9907                                          /*check_dependency_p=*/true,
9908                                          /*type_p=*/true,
9909                                          is_declaration);
9910   /* For everything but enumeration types, consider a template-id.  */
9911   if (tag_type != enum_type)
9912     {
9913       bool template_p = false;
9914       tree decl;
9915
9916       /* Allow the `template' keyword.  */
9917       template_p = cp_parser_optional_template_keyword (parser);
9918       /* If we didn't see `template', we don't know if there's a
9919          template-id or not.  */
9920       if (!template_p)
9921         cp_parser_parse_tentatively (parser);
9922       /* Parse the template-id.  */
9923       decl = cp_parser_template_id (parser, template_p,
9924                                     /*check_dependency_p=*/true,
9925                                     is_declaration);
9926       /* If we didn't find a template-id, look for an ordinary
9927          identifier.  */
9928       if (!template_p && !cp_parser_parse_definitely (parser))
9929         ;
9930       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9931          in effect, then we must assume that, upon instantiation, the
9932          template will correspond to a class.  */
9933       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9934                && tag_type == typename_type)
9935         type = make_typename_type (parser->scope, decl,
9936                                    typename_type,
9937                                    /*complain=*/1);
9938       else
9939         type = TREE_TYPE (decl);
9940     }
9941
9942   /* For an enumeration type, consider only a plain identifier.  */
9943   if (!type)
9944     {
9945       identifier = cp_parser_identifier (parser);
9946
9947       if (identifier == error_mark_node)
9948         {
9949           parser->scope = NULL_TREE;
9950           return error_mark_node;
9951         }
9952
9953       /* For a `typename', we needn't call xref_tag.  */
9954       if (tag_type == typename_type
9955           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9956         return cp_parser_make_typename_type (parser, parser->scope,
9957                                              identifier);
9958       /* Look up a qualified name in the usual way.  */
9959       if (parser->scope)
9960         {
9961           tree decl;
9962
9963           decl = cp_parser_lookup_name (parser, identifier,
9964                                         tag_type,
9965                                         /*is_template=*/false,
9966                                         /*is_namespace=*/false,
9967                                         /*check_dependency=*/true,
9968                                         /*ambiguous_p=*/NULL);
9969
9970           /* If we are parsing friend declaration, DECL may be a
9971              TEMPLATE_DECL tree node here.  However, we need to check
9972              whether this TEMPLATE_DECL results in valid code.  Consider
9973              the following example:
9974
9975                namespace N {
9976                  template <class T> class C {};
9977                }
9978                class X {
9979                  template <class T> friend class N::C; // #1, valid code
9980                };
9981                template <class T> class Y {
9982                  friend class N::C;                    // #2, invalid code
9983                };
9984
9985              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9986              name lookup of `N::C'.  We see that friend declaration must
9987              be template for the code to be valid.  Note that
9988              processing_template_decl does not work here since it is
9989              always 1 for the above two cases.  */
9990
9991           decl = (cp_parser_maybe_treat_template_as_class
9992                   (decl, /*tag_name_p=*/is_friend
9993                          && parser->num_template_parameter_lists));
9994
9995           if (TREE_CODE (decl) != TYPE_DECL)
9996             {
9997               cp_parser_diagnose_invalid_type_name (parser,
9998                                                     parser->scope,
9999                                                     identifier);
10000               return error_mark_node;
10001             }
10002
10003           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10004             check_elaborated_type_specifier
10005               (tag_type, decl,
10006                (parser->num_template_parameter_lists
10007                 || DECL_SELF_REFERENCE_P (decl)));
10008
10009           type = TREE_TYPE (decl);
10010         }
10011       else
10012         {
10013           /* An elaborated-type-specifier sometimes introduces a new type and
10014              sometimes names an existing type.  Normally, the rule is that it
10015              introduces a new type only if there is not an existing type of
10016              the same name already in scope.  For example, given:
10017
10018                struct S {};
10019                void f() { struct S s; }
10020
10021              the `struct S' in the body of `f' is the same `struct S' as in
10022              the global scope; the existing definition is used.  However, if
10023              there were no global declaration, this would introduce a new
10024              local class named `S'.
10025
10026              An exception to this rule applies to the following code:
10027
10028                namespace N { struct S; }
10029
10030              Here, the elaborated-type-specifier names a new type
10031              unconditionally; even if there is already an `S' in the
10032              containing scope this declaration names a new type.
10033              This exception only applies if the elaborated-type-specifier
10034              forms the complete declaration:
10035
10036                [class.name]
10037
10038                A declaration consisting solely of `class-key identifier ;' is
10039                either a redeclaration of the name in the current scope or a
10040                forward declaration of the identifier as a class name.  It
10041                introduces the name into the current scope.
10042
10043              We are in this situation precisely when the next token is a `;'.
10044
10045              An exception to the exception is that a `friend' declaration does
10046              *not* name a new type; i.e., given:
10047
10048                struct S { friend struct T; };
10049
10050              `T' is not a new type in the scope of `S'.
10051
10052              Also, `new struct S' or `sizeof (struct S)' never results in the
10053              definition of a new type; a new type can only be declared in a
10054              declaration context.  */
10055
10056           tag_scope ts;
10057           if (is_friend)
10058             /* Friends have special name lookup rules.  */
10059             ts = ts_within_enclosing_non_class;
10060           else if (is_declaration
10061                    && cp_lexer_next_token_is (parser->lexer,
10062                                               CPP_SEMICOLON))
10063             /* This is a `class-key identifier ;' */
10064             ts = ts_current;
10065           else
10066             ts = ts_global;
10067
10068           /* Warn about attributes. They are ignored.  */
10069           if (attributes)
10070             warning (OPT_Wattributes,
10071                      "type attributes are honored only at type definition");
10072
10073           type = xref_tag (tag_type, identifier, ts,
10074                            parser->num_template_parameter_lists);
10075         }
10076     }
10077   if (tag_type != enum_type)
10078     cp_parser_check_class_key (tag_type, type);
10079
10080   /* A "<" cannot follow an elaborated type specifier.  If that
10081      happens, the user was probably trying to form a template-id.  */
10082   cp_parser_check_for_invalid_template_id (parser, type);
10083
10084   return type;
10085 }
10086
10087 /* Parse an enum-specifier.
10088
10089    enum-specifier:
10090      enum identifier [opt] { enumerator-list [opt] }
10091
10092    GNU Extensions:
10093      enum identifier [opt] { enumerator-list [opt] } attributes
10094
10095    Returns an ENUM_TYPE representing the enumeration.  */
10096
10097 static tree
10098 cp_parser_enum_specifier (cp_parser* parser)
10099 {
10100   tree identifier;
10101   tree type;
10102
10103   /* Caller guarantees that the current token is 'enum', an identifier
10104      possibly follows, and the token after that is an opening brace.
10105      If we don't have an identifier, fabricate an anonymous name for
10106      the enumeration being defined.  */
10107   cp_lexer_consume_token (parser->lexer);
10108
10109   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10110     identifier = cp_parser_identifier (parser);
10111   else
10112     identifier = make_anon_name ();
10113
10114   /* Issue an error message if type-definitions are forbidden here.  */
10115   cp_parser_check_type_definition (parser);
10116
10117   /* Create the new type.  We do this before consuming the opening brace
10118      so the enum will be recorded as being on the line of its tag (or the
10119      'enum' keyword, if there is no tag).  */
10120   type = start_enum (identifier);
10121
10122   /* Consume the opening brace.  */
10123   cp_lexer_consume_token (parser->lexer);
10124
10125   /* If the next token is not '}', then there are some enumerators.  */
10126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10127     cp_parser_enumerator_list (parser, type);
10128
10129   /* Consume the final '}'.  */
10130   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10131
10132   /* Look for trailing attributes to apply to this enumeration, and
10133      apply them if appropriate.  */
10134   if (cp_parser_allow_gnu_extensions_p (parser))
10135     {
10136       tree trailing_attr = cp_parser_attributes_opt (parser);
10137       cplus_decl_attributes (&type,
10138                              trailing_attr,
10139                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10140     }
10141
10142   /* Finish up the enumeration.  */
10143   finish_enum (type);
10144
10145   return type;
10146 }
10147
10148 /* Parse an enumerator-list.  The enumerators all have the indicated
10149    TYPE.
10150
10151    enumerator-list:
10152      enumerator-definition
10153      enumerator-list , enumerator-definition  */
10154
10155 static void
10156 cp_parser_enumerator_list (cp_parser* parser, tree type)
10157 {
10158   while (true)
10159     {
10160       /* Parse an enumerator-definition.  */
10161       cp_parser_enumerator_definition (parser, type);
10162
10163       /* If the next token is not a ',', we've reached the end of
10164          the list.  */
10165       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10166         break;
10167       /* Otherwise, consume the `,' and keep going.  */
10168       cp_lexer_consume_token (parser->lexer);
10169       /* If the next token is a `}', there is a trailing comma.  */
10170       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10171         {
10172           if (pedantic && !in_system_header)
10173             pedwarn ("comma at end of enumerator list");
10174           break;
10175         }
10176     }
10177 }
10178
10179 /* Parse an enumerator-definition.  The enumerator has the indicated
10180    TYPE.
10181
10182    enumerator-definition:
10183      enumerator
10184      enumerator = constant-expression
10185
10186    enumerator:
10187      identifier  */
10188
10189 static void
10190 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10191 {
10192   tree identifier;
10193   tree value;
10194
10195   /* Look for the identifier.  */
10196   identifier = cp_parser_identifier (parser);
10197   if (identifier == error_mark_node)
10198     return;
10199
10200   /* If the next token is an '=', then there is an explicit value.  */
10201   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10202     {
10203       /* Consume the `=' token.  */
10204       cp_lexer_consume_token (parser->lexer);
10205       /* Parse the value.  */
10206       value = cp_parser_constant_expression (parser,
10207                                              /*allow_non_constant_p=*/false,
10208                                              NULL);
10209     }
10210   else
10211     value = NULL_TREE;
10212
10213   /* Create the enumerator.  */
10214   build_enumerator (identifier, value, type);
10215 }
10216
10217 /* Parse a namespace-name.
10218
10219    namespace-name:
10220      original-namespace-name
10221      namespace-alias
10222
10223    Returns the NAMESPACE_DECL for the namespace.  */
10224
10225 static tree
10226 cp_parser_namespace_name (cp_parser* parser)
10227 {
10228   tree identifier;
10229   tree namespace_decl;
10230
10231   /* Get the name of the namespace.  */
10232   identifier = cp_parser_identifier (parser);
10233   if (identifier == error_mark_node)
10234     return error_mark_node;
10235
10236   /* Look up the identifier in the currently active scope.  Look only
10237      for namespaces, due to:
10238
10239        [basic.lookup.udir]
10240
10241        When looking up a namespace-name in a using-directive or alias
10242        definition, only namespace names are considered.
10243
10244      And:
10245
10246        [basic.lookup.qual]
10247
10248        During the lookup of a name preceding the :: scope resolution
10249        operator, object, function, and enumerator names are ignored.
10250
10251      (Note that cp_parser_class_or_namespace_name only calls this
10252      function if the token after the name is the scope resolution
10253      operator.)  */
10254   namespace_decl = cp_parser_lookup_name (parser, identifier,
10255                                           none_type,
10256                                           /*is_template=*/false,
10257                                           /*is_namespace=*/true,
10258                                           /*check_dependency=*/true,
10259                                           /*ambiguous_p=*/NULL);
10260   /* If it's not a namespace, issue an error.  */
10261   if (namespace_decl == error_mark_node
10262       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10263     {
10264       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10265         error ("%qD is not a namespace-name", identifier);
10266       cp_parser_error (parser, "expected namespace-name");
10267       namespace_decl = error_mark_node;
10268     }
10269
10270   return namespace_decl;
10271 }
10272
10273 /* Parse a namespace-definition.
10274
10275    namespace-definition:
10276      named-namespace-definition
10277      unnamed-namespace-definition
10278
10279    named-namespace-definition:
10280      original-namespace-definition
10281      extension-namespace-definition
10282
10283    original-namespace-definition:
10284      namespace identifier { namespace-body }
10285
10286    extension-namespace-definition:
10287      namespace original-namespace-name { namespace-body }
10288
10289    unnamed-namespace-definition:
10290      namespace { namespace-body } */
10291
10292 static void
10293 cp_parser_namespace_definition (cp_parser* parser)
10294 {
10295   tree identifier;
10296
10297   /* Look for the `namespace' keyword.  */
10298   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10299
10300   /* Get the name of the namespace.  We do not attempt to distinguish
10301      between an original-namespace-definition and an
10302      extension-namespace-definition at this point.  The semantic
10303      analysis routines are responsible for that.  */
10304   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10305     identifier = cp_parser_identifier (parser);
10306   else
10307     identifier = NULL_TREE;
10308
10309   /* Look for the `{' to start the namespace.  */
10310   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10311   /* Start the namespace.  */
10312   push_namespace (identifier);
10313   /* Parse the body of the namespace.  */
10314   cp_parser_namespace_body (parser);
10315   /* Finish the namespace.  */
10316   pop_namespace ();
10317   /* Look for the final `}'.  */
10318   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10319 }
10320
10321 /* Parse a namespace-body.
10322
10323    namespace-body:
10324      declaration-seq [opt]  */
10325
10326 static void
10327 cp_parser_namespace_body (cp_parser* parser)
10328 {
10329   cp_parser_declaration_seq_opt (parser);
10330 }
10331
10332 /* Parse a namespace-alias-definition.
10333
10334    namespace-alias-definition:
10335      namespace identifier = qualified-namespace-specifier ;  */
10336
10337 static void
10338 cp_parser_namespace_alias_definition (cp_parser* parser)
10339 {
10340   tree identifier;
10341   tree namespace_specifier;
10342
10343   /* Look for the `namespace' keyword.  */
10344   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10345   /* Look for the identifier.  */
10346   identifier = cp_parser_identifier (parser);
10347   if (identifier == error_mark_node)
10348     return;
10349   /* Look for the `=' token.  */
10350   cp_parser_require (parser, CPP_EQ, "`='");
10351   /* Look for the qualified-namespace-specifier.  */
10352   namespace_specifier
10353     = cp_parser_qualified_namespace_specifier (parser);
10354   /* Look for the `;' token.  */
10355   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10356
10357   /* Register the alias in the symbol table.  */
10358   do_namespace_alias (identifier, namespace_specifier);
10359 }
10360
10361 /* Parse a qualified-namespace-specifier.
10362
10363    qualified-namespace-specifier:
10364      :: [opt] nested-name-specifier [opt] namespace-name
10365
10366    Returns a NAMESPACE_DECL corresponding to the specified
10367    namespace.  */
10368
10369 static tree
10370 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10371 {
10372   /* Look for the optional `::'.  */
10373   cp_parser_global_scope_opt (parser,
10374                               /*current_scope_valid_p=*/false);
10375
10376   /* Look for the optional nested-name-specifier.  */
10377   cp_parser_nested_name_specifier_opt (parser,
10378                                        /*typename_keyword_p=*/false,
10379                                        /*check_dependency_p=*/true,
10380                                        /*type_p=*/false,
10381                                        /*is_declaration=*/true);
10382
10383   return cp_parser_namespace_name (parser);
10384 }
10385
10386 /* Parse a using-declaration.
10387
10388    using-declaration:
10389      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10390      using :: unqualified-id ;  */
10391
10392 static void
10393 cp_parser_using_declaration (cp_parser* parser)
10394 {
10395   cp_token *token;
10396   bool typename_p = false;
10397   bool global_scope_p;
10398   tree decl;
10399   tree identifier;
10400   tree qscope;
10401
10402   /* Look for the `using' keyword.  */
10403   cp_parser_require_keyword (parser, RID_USING, "`using'");
10404
10405   /* Peek at the next token.  */
10406   token = cp_lexer_peek_token (parser->lexer);
10407   /* See if it's `typename'.  */
10408   if (token->keyword == RID_TYPENAME)
10409     {
10410       /* Remember that we've seen it.  */
10411       typename_p = true;
10412       /* Consume the `typename' token.  */
10413       cp_lexer_consume_token (parser->lexer);
10414     }
10415
10416   /* Look for the optional global scope qualification.  */
10417   global_scope_p
10418     = (cp_parser_global_scope_opt (parser,
10419                                    /*current_scope_valid_p=*/false)
10420        != NULL_TREE);
10421
10422   /* If we saw `typename', or didn't see `::', then there must be a
10423      nested-name-specifier present.  */
10424   if (typename_p || !global_scope_p)
10425     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10426                                               /*check_dependency_p=*/true,
10427                                               /*type_p=*/false,
10428                                               /*is_declaration=*/true);
10429   /* Otherwise, we could be in either of the two productions.  In that
10430      case, treat the nested-name-specifier as optional.  */
10431   else
10432     qscope = cp_parser_nested_name_specifier_opt (parser,
10433                                                   /*typename_keyword_p=*/false,
10434                                                   /*check_dependency_p=*/true,
10435                                                   /*type_p=*/false,
10436                                                   /*is_declaration=*/true);
10437   if (!qscope)
10438     qscope = global_namespace;
10439
10440   /* Parse the unqualified-id.  */
10441   identifier = cp_parser_unqualified_id (parser,
10442                                          /*template_keyword_p=*/false,
10443                                          /*check_dependency_p=*/true,
10444                                          /*declarator_p=*/true);
10445
10446   /* The function we call to handle a using-declaration is different
10447      depending on what scope we are in.  */
10448   if (identifier == error_mark_node)
10449     ;
10450   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10451            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10452     /* [namespace.udecl]
10453
10454        A using declaration shall not name a template-id.  */
10455     error ("a template-id may not appear in a using-declaration");
10456   else
10457     {
10458       if (at_class_scope_p ())
10459         {
10460           /* Create the USING_DECL.  */
10461           decl = do_class_using_decl (parser->scope, identifier);
10462           /* Add it to the list of members in this class.  */
10463           finish_member_declaration (decl);
10464         }
10465       else
10466         {
10467           decl = cp_parser_lookup_name_simple (parser, identifier);
10468           if (decl == error_mark_node)
10469             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10470           else if (!at_namespace_scope_p ())
10471             do_local_using_decl (decl, qscope, identifier);
10472           else
10473             do_toplevel_using_decl (decl, qscope, identifier);
10474         }
10475     }
10476
10477   /* Look for the final `;'.  */
10478   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10479 }
10480
10481 /* Parse a using-directive.
10482
10483    using-directive:
10484      using namespace :: [opt] nested-name-specifier [opt]
10485        namespace-name ;  */
10486
10487 static void
10488 cp_parser_using_directive (cp_parser* parser)
10489 {
10490   tree namespace_decl;
10491   tree attribs;
10492
10493   /* Look for the `using' keyword.  */
10494   cp_parser_require_keyword (parser, RID_USING, "`using'");
10495   /* And the `namespace' keyword.  */
10496   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10497   /* Look for the optional `::' operator.  */
10498   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10499   /* And the optional nested-name-specifier.  */
10500   cp_parser_nested_name_specifier_opt (parser,
10501                                        /*typename_keyword_p=*/false,
10502                                        /*check_dependency_p=*/true,
10503                                        /*type_p=*/false,
10504                                        /*is_declaration=*/true);
10505   /* Get the namespace being used.  */
10506   namespace_decl = cp_parser_namespace_name (parser);
10507   /* And any specified attributes.  */
10508   attribs = cp_parser_attributes_opt (parser);
10509   /* Update the symbol table.  */
10510   parse_using_directive (namespace_decl, attribs);
10511   /* Look for the final `;'.  */
10512   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10513 }
10514
10515 /* Parse an asm-definition.
10516
10517    asm-definition:
10518      asm ( string-literal ) ;
10519
10520    GNU Extension:
10521
10522    asm-definition:
10523      asm volatile [opt] ( string-literal ) ;
10524      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10525      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10526                           : asm-operand-list [opt] ) ;
10527      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10528                           : asm-operand-list [opt]
10529                           : asm-operand-list [opt] ) ;  */
10530
10531 static void
10532 cp_parser_asm_definition (cp_parser* parser)
10533 {
10534   tree string;
10535   tree outputs = NULL_TREE;
10536   tree inputs = NULL_TREE;
10537   tree clobbers = NULL_TREE;
10538   tree asm_stmt;
10539   bool volatile_p = false;
10540   bool extended_p = false;
10541
10542   /* Look for the `asm' keyword.  */
10543   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10544   /* See if the next token is `volatile'.  */
10545   if (cp_parser_allow_gnu_extensions_p (parser)
10546       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10547     {
10548       /* Remember that we saw the `volatile' keyword.  */
10549       volatile_p = true;
10550       /* Consume the token.  */
10551       cp_lexer_consume_token (parser->lexer);
10552     }
10553   /* Look for the opening `('.  */
10554   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10555     return;
10556   /* Look for the string.  */
10557   string = cp_parser_string_literal (parser, false, false);
10558   if (string == error_mark_node)
10559     {
10560       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10561                                              /*consume_paren=*/true);
10562       return;
10563     }
10564
10565   /* If we're allowing GNU extensions, check for the extended assembly
10566      syntax.  Unfortunately, the `:' tokens need not be separated by
10567      a space in C, and so, for compatibility, we tolerate that here
10568      too.  Doing that means that we have to treat the `::' operator as
10569      two `:' tokens.  */
10570   if (cp_parser_allow_gnu_extensions_p (parser)
10571       && at_function_scope_p ()
10572       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10573           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10574     {
10575       bool inputs_p = false;
10576       bool clobbers_p = false;
10577
10578       /* The extended syntax was used.  */
10579       extended_p = true;
10580
10581       /* Look for outputs.  */
10582       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10583         {
10584           /* Consume the `:'.  */
10585           cp_lexer_consume_token (parser->lexer);
10586           /* Parse the output-operands.  */
10587           if (cp_lexer_next_token_is_not (parser->lexer,
10588                                           CPP_COLON)
10589               && cp_lexer_next_token_is_not (parser->lexer,
10590                                              CPP_SCOPE)
10591               && cp_lexer_next_token_is_not (parser->lexer,
10592                                              CPP_CLOSE_PAREN))
10593             outputs = cp_parser_asm_operand_list (parser);
10594         }
10595       /* If the next token is `::', there are no outputs, and the
10596          next token is the beginning of the inputs.  */
10597       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10598         /* The inputs are coming next.  */
10599         inputs_p = true;
10600
10601       /* Look for inputs.  */
10602       if (inputs_p
10603           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10604         {
10605           /* Consume the `:' or `::'.  */
10606           cp_lexer_consume_token (parser->lexer);
10607           /* Parse the output-operands.  */
10608           if (cp_lexer_next_token_is_not (parser->lexer,
10609                                           CPP_COLON)
10610               && cp_lexer_next_token_is_not (parser->lexer,
10611                                              CPP_CLOSE_PAREN))
10612             inputs = cp_parser_asm_operand_list (parser);
10613         }
10614       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10615         /* The clobbers are coming next.  */
10616         clobbers_p = true;
10617
10618       /* Look for clobbers.  */
10619       if (clobbers_p
10620           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10621         {
10622           /* Consume the `:' or `::'.  */
10623           cp_lexer_consume_token (parser->lexer);
10624           /* Parse the clobbers.  */
10625           if (cp_lexer_next_token_is_not (parser->lexer,
10626                                           CPP_CLOSE_PAREN))
10627             clobbers = cp_parser_asm_clobber_list (parser);
10628         }
10629     }
10630   /* Look for the closing `)'.  */
10631   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10632     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10633                                            /*consume_paren=*/true);
10634   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10635
10636   /* Create the ASM_EXPR.  */
10637   if (at_function_scope_p ())
10638     {
10639       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10640                                   inputs, clobbers);
10641       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10642       if (!extended_p)
10643         {
10644           tree temp = asm_stmt;
10645           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10646             temp = TREE_OPERAND (temp, 0);
10647
10648           ASM_INPUT_P (temp) = 1;
10649         }
10650     }
10651   else
10652     assemble_asm (string);
10653 }
10654
10655 /* Declarators [gram.dcl.decl] */
10656
10657 /* Parse an init-declarator.
10658
10659    init-declarator:
10660      declarator initializer [opt]
10661
10662    GNU Extension:
10663
10664    init-declarator:
10665      declarator asm-specification [opt] attributes [opt] initializer [opt]
10666
10667    function-definition:
10668      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10669        function-body
10670      decl-specifier-seq [opt] declarator function-try-block
10671
10672    GNU Extension:
10673
10674    function-definition:
10675      __extension__ function-definition
10676
10677    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10678    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10679    then this declarator appears in a class scope.  The new DECL created
10680    by this declarator is returned.
10681
10682    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10683    for a function-definition here as well.  If the declarator is a
10684    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10685    be TRUE upon return.  By that point, the function-definition will
10686    have been completely parsed.
10687
10688    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10689    is FALSE.  */
10690
10691 static tree
10692 cp_parser_init_declarator (cp_parser* parser,
10693                            cp_decl_specifier_seq *decl_specifiers,
10694                            bool function_definition_allowed_p,
10695                            bool member_p,
10696                            int declares_class_or_enum,
10697                            bool* function_definition_p)
10698 {
10699   cp_token *token;
10700   cp_declarator *declarator;
10701   tree prefix_attributes;
10702   tree attributes;
10703   tree asm_specification;
10704   tree initializer;
10705   tree decl = NULL_TREE;
10706   tree scope;
10707   bool is_initialized;
10708   bool is_parenthesized_init;
10709   bool is_non_constant_init;
10710   int ctor_dtor_or_conv_p;
10711   bool friend_p;
10712   tree pushed_scope = NULL;
10713
10714   /* Gather the attributes that were provided with the
10715      decl-specifiers.  */
10716   prefix_attributes = decl_specifiers->attributes;
10717
10718   /* Assume that this is not the declarator for a function
10719      definition.  */
10720   if (function_definition_p)
10721     *function_definition_p = false;
10722
10723   /* Defer access checks while parsing the declarator; we cannot know
10724      what names are accessible until we know what is being
10725      declared.  */
10726   resume_deferring_access_checks ();
10727
10728   /* Parse the declarator.  */
10729   declarator
10730     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10731                             &ctor_dtor_or_conv_p,
10732                             /*parenthesized_p=*/NULL,
10733                             /*member_p=*/false);
10734   /* Gather up the deferred checks.  */
10735   stop_deferring_access_checks ();
10736
10737   /* If the DECLARATOR was erroneous, there's no need to go
10738      further.  */
10739   if (declarator == cp_error_declarator)
10740     return error_mark_node;
10741
10742   if (declares_class_or_enum & 2)
10743     cp_parser_check_for_definition_in_return_type (declarator,
10744                                                    decl_specifiers->type);
10745
10746   /* Figure out what scope the entity declared by the DECLARATOR is
10747      located in.  `grokdeclarator' sometimes changes the scope, so
10748      we compute it now.  */
10749   scope = get_scope_of_declarator (declarator);
10750
10751   /* If we're allowing GNU extensions, look for an asm-specification
10752      and attributes.  */
10753   if (cp_parser_allow_gnu_extensions_p (parser))
10754     {
10755       /* Look for an asm-specification.  */
10756       asm_specification = cp_parser_asm_specification_opt (parser);
10757       /* And attributes.  */
10758       attributes = cp_parser_attributes_opt (parser);
10759     }
10760   else
10761     {
10762       asm_specification = NULL_TREE;
10763       attributes = NULL_TREE;
10764     }
10765
10766   /* Peek at the next token.  */
10767   token = cp_lexer_peek_token (parser->lexer);
10768   /* Check to see if the token indicates the start of a
10769      function-definition.  */
10770   if (cp_parser_token_starts_function_definition_p (token))
10771     {
10772       if (!function_definition_allowed_p)
10773         {
10774           /* If a function-definition should not appear here, issue an
10775              error message.  */
10776           cp_parser_error (parser,
10777                            "a function-definition is not allowed here");
10778           return error_mark_node;
10779         }
10780       else
10781         {
10782           /* Neither attributes nor an asm-specification are allowed
10783              on a function-definition.  */
10784           if (asm_specification)
10785             error ("an asm-specification is not allowed on a function-definition");
10786           if (attributes)
10787             error ("attributes are not allowed on a function-definition");
10788           /* This is a function-definition.  */
10789           *function_definition_p = true;
10790
10791           /* Parse the function definition.  */
10792           if (member_p)
10793             decl = cp_parser_save_member_function_body (parser,
10794                                                         decl_specifiers,
10795                                                         declarator,
10796                                                         prefix_attributes);
10797           else
10798             decl
10799               = (cp_parser_function_definition_from_specifiers_and_declarator
10800                  (parser, decl_specifiers, prefix_attributes, declarator));
10801
10802           return decl;
10803         }
10804     }
10805
10806   /* [dcl.dcl]
10807
10808      Only in function declarations for constructors, destructors, and
10809      type conversions can the decl-specifier-seq be omitted.
10810
10811      We explicitly postpone this check past the point where we handle
10812      function-definitions because we tolerate function-definitions
10813      that are missing their return types in some modes.  */
10814   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10815     {
10816       cp_parser_error (parser,
10817                        "expected constructor, destructor, or type conversion");
10818       return error_mark_node;
10819     }
10820
10821   /* An `=' or an `(' indicates an initializer.  */
10822   is_initialized = (token->type == CPP_EQ
10823                      || token->type == CPP_OPEN_PAREN);
10824   /* If the init-declarator isn't initialized and isn't followed by a
10825      `,' or `;', it's not a valid init-declarator.  */
10826   if (!is_initialized
10827       && token->type != CPP_COMMA
10828       && token->type != CPP_SEMICOLON)
10829     {
10830       cp_parser_error (parser, "expected initializer");
10831       return error_mark_node;
10832     }
10833
10834   /* Because start_decl has side-effects, we should only call it if we
10835      know we're going ahead.  By this point, we know that we cannot
10836      possibly be looking at any other construct.  */
10837   cp_parser_commit_to_tentative_parse (parser);
10838
10839   /* If the decl specifiers were bad, issue an error now that we're
10840      sure this was intended to be a declarator.  Then continue
10841      declaring the variable(s), as int, to try to cut down on further
10842      errors.  */
10843   if (decl_specifiers->any_specifiers_p
10844       && decl_specifiers->type == error_mark_node)
10845     {
10846       cp_parser_error (parser, "invalid type in declaration");
10847       decl_specifiers->type = integer_type_node;
10848     }
10849
10850   /* Check to see whether or not this declaration is a friend.  */
10851   friend_p = cp_parser_friend_p (decl_specifiers);
10852
10853   /* Check that the number of template-parameter-lists is OK.  */
10854   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10855     return error_mark_node;
10856
10857   /* Enter the newly declared entry in the symbol table.  If we're
10858      processing a declaration in a class-specifier, we wait until
10859      after processing the initializer.  */
10860   if (!member_p)
10861     {
10862       if (parser->in_unbraced_linkage_specification_p)
10863         {
10864           decl_specifiers->storage_class = sc_extern;
10865           have_extern_spec = false;
10866         }
10867       decl = start_decl (declarator, decl_specifiers,
10868                          is_initialized, attributes, prefix_attributes,
10869                          &pushed_scope);
10870     }
10871   else if (scope)
10872     /* Enter the SCOPE.  That way unqualified names appearing in the
10873        initializer will be looked up in SCOPE.  */
10874     pushed_scope = push_scope (scope);
10875
10876   /* Perform deferred access control checks, now that we know in which
10877      SCOPE the declared entity resides.  */
10878   if (!member_p && decl)
10879     {
10880       tree saved_current_function_decl = NULL_TREE;
10881
10882       /* If the entity being declared is a function, pretend that we
10883          are in its scope.  If it is a `friend', it may have access to
10884          things that would not otherwise be accessible.  */
10885       if (TREE_CODE (decl) == FUNCTION_DECL)
10886         {
10887           saved_current_function_decl = current_function_decl;
10888           current_function_decl = decl;
10889         }
10890
10891       /* Perform the access control checks for the declarator and the
10892          the decl-specifiers.  */
10893       perform_deferred_access_checks ();
10894
10895       /* Restore the saved value.  */
10896       if (TREE_CODE (decl) == FUNCTION_DECL)
10897         current_function_decl = saved_current_function_decl;
10898     }
10899
10900   /* Parse the initializer.  */
10901   if (is_initialized)
10902     initializer = cp_parser_initializer (parser,
10903                                          &is_parenthesized_init,
10904                                          &is_non_constant_init);
10905   else
10906     {
10907       initializer = NULL_TREE;
10908       is_parenthesized_init = false;
10909       is_non_constant_init = true;
10910     }
10911
10912   /* The old parser allows attributes to appear after a parenthesized
10913      initializer.  Mark Mitchell proposed removing this functionality
10914      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10915      attributes -- but ignores them.  */
10916   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10917     if (cp_parser_attributes_opt (parser))
10918       warning (OPT_Wattributes,
10919                "attributes after parenthesized initializer ignored");
10920
10921   /* For an in-class declaration, use `grokfield' to create the
10922      declaration.  */
10923   if (member_p)
10924     {
10925       if (pushed_scope)
10926         {
10927           pop_scope (pushed_scope);
10928           pushed_scope = false;
10929         }
10930       decl = grokfield (declarator, decl_specifiers,
10931                         initializer, /*asmspec=*/NULL_TREE,
10932                         /*attributes=*/NULL_TREE);
10933       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10934         cp_parser_save_default_args (parser, decl);
10935     }
10936
10937   /* Finish processing the declaration.  But, skip friend
10938      declarations.  */
10939   if (!friend_p && decl && decl != error_mark_node)
10940     {
10941       cp_finish_decl (decl,
10942                       initializer,
10943                       asm_specification,
10944                       /* If the initializer is in parentheses, then this is
10945                          a direct-initialization, which means that an
10946                          `explicit' constructor is OK.  Otherwise, an
10947                          `explicit' constructor cannot be used.  */
10948                       ((is_parenthesized_init || !is_initialized)
10949                      ? 0 : LOOKUP_ONLYCONVERTING));
10950     }
10951   if (!friend_p && pushed_scope)
10952     pop_scope (pushed_scope);
10953
10954   /* Remember whether or not variables were initialized by
10955      constant-expressions.  */
10956   if (decl && TREE_CODE (decl) == VAR_DECL
10957       && is_initialized && !is_non_constant_init)
10958     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10959
10960   return decl;
10961 }
10962
10963 /* Parse a declarator.
10964
10965    declarator:
10966      direct-declarator
10967      ptr-operator declarator
10968
10969    abstract-declarator:
10970      ptr-operator abstract-declarator [opt]
10971      direct-abstract-declarator
10972
10973    GNU Extensions:
10974
10975    declarator:
10976      attributes [opt] direct-declarator
10977      attributes [opt] ptr-operator declarator
10978
10979    abstract-declarator:
10980      attributes [opt] ptr-operator abstract-declarator [opt]
10981      attributes [opt] direct-abstract-declarator
10982
10983    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10984    detect constructor, destructor or conversion operators. It is set
10985    to -1 if the declarator is a name, and +1 if it is a
10986    function. Otherwise it is set to zero. Usually you just want to
10987    test for >0, but internally the negative value is used.
10988
10989    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10990    a decl-specifier-seq unless it declares a constructor, destructor,
10991    or conversion.  It might seem that we could check this condition in
10992    semantic analysis, rather than parsing, but that makes it difficult
10993    to handle something like `f()'.  We want to notice that there are
10994    no decl-specifiers, and therefore realize that this is an
10995    expression, not a declaration.)
10996
10997    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10998    the declarator is a direct-declarator of the form "(...)".
10999
11000    MEMBER_P is true iff this declarator is a member-declarator.  */
11001
11002 static cp_declarator *
11003 cp_parser_declarator (cp_parser* parser,
11004                       cp_parser_declarator_kind dcl_kind,
11005                       int* ctor_dtor_or_conv_p,
11006                       bool* parenthesized_p,
11007                       bool member_p)
11008 {
11009   cp_token *token;
11010   cp_declarator *declarator;
11011   enum tree_code code;
11012   cp_cv_quals cv_quals;
11013   tree class_type;
11014   tree attributes = NULL_TREE;
11015
11016   /* Assume this is not a constructor, destructor, or type-conversion
11017      operator.  */
11018   if (ctor_dtor_or_conv_p)
11019     *ctor_dtor_or_conv_p = 0;
11020
11021   if (cp_parser_allow_gnu_extensions_p (parser))
11022     attributes = cp_parser_attributes_opt (parser);
11023
11024   /* Peek at the next token.  */
11025   token = cp_lexer_peek_token (parser->lexer);
11026
11027   /* Check for the ptr-operator production.  */
11028   cp_parser_parse_tentatively (parser);
11029   /* Parse the ptr-operator.  */
11030   code = cp_parser_ptr_operator (parser,
11031                                  &class_type,
11032                                  &cv_quals);
11033   /* If that worked, then we have a ptr-operator.  */
11034   if (cp_parser_parse_definitely (parser))
11035     {
11036       /* If a ptr-operator was found, then this declarator was not
11037          parenthesized.  */
11038       if (parenthesized_p)
11039         *parenthesized_p = true;
11040       /* The dependent declarator is optional if we are parsing an
11041          abstract-declarator.  */
11042       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11043         cp_parser_parse_tentatively (parser);
11044
11045       /* Parse the dependent declarator.  */
11046       declarator = cp_parser_declarator (parser, dcl_kind,
11047                                          /*ctor_dtor_or_conv_p=*/NULL,
11048                                          /*parenthesized_p=*/NULL,
11049                                          /*member_p=*/false);
11050
11051       /* If we are parsing an abstract-declarator, we must handle the
11052          case where the dependent declarator is absent.  */
11053       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11054           && !cp_parser_parse_definitely (parser))
11055         declarator = NULL;
11056
11057       /* Build the representation of the ptr-operator.  */
11058       if (class_type)
11059         declarator = make_ptrmem_declarator (cv_quals,
11060                                              class_type,
11061                                              declarator);
11062       else if (code == INDIRECT_REF)
11063         declarator = make_pointer_declarator (cv_quals, declarator);
11064       else
11065         declarator = make_reference_declarator (cv_quals, declarator);
11066     }
11067   /* Everything else is a direct-declarator.  */
11068   else
11069     {
11070       if (parenthesized_p)
11071         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11072                                                    CPP_OPEN_PAREN);
11073       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11074                                                 ctor_dtor_or_conv_p,
11075                                                 member_p);
11076     }
11077
11078   if (attributes && declarator != cp_error_declarator)
11079     declarator->attributes = attributes;
11080
11081   return declarator;
11082 }
11083
11084 /* Parse a direct-declarator or direct-abstract-declarator.
11085
11086    direct-declarator:
11087      declarator-id
11088      direct-declarator ( parameter-declaration-clause )
11089        cv-qualifier-seq [opt]
11090        exception-specification [opt]
11091      direct-declarator [ constant-expression [opt] ]
11092      ( declarator )
11093
11094    direct-abstract-declarator:
11095      direct-abstract-declarator [opt]
11096        ( parameter-declaration-clause )
11097        cv-qualifier-seq [opt]
11098        exception-specification [opt]
11099      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11100      ( abstract-declarator )
11101
11102    Returns a representation of the declarator.  DCL_KIND is
11103    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11104    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11105    we are parsing a direct-declarator.  It is
11106    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11107    of ambiguity we prefer an abstract declarator, as per
11108    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11109    cp_parser_declarator.  */
11110
11111 static cp_declarator *
11112 cp_parser_direct_declarator (cp_parser* parser,
11113                              cp_parser_declarator_kind dcl_kind,
11114                              int* ctor_dtor_or_conv_p,
11115                              bool member_p)
11116 {
11117   cp_token *token;
11118   cp_declarator *declarator = NULL;
11119   tree scope = NULL_TREE;
11120   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11121   bool saved_in_declarator_p = parser->in_declarator_p;
11122   bool first = true;
11123   tree pushed_scope = NULL_TREE;
11124
11125   while (true)
11126     {
11127       /* Peek at the next token.  */
11128       token = cp_lexer_peek_token (parser->lexer);
11129       if (token->type == CPP_OPEN_PAREN)
11130         {
11131           /* This is either a parameter-declaration-clause, or a
11132              parenthesized declarator. When we know we are parsing a
11133              named declarator, it must be a parenthesized declarator
11134              if FIRST is true. For instance, `(int)' is a
11135              parameter-declaration-clause, with an omitted
11136              direct-abstract-declarator. But `((*))', is a
11137              parenthesized abstract declarator. Finally, when T is a
11138              template parameter `(T)' is a
11139              parameter-declaration-clause, and not a parenthesized
11140              named declarator.
11141
11142              We first try and parse a parameter-declaration-clause,
11143              and then try a nested declarator (if FIRST is true).
11144
11145              It is not an error for it not to be a
11146              parameter-declaration-clause, even when FIRST is
11147              false. Consider,
11148
11149                int i (int);
11150                int i (3);
11151
11152              The first is the declaration of a function while the
11153              second is a the definition of a variable, including its
11154              initializer.
11155
11156              Having seen only the parenthesis, we cannot know which of
11157              these two alternatives should be selected.  Even more
11158              complex are examples like:
11159
11160                int i (int (a));
11161                int i (int (3));
11162
11163              The former is a function-declaration; the latter is a
11164              variable initialization.
11165
11166              Thus again, we try a parameter-declaration-clause, and if
11167              that fails, we back out and return.  */
11168
11169           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11170             {
11171               cp_parameter_declarator *params;
11172               unsigned saved_num_template_parameter_lists;
11173
11174               /* In a member-declarator, the only valid interpretation
11175                  of a parenthesis is the start of a
11176                  parameter-declaration-clause.  (It is invalid to
11177                  initialize a static data member with a parenthesized
11178                  initializer; only the "=" form of initialization is
11179                  permitted.)  */
11180               if (!member_p)
11181                 cp_parser_parse_tentatively (parser);
11182
11183               /* Consume the `('.  */
11184               cp_lexer_consume_token (parser->lexer);
11185               if (first)
11186                 {
11187                   /* If this is going to be an abstract declarator, we're
11188                      in a declarator and we can't have default args.  */
11189                   parser->default_arg_ok_p = false;
11190                   parser->in_declarator_p = true;
11191                 }
11192
11193               /* Inside the function parameter list, surrounding
11194                  template-parameter-lists do not apply.  */
11195               saved_num_template_parameter_lists
11196                 = parser->num_template_parameter_lists;
11197               parser->num_template_parameter_lists = 0;
11198
11199               /* Parse the parameter-declaration-clause.  */
11200               params = cp_parser_parameter_declaration_clause (parser);
11201
11202               parser->num_template_parameter_lists
11203                 = saved_num_template_parameter_lists;
11204
11205               /* If all went well, parse the cv-qualifier-seq and the
11206                  exception-specification.  */
11207               if (member_p || cp_parser_parse_definitely (parser))
11208                 {
11209                   cp_cv_quals cv_quals;
11210                   tree exception_specification;
11211
11212                   if (ctor_dtor_or_conv_p)
11213                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11214                   first = false;
11215                   /* Consume the `)'.  */
11216                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11217
11218                   /* Parse the cv-qualifier-seq.  */
11219                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11220                   /* And the exception-specification.  */
11221                   exception_specification
11222                     = cp_parser_exception_specification_opt (parser);
11223
11224                   /* Create the function-declarator.  */
11225                   declarator = make_call_declarator (declarator,
11226                                                      params,
11227                                                      cv_quals,
11228                                                      exception_specification);
11229                   /* Any subsequent parameter lists are to do with
11230                      return type, so are not those of the declared
11231                      function.  */
11232                   parser->default_arg_ok_p = false;
11233
11234                   /* Repeat the main loop.  */
11235                   continue;
11236                 }
11237             }
11238
11239           /* If this is the first, we can try a parenthesized
11240              declarator.  */
11241           if (first)
11242             {
11243               bool saved_in_type_id_in_expr_p;
11244
11245               parser->default_arg_ok_p = saved_default_arg_ok_p;
11246               parser->in_declarator_p = saved_in_declarator_p;
11247
11248               /* Consume the `('.  */
11249               cp_lexer_consume_token (parser->lexer);
11250               /* Parse the nested declarator.  */
11251               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11252               parser->in_type_id_in_expr_p = true;
11253               declarator
11254                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11255                                         /*parenthesized_p=*/NULL,
11256                                         member_p);
11257               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11258               first = false;
11259               /* Expect a `)'.  */
11260               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11261                 declarator = cp_error_declarator;
11262               if (declarator == cp_error_declarator)
11263                 break;
11264
11265               goto handle_declarator;
11266             }
11267           /* Otherwise, we must be done.  */
11268           else
11269             break;
11270         }
11271       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11272                && token->type == CPP_OPEN_SQUARE)
11273         {
11274           /* Parse an array-declarator.  */
11275           tree bounds;
11276
11277           if (ctor_dtor_or_conv_p)
11278             *ctor_dtor_or_conv_p = 0;
11279
11280           first = false;
11281           parser->default_arg_ok_p = false;
11282           parser->in_declarator_p = true;
11283           /* Consume the `['.  */
11284           cp_lexer_consume_token (parser->lexer);
11285           /* Peek at the next token.  */
11286           token = cp_lexer_peek_token (parser->lexer);
11287           /* If the next token is `]', then there is no
11288              constant-expression.  */
11289           if (token->type != CPP_CLOSE_SQUARE)
11290             {
11291               bool non_constant_p;
11292
11293               bounds
11294                 = cp_parser_constant_expression (parser,
11295                                                  /*allow_non_constant=*/true,
11296                                                  &non_constant_p);
11297               if (!non_constant_p)
11298                 bounds = fold_non_dependent_expr (bounds);
11299               /* Normally, the array bound must be an integral constant
11300                  expression.  However, as an extension, we allow VLAs
11301                  in function scopes.  */
11302               else if (!at_function_scope_p ())
11303                 {
11304                   error ("array bound is not an integer constant");
11305                   bounds = error_mark_node;
11306                 }
11307             }
11308           else
11309             bounds = NULL_TREE;
11310           /* Look for the closing `]'.  */
11311           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11312             {
11313               declarator = cp_error_declarator;
11314               break;
11315             }
11316
11317           declarator = make_array_declarator (declarator, bounds);
11318         }
11319       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11320         {
11321           tree qualifying_scope;
11322           tree unqualified_name;
11323
11324           /* Parse a declarator-id */
11325           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11326             cp_parser_parse_tentatively (parser);
11327           unqualified_name = cp_parser_declarator_id (parser);
11328           qualifying_scope = parser->scope;
11329           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11330             {
11331               if (!cp_parser_parse_definitely (parser))
11332                 unqualified_name = error_mark_node;
11333               else if (qualifying_scope
11334                        || (TREE_CODE (unqualified_name)
11335                            != IDENTIFIER_NODE))
11336                 {
11337                   cp_parser_error (parser, "expected unqualified-id");
11338                   unqualified_name = error_mark_node;
11339                 }
11340             }
11341
11342           if (unqualified_name == error_mark_node)
11343             {
11344               declarator = cp_error_declarator;
11345               break;
11346             }
11347
11348           if (qualifying_scope && at_namespace_scope_p ()
11349               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11350             {
11351               /* In the declaration of a member of a template class
11352                  outside of the class itself, the SCOPE will sometimes
11353                  be a TYPENAME_TYPE.  For example, given:
11354
11355                  template <typename T>
11356                  int S<T>::R::i = 3;
11357
11358                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11359                  this context, we must resolve S<T>::R to an ordinary
11360                  type, rather than a typename type.
11361
11362                  The reason we normally avoid resolving TYPENAME_TYPEs
11363                  is that a specialization of `S' might render
11364                  `S<T>::R' not a type.  However, if `S' is
11365                  specialized, then this `i' will not be used, so there
11366                  is no harm in resolving the types here.  */
11367               tree type;
11368
11369               /* Resolve the TYPENAME_TYPE.  */
11370               type = resolve_typename_type (qualifying_scope,
11371                                             /*only_current_p=*/false);
11372               /* If that failed, the declarator is invalid.  */
11373               if (type == error_mark_node)
11374                 error ("%<%T::%D%> is not a type",
11375                        TYPE_CONTEXT (qualifying_scope),
11376                        TYPE_IDENTIFIER (qualifying_scope));
11377               qualifying_scope = type;
11378             }
11379
11380           declarator = make_id_declarator (qualifying_scope,
11381                                            unqualified_name);
11382           declarator->id_loc = token->location;
11383           if (unqualified_name)
11384             {
11385               tree class_type;
11386
11387               if (qualifying_scope
11388                   && CLASS_TYPE_P (qualifying_scope))
11389                 class_type = qualifying_scope;
11390               else
11391                 class_type = current_class_type;
11392
11393               if (class_type)
11394                 {
11395                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11396                     declarator->u.id.sfk = sfk_destructor;
11397                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11398                     declarator->u.id.sfk = sfk_conversion;
11399                   else if (/* There's no way to declare a constructor
11400                               for an anonymous type, even if the type
11401                               got a name for linkage purposes.  */
11402                            !TYPE_WAS_ANONYMOUS (class_type)
11403                            && (constructor_name_p (unqualified_name,
11404                                                    class_type)
11405                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11406                                    && (same_type_p
11407                                        (TREE_TYPE (unqualified_name),
11408                                         class_type)))))
11409                     declarator->u.id.sfk = sfk_constructor;
11410
11411                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11412                     *ctor_dtor_or_conv_p = -1;
11413                   if (qualifying_scope
11414                       && TREE_CODE (unqualified_name) == TYPE_DECL
11415                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11416                     {
11417                       error ("invalid use of constructor as a template");
11418                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11419                               "the constructor in a qualified name",
11420                               class_type,
11421                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11422                               class_type, class_type);
11423                     }
11424                 }
11425             }
11426
11427         handle_declarator:;
11428           scope = get_scope_of_declarator (declarator);
11429           if (scope)
11430             /* Any names that appear after the declarator-id for a
11431                member are looked up in the containing scope.  */
11432             pushed_scope = push_scope (scope);
11433           parser->in_declarator_p = true;
11434           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11435               || (declarator && declarator->kind == cdk_id))
11436             /* Default args are only allowed on function
11437                declarations.  */
11438             parser->default_arg_ok_p = saved_default_arg_ok_p;
11439           else
11440             parser->default_arg_ok_p = false;
11441
11442           first = false;
11443         }
11444       /* We're done.  */
11445       else
11446         break;
11447     }
11448
11449   /* For an abstract declarator, we might wind up with nothing at this
11450      point.  That's an error; the declarator is not optional.  */
11451   if (!declarator)
11452     cp_parser_error (parser, "expected declarator");
11453
11454   /* If we entered a scope, we must exit it now.  */
11455   if (pushed_scope)
11456     pop_scope (pushed_scope);
11457
11458   parser->default_arg_ok_p = saved_default_arg_ok_p;
11459   parser->in_declarator_p = saved_in_declarator_p;
11460
11461   return declarator;
11462 }
11463
11464 /* Parse a ptr-operator.
11465
11466    ptr-operator:
11467      * cv-qualifier-seq [opt]
11468      &
11469      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11470
11471    GNU Extension:
11472
11473    ptr-operator:
11474      & cv-qualifier-seq [opt]
11475
11476    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11477    Returns ADDR_EXPR if a reference was used.  In the case of a
11478    pointer-to-member, *TYPE is filled in with the TYPE containing the
11479    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11480    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11481    ERROR_MARK if an error occurred.  */
11482
11483 static enum tree_code
11484 cp_parser_ptr_operator (cp_parser* parser,
11485                         tree* type,
11486                         cp_cv_quals *cv_quals)
11487 {
11488   enum tree_code code = ERROR_MARK;
11489   cp_token *token;
11490
11491   /* Assume that it's not a pointer-to-member.  */
11492   *type = NULL_TREE;
11493   /* And that there are no cv-qualifiers.  */
11494   *cv_quals = TYPE_UNQUALIFIED;
11495
11496   /* Peek at the next token.  */
11497   token = cp_lexer_peek_token (parser->lexer);
11498   /* If it's a `*' or `&' we have a pointer or reference.  */
11499   if (token->type == CPP_MULT || token->type == CPP_AND)
11500     {
11501       /* Remember which ptr-operator we were processing.  */
11502       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11503
11504       /* Consume the `*' or `&'.  */
11505       cp_lexer_consume_token (parser->lexer);
11506
11507       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11508          `&', if we are allowing GNU extensions.  (The only qualifier
11509          that can legally appear after `&' is `restrict', but that is
11510          enforced during semantic analysis.  */
11511       if (code == INDIRECT_REF
11512           || cp_parser_allow_gnu_extensions_p (parser))
11513         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11514     }
11515   else
11516     {
11517       /* Try the pointer-to-member case.  */
11518       cp_parser_parse_tentatively (parser);
11519       /* Look for the optional `::' operator.  */
11520       cp_parser_global_scope_opt (parser,
11521                                   /*current_scope_valid_p=*/false);
11522       /* Look for the nested-name specifier.  */
11523       cp_parser_nested_name_specifier (parser,
11524                                        /*typename_keyword_p=*/false,
11525                                        /*check_dependency_p=*/true,
11526                                        /*type_p=*/false,
11527                                        /*is_declaration=*/false);
11528       /* If we found it, and the next token is a `*', then we are
11529          indeed looking at a pointer-to-member operator.  */
11530       if (!cp_parser_error_occurred (parser)
11531           && cp_parser_require (parser, CPP_MULT, "`*'"))
11532         {
11533           /* The type of which the member is a member is given by the
11534              current SCOPE.  */
11535           *type = parser->scope;
11536           /* The next name will not be qualified.  */
11537           parser->scope = NULL_TREE;
11538           parser->qualifying_scope = NULL_TREE;
11539           parser->object_scope = NULL_TREE;
11540           /* Indicate that the `*' operator was used.  */
11541           code = INDIRECT_REF;
11542           /* Look for the optional cv-qualifier-seq.  */
11543           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11544         }
11545       /* If that didn't work we don't have a ptr-operator.  */
11546       if (!cp_parser_parse_definitely (parser))
11547         cp_parser_error (parser, "expected ptr-operator");
11548     }
11549
11550   return code;
11551 }
11552
11553 /* Parse an (optional) cv-qualifier-seq.
11554
11555    cv-qualifier-seq:
11556      cv-qualifier cv-qualifier-seq [opt]
11557
11558    cv-qualifier:
11559      const
11560      volatile
11561
11562    GNU Extension:
11563
11564    cv-qualifier:
11565      __restrict__
11566
11567    Returns a bitmask representing the cv-qualifiers.  */
11568
11569 static cp_cv_quals
11570 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11571 {
11572   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11573
11574   while (true)
11575     {
11576       cp_token *token;
11577       cp_cv_quals cv_qualifier;
11578
11579       /* Peek at the next token.  */
11580       token = cp_lexer_peek_token (parser->lexer);
11581       /* See if it's a cv-qualifier.  */
11582       switch (token->keyword)
11583         {
11584         case RID_CONST:
11585           cv_qualifier = TYPE_QUAL_CONST;
11586           break;
11587
11588         case RID_VOLATILE:
11589           cv_qualifier = TYPE_QUAL_VOLATILE;
11590           break;
11591
11592         case RID_RESTRICT:
11593           cv_qualifier = TYPE_QUAL_RESTRICT;
11594           break;
11595
11596         default:
11597           cv_qualifier = TYPE_UNQUALIFIED;
11598           break;
11599         }
11600
11601       if (!cv_qualifier)
11602         break;
11603
11604       if (cv_quals & cv_qualifier)
11605         {
11606           error ("duplicate cv-qualifier");
11607           cp_lexer_purge_token (parser->lexer);
11608         }
11609       else
11610         {
11611           cp_lexer_consume_token (parser->lexer);
11612           cv_quals |= cv_qualifier;
11613         }
11614     }
11615
11616   return cv_quals;
11617 }
11618
11619 /* Parse a declarator-id.
11620
11621    declarator-id:
11622      id-expression
11623      :: [opt] nested-name-specifier [opt] type-name
11624
11625    In the `id-expression' case, the value returned is as for
11626    cp_parser_id_expression if the id-expression was an unqualified-id.
11627    If the id-expression was a qualified-id, then a SCOPE_REF is
11628    returned.  The first operand is the scope (either a NAMESPACE_DECL
11629    or TREE_TYPE), but the second is still just a representation of an
11630    unqualified-id.  */
11631
11632 static tree
11633 cp_parser_declarator_id (cp_parser* parser)
11634 {
11635   /* The expression must be an id-expression.  Assume that qualified
11636      names are the names of types so that:
11637
11638        template <class T>
11639        int S<T>::R::i = 3;
11640
11641      will work; we must treat `S<T>::R' as the name of a type.
11642      Similarly, assume that qualified names are templates, where
11643      required, so that:
11644
11645        template <class T>
11646        int S<T>::R<T>::i = 3;
11647
11648      will work, too.  */
11649   return cp_parser_id_expression (parser,
11650                                   /*template_keyword_p=*/false,
11651                                   /*check_dependency_p=*/false,
11652                                   /*template_p=*/NULL,
11653                                   /*declarator_p=*/true);
11654 }
11655
11656 /* Parse a type-id.
11657
11658    type-id:
11659      type-specifier-seq abstract-declarator [opt]
11660
11661    Returns the TYPE specified.  */
11662
11663 static tree
11664 cp_parser_type_id (cp_parser* parser)
11665 {
11666   cp_decl_specifier_seq type_specifier_seq;
11667   cp_declarator *abstract_declarator;
11668
11669   /* Parse the type-specifier-seq.  */
11670   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11671                                 &type_specifier_seq);
11672   if (type_specifier_seq.type == error_mark_node)
11673     return error_mark_node;
11674
11675   /* There might or might not be an abstract declarator.  */
11676   cp_parser_parse_tentatively (parser);
11677   /* Look for the declarator.  */
11678   abstract_declarator
11679     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11680                             /*parenthesized_p=*/NULL,
11681                             /*member_p=*/false);
11682   /* Check to see if there really was a declarator.  */
11683   if (!cp_parser_parse_definitely (parser))
11684     abstract_declarator = NULL;
11685
11686   return groktypename (&type_specifier_seq, abstract_declarator);
11687 }
11688
11689 /* Parse a type-specifier-seq.
11690
11691    type-specifier-seq:
11692      type-specifier type-specifier-seq [opt]
11693
11694    GNU extension:
11695
11696    type-specifier-seq:
11697      attributes type-specifier-seq [opt]
11698
11699    If IS_CONDITION is true, we are at the start of a "condition",
11700    e.g., we've just seen "if (".
11701
11702    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11703
11704 static void
11705 cp_parser_type_specifier_seq (cp_parser* parser,
11706                               bool is_condition,
11707                               cp_decl_specifier_seq *type_specifier_seq)
11708 {
11709   bool seen_type_specifier = false;
11710   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11711
11712   /* Clear the TYPE_SPECIFIER_SEQ.  */
11713   clear_decl_specs (type_specifier_seq);
11714
11715   /* Parse the type-specifiers and attributes.  */
11716   while (true)
11717     {
11718       tree type_specifier;
11719       bool is_cv_qualifier;
11720
11721       /* Check for attributes first.  */
11722       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11723         {
11724           type_specifier_seq->attributes =
11725             chainon (type_specifier_seq->attributes,
11726                      cp_parser_attributes_opt (parser));
11727           continue;
11728         }
11729
11730       /* Look for the type-specifier.  */
11731       type_specifier = cp_parser_type_specifier (parser,
11732                                                  flags,
11733                                                  type_specifier_seq,
11734                                                  /*is_declaration=*/false,
11735                                                  NULL,
11736                                                  &is_cv_qualifier);
11737       if (!type_specifier)
11738         {
11739           /* If the first type-specifier could not be found, this is not a
11740              type-specifier-seq at all.  */
11741           if (!seen_type_specifier)
11742             {
11743               cp_parser_error (parser, "expected type-specifier");
11744               type_specifier_seq->type = error_mark_node;
11745               return;
11746             }
11747           /* If subsequent type-specifiers could not be found, the
11748              type-specifier-seq is complete.  */
11749           break;
11750         }
11751
11752       seen_type_specifier = true;
11753       /* The standard says that a condition can be:
11754
11755             type-specifier-seq declarator = assignment-expression
11756
11757          However, given:
11758
11759            struct S {};
11760            if (int S = ...)
11761
11762          we should treat the "S" as a declarator, not as a
11763          type-specifier.  The standard doesn't say that explicitly for
11764          type-specifier-seq, but it does say that for
11765          decl-specifier-seq in an ordinary declaration.  Perhaps it
11766          would be clearer just to allow a decl-specifier-seq here, and
11767          then add a semantic restriction that if any decl-specifiers
11768          that are not type-specifiers appear, the program is invalid.  */
11769       if (is_condition && !is_cv_qualifier)
11770         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11771     }
11772
11773   return;
11774 }
11775
11776 /* Parse a parameter-declaration-clause.
11777
11778    parameter-declaration-clause:
11779      parameter-declaration-list [opt] ... [opt]
11780      parameter-declaration-list , ...
11781
11782    Returns a representation for the parameter declarations.  A return
11783    value of NULL indicates a parameter-declaration-clause consisting
11784    only of an ellipsis.  */
11785
11786 static cp_parameter_declarator *
11787 cp_parser_parameter_declaration_clause (cp_parser* parser)
11788 {
11789   cp_parameter_declarator *parameters;
11790   cp_token *token;
11791   bool ellipsis_p;
11792   bool is_error;
11793
11794   /* Peek at the next token.  */
11795   token = cp_lexer_peek_token (parser->lexer);
11796   /* Check for trivial parameter-declaration-clauses.  */
11797   if (token->type == CPP_ELLIPSIS)
11798     {
11799       /* Consume the `...' token.  */
11800       cp_lexer_consume_token (parser->lexer);
11801       return NULL;
11802     }
11803   else if (token->type == CPP_CLOSE_PAREN)
11804     /* There are no parameters.  */
11805     {
11806 #ifndef NO_IMPLICIT_EXTERN_C
11807       if (in_system_header && current_class_type == NULL
11808           && current_lang_name == lang_name_c)
11809         return NULL;
11810       else
11811 #endif
11812         return no_parameters;
11813     }
11814   /* Check for `(void)', too, which is a special case.  */
11815   else if (token->keyword == RID_VOID
11816            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11817                == CPP_CLOSE_PAREN))
11818     {
11819       /* Consume the `void' token.  */
11820       cp_lexer_consume_token (parser->lexer);
11821       /* There are no parameters.  */
11822       return no_parameters;
11823     }
11824
11825   /* Parse the parameter-declaration-list.  */
11826   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11827   /* If a parse error occurred while parsing the
11828      parameter-declaration-list, then the entire
11829      parameter-declaration-clause is erroneous.  */
11830   if (is_error)
11831     return NULL;
11832
11833   /* Peek at the next token.  */
11834   token = cp_lexer_peek_token (parser->lexer);
11835   /* If it's a `,', the clause should terminate with an ellipsis.  */
11836   if (token->type == CPP_COMMA)
11837     {
11838       /* Consume the `,'.  */
11839       cp_lexer_consume_token (parser->lexer);
11840       /* Expect an ellipsis.  */
11841       ellipsis_p
11842         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11843     }
11844   /* It might also be `...' if the optional trailing `,' was
11845      omitted.  */
11846   else if (token->type == CPP_ELLIPSIS)
11847     {
11848       /* Consume the `...' token.  */
11849       cp_lexer_consume_token (parser->lexer);
11850       /* And remember that we saw it.  */
11851       ellipsis_p = true;
11852     }
11853   else
11854     ellipsis_p = false;
11855
11856   /* Finish the parameter list.  */
11857   if (parameters && ellipsis_p)
11858     parameters->ellipsis_p = true;
11859
11860   return parameters;
11861 }
11862
11863 /* Parse a parameter-declaration-list.
11864
11865    parameter-declaration-list:
11866      parameter-declaration
11867      parameter-declaration-list , parameter-declaration
11868
11869    Returns a representation of the parameter-declaration-list, as for
11870    cp_parser_parameter_declaration_clause.  However, the
11871    `void_list_node' is never appended to the list.  Upon return,
11872    *IS_ERROR will be true iff an error occurred.  */
11873
11874 static cp_parameter_declarator *
11875 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11876 {
11877   cp_parameter_declarator *parameters = NULL;
11878   cp_parameter_declarator **tail = &parameters;
11879
11880   /* Assume all will go well.  */
11881   *is_error = false;
11882
11883   /* Look for more parameters.  */
11884   while (true)
11885     {
11886       cp_parameter_declarator *parameter;
11887       bool parenthesized_p;
11888       /* Parse the parameter.  */
11889       parameter
11890         = cp_parser_parameter_declaration (parser,
11891                                            /*template_parm_p=*/false,
11892                                            &parenthesized_p);
11893
11894       /* If a parse error occurred parsing the parameter declaration,
11895          then the entire parameter-declaration-list is erroneous.  */
11896       if (!parameter)
11897         {
11898           *is_error = true;
11899           parameters = NULL;
11900           break;
11901         }
11902       /* Add the new parameter to the list.  */
11903       *tail = parameter;
11904       tail = &parameter->next;
11905
11906       /* Peek at the next token.  */
11907       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11908           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11909           /* These are for Objective-C++ */
11910           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11911           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11912         /* The parameter-declaration-list is complete.  */
11913         break;
11914       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11915         {
11916           cp_token *token;
11917
11918           /* Peek at the next token.  */
11919           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11920           /* If it's an ellipsis, then the list is complete.  */
11921           if (token->type == CPP_ELLIPSIS)
11922             break;
11923           /* Otherwise, there must be more parameters.  Consume the
11924              `,'.  */
11925           cp_lexer_consume_token (parser->lexer);
11926           /* When parsing something like:
11927
11928                 int i(float f, double d)
11929
11930              we can tell after seeing the declaration for "f" that we
11931              are not looking at an initialization of a variable "i",
11932              but rather at the declaration of a function "i".
11933
11934              Due to the fact that the parsing of template arguments
11935              (as specified to a template-id) requires backtracking we
11936              cannot use this technique when inside a template argument
11937              list.  */
11938           if (!parser->in_template_argument_list_p
11939               && !parser->in_type_id_in_expr_p
11940               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11941               /* However, a parameter-declaration of the form
11942                  "foat(f)" (which is a valid declaration of a
11943                  parameter "f") can also be interpreted as an
11944                  expression (the conversion of "f" to "float").  */
11945               && !parenthesized_p)
11946             cp_parser_commit_to_tentative_parse (parser);
11947         }
11948       else
11949         {
11950           cp_parser_error (parser, "expected %<,%> or %<...%>");
11951           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11952             cp_parser_skip_to_closing_parenthesis (parser,
11953                                                    /*recovering=*/true,
11954                                                    /*or_comma=*/false,
11955                                                    /*consume_paren=*/false);
11956           break;
11957         }
11958     }
11959
11960   return parameters;
11961 }
11962
11963 /* Parse a parameter declaration.
11964
11965    parameter-declaration:
11966      decl-specifier-seq declarator
11967      decl-specifier-seq declarator = assignment-expression
11968      decl-specifier-seq abstract-declarator [opt]
11969      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11970
11971    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11972    declares a template parameter.  (In that case, a non-nested `>'
11973    token encountered during the parsing of the assignment-expression
11974    is not interpreted as a greater-than operator.)
11975
11976    Returns a representation of the parameter, or NULL if an error
11977    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11978    true iff the declarator is of the form "(p)".  */
11979
11980 static cp_parameter_declarator *
11981 cp_parser_parameter_declaration (cp_parser *parser,
11982                                  bool template_parm_p,
11983                                  bool *parenthesized_p)
11984 {
11985   int declares_class_or_enum;
11986   bool greater_than_is_operator_p;
11987   cp_decl_specifier_seq decl_specifiers;
11988   cp_declarator *declarator;
11989   tree default_argument;
11990   cp_token *token;
11991   const char *saved_message;
11992
11993   /* In a template parameter, `>' is not an operator.
11994
11995      [temp.param]
11996
11997      When parsing a default template-argument for a non-type
11998      template-parameter, the first non-nested `>' is taken as the end
11999      of the template parameter-list rather than a greater-than
12000      operator.  */
12001   greater_than_is_operator_p = !template_parm_p;
12002
12003   /* Type definitions may not appear in parameter types.  */
12004   saved_message = parser->type_definition_forbidden_message;
12005   parser->type_definition_forbidden_message
12006     = "types may not be defined in parameter types";
12007
12008   /* Parse the declaration-specifiers.  */
12009   cp_parser_decl_specifier_seq (parser,
12010                                 CP_PARSER_FLAGS_NONE,
12011                                 &decl_specifiers,
12012                                 &declares_class_or_enum);
12013   /* If an error occurred, there's no reason to attempt to parse the
12014      rest of the declaration.  */
12015   if (cp_parser_error_occurred (parser))
12016     {
12017       parser->type_definition_forbidden_message = saved_message;
12018       return NULL;
12019     }
12020
12021   /* Peek at the next token.  */
12022   token = cp_lexer_peek_token (parser->lexer);
12023   /* If the next token is a `)', `,', `=', `>', or `...', then there
12024      is no declarator.  */
12025   if (token->type == CPP_CLOSE_PAREN
12026       || token->type == CPP_COMMA
12027       || token->type == CPP_EQ
12028       || token->type == CPP_ELLIPSIS
12029       || token->type == CPP_GREATER)
12030     {
12031       declarator = NULL;
12032       if (parenthesized_p)
12033         *parenthesized_p = false;
12034     }
12035   /* Otherwise, there should be a declarator.  */
12036   else
12037     {
12038       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12039       parser->default_arg_ok_p = false;
12040
12041       /* After seeing a decl-specifier-seq, if the next token is not a
12042          "(", there is no possibility that the code is a valid
12043          expression.  Therefore, if parsing tentatively, we commit at
12044          this point.  */
12045       if (!parser->in_template_argument_list_p
12046           /* In an expression context, having seen:
12047
12048                (int((char ...
12049
12050              we cannot be sure whether we are looking at a
12051              function-type (taking a "char" as a parameter) or a cast
12052              of some object of type "char" to "int".  */
12053           && !parser->in_type_id_in_expr_p
12054           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12055           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12056         cp_parser_commit_to_tentative_parse (parser);
12057       /* Parse the declarator.  */
12058       declarator = cp_parser_declarator (parser,
12059                                          CP_PARSER_DECLARATOR_EITHER,
12060                                          /*ctor_dtor_or_conv_p=*/NULL,
12061                                          parenthesized_p,
12062                                          /*member_p=*/false);
12063       parser->default_arg_ok_p = saved_default_arg_ok_p;
12064       /* After the declarator, allow more attributes.  */
12065       decl_specifiers.attributes
12066         = chainon (decl_specifiers.attributes,
12067                    cp_parser_attributes_opt (parser));
12068     }
12069
12070   /* The restriction on defining new types applies only to the type
12071      of the parameter, not to the default argument.  */
12072   parser->type_definition_forbidden_message = saved_message;
12073
12074   /* If the next token is `=', then process a default argument.  */
12075   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12076     {
12077       bool saved_greater_than_is_operator_p;
12078       /* Consume the `='.  */
12079       cp_lexer_consume_token (parser->lexer);
12080
12081       /* If we are defining a class, then the tokens that make up the
12082          default argument must be saved and processed later.  */
12083       if (!template_parm_p && at_class_scope_p ()
12084           && TYPE_BEING_DEFINED (current_class_type))
12085         {
12086           unsigned depth = 0;
12087           cp_token *first_token;
12088           cp_token *token;
12089
12090           /* Add tokens until we have processed the entire default
12091              argument.  We add the range [first_token, token).  */
12092           first_token = cp_lexer_peek_token (parser->lexer);
12093           while (true)
12094             {
12095               bool done = false;
12096
12097               /* Peek at the next token.  */
12098               token = cp_lexer_peek_token (parser->lexer);
12099               /* What we do depends on what token we have.  */
12100               switch (token->type)
12101                 {
12102                   /* In valid code, a default argument must be
12103                      immediately followed by a `,' `)', or `...'.  */
12104                 case CPP_COMMA:
12105                 case CPP_CLOSE_PAREN:
12106                 case CPP_ELLIPSIS:
12107                   /* If we run into a non-nested `;', `}', or `]',
12108                      then the code is invalid -- but the default
12109                      argument is certainly over.  */
12110                 case CPP_SEMICOLON:
12111                 case CPP_CLOSE_BRACE:
12112                 case CPP_CLOSE_SQUARE:
12113                   if (depth == 0)
12114                     done = true;
12115                   /* Update DEPTH, if necessary.  */
12116                   else if (token->type == CPP_CLOSE_PAREN
12117                            || token->type == CPP_CLOSE_BRACE
12118                            || token->type == CPP_CLOSE_SQUARE)
12119                     --depth;
12120                   break;
12121
12122                 case CPP_OPEN_PAREN:
12123                 case CPP_OPEN_SQUARE:
12124                 case CPP_OPEN_BRACE:
12125                   ++depth;
12126                   break;
12127
12128                 case CPP_GREATER:
12129                   /* If we see a non-nested `>', and `>' is not an
12130                      operator, then it marks the end of the default
12131                      argument.  */
12132                   if (!depth && !greater_than_is_operator_p)
12133                     done = true;
12134                   break;
12135
12136                   /* If we run out of tokens, issue an error message.  */
12137                 case CPP_EOF:
12138                   error ("file ends in default argument");
12139                   done = true;
12140                   break;
12141
12142                 case CPP_NAME:
12143                 case CPP_SCOPE:
12144                   /* In these cases, we should look for template-ids.
12145                      For example, if the default argument is
12146                      `X<int, double>()', we need to do name lookup to
12147                      figure out whether or not `X' is a template; if
12148                      so, the `,' does not end the default argument.
12149
12150                      That is not yet done.  */
12151                   break;
12152
12153                 default:
12154                   break;
12155                 }
12156
12157               /* If we've reached the end, stop.  */
12158               if (done)
12159                 break;
12160
12161               /* Add the token to the token block.  */
12162               token = cp_lexer_consume_token (parser->lexer);
12163             }
12164
12165           /* Create a DEFAULT_ARG to represented the unparsed default
12166              argument.  */
12167           default_argument = make_node (DEFAULT_ARG);
12168           DEFARG_TOKENS (default_argument)
12169             = cp_token_cache_new (first_token, token);
12170           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12171         }
12172       /* Outside of a class definition, we can just parse the
12173          assignment-expression.  */
12174       else
12175         {
12176           bool saved_local_variables_forbidden_p;
12177
12178           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12179              set correctly.  */
12180           saved_greater_than_is_operator_p
12181             = parser->greater_than_is_operator_p;
12182           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12183           /* Local variable names (and the `this' keyword) may not
12184              appear in a default argument.  */
12185           saved_local_variables_forbidden_p
12186             = parser->local_variables_forbidden_p;
12187           parser->local_variables_forbidden_p = true;
12188           /* Parse the assignment-expression.  */
12189           default_argument
12190             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12191           /* Restore saved state.  */
12192           parser->greater_than_is_operator_p
12193             = saved_greater_than_is_operator_p;
12194           parser->local_variables_forbidden_p
12195             = saved_local_variables_forbidden_p;
12196         }
12197       if (!parser->default_arg_ok_p)
12198         {
12199           if (!flag_pedantic_errors)
12200             warning (0, "deprecated use of default argument for parameter of non-function");
12201           else
12202             {
12203               error ("default arguments are only permitted for function parameters");
12204               default_argument = NULL_TREE;
12205             }
12206         }
12207     }
12208   else
12209     default_argument = NULL_TREE;
12210
12211   return make_parameter_declarator (&decl_specifiers,
12212                                     declarator,
12213                                     default_argument);
12214 }
12215
12216 /* Parse a function-body.
12217
12218    function-body:
12219      compound_statement  */
12220
12221 static void
12222 cp_parser_function_body (cp_parser *parser)
12223 {
12224   cp_parser_compound_statement (parser, NULL, false);
12225 }
12226
12227 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12228    true if a ctor-initializer was present.  */
12229
12230 static bool
12231 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12232 {
12233   tree body;
12234   bool ctor_initializer_p;
12235
12236   /* Begin the function body.  */
12237   body = begin_function_body ();
12238   /* Parse the optional ctor-initializer.  */
12239   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12240   /* Parse the function-body.  */
12241   cp_parser_function_body (parser);
12242   /* Finish the function body.  */
12243   finish_function_body (body);
12244
12245   return ctor_initializer_p;
12246 }
12247
12248 /* Parse an initializer.
12249
12250    initializer:
12251      = initializer-clause
12252      ( expression-list )
12253
12254    Returns an expression representing the initializer.  If no
12255    initializer is present, NULL_TREE is returned.
12256
12257    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12258    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12259    set to FALSE if there is no initializer present.  If there is an
12260    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12261    is set to true; otherwise it is set to false.  */
12262
12263 static tree
12264 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12265                        bool* non_constant_p)
12266 {
12267   cp_token *token;
12268   tree init;
12269
12270   /* Peek at the next token.  */
12271   token = cp_lexer_peek_token (parser->lexer);
12272
12273   /* Let our caller know whether or not this initializer was
12274      parenthesized.  */
12275   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12276   /* Assume that the initializer is constant.  */
12277   *non_constant_p = false;
12278
12279   if (token->type == CPP_EQ)
12280     {
12281       /* Consume the `='.  */
12282       cp_lexer_consume_token (parser->lexer);
12283       /* Parse the initializer-clause.  */
12284       init = cp_parser_initializer_clause (parser, non_constant_p);
12285     }
12286   else if (token->type == CPP_OPEN_PAREN)
12287     init = cp_parser_parenthesized_expression_list (parser, false,
12288                                                     /*cast_p=*/false,
12289                                                     non_constant_p);
12290   else
12291     {
12292       /* Anything else is an error.  */
12293       cp_parser_error (parser, "expected initializer");
12294       init = error_mark_node;
12295     }
12296
12297   return init;
12298 }
12299
12300 /* Parse an initializer-clause.
12301
12302    initializer-clause:
12303      assignment-expression
12304      { initializer-list , [opt] }
12305      { }
12306
12307    Returns an expression representing the initializer.
12308
12309    If the `assignment-expression' production is used the value
12310    returned is simply a representation for the expression.
12311
12312    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12313    the elements of the initializer-list (or NULL, if the last
12314    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12315    NULL_TREE.  There is no way to detect whether or not the optional
12316    trailing `,' was provided.  NON_CONSTANT_P is as for
12317    cp_parser_initializer.  */
12318
12319 static tree
12320 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12321 {
12322   tree initializer;
12323
12324   /* Assume the expression is constant.  */
12325   *non_constant_p = false;
12326
12327   /* If it is not a `{', then we are looking at an
12328      assignment-expression.  */
12329   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12330     {
12331       initializer
12332         = cp_parser_constant_expression (parser,
12333                                         /*allow_non_constant_p=*/true,
12334                                         non_constant_p);
12335       if (!*non_constant_p)
12336         initializer = fold_non_dependent_expr (initializer);
12337     }
12338   else
12339     {
12340       /* Consume the `{' token.  */
12341       cp_lexer_consume_token (parser->lexer);
12342       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12343       initializer = make_node (CONSTRUCTOR);
12344       /* If it's not a `}', then there is a non-trivial initializer.  */
12345       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12346         {
12347           /* Parse the initializer list.  */
12348           CONSTRUCTOR_ELTS (initializer)
12349             = cp_parser_initializer_list (parser, non_constant_p);
12350           /* A trailing `,' token is allowed.  */
12351           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12352             cp_lexer_consume_token (parser->lexer);
12353         }
12354       /* Now, there should be a trailing `}'.  */
12355       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12356     }
12357
12358   return initializer;
12359 }
12360
12361 /* Parse an initializer-list.
12362
12363    initializer-list:
12364      initializer-clause
12365      initializer-list , initializer-clause
12366
12367    GNU Extension:
12368
12369    initializer-list:
12370      identifier : initializer-clause
12371      initializer-list, identifier : initializer-clause
12372
12373    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12374    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12375    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12376    as for cp_parser_initializer.  */
12377
12378 static VEC(constructor_elt,gc) *
12379 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12380 {
12381   VEC(constructor_elt,gc) *v = NULL;
12382
12383   /* Assume all of the expressions are constant.  */
12384   *non_constant_p = false;
12385
12386   /* Parse the rest of the list.  */
12387   while (true)
12388     {
12389       cp_token *token;
12390       tree identifier;
12391       tree initializer;
12392       bool clause_non_constant_p;
12393
12394       /* If the next token is an identifier and the following one is a
12395          colon, we are looking at the GNU designated-initializer
12396          syntax.  */
12397       if (cp_parser_allow_gnu_extensions_p (parser)
12398           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12399           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12400         {
12401           /* Consume the identifier.  */
12402           identifier = cp_lexer_consume_token (parser->lexer)->value;
12403           /* Consume the `:'.  */
12404           cp_lexer_consume_token (parser->lexer);
12405         }
12406       else
12407         identifier = NULL_TREE;
12408
12409       /* Parse the initializer.  */
12410       initializer = cp_parser_initializer_clause (parser,
12411                                                   &clause_non_constant_p);
12412       /* If any clause is non-constant, so is the entire initializer.  */
12413       if (clause_non_constant_p)
12414         *non_constant_p = true;
12415
12416       /* Add it to the vector.  */
12417       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12418
12419       /* If the next token is not a comma, we have reached the end of
12420          the list.  */
12421       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12422         break;
12423
12424       /* Peek at the next token.  */
12425       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12426       /* If the next token is a `}', then we're still done.  An
12427          initializer-clause can have a trailing `,' after the
12428          initializer-list and before the closing `}'.  */
12429       if (token->type == CPP_CLOSE_BRACE)
12430         break;
12431
12432       /* Consume the `,' token.  */
12433       cp_lexer_consume_token (parser->lexer);
12434     }
12435
12436   return v;
12437 }
12438
12439 /* Classes [gram.class] */
12440
12441 /* Parse a class-name.
12442
12443    class-name:
12444      identifier
12445      template-id
12446
12447    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12448    to indicate that names looked up in dependent types should be
12449    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12450    keyword has been used to indicate that the name that appears next
12451    is a template.  TAG_TYPE indicates the explicit tag given before
12452    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12453    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12454    is the class being defined in a class-head.
12455
12456    Returns the TYPE_DECL representing the class.  */
12457
12458 static tree
12459 cp_parser_class_name (cp_parser *parser,
12460                       bool typename_keyword_p,
12461                       bool template_keyword_p,
12462                       enum tag_types tag_type,
12463                       bool check_dependency_p,
12464                       bool class_head_p,
12465                       bool is_declaration)
12466 {
12467   tree decl;
12468   tree scope;
12469   bool typename_p;
12470   cp_token *token;
12471
12472   /* All class-names start with an identifier.  */
12473   token = cp_lexer_peek_token (parser->lexer);
12474   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12475     {
12476       cp_parser_error (parser, "expected class-name");
12477       return error_mark_node;
12478     }
12479
12480   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12481      to a template-id, so we save it here.  */
12482   scope = parser->scope;
12483   if (scope == error_mark_node)
12484     return error_mark_node;
12485
12486   /* Any name names a type if we're following the `typename' keyword
12487      in a qualified name where the enclosing scope is type-dependent.  */
12488   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12489                 && dependent_type_p (scope));
12490   /* Handle the common case (an identifier, but not a template-id)
12491      efficiently.  */
12492   if (token->type == CPP_NAME
12493       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12494     {
12495       tree identifier;
12496
12497       /* Look for the identifier.  */
12498       identifier = cp_parser_identifier (parser);
12499       /* If the next token isn't an identifier, we are certainly not
12500          looking at a class-name.  */
12501       if (identifier == error_mark_node)
12502         decl = error_mark_node;
12503       /* If we know this is a type-name, there's no need to look it
12504          up.  */
12505       else if (typename_p)
12506         decl = identifier;
12507       else
12508         {
12509           /* If the next token is a `::', then the name must be a type
12510              name.
12511
12512              [basic.lookup.qual]
12513
12514              During the lookup for a name preceding the :: scope
12515              resolution operator, object, function, and enumerator
12516              names are ignored.  */
12517           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12518             tag_type = typename_type;
12519           /* Look up the name.  */
12520           decl = cp_parser_lookup_name (parser, identifier,
12521                                         tag_type,
12522                                         /*is_template=*/false,
12523                                         /*is_namespace=*/false,
12524                                         check_dependency_p,
12525                                         /*ambiguous_p=*/NULL);
12526         }
12527     }
12528   else
12529     {
12530       /* Try a template-id.  */
12531       decl = cp_parser_template_id (parser, template_keyword_p,
12532                                     check_dependency_p,
12533                                     is_declaration);
12534       if (decl == error_mark_node)
12535         return error_mark_node;
12536     }
12537
12538   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12539
12540   /* If this is a typename, create a TYPENAME_TYPE.  */
12541   if (typename_p && decl != error_mark_node)
12542     {
12543       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12544       if (decl != error_mark_node)
12545         decl = TYPE_NAME (decl);
12546     }
12547
12548   /* Check to see that it is really the name of a class.  */
12549   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12550       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12551       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12552     /* Situations like this:
12553
12554          template <typename T> struct A {
12555            typename T::template X<int>::I i;
12556          };
12557
12558        are problematic.  Is `T::template X<int>' a class-name?  The
12559        standard does not seem to be definitive, but there is no other
12560        valid interpretation of the following `::'.  Therefore, those
12561        names are considered class-names.  */
12562     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12563   else if (decl == error_mark_node
12564            || TREE_CODE (decl) != TYPE_DECL
12565            || TREE_TYPE (decl) == error_mark_node
12566            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12567     {
12568       cp_parser_error (parser, "expected class-name");
12569       return error_mark_node;
12570     }
12571
12572   return decl;
12573 }
12574
12575 /* Parse a class-specifier.
12576
12577    class-specifier:
12578      class-head { member-specification [opt] }
12579
12580    Returns the TREE_TYPE representing the class.  */
12581
12582 static tree
12583 cp_parser_class_specifier (cp_parser* parser)
12584 {
12585   cp_token *token;
12586   tree type;
12587   tree attributes = NULL_TREE;
12588   int has_trailing_semicolon;
12589   bool nested_name_specifier_p;
12590   unsigned saved_num_template_parameter_lists;
12591   tree old_scope = NULL_TREE;
12592   tree scope = NULL_TREE;
12593
12594   push_deferring_access_checks (dk_no_deferred);
12595
12596   /* Parse the class-head.  */
12597   type = cp_parser_class_head (parser,
12598                                &nested_name_specifier_p,
12599                                &attributes);
12600   /* If the class-head was a semantic disaster, skip the entire body
12601      of the class.  */
12602   if (!type)
12603     {
12604       cp_parser_skip_to_end_of_block_or_statement (parser);
12605       pop_deferring_access_checks ();
12606       return error_mark_node;
12607     }
12608
12609   /* Look for the `{'.  */
12610   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12611     {
12612       pop_deferring_access_checks ();
12613       return error_mark_node;
12614     }
12615
12616   /* Issue an error message if type-definitions are forbidden here.  */
12617   cp_parser_check_type_definition (parser);
12618   /* Remember that we are defining one more class.  */
12619   ++parser->num_classes_being_defined;
12620   /* Inside the class, surrounding template-parameter-lists do not
12621      apply.  */
12622   saved_num_template_parameter_lists
12623     = parser->num_template_parameter_lists;
12624   parser->num_template_parameter_lists = 0;
12625
12626   /* Start the class.  */
12627   if (nested_name_specifier_p)
12628     {
12629       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12630       old_scope = push_inner_scope (scope);
12631     }
12632   type = begin_class_definition (type);
12633
12634   if (type == error_mark_node)
12635     /* If the type is erroneous, skip the entire body of the class.  */
12636     cp_parser_skip_to_closing_brace (parser);
12637   else
12638     /* Parse the member-specification.  */
12639     cp_parser_member_specification_opt (parser);
12640
12641   /* Look for the trailing `}'.  */
12642   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12643   /* We get better error messages by noticing a common problem: a
12644      missing trailing `;'.  */
12645   token = cp_lexer_peek_token (parser->lexer);
12646   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12647   /* Look for trailing attributes to apply to this class.  */
12648   if (cp_parser_allow_gnu_extensions_p (parser))
12649     {
12650       tree sub_attr = cp_parser_attributes_opt (parser);
12651       attributes = chainon (attributes, sub_attr);
12652     }
12653   if (type != error_mark_node)
12654     type = finish_struct (type, attributes);
12655   if (nested_name_specifier_p)
12656     pop_inner_scope (old_scope, scope);
12657   /* If this class is not itself within the scope of another class,
12658      then we need to parse the bodies of all of the queued function
12659      definitions.  Note that the queued functions defined in a class
12660      are not always processed immediately following the
12661      class-specifier for that class.  Consider:
12662
12663        struct A {
12664          struct B { void f() { sizeof (A); } };
12665        };
12666
12667      If `f' were processed before the processing of `A' were
12668      completed, there would be no way to compute the size of `A'.
12669      Note that the nesting we are interested in here is lexical --
12670      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12671      for:
12672
12673        struct A { struct B; };
12674        struct A::B { void f() { } };
12675
12676      there is no need to delay the parsing of `A::B::f'.  */
12677   if (--parser->num_classes_being_defined == 0)
12678     {
12679       tree queue_entry;
12680       tree fn;
12681       tree class_type = NULL_TREE;
12682       tree pushed_scope = NULL_TREE;
12683  
12684       /* In a first pass, parse default arguments to the functions.
12685          Then, in a second pass, parse the bodies of the functions.
12686          This two-phased approach handles cases like:
12687
12688             struct S {
12689               void f() { g(); }
12690               void g(int i = 3);
12691             };
12692
12693          */
12694       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12695              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12696            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12697            TREE_PURPOSE (parser->unparsed_functions_queues)
12698              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12699         {
12700           fn = TREE_VALUE (queue_entry);
12701           /* If there are default arguments that have not yet been processed,
12702              take care of them now.  */
12703           if (class_type != TREE_PURPOSE (queue_entry))
12704             {
12705               if (pushed_scope)
12706                 pop_scope (pushed_scope);
12707               class_type = TREE_PURPOSE (queue_entry);
12708               pushed_scope = push_scope (class_type);
12709             }
12710           /* Make sure that any template parameters are in scope.  */
12711           maybe_begin_member_template_processing (fn);
12712           /* Parse the default argument expressions.  */
12713           cp_parser_late_parsing_default_args (parser, fn);
12714           /* Remove any template parameters from the symbol table.  */
12715           maybe_end_member_template_processing ();
12716         }
12717       if (pushed_scope)
12718         pop_scope (pushed_scope);
12719       /* Now parse the body of the functions.  */
12720       for (TREE_VALUE (parser->unparsed_functions_queues)
12721              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12722            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12723            TREE_VALUE (parser->unparsed_functions_queues)
12724              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12725         {
12726           /* Figure out which function we need to process.  */
12727           fn = TREE_VALUE (queue_entry);
12728           /* Parse the function.  */
12729           cp_parser_late_parsing_for_member (parser, fn);
12730         }
12731     }
12732
12733   /* Put back any saved access checks.  */
12734   pop_deferring_access_checks ();
12735
12736   /* Restore the count of active template-parameter-lists.  */
12737   parser->num_template_parameter_lists
12738     = saved_num_template_parameter_lists;
12739
12740   return type;
12741 }
12742
12743 /* Parse a class-head.
12744
12745    class-head:
12746      class-key identifier [opt] base-clause [opt]
12747      class-key nested-name-specifier identifier base-clause [opt]
12748      class-key nested-name-specifier [opt] template-id
12749        base-clause [opt]
12750
12751    GNU Extensions:
12752      class-key attributes identifier [opt] base-clause [opt]
12753      class-key attributes nested-name-specifier identifier base-clause [opt]
12754      class-key attributes nested-name-specifier [opt] template-id
12755        base-clause [opt]
12756
12757    Returns the TYPE of the indicated class.  Sets
12758    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12759    involving a nested-name-specifier was used, and FALSE otherwise.
12760
12761    Returns error_mark_node if this is not a class-head.
12762
12763    Returns NULL_TREE if the class-head is syntactically valid, but
12764    semantically invalid in a way that means we should skip the entire
12765    body of the class.  */
12766
12767 static tree
12768 cp_parser_class_head (cp_parser* parser,
12769                       bool* nested_name_specifier_p,
12770                       tree *attributes_p)
12771 {
12772   tree nested_name_specifier;
12773   enum tag_types class_key;
12774   tree id = NULL_TREE;
12775   tree type = NULL_TREE;
12776   tree attributes;
12777   bool template_id_p = false;
12778   bool qualified_p = false;
12779   bool invalid_nested_name_p = false;
12780   bool invalid_explicit_specialization_p = false;
12781   tree pushed_scope = NULL_TREE;
12782   unsigned num_templates;
12783   tree bases;
12784
12785   /* Assume no nested-name-specifier will be present.  */
12786   *nested_name_specifier_p = false;
12787   /* Assume no template parameter lists will be used in defining the
12788      type.  */
12789   num_templates = 0;
12790
12791   /* Look for the class-key.  */
12792   class_key = cp_parser_class_key (parser);
12793   if (class_key == none_type)
12794     return error_mark_node;
12795
12796   /* Parse the attributes.  */
12797   attributes = cp_parser_attributes_opt (parser);
12798
12799   /* If the next token is `::', that is invalid -- but sometimes
12800      people do try to write:
12801
12802        struct ::S {};
12803
12804      Handle this gracefully by accepting the extra qualifier, and then
12805      issuing an error about it later if this really is a
12806      class-head.  If it turns out just to be an elaborated type
12807      specifier, remain silent.  */
12808   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12809     qualified_p = true;
12810
12811   push_deferring_access_checks (dk_no_check);
12812
12813   /* Determine the name of the class.  Begin by looking for an
12814      optional nested-name-specifier.  */
12815   nested_name_specifier
12816     = cp_parser_nested_name_specifier_opt (parser,
12817                                            /*typename_keyword_p=*/false,
12818                                            /*check_dependency_p=*/false,
12819                                            /*type_p=*/false,
12820                                            /*is_declaration=*/false);
12821   /* If there was a nested-name-specifier, then there *must* be an
12822      identifier.  */
12823   if (nested_name_specifier)
12824     {
12825       /* Although the grammar says `identifier', it really means
12826          `class-name' or `template-name'.  You are only allowed to
12827          define a class that has already been declared with this
12828          syntax.
12829
12830          The proposed resolution for Core Issue 180 says that whever
12831          you see `class T::X' you should treat `X' as a type-name.
12832
12833          It is OK to define an inaccessible class; for example:
12834
12835            class A { class B; };
12836            class A::B {};
12837
12838          We do not know if we will see a class-name, or a
12839          template-name.  We look for a class-name first, in case the
12840          class-name is a template-id; if we looked for the
12841          template-name first we would stop after the template-name.  */
12842       cp_parser_parse_tentatively (parser);
12843       type = cp_parser_class_name (parser,
12844                                    /*typename_keyword_p=*/false,
12845                                    /*template_keyword_p=*/false,
12846                                    class_type,
12847                                    /*check_dependency_p=*/false,
12848                                    /*class_head_p=*/true,
12849                                    /*is_declaration=*/false);
12850       /* If that didn't work, ignore the nested-name-specifier.  */
12851       if (!cp_parser_parse_definitely (parser))
12852         {
12853           invalid_nested_name_p = true;
12854           id = cp_parser_identifier (parser);
12855           if (id == error_mark_node)
12856             id = NULL_TREE;
12857         }
12858       /* If we could not find a corresponding TYPE, treat this
12859          declaration like an unqualified declaration.  */
12860       if (type == error_mark_node)
12861         nested_name_specifier = NULL_TREE;
12862       /* Otherwise, count the number of templates used in TYPE and its
12863          containing scopes.  */
12864       else
12865         {
12866           tree scope;
12867
12868           for (scope = TREE_TYPE (type);
12869                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12870                scope = (TYPE_P (scope)
12871                         ? TYPE_CONTEXT (scope)
12872                         : DECL_CONTEXT (scope)))
12873             if (TYPE_P (scope)
12874                 && CLASS_TYPE_P (scope)
12875                 && CLASSTYPE_TEMPLATE_INFO (scope)
12876                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12877                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12878               ++num_templates;
12879         }
12880     }
12881   /* Otherwise, the identifier is optional.  */
12882   else
12883     {
12884       /* We don't know whether what comes next is a template-id,
12885          an identifier, or nothing at all.  */
12886       cp_parser_parse_tentatively (parser);
12887       /* Check for a template-id.  */
12888       id = cp_parser_template_id (parser,
12889                                   /*template_keyword_p=*/false,
12890                                   /*check_dependency_p=*/true,
12891                                   /*is_declaration=*/true);
12892       /* If that didn't work, it could still be an identifier.  */
12893       if (!cp_parser_parse_definitely (parser))
12894         {
12895           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12896             id = cp_parser_identifier (parser);
12897           else
12898             id = NULL_TREE;
12899         }
12900       else
12901         {
12902           template_id_p = true;
12903           ++num_templates;
12904         }
12905     }
12906
12907   pop_deferring_access_checks ();
12908
12909   if (id)
12910     cp_parser_check_for_invalid_template_id (parser, id);
12911
12912   /* If it's not a `:' or a `{' then we can't really be looking at a
12913      class-head, since a class-head only appears as part of a
12914      class-specifier.  We have to detect this situation before calling
12915      xref_tag, since that has irreversible side-effects.  */
12916   if (!cp_parser_next_token_starts_class_definition_p (parser))
12917     {
12918       cp_parser_error (parser, "expected %<{%> or %<:%>");
12919       return error_mark_node;
12920     }
12921
12922   /* At this point, we're going ahead with the class-specifier, even
12923      if some other problem occurs.  */
12924   cp_parser_commit_to_tentative_parse (parser);
12925   /* Issue the error about the overly-qualified name now.  */
12926   if (qualified_p)
12927     cp_parser_error (parser,
12928                      "global qualification of class name is invalid");
12929   else if (invalid_nested_name_p)
12930     cp_parser_error (parser,
12931                      "qualified name does not name a class");
12932   else if (nested_name_specifier)
12933     {
12934       tree scope;
12935
12936       /* Reject typedef-names in class heads.  */
12937       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12938         {
12939           error ("invalid class name in declaration of %qD", type);
12940           type = NULL_TREE;
12941           goto done;
12942         }
12943
12944       /* Figure out in what scope the declaration is being placed.  */
12945       scope = current_scope ();
12946       /* If that scope does not contain the scope in which the
12947          class was originally declared, the program is invalid.  */
12948       if (scope && !is_ancestor (scope, nested_name_specifier))
12949         {
12950           error ("declaration of %qD in %qD which does not enclose %qD",
12951                  type, scope, nested_name_specifier);
12952           type = NULL_TREE;
12953           goto done;
12954         }
12955       /* [dcl.meaning]
12956
12957          A declarator-id shall not be qualified exception of the
12958          definition of a ... nested class outside of its class
12959          ... [or] a the definition or explicit instantiation of a
12960          class member of a namespace outside of its namespace.  */
12961       if (scope == nested_name_specifier)
12962         {
12963           pedwarn ("extra qualification ignored");
12964           nested_name_specifier = NULL_TREE;
12965           num_templates = 0;
12966         }
12967     }
12968   /* An explicit-specialization must be preceded by "template <>".  If
12969      it is not, try to recover gracefully.  */
12970   if (at_namespace_scope_p ()
12971       && parser->num_template_parameter_lists == 0
12972       && template_id_p)
12973     {
12974       error ("an explicit specialization must be preceded by %<template <>%>");
12975       invalid_explicit_specialization_p = true;
12976       /* Take the same action that would have been taken by
12977          cp_parser_explicit_specialization.  */
12978       ++parser->num_template_parameter_lists;
12979       begin_specialization ();
12980     }
12981   /* There must be no "return" statements between this point and the
12982      end of this function; set "type "to the correct return value and
12983      use "goto done;" to return.  */
12984   /* Make sure that the right number of template parameters were
12985      present.  */
12986   if (!cp_parser_check_template_parameters (parser, num_templates))
12987     {
12988       /* If something went wrong, there is no point in even trying to
12989          process the class-definition.  */
12990       type = NULL_TREE;
12991       goto done;
12992     }
12993
12994   /* Look up the type.  */
12995   if (template_id_p)
12996     {
12997       type = TREE_TYPE (id);
12998       maybe_process_partial_specialization (type);
12999       if (nested_name_specifier)
13000         pushed_scope = push_scope (nested_name_specifier);
13001     }
13002   else if (nested_name_specifier)
13003     {
13004       tree class_type;
13005
13006       /* Given:
13007
13008             template <typename T> struct S { struct T };
13009             template <typename T> struct S<T>::T { };
13010
13011          we will get a TYPENAME_TYPE when processing the definition of
13012          `S::T'.  We need to resolve it to the actual type before we
13013          try to define it.  */
13014       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13015         {
13016           class_type = resolve_typename_type (TREE_TYPE (type),
13017                                               /*only_current_p=*/false);
13018           if (class_type != error_mark_node)
13019             type = TYPE_NAME (class_type);
13020           else
13021             {
13022               cp_parser_error (parser, "could not resolve typename type");
13023               type = error_mark_node;
13024             }
13025         }
13026
13027       maybe_process_partial_specialization (TREE_TYPE (type));
13028       class_type = current_class_type;
13029       /* Enter the scope indicated by the nested-name-specifier.  */
13030       pushed_scope = push_scope (nested_name_specifier);
13031       /* Get the canonical version of this type.  */
13032       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13033       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13034           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13035         {
13036           type = push_template_decl (type);
13037           if (type == error_mark_node)
13038             {
13039               type = NULL_TREE;
13040               goto done;
13041             }
13042         }
13043
13044       type = TREE_TYPE (type);
13045       *nested_name_specifier_p = true;
13046     }
13047   else      /* The name is not a nested name.  */
13048     {
13049       /* If the class was unnamed, create a dummy name.  */
13050       if (!id)
13051         id = make_anon_name ();
13052       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13053                        parser->num_template_parameter_lists);
13054     }
13055
13056   /* Indicate whether this class was declared as a `class' or as a
13057      `struct'.  */
13058   if (TREE_CODE (type) == RECORD_TYPE)
13059     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13060   cp_parser_check_class_key (class_key, type);
13061
13062   /* If this type was already complete, and we see another definition,
13063      that's an error.  */
13064   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13065     {
13066       error ("redefinition of %q#T", type);
13067       error ("previous definition of %q+#T", type);
13068       type = NULL_TREE;
13069       goto done;
13070     }
13071
13072   /* We will have entered the scope containing the class; the names of
13073      base classes should be looked up in that context.  For example:
13074
13075        struct A { struct B {}; struct C; };
13076        struct A::C : B {};
13077
13078      is valid.  */
13079   bases = NULL_TREE;
13080
13081   /* Get the list of base-classes, if there is one.  */
13082   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13083     bases = cp_parser_base_clause (parser);
13084
13085   /* Process the base classes.  */
13086   xref_basetypes (type, bases);
13087
13088  done:
13089   /* Leave the scope given by the nested-name-specifier.  We will
13090      enter the class scope itself while processing the members.  */
13091   if (pushed_scope)
13092     pop_scope (pushed_scope);
13093
13094   if (invalid_explicit_specialization_p)
13095     {
13096       end_specialization ();
13097       --parser->num_template_parameter_lists;
13098     }
13099   *attributes_p = attributes;
13100   return type;
13101 }
13102
13103 /* Parse a class-key.
13104
13105    class-key:
13106      class
13107      struct
13108      union
13109
13110    Returns the kind of class-key specified, or none_type to indicate
13111    error.  */
13112
13113 static enum tag_types
13114 cp_parser_class_key (cp_parser* parser)
13115 {
13116   cp_token *token;
13117   enum tag_types tag_type;
13118
13119   /* Look for the class-key.  */
13120   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13121   if (!token)
13122     return none_type;
13123
13124   /* Check to see if the TOKEN is a class-key.  */
13125   tag_type = cp_parser_token_is_class_key (token);
13126   if (!tag_type)
13127     cp_parser_error (parser, "expected class-key");
13128   return tag_type;
13129 }
13130
13131 /* Parse an (optional) member-specification.
13132
13133    member-specification:
13134      member-declaration member-specification [opt]
13135      access-specifier : member-specification [opt]  */
13136
13137 static void
13138 cp_parser_member_specification_opt (cp_parser* parser)
13139 {
13140   while (true)
13141     {
13142       cp_token *token;
13143       enum rid keyword;
13144
13145       /* Peek at the next token.  */
13146       token = cp_lexer_peek_token (parser->lexer);
13147       /* If it's a `}', or EOF then we've seen all the members.  */
13148       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13149         break;
13150
13151       /* See if this token is a keyword.  */
13152       keyword = token->keyword;
13153       switch (keyword)
13154         {
13155         case RID_PUBLIC:
13156         case RID_PROTECTED:
13157         case RID_PRIVATE:
13158           /* Consume the access-specifier.  */
13159           cp_lexer_consume_token (parser->lexer);
13160           /* Remember which access-specifier is active.  */
13161           current_access_specifier = token->value;
13162           /* Look for the `:'.  */
13163           cp_parser_require (parser, CPP_COLON, "`:'");
13164           break;
13165
13166         default:
13167           /* Accept #pragmas at class scope.  */
13168           if (token->type == CPP_PRAGMA)
13169             {
13170               cp_lexer_handle_pragma (parser->lexer);
13171               break;
13172             }
13173
13174           /* Otherwise, the next construction must be a
13175              member-declaration.  */
13176           cp_parser_member_declaration (parser);
13177         }
13178     }
13179 }
13180
13181 /* Parse a member-declaration.
13182
13183    member-declaration:
13184      decl-specifier-seq [opt] member-declarator-list [opt] ;
13185      function-definition ; [opt]
13186      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13187      using-declaration
13188      template-declaration
13189
13190    member-declarator-list:
13191      member-declarator
13192      member-declarator-list , member-declarator
13193
13194    member-declarator:
13195      declarator pure-specifier [opt]
13196      declarator constant-initializer [opt]
13197      identifier [opt] : constant-expression
13198
13199    GNU Extensions:
13200
13201    member-declaration:
13202      __extension__ member-declaration
13203
13204    member-declarator:
13205      declarator attributes [opt] pure-specifier [opt]
13206      declarator attributes [opt] constant-initializer [opt]
13207      identifier [opt] attributes [opt] : constant-expression  */
13208
13209 static void
13210 cp_parser_member_declaration (cp_parser* parser)
13211 {
13212   cp_decl_specifier_seq decl_specifiers;
13213   tree prefix_attributes;
13214   tree decl;
13215   int declares_class_or_enum;
13216   bool friend_p;
13217   cp_token *token;
13218   int saved_pedantic;
13219
13220   /* Check for the `__extension__' keyword.  */
13221   if (cp_parser_extension_opt (parser, &saved_pedantic))
13222     {
13223       /* Recurse.  */
13224       cp_parser_member_declaration (parser);
13225       /* Restore the old value of the PEDANTIC flag.  */
13226       pedantic = saved_pedantic;
13227
13228       return;
13229     }
13230
13231   /* Check for a template-declaration.  */
13232   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13233     {
13234       /* Parse the template-declaration.  */
13235       cp_parser_template_declaration (parser, /*member_p=*/true);
13236
13237       return;
13238     }
13239
13240   /* Check for a using-declaration.  */
13241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13242     {
13243       /* Parse the using-declaration.  */
13244       cp_parser_using_declaration (parser);
13245
13246       return;
13247     }
13248
13249   /* Check for @defs.  */
13250   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13251     {
13252       tree ivar, member;
13253       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13254       ivar = ivar_chains;
13255       while (ivar)
13256         {
13257           member = ivar;
13258           ivar = TREE_CHAIN (member);
13259           TREE_CHAIN (member) = NULL_TREE;
13260           finish_member_declaration (member);
13261         }
13262       return;
13263     }
13264
13265   /* Parse the decl-specifier-seq.  */
13266   cp_parser_decl_specifier_seq (parser,
13267                                 CP_PARSER_FLAGS_OPTIONAL,
13268                                 &decl_specifiers,
13269                                 &declares_class_or_enum);
13270   prefix_attributes = decl_specifiers.attributes;
13271   decl_specifiers.attributes = NULL_TREE;
13272   /* Check for an invalid type-name.  */
13273   if (!decl_specifiers.type
13274       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13275     return;
13276   /* If there is no declarator, then the decl-specifier-seq should
13277      specify a type.  */
13278   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13279     {
13280       /* If there was no decl-specifier-seq, and the next token is a
13281          `;', then we have something like:
13282
13283            struct S { ; };
13284
13285          [class.mem]
13286
13287          Each member-declaration shall declare at least one member
13288          name of the class.  */
13289       if (!decl_specifiers.any_specifiers_p)
13290         {
13291           cp_token *token = cp_lexer_peek_token (parser->lexer);
13292           if (pedantic && !token->in_system_header)
13293             pedwarn ("%Hextra %<;%>", &token->location);
13294         }
13295       else
13296         {
13297           tree type;
13298
13299           /* See if this declaration is a friend.  */
13300           friend_p = cp_parser_friend_p (&decl_specifiers);
13301           /* If there were decl-specifiers, check to see if there was
13302              a class-declaration.  */
13303           type = check_tag_decl (&decl_specifiers);
13304           /* Nested classes have already been added to the class, but
13305              a `friend' needs to be explicitly registered.  */
13306           if (friend_p)
13307             {
13308               /* If the `friend' keyword was present, the friend must
13309                  be introduced with a class-key.  */
13310                if (!declares_class_or_enum)
13311                  error ("a class-key must be used when declaring a friend");
13312                /* In this case:
13313
13314                     template <typename T> struct A {
13315                       friend struct A<T>::B;
13316                     };
13317
13318                   A<T>::B will be represented by a TYPENAME_TYPE, and
13319                   therefore not recognized by check_tag_decl.  */
13320                if (!type
13321                    && decl_specifiers.type
13322                    && TYPE_P (decl_specifiers.type))
13323                  type = decl_specifiers.type;
13324                if (!type || !TYPE_P (type))
13325                  error ("friend declaration does not name a class or "
13326                         "function");
13327                else
13328                  make_friend_class (current_class_type, type,
13329                                     /*complain=*/true);
13330             }
13331           /* If there is no TYPE, an error message will already have
13332              been issued.  */
13333           else if (!type || type == error_mark_node)
13334             ;
13335           /* An anonymous aggregate has to be handled specially; such
13336              a declaration really declares a data member (with a
13337              particular type), as opposed to a nested class.  */
13338           else if (ANON_AGGR_TYPE_P (type))
13339             {
13340               /* Remove constructors and such from TYPE, now that we
13341                  know it is an anonymous aggregate.  */
13342               fixup_anonymous_aggr (type);
13343               /* And make the corresponding data member.  */
13344               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13345               /* Add it to the class.  */
13346               finish_member_declaration (decl);
13347             }
13348           else
13349             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13350         }
13351     }
13352   else
13353     {
13354       /* See if these declarations will be friends.  */
13355       friend_p = cp_parser_friend_p (&decl_specifiers);
13356
13357       /* Keep going until we hit the `;' at the end of the
13358          declaration.  */
13359       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13360         {
13361           tree attributes = NULL_TREE;
13362           tree first_attribute;
13363
13364           /* Peek at the next token.  */
13365           token = cp_lexer_peek_token (parser->lexer);
13366
13367           /* Check for a bitfield declaration.  */
13368           if (token->type == CPP_COLON
13369               || (token->type == CPP_NAME
13370                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13371                   == CPP_COLON))
13372             {
13373               tree identifier;
13374               tree width;
13375
13376               /* Get the name of the bitfield.  Note that we cannot just
13377                  check TOKEN here because it may have been invalidated by
13378                  the call to cp_lexer_peek_nth_token above.  */
13379               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13380                 identifier = cp_parser_identifier (parser);
13381               else
13382                 identifier = NULL_TREE;
13383
13384               /* Consume the `:' token.  */
13385               cp_lexer_consume_token (parser->lexer);
13386               /* Get the width of the bitfield.  */
13387               width
13388                 = cp_parser_constant_expression (parser,
13389                                                  /*allow_non_constant=*/false,
13390                                                  NULL);
13391
13392               /* Look for attributes that apply to the bitfield.  */
13393               attributes = cp_parser_attributes_opt (parser);
13394               /* Remember which attributes are prefix attributes and
13395                  which are not.  */
13396               first_attribute = attributes;
13397               /* Combine the attributes.  */
13398               attributes = chainon (prefix_attributes, attributes);
13399
13400               /* Create the bitfield declaration.  */
13401               decl = grokbitfield (identifier
13402                                    ? make_id_declarator (NULL_TREE,
13403                                                          identifier)
13404                                    : NULL,
13405                                    &decl_specifiers,
13406                                    width);
13407               /* Apply the attributes.  */
13408               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13409             }
13410           else
13411             {
13412               cp_declarator *declarator;
13413               tree initializer;
13414               tree asm_specification;
13415               int ctor_dtor_or_conv_p;
13416
13417               /* Parse the declarator.  */
13418               declarator
13419                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13420                                         &ctor_dtor_or_conv_p,
13421                                         /*parenthesized_p=*/NULL,
13422                                         /*member_p=*/true);
13423
13424               /* If something went wrong parsing the declarator, make sure
13425                  that we at least consume some tokens.  */
13426               if (declarator == cp_error_declarator)
13427                 {
13428                   /* Skip to the end of the statement.  */
13429                   cp_parser_skip_to_end_of_statement (parser);
13430                   /* If the next token is not a semicolon, that is
13431                      probably because we just skipped over the body of
13432                      a function.  So, we consume a semicolon if
13433                      present, but do not issue an error message if it
13434                      is not present.  */
13435                   if (cp_lexer_next_token_is (parser->lexer,
13436                                               CPP_SEMICOLON))
13437                     cp_lexer_consume_token (parser->lexer);
13438                   return;
13439                 }
13440
13441               if (declares_class_or_enum & 2)
13442                 cp_parser_check_for_definition_in_return_type
13443                   (declarator, decl_specifiers.type);
13444
13445               /* Look for an asm-specification.  */
13446               asm_specification = cp_parser_asm_specification_opt (parser);
13447               /* Look for attributes that apply to the declaration.  */
13448               attributes = cp_parser_attributes_opt (parser);
13449               /* Remember which attributes are prefix attributes and
13450                  which are not.  */
13451               first_attribute = attributes;
13452               /* Combine the attributes.  */
13453               attributes = chainon (prefix_attributes, attributes);
13454
13455               /* If it's an `=', then we have a constant-initializer or a
13456                  pure-specifier.  It is not correct to parse the
13457                  initializer before registering the member declaration
13458                  since the member declaration should be in scope while
13459                  its initializer is processed.  However, the rest of the
13460                  front end does not yet provide an interface that allows
13461                  us to handle this correctly.  */
13462               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13463                 {
13464                   /* In [class.mem]:
13465
13466                      A pure-specifier shall be used only in the declaration of
13467                      a virtual function.
13468
13469                      A member-declarator can contain a constant-initializer
13470                      only if it declares a static member of integral or
13471                      enumeration type.
13472
13473                      Therefore, if the DECLARATOR is for a function, we look
13474                      for a pure-specifier; otherwise, we look for a
13475                      constant-initializer.  When we call `grokfield', it will
13476                      perform more stringent semantics checks.  */
13477                   if (declarator->kind == cdk_function)
13478                     initializer = cp_parser_pure_specifier (parser);
13479                   else
13480                     /* Parse the initializer.  */
13481                     initializer = cp_parser_constant_initializer (parser);
13482                 }
13483               /* Otherwise, there is no initializer.  */
13484               else
13485                 initializer = NULL_TREE;
13486
13487               /* See if we are probably looking at a function
13488                  definition.  We are certainly not looking at a
13489                  member-declarator.  Calling `grokfield' has
13490                  side-effects, so we must not do it unless we are sure
13491                  that we are looking at a member-declarator.  */
13492               if (cp_parser_token_starts_function_definition_p
13493                   (cp_lexer_peek_token (parser->lexer)))
13494                 {
13495                   /* The grammar does not allow a pure-specifier to be
13496                      used when a member function is defined.  (It is
13497                      possible that this fact is an oversight in the
13498                      standard, since a pure function may be defined
13499                      outside of the class-specifier.  */
13500                   if (initializer)
13501                     error ("pure-specifier on function-definition");
13502                   decl = cp_parser_save_member_function_body (parser,
13503                                                               &decl_specifiers,
13504                                                               declarator,
13505                                                               attributes);
13506                   /* If the member was not a friend, declare it here.  */
13507                   if (!friend_p)
13508                     finish_member_declaration (decl);
13509                   /* Peek at the next token.  */
13510                   token = cp_lexer_peek_token (parser->lexer);
13511                   /* If the next token is a semicolon, consume it.  */
13512                   if (token->type == CPP_SEMICOLON)
13513                     cp_lexer_consume_token (parser->lexer);
13514                   return;
13515                 }
13516               else
13517                 {
13518                   /* Create the declaration.  */
13519                   decl = grokfield (declarator, &decl_specifiers,
13520                                     initializer, asm_specification,
13521                                     attributes);
13522                   /* Any initialization must have been from a
13523                      constant-expression.  */
13524                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13525                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13526                 }
13527             }
13528
13529           /* Reset PREFIX_ATTRIBUTES.  */
13530           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13531             attributes = TREE_CHAIN (attributes);
13532           if (attributes)
13533             TREE_CHAIN (attributes) = NULL_TREE;
13534
13535           /* If there is any qualification still in effect, clear it
13536              now; we will be starting fresh with the next declarator.  */
13537           parser->scope = NULL_TREE;
13538           parser->qualifying_scope = NULL_TREE;
13539           parser->object_scope = NULL_TREE;
13540           /* If it's a `,', then there are more declarators.  */
13541           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13542             cp_lexer_consume_token (parser->lexer);
13543           /* If the next token isn't a `;', then we have a parse error.  */
13544           else if (cp_lexer_next_token_is_not (parser->lexer,
13545                                                CPP_SEMICOLON))
13546             {
13547               cp_parser_error (parser, "expected %<;%>");
13548               /* Skip tokens until we find a `;'.  */
13549               cp_parser_skip_to_end_of_statement (parser);
13550
13551               break;
13552             }
13553
13554           if (decl)
13555             {
13556               /* Add DECL to the list of members.  */
13557               if (!friend_p)
13558                 finish_member_declaration (decl);
13559
13560               if (TREE_CODE (decl) == FUNCTION_DECL)
13561                 cp_parser_save_default_args (parser, decl);
13562             }
13563         }
13564     }
13565
13566   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13567 }
13568
13569 /* Parse a pure-specifier.
13570
13571    pure-specifier:
13572      = 0
13573
13574    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13575    Otherwise, ERROR_MARK_NODE is returned.  */
13576
13577 static tree
13578 cp_parser_pure_specifier (cp_parser* parser)
13579 {
13580   cp_token *token;
13581
13582   /* Look for the `=' token.  */
13583   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13584     return error_mark_node;
13585   /* Look for the `0' token.  */
13586   token = cp_lexer_consume_token (parser->lexer);
13587   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13588     {
13589       cp_parser_error (parser,
13590                        "invalid pure specifier (only `= 0' is allowed)");
13591       cp_parser_skip_to_end_of_statement (parser);
13592       return error_mark_node;
13593     }
13594
13595   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13596      We need to get information from the lexer about how the number
13597      was spelled in order to fix this problem.  */
13598   return integer_zero_node;
13599 }
13600
13601 /* Parse a constant-initializer.
13602
13603    constant-initializer:
13604      = constant-expression
13605
13606    Returns a representation of the constant-expression.  */
13607
13608 static tree
13609 cp_parser_constant_initializer (cp_parser* parser)
13610 {
13611   /* Look for the `=' token.  */
13612   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13613     return error_mark_node;
13614
13615   /* It is invalid to write:
13616
13617        struct S { static const int i = { 7 }; };
13618
13619      */
13620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13621     {
13622       cp_parser_error (parser,
13623                        "a brace-enclosed initializer is not allowed here");
13624       /* Consume the opening brace.  */
13625       cp_lexer_consume_token (parser->lexer);
13626       /* Skip the initializer.  */
13627       cp_parser_skip_to_closing_brace (parser);
13628       /* Look for the trailing `}'.  */
13629       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13630
13631       return error_mark_node;
13632     }
13633
13634   return cp_parser_constant_expression (parser,
13635                                         /*allow_non_constant=*/false,
13636                                         NULL);
13637 }
13638
13639 /* Derived classes [gram.class.derived] */
13640
13641 /* Parse a base-clause.
13642
13643    base-clause:
13644      : base-specifier-list
13645
13646    base-specifier-list:
13647      base-specifier
13648      base-specifier-list , base-specifier
13649
13650    Returns a TREE_LIST representing the base-classes, in the order in
13651    which they were declared.  The representation of each node is as
13652    described by cp_parser_base_specifier.
13653
13654    In the case that no bases are specified, this function will return
13655    NULL_TREE, not ERROR_MARK_NODE.  */
13656
13657 static tree
13658 cp_parser_base_clause (cp_parser* parser)
13659 {
13660   tree bases = NULL_TREE;
13661
13662   /* Look for the `:' that begins the list.  */
13663   cp_parser_require (parser, CPP_COLON, "`:'");
13664
13665   /* Scan the base-specifier-list.  */
13666   while (true)
13667     {
13668       cp_token *token;
13669       tree base;
13670
13671       /* Look for the base-specifier.  */
13672       base = cp_parser_base_specifier (parser);
13673       /* Add BASE to the front of the list.  */
13674       if (base != error_mark_node)
13675         {
13676           TREE_CHAIN (base) = bases;
13677           bases = base;
13678         }
13679       /* Peek at the next token.  */
13680       token = cp_lexer_peek_token (parser->lexer);
13681       /* If it's not a comma, then the list is complete.  */
13682       if (token->type != CPP_COMMA)
13683         break;
13684       /* Consume the `,'.  */
13685       cp_lexer_consume_token (parser->lexer);
13686     }
13687
13688   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13689      base class had a qualified name.  However, the next name that
13690      appears is certainly not qualified.  */
13691   parser->scope = NULL_TREE;
13692   parser->qualifying_scope = NULL_TREE;
13693   parser->object_scope = NULL_TREE;
13694
13695   return nreverse (bases);
13696 }
13697
13698 /* Parse a base-specifier.
13699
13700    base-specifier:
13701      :: [opt] nested-name-specifier [opt] class-name
13702      virtual access-specifier [opt] :: [opt] nested-name-specifier
13703        [opt] class-name
13704      access-specifier virtual [opt] :: [opt] nested-name-specifier
13705        [opt] class-name
13706
13707    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13708    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13709    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13710    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13711
13712 static tree
13713 cp_parser_base_specifier (cp_parser* parser)
13714 {
13715   cp_token *token;
13716   bool done = false;
13717   bool virtual_p = false;
13718   bool duplicate_virtual_error_issued_p = false;
13719   bool duplicate_access_error_issued_p = false;
13720   bool class_scope_p, template_p;
13721   tree access = access_default_node;
13722   tree type;
13723
13724   /* Process the optional `virtual' and `access-specifier'.  */
13725   while (!done)
13726     {
13727       /* Peek at the next token.  */
13728       token = cp_lexer_peek_token (parser->lexer);
13729       /* Process `virtual'.  */
13730       switch (token->keyword)
13731         {
13732         case RID_VIRTUAL:
13733           /* If `virtual' appears more than once, issue an error.  */
13734           if (virtual_p && !duplicate_virtual_error_issued_p)
13735             {
13736               cp_parser_error (parser,
13737                                "%<virtual%> specified more than once in base-specified");
13738               duplicate_virtual_error_issued_p = true;
13739             }
13740
13741           virtual_p = true;
13742
13743           /* Consume the `virtual' token.  */
13744           cp_lexer_consume_token (parser->lexer);
13745
13746           break;
13747
13748         case RID_PUBLIC:
13749         case RID_PROTECTED:
13750         case RID_PRIVATE:
13751           /* If more than one access specifier appears, issue an
13752              error.  */
13753           if (access != access_default_node
13754               && !duplicate_access_error_issued_p)
13755             {
13756               cp_parser_error (parser,
13757                                "more than one access specifier in base-specified");
13758               duplicate_access_error_issued_p = true;
13759             }
13760
13761           access = ridpointers[(int) token->keyword];
13762
13763           /* Consume the access-specifier.  */
13764           cp_lexer_consume_token (parser->lexer);
13765
13766           break;
13767
13768         default:
13769           done = true;
13770           break;
13771         }
13772     }
13773   /* It is not uncommon to see programs mechanically, erroneously, use
13774      the 'typename' keyword to denote (dependent) qualified types
13775      as base classes.  */
13776   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13777     {
13778       if (!processing_template_decl)
13779         error ("keyword %<typename%> not allowed outside of templates");
13780       else
13781         error ("keyword %<typename%> not allowed in this context "
13782                "(the base class is implicitly a type)");
13783       cp_lexer_consume_token (parser->lexer);
13784     }
13785
13786   /* Look for the optional `::' operator.  */
13787   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13788   /* Look for the nested-name-specifier.  The simplest way to
13789      implement:
13790
13791        [temp.res]
13792
13793        The keyword `typename' is not permitted in a base-specifier or
13794        mem-initializer; in these contexts a qualified name that
13795        depends on a template-parameter is implicitly assumed to be a
13796        type name.
13797
13798      is to pretend that we have seen the `typename' keyword at this
13799      point.  */
13800   cp_parser_nested_name_specifier_opt (parser,
13801                                        /*typename_keyword_p=*/true,
13802                                        /*check_dependency_p=*/true,
13803                                        typename_type,
13804                                        /*is_declaration=*/true);
13805   /* If the base class is given by a qualified name, assume that names
13806      we see are type names or templates, as appropriate.  */
13807   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13808   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13809
13810   /* Finally, look for the class-name.  */
13811   type = cp_parser_class_name (parser,
13812                                class_scope_p,
13813                                template_p,
13814                                typename_type,
13815                                /*check_dependency_p=*/true,
13816                                /*class_head_p=*/false,
13817                                /*is_declaration=*/true);
13818
13819   if (type == error_mark_node)
13820     return error_mark_node;
13821
13822   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13823 }
13824
13825 /* Exception handling [gram.exception] */
13826
13827 /* Parse an (optional) exception-specification.
13828
13829    exception-specification:
13830      throw ( type-id-list [opt] )
13831
13832    Returns a TREE_LIST representing the exception-specification.  The
13833    TREE_VALUE of each node is a type.  */
13834
13835 static tree
13836 cp_parser_exception_specification_opt (cp_parser* parser)
13837 {
13838   cp_token *token;
13839   tree type_id_list;
13840
13841   /* Peek at the next token.  */
13842   token = cp_lexer_peek_token (parser->lexer);
13843   /* If it's not `throw', then there's no exception-specification.  */
13844   if (!cp_parser_is_keyword (token, RID_THROW))
13845     return NULL_TREE;
13846
13847   /* Consume the `throw'.  */
13848   cp_lexer_consume_token (parser->lexer);
13849
13850   /* Look for the `('.  */
13851   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13852
13853   /* Peek at the next token.  */
13854   token = cp_lexer_peek_token (parser->lexer);
13855   /* If it's not a `)', then there is a type-id-list.  */
13856   if (token->type != CPP_CLOSE_PAREN)
13857     {
13858       const char *saved_message;
13859
13860       /* Types may not be defined in an exception-specification.  */
13861       saved_message = parser->type_definition_forbidden_message;
13862       parser->type_definition_forbidden_message
13863         = "types may not be defined in an exception-specification";
13864       /* Parse the type-id-list.  */
13865       type_id_list = cp_parser_type_id_list (parser);
13866       /* Restore the saved message.  */
13867       parser->type_definition_forbidden_message = saved_message;
13868     }
13869   else
13870     type_id_list = empty_except_spec;
13871
13872   /* Look for the `)'.  */
13873   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13874
13875   return type_id_list;
13876 }
13877
13878 /* Parse an (optional) type-id-list.
13879
13880    type-id-list:
13881      type-id
13882      type-id-list , type-id
13883
13884    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13885    in the order that the types were presented.  */
13886
13887 static tree
13888 cp_parser_type_id_list (cp_parser* parser)
13889 {
13890   tree types = NULL_TREE;
13891
13892   while (true)
13893     {
13894       cp_token *token;
13895       tree type;
13896
13897       /* Get the next type-id.  */
13898       type = cp_parser_type_id (parser);
13899       /* Add it to the list.  */
13900       types = add_exception_specifier (types, type, /*complain=*/1);
13901       /* Peek at the next token.  */
13902       token = cp_lexer_peek_token (parser->lexer);
13903       /* If it is not a `,', we are done.  */
13904       if (token->type != CPP_COMMA)
13905         break;
13906       /* Consume the `,'.  */
13907       cp_lexer_consume_token (parser->lexer);
13908     }
13909
13910   return nreverse (types);
13911 }
13912
13913 /* Parse a try-block.
13914
13915    try-block:
13916      try compound-statement handler-seq  */
13917
13918 static tree
13919 cp_parser_try_block (cp_parser* parser)
13920 {
13921   tree try_block;
13922
13923   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13924   try_block = begin_try_block ();
13925   cp_parser_compound_statement (parser, NULL, true);
13926   finish_try_block (try_block);
13927   cp_parser_handler_seq (parser);
13928   finish_handler_sequence (try_block);
13929
13930   return try_block;
13931 }
13932
13933 /* Parse a function-try-block.
13934
13935    function-try-block:
13936      try ctor-initializer [opt] function-body handler-seq  */
13937
13938 static bool
13939 cp_parser_function_try_block (cp_parser* parser)
13940 {
13941   tree try_block;
13942   bool ctor_initializer_p;
13943
13944   /* Look for the `try' keyword.  */
13945   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13946     return false;
13947   /* Let the rest of the front-end know where we are.  */
13948   try_block = begin_function_try_block ();
13949   /* Parse the function-body.  */
13950   ctor_initializer_p
13951     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13952   /* We're done with the `try' part.  */
13953   finish_function_try_block (try_block);
13954   /* Parse the handlers.  */
13955   cp_parser_handler_seq (parser);
13956   /* We're done with the handlers.  */
13957   finish_function_handler_sequence (try_block);
13958
13959   return ctor_initializer_p;
13960 }
13961
13962 /* Parse a handler-seq.
13963
13964    handler-seq:
13965      handler handler-seq [opt]  */
13966
13967 static void
13968 cp_parser_handler_seq (cp_parser* parser)
13969 {
13970   while (true)
13971     {
13972       cp_token *token;
13973
13974       /* Parse the handler.  */
13975       cp_parser_handler (parser);
13976       /* Peek at the next token.  */
13977       token = cp_lexer_peek_token (parser->lexer);
13978       /* If it's not `catch' then there are no more handlers.  */
13979       if (!cp_parser_is_keyword (token, RID_CATCH))
13980         break;
13981     }
13982 }
13983
13984 /* Parse a handler.
13985
13986    handler:
13987      catch ( exception-declaration ) compound-statement  */
13988
13989 static void
13990 cp_parser_handler (cp_parser* parser)
13991 {
13992   tree handler;
13993   tree declaration;
13994
13995   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13996   handler = begin_handler ();
13997   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13998   declaration = cp_parser_exception_declaration (parser);
13999   finish_handler_parms (declaration, handler);
14000   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14001   cp_parser_compound_statement (parser, NULL, false);
14002   finish_handler (handler);
14003 }
14004
14005 /* Parse an exception-declaration.
14006
14007    exception-declaration:
14008      type-specifier-seq declarator
14009      type-specifier-seq abstract-declarator
14010      type-specifier-seq
14011      ...
14012
14013    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14014    ellipsis variant is used.  */
14015
14016 static tree
14017 cp_parser_exception_declaration (cp_parser* parser)
14018 {
14019   tree decl;
14020   cp_decl_specifier_seq type_specifiers;
14021   cp_declarator *declarator;
14022   const char *saved_message;
14023
14024   /* If it's an ellipsis, it's easy to handle.  */
14025   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14026     {
14027       /* Consume the `...' token.  */
14028       cp_lexer_consume_token (parser->lexer);
14029       return NULL_TREE;
14030     }
14031
14032   /* Types may not be defined in exception-declarations.  */
14033   saved_message = parser->type_definition_forbidden_message;
14034   parser->type_definition_forbidden_message
14035     = "types may not be defined in exception-declarations";
14036
14037   /* Parse the type-specifier-seq.  */
14038   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14039                                 &type_specifiers);
14040   /* If it's a `)', then there is no declarator.  */
14041   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14042     declarator = NULL;
14043   else
14044     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14045                                        /*ctor_dtor_or_conv_p=*/NULL,
14046                                        /*parenthesized_p=*/NULL,
14047                                        /*member_p=*/false);
14048
14049   /* Restore the saved message.  */
14050   parser->type_definition_forbidden_message = saved_message;
14051
14052   if (type_specifiers.any_specifiers_p)
14053     {
14054       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14055       if (decl == NULL_TREE)
14056         error ("invalid catch parameter");
14057     }
14058   else
14059     decl = NULL_TREE;
14060
14061   return decl;
14062 }
14063
14064 /* Parse a throw-expression.
14065
14066    throw-expression:
14067      throw assignment-expression [opt]
14068
14069    Returns a THROW_EXPR representing the throw-expression.  */
14070
14071 static tree
14072 cp_parser_throw_expression (cp_parser* parser)
14073 {
14074   tree expression;
14075   cp_token* token;
14076
14077   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14078   token = cp_lexer_peek_token (parser->lexer);
14079   /* Figure out whether or not there is an assignment-expression
14080      following the "throw" keyword.  */
14081   if (token->type == CPP_COMMA
14082       || token->type == CPP_SEMICOLON
14083       || token->type == CPP_CLOSE_PAREN
14084       || token->type == CPP_CLOSE_SQUARE
14085       || token->type == CPP_CLOSE_BRACE
14086       || token->type == CPP_COLON)
14087     expression = NULL_TREE;
14088   else
14089     expression = cp_parser_assignment_expression (parser,
14090                                                   /*cast_p=*/false);
14091
14092   return build_throw (expression);
14093 }
14094
14095 /* GNU Extensions */
14096
14097 /* Parse an (optional) asm-specification.
14098
14099    asm-specification:
14100      asm ( string-literal )
14101
14102    If the asm-specification is present, returns a STRING_CST
14103    corresponding to the string-literal.  Otherwise, returns
14104    NULL_TREE.  */
14105
14106 static tree
14107 cp_parser_asm_specification_opt (cp_parser* parser)
14108 {
14109   cp_token *token;
14110   tree asm_specification;
14111
14112   /* Peek at the next token.  */
14113   token = cp_lexer_peek_token (parser->lexer);
14114   /* If the next token isn't the `asm' keyword, then there's no
14115      asm-specification.  */
14116   if (!cp_parser_is_keyword (token, RID_ASM))
14117     return NULL_TREE;
14118
14119   /* Consume the `asm' token.  */
14120   cp_lexer_consume_token (parser->lexer);
14121   /* Look for the `('.  */
14122   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14123
14124   /* Look for the string-literal.  */
14125   asm_specification = cp_parser_string_literal (parser, false, false);
14126
14127   /* Look for the `)'.  */
14128   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14129
14130   return asm_specification;
14131 }
14132
14133 /* Parse an asm-operand-list.
14134
14135    asm-operand-list:
14136      asm-operand
14137      asm-operand-list , asm-operand
14138
14139    asm-operand:
14140      string-literal ( expression )
14141      [ string-literal ] string-literal ( expression )
14142
14143    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14144    each node is the expression.  The TREE_PURPOSE is itself a
14145    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14146    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14147    is a STRING_CST for the string literal before the parenthesis.  */
14148
14149 static tree
14150 cp_parser_asm_operand_list (cp_parser* parser)
14151 {
14152   tree asm_operands = NULL_TREE;
14153
14154   while (true)
14155     {
14156       tree string_literal;
14157       tree expression;
14158       tree name;
14159
14160       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14161         {
14162           /* Consume the `[' token.  */
14163           cp_lexer_consume_token (parser->lexer);
14164           /* Read the operand name.  */
14165           name = cp_parser_identifier (parser);
14166           if (name != error_mark_node)
14167             name = build_string (IDENTIFIER_LENGTH (name),
14168                                  IDENTIFIER_POINTER (name));
14169           /* Look for the closing `]'.  */
14170           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14171         }
14172       else
14173         name = NULL_TREE;
14174       /* Look for the string-literal.  */
14175       string_literal = cp_parser_string_literal (parser, false, false);
14176
14177       /* Look for the `('.  */
14178       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14179       /* Parse the expression.  */
14180       expression = cp_parser_expression (parser, /*cast_p=*/false);
14181       /* Look for the `)'.  */
14182       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14183
14184       /* Add this operand to the list.  */
14185       asm_operands = tree_cons (build_tree_list (name, string_literal),
14186                                 expression,
14187                                 asm_operands);
14188       /* If the next token is not a `,', there are no more
14189          operands.  */
14190       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14191         break;
14192       /* Consume the `,'.  */
14193       cp_lexer_consume_token (parser->lexer);
14194     }
14195
14196   return nreverse (asm_operands);
14197 }
14198
14199 /* Parse an asm-clobber-list.
14200
14201    asm-clobber-list:
14202      string-literal
14203      asm-clobber-list , string-literal
14204
14205    Returns a TREE_LIST, indicating the clobbers in the order that they
14206    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14207
14208 static tree
14209 cp_parser_asm_clobber_list (cp_parser* parser)
14210 {
14211   tree clobbers = NULL_TREE;
14212
14213   while (true)
14214     {
14215       tree string_literal;
14216
14217       /* Look for the string literal.  */
14218       string_literal = cp_parser_string_literal (parser, false, false);
14219       /* Add it to the list.  */
14220       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14221       /* If the next token is not a `,', then the list is
14222          complete.  */
14223       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14224         break;
14225       /* Consume the `,' token.  */
14226       cp_lexer_consume_token (parser->lexer);
14227     }
14228
14229   return clobbers;
14230 }
14231
14232 /* Parse an (optional) series of attributes.
14233
14234    attributes:
14235      attributes attribute
14236
14237    attribute:
14238      __attribute__ (( attribute-list [opt] ))
14239
14240    The return value is as for cp_parser_attribute_list.  */
14241
14242 static tree
14243 cp_parser_attributes_opt (cp_parser* parser)
14244 {
14245   tree attributes = NULL_TREE;
14246
14247   while (true)
14248     {
14249       cp_token *token;
14250       tree attribute_list;
14251
14252       /* Peek at the next token.  */
14253       token = cp_lexer_peek_token (parser->lexer);
14254       /* If it's not `__attribute__', then we're done.  */
14255       if (token->keyword != RID_ATTRIBUTE)
14256         break;
14257
14258       /* Consume the `__attribute__' keyword.  */
14259       cp_lexer_consume_token (parser->lexer);
14260       /* Look for the two `(' tokens.  */
14261       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14262       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14263
14264       /* Peek at the next token.  */
14265       token = cp_lexer_peek_token (parser->lexer);
14266       if (token->type != CPP_CLOSE_PAREN)
14267         /* Parse the attribute-list.  */
14268         attribute_list = cp_parser_attribute_list (parser);
14269       else
14270         /* If the next token is a `)', then there is no attribute
14271            list.  */
14272         attribute_list = NULL;
14273
14274       /* Look for the two `)' tokens.  */
14275       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14276       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14277
14278       /* Add these new attributes to the list.  */
14279       attributes = chainon (attributes, attribute_list);
14280     }
14281
14282   return attributes;
14283 }
14284
14285 /* Parse an attribute-list.
14286
14287    attribute-list:
14288      attribute
14289      attribute-list , attribute
14290
14291    attribute:
14292      identifier
14293      identifier ( identifier )
14294      identifier ( identifier , expression-list )
14295      identifier ( expression-list )
14296
14297    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14298    to an attribute.  The TREE_PURPOSE of each node is the identifier
14299    indicating which attribute is in use.  The TREE_VALUE represents
14300    the arguments, if any.  */
14301
14302 static tree
14303 cp_parser_attribute_list (cp_parser* parser)
14304 {
14305   tree attribute_list = NULL_TREE;
14306   bool save_translate_strings_p = parser->translate_strings_p;
14307
14308   parser->translate_strings_p = false;
14309   while (true)
14310     {
14311       cp_token *token;
14312       tree identifier;
14313       tree attribute;
14314
14315       /* Look for the identifier.  We also allow keywords here; for
14316          example `__attribute__ ((const))' is legal.  */
14317       token = cp_lexer_peek_token (parser->lexer);
14318       if (token->type == CPP_NAME
14319           || token->type == CPP_KEYWORD)
14320         {
14321           /* Consume the token.  */
14322           token = cp_lexer_consume_token (parser->lexer);
14323
14324           /* Save away the identifier that indicates which attribute
14325              this is.  */
14326           identifier = token->value;
14327           attribute = build_tree_list (identifier, NULL_TREE);
14328
14329           /* Peek at the next token.  */
14330           token = cp_lexer_peek_token (parser->lexer);
14331           /* If it's an `(', then parse the attribute arguments.  */
14332           if (token->type == CPP_OPEN_PAREN)
14333             {
14334               tree arguments;
14335
14336               arguments = (cp_parser_parenthesized_expression_list
14337                            (parser, true, /*cast_p=*/false,
14338                             /*non_constant_p=*/NULL));
14339               /* Save the identifier and arguments away.  */
14340               TREE_VALUE (attribute) = arguments;
14341             }
14342
14343           /* Add this attribute to the list.  */
14344           TREE_CHAIN (attribute) = attribute_list;
14345           attribute_list = attribute;
14346
14347           token = cp_lexer_peek_token (parser->lexer);
14348         }
14349       /* Now, look for more attributes.  If the next token isn't a
14350          `,', we're done.  */
14351       if (token->type != CPP_COMMA)
14352         break;
14353
14354       /* Consume the comma and keep going.  */
14355       cp_lexer_consume_token (parser->lexer);
14356     }
14357   parser->translate_strings_p = save_translate_strings_p;
14358
14359   /* We built up the list in reverse order.  */
14360   return nreverse (attribute_list);
14361 }
14362
14363 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14364    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14365    current value of the PEDANTIC flag, regardless of whether or not
14366    the `__extension__' keyword is present.  The caller is responsible
14367    for restoring the value of the PEDANTIC flag.  */
14368
14369 static bool
14370 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14371 {
14372   /* Save the old value of the PEDANTIC flag.  */
14373   *saved_pedantic = pedantic;
14374
14375   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14376     {
14377       /* Consume the `__extension__' token.  */
14378       cp_lexer_consume_token (parser->lexer);
14379       /* We're not being pedantic while the `__extension__' keyword is
14380          in effect.  */
14381       pedantic = 0;
14382
14383       return true;
14384     }
14385
14386   return false;
14387 }
14388
14389 /* Parse a label declaration.
14390
14391    label-declaration:
14392      __label__ label-declarator-seq ;
14393
14394    label-declarator-seq:
14395      identifier , label-declarator-seq
14396      identifier  */
14397
14398 static void
14399 cp_parser_label_declaration (cp_parser* parser)
14400 {
14401   /* Look for the `__label__' keyword.  */
14402   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14403
14404   while (true)
14405     {
14406       tree identifier;
14407
14408       /* Look for an identifier.  */
14409       identifier = cp_parser_identifier (parser);
14410       /* If we failed, stop.  */
14411       if (identifier == error_mark_node)
14412         break;
14413       /* Declare it as a label.  */
14414       finish_label_decl (identifier);
14415       /* If the next token is a `;', stop.  */
14416       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14417         break;
14418       /* Look for the `,' separating the label declarations.  */
14419       cp_parser_require (parser, CPP_COMMA, "`,'");
14420     }
14421
14422   /* Look for the final `;'.  */
14423   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14424 }
14425
14426 /* Support Functions */
14427
14428 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14429    NAME should have one of the representations used for an
14430    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14431    is returned.  If PARSER->SCOPE is a dependent type, then a
14432    SCOPE_REF is returned.
14433
14434    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14435    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14436    was formed.  Abstractly, such entities should not be passed to this
14437    function, because they do not need to be looked up, but it is
14438    simpler to check for this special case here, rather than at the
14439    call-sites.
14440
14441    In cases not explicitly covered above, this function returns a
14442    DECL, OVERLOAD, or baselink representing the result of the lookup.
14443    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14444    is returned.
14445
14446    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14447    (e.g., "struct") that was used.  In that case bindings that do not
14448    refer to types are ignored.
14449
14450    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14451    ignored.
14452
14453    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14454    are ignored.
14455
14456    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14457    types.
14458
14459    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14460    results in an ambiguity, and false otherwise.  */
14461
14462 static tree
14463 cp_parser_lookup_name (cp_parser *parser, tree name,
14464                        enum tag_types tag_type,
14465                        bool is_template, bool is_namespace,
14466                        bool check_dependency,
14467                        bool *ambiguous_p)
14468 {
14469   int flags = 0;
14470   tree decl;
14471   tree object_type = parser->context->object_type;
14472
14473   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14474     flags |= LOOKUP_COMPLAIN;
14475
14476   /* Assume that the lookup will be unambiguous.  */
14477   if (ambiguous_p)
14478     *ambiguous_p = false;
14479
14480   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14481      no longer valid.  Note that if we are parsing tentatively, and
14482      the parse fails, OBJECT_TYPE will be automatically restored.  */
14483   parser->context->object_type = NULL_TREE;
14484
14485   if (name == error_mark_node)
14486     return error_mark_node;
14487
14488   /* A template-id has already been resolved; there is no lookup to
14489      do.  */
14490   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14491     return name;
14492   if (BASELINK_P (name))
14493     {
14494       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14495                   == TEMPLATE_ID_EXPR);
14496       return name;
14497     }
14498
14499   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14500      it should already have been checked to make sure that the name
14501      used matches the type being destroyed.  */
14502   if (TREE_CODE (name) == BIT_NOT_EXPR)
14503     {
14504       tree type;
14505
14506       /* Figure out to which type this destructor applies.  */
14507       if (parser->scope)
14508         type = parser->scope;
14509       else if (object_type)
14510         type = object_type;
14511       else
14512         type = current_class_type;
14513       /* If that's not a class type, there is no destructor.  */
14514       if (!type || !CLASS_TYPE_P (type))
14515         return error_mark_node;
14516       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14517         lazily_declare_fn (sfk_destructor, type);
14518       if (!CLASSTYPE_DESTRUCTORS (type))
14519           return error_mark_node;
14520       /* If it was a class type, return the destructor.  */
14521       return CLASSTYPE_DESTRUCTORS (type);
14522     }
14523
14524   /* By this point, the NAME should be an ordinary identifier.  If
14525      the id-expression was a qualified name, the qualifying scope is
14526      stored in PARSER->SCOPE at this point.  */
14527   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14528
14529   /* Perform the lookup.  */
14530   if (parser->scope)
14531     {
14532       bool dependent_p;
14533
14534       if (parser->scope == error_mark_node)
14535         return error_mark_node;
14536
14537       /* If the SCOPE is dependent, the lookup must be deferred until
14538          the template is instantiated -- unless we are explicitly
14539          looking up names in uninstantiated templates.  Even then, we
14540          cannot look up the name if the scope is not a class type; it
14541          might, for example, be a template type parameter.  */
14542       dependent_p = (TYPE_P (parser->scope)
14543                      && !(parser->in_declarator_p
14544                           && currently_open_class (parser->scope))
14545                      && dependent_type_p (parser->scope));
14546       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14547            && dependent_p)
14548         {
14549           if (tag_type)
14550             {
14551               tree type;
14552
14553               /* The resolution to Core Issue 180 says that `struct
14554                  A::B' should be considered a type-name, even if `A'
14555                  is dependent.  */
14556               type = make_typename_type (parser->scope, name, tag_type,
14557                                          /*complain=*/1);
14558               decl = TYPE_NAME (type);
14559             }
14560           else if (is_template)
14561             decl = make_unbound_class_template (parser->scope,
14562                                                 name, NULL_TREE,
14563                                                 /*complain=*/1);
14564           else
14565             decl = build_nt (SCOPE_REF, parser->scope, name);
14566         }
14567       else
14568         {
14569           tree pushed_scope = NULL_TREE;
14570
14571           /* If PARSER->SCOPE is a dependent type, then it must be a
14572              class type, and we must not be checking dependencies;
14573              otherwise, we would have processed this lookup above.  So
14574              that PARSER->SCOPE is not considered a dependent base by
14575              lookup_member, we must enter the scope here.  */
14576           if (dependent_p)
14577             pushed_scope = push_scope (parser->scope);
14578           /* If the PARSER->SCOPE is a template specialization, it
14579              may be instantiated during name lookup.  In that case,
14580              errors may be issued.  Even if we rollback the current
14581              tentative parse, those errors are valid.  */
14582           decl = lookup_qualified_name (parser->scope, name,
14583                                         tag_type != none_type,
14584                                         /*complain=*/true);
14585           if (pushed_scope)
14586             pop_scope (pushed_scope);
14587         }
14588       parser->qualifying_scope = parser->scope;
14589       parser->object_scope = NULL_TREE;
14590     }
14591   else if (object_type)
14592     {
14593       tree object_decl = NULL_TREE;
14594       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14595          OBJECT_TYPE is not a class.  */
14596       if (CLASS_TYPE_P (object_type))
14597         /* If the OBJECT_TYPE is a template specialization, it may
14598            be instantiated during name lookup.  In that case, errors
14599            may be issued.  Even if we rollback the current tentative
14600            parse, those errors are valid.  */
14601         object_decl = lookup_member (object_type,
14602                                      name,
14603                                      /*protect=*/0,
14604                                      tag_type != none_type);
14605       /* Look it up in the enclosing context, too.  */
14606       decl = lookup_name_real (name, tag_type != none_type,
14607                                /*nonclass=*/0,
14608                                /*block_p=*/true, is_namespace, flags);
14609       parser->object_scope = object_type;
14610       parser->qualifying_scope = NULL_TREE;
14611       if (object_decl)
14612         decl = object_decl;
14613     }
14614   else
14615     {
14616       decl = lookup_name_real (name, tag_type != none_type,
14617                                /*nonclass=*/0,
14618                                /*block_p=*/true, is_namespace, flags);
14619       parser->qualifying_scope = NULL_TREE;
14620       parser->object_scope = NULL_TREE;
14621     }
14622
14623   /* If the lookup failed, let our caller know.  */
14624   if (!decl || decl == error_mark_node)
14625     return error_mark_node;
14626
14627   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14628   if (TREE_CODE (decl) == TREE_LIST)
14629     {
14630       if (ambiguous_p)
14631         *ambiguous_p = true;
14632       /* The error message we have to print is too complicated for
14633          cp_parser_error, so we incorporate its actions directly.  */
14634       if (!cp_parser_simulate_error (parser))
14635         {
14636           error ("reference to %qD is ambiguous", name);
14637           print_candidates (decl);
14638         }
14639       return error_mark_node;
14640     }
14641
14642   gcc_assert (DECL_P (decl)
14643               || TREE_CODE (decl) == OVERLOAD
14644               || TREE_CODE (decl) == SCOPE_REF
14645               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14646               || BASELINK_P (decl));
14647
14648   /* If we have resolved the name of a member declaration, check to
14649      see if the declaration is accessible.  When the name resolves to
14650      set of overloaded functions, accessibility is checked when
14651      overload resolution is done.
14652
14653      During an explicit instantiation, access is not checked at all,
14654      as per [temp.explicit].  */
14655   if (DECL_P (decl))
14656     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14657
14658   return decl;
14659 }
14660
14661 /* Like cp_parser_lookup_name, but for use in the typical case where
14662    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14663    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14664
14665 static tree
14666 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14667 {
14668   return cp_parser_lookup_name (parser, name,
14669                                 none_type,
14670                                 /*is_template=*/false,
14671                                 /*is_namespace=*/false,
14672                                 /*check_dependency=*/true,
14673                                 /*ambiguous_p=*/NULL);
14674 }
14675
14676 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14677    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14678    true, the DECL indicates the class being defined in a class-head,
14679    or declared in an elaborated-type-specifier.
14680
14681    Otherwise, return DECL.  */
14682
14683 static tree
14684 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14685 {
14686   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14687      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14688
14689        struct A {
14690          template <typename T> struct B;
14691        };
14692
14693        template <typename T> struct A::B {};
14694
14695      Similarly, in an elaborated-type-specifier:
14696
14697        namespace N { struct X{}; }
14698
14699        struct A {
14700          template <typename T> friend struct N::X;
14701        };
14702
14703      However, if the DECL refers to a class type, and we are in
14704      the scope of the class, then the name lookup automatically
14705      finds the TYPE_DECL created by build_self_reference rather
14706      than a TEMPLATE_DECL.  For example, in:
14707
14708        template <class T> struct S {
14709          S s;
14710        };
14711
14712      there is no need to handle such case.  */
14713
14714   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14715     return DECL_TEMPLATE_RESULT (decl);
14716
14717   return decl;
14718 }
14719
14720 /* If too many, or too few, template-parameter lists apply to the
14721    declarator, issue an error message.  Returns TRUE if all went well,
14722    and FALSE otherwise.  */
14723
14724 static bool
14725 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14726                                                 cp_declarator *declarator)
14727 {
14728   unsigned num_templates;
14729
14730   /* We haven't seen any classes that involve template parameters yet.  */
14731   num_templates = 0;
14732
14733   switch (declarator->kind)
14734     {
14735     case cdk_id:
14736       if (declarator->u.id.qualifying_scope)
14737         {
14738           tree scope;
14739           tree member;
14740
14741           scope = declarator->u.id.qualifying_scope;
14742           member = declarator->u.id.unqualified_name;
14743
14744           while (scope && CLASS_TYPE_P (scope))
14745             {
14746               /* You're supposed to have one `template <...>'
14747                  for every template class, but you don't need one
14748                  for a full specialization.  For example:
14749
14750                  template <class T> struct S{};
14751                  template <> struct S<int> { void f(); };
14752                  void S<int>::f () {}
14753
14754                  is correct; there shouldn't be a `template <>' for
14755                  the definition of `S<int>::f'.  */
14756               if (CLASSTYPE_TEMPLATE_INFO (scope)
14757                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14758                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14759                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14760                 ++num_templates;
14761
14762               scope = TYPE_CONTEXT (scope);
14763             }
14764         }
14765       else if (TREE_CODE (declarator->u.id.unqualified_name)
14766                == TEMPLATE_ID_EXPR)
14767         /* If the DECLARATOR has the form `X<y>' then it uses one
14768            additional level of template parameters.  */
14769         ++num_templates;
14770
14771       return cp_parser_check_template_parameters (parser,
14772                                                   num_templates);
14773
14774     case cdk_function:
14775     case cdk_array:
14776     case cdk_pointer:
14777     case cdk_reference:
14778     case cdk_ptrmem:
14779       return (cp_parser_check_declarator_template_parameters
14780               (parser, declarator->declarator));
14781
14782     case cdk_error:
14783       return true;
14784
14785     default:
14786       gcc_unreachable ();
14787     }
14788   return false;
14789 }
14790
14791 /* NUM_TEMPLATES were used in the current declaration.  If that is
14792    invalid, return FALSE and issue an error messages.  Otherwise,
14793    return TRUE.  */
14794
14795 static bool
14796 cp_parser_check_template_parameters (cp_parser* parser,
14797                                      unsigned num_templates)
14798 {
14799   /* If there are more template classes than parameter lists, we have
14800      something like:
14801
14802        template <class T> void S<T>::R<T>::f ();  */
14803   if (parser->num_template_parameter_lists < num_templates)
14804     {
14805       error ("too few template-parameter-lists");
14806       return false;
14807     }
14808   /* If there are the same number of template classes and parameter
14809      lists, that's OK.  */
14810   if (parser->num_template_parameter_lists == num_templates)
14811     return true;
14812   /* If there are more, but only one more, then we are referring to a
14813      member template.  That's OK too.  */
14814   if (parser->num_template_parameter_lists == num_templates + 1)
14815       return true;
14816   /* Otherwise, there are too many template parameter lists.  We have
14817      something like:
14818
14819      template <class T> template <class U> void S::f();  */
14820   error ("too many template-parameter-lists");
14821   return false;
14822 }
14823
14824 /* Parse an optional `::' token indicating that the following name is
14825    from the global namespace.  If so, PARSER->SCOPE is set to the
14826    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14827    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14828    Returns the new value of PARSER->SCOPE, if the `::' token is
14829    present, and NULL_TREE otherwise.  */
14830
14831 static tree
14832 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14833 {
14834   cp_token *token;
14835
14836   /* Peek at the next token.  */
14837   token = cp_lexer_peek_token (parser->lexer);
14838   /* If we're looking at a `::' token then we're starting from the
14839      global namespace, not our current location.  */
14840   if (token->type == CPP_SCOPE)
14841     {
14842       /* Consume the `::' token.  */
14843       cp_lexer_consume_token (parser->lexer);
14844       /* Set the SCOPE so that we know where to start the lookup.  */
14845       parser->scope = global_namespace;
14846       parser->qualifying_scope = global_namespace;
14847       parser->object_scope = NULL_TREE;
14848
14849       return parser->scope;
14850     }
14851   else if (!current_scope_valid_p)
14852     {
14853       parser->scope = NULL_TREE;
14854       parser->qualifying_scope = NULL_TREE;
14855       parser->object_scope = NULL_TREE;
14856     }
14857
14858   return NULL_TREE;
14859 }
14860
14861 /* Returns TRUE if the upcoming token sequence is the start of a
14862    constructor declarator.  If FRIEND_P is true, the declarator is
14863    preceded by the `friend' specifier.  */
14864
14865 static bool
14866 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14867 {
14868   bool constructor_p;
14869   tree type_decl = NULL_TREE;
14870   bool nested_name_p;
14871   cp_token *next_token;
14872
14873   /* The common case is that this is not a constructor declarator, so
14874      try to avoid doing lots of work if at all possible.  It's not
14875      valid declare a constructor at function scope.  */
14876   if (at_function_scope_p ())
14877     return false;
14878   /* And only certain tokens can begin a constructor declarator.  */
14879   next_token = cp_lexer_peek_token (parser->lexer);
14880   if (next_token->type != CPP_NAME
14881       && next_token->type != CPP_SCOPE
14882       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14883       && next_token->type != CPP_TEMPLATE_ID)
14884     return false;
14885
14886   /* Parse tentatively; we are going to roll back all of the tokens
14887      consumed here.  */
14888   cp_parser_parse_tentatively (parser);
14889   /* Assume that we are looking at a constructor declarator.  */
14890   constructor_p = true;
14891
14892   /* Look for the optional `::' operator.  */
14893   cp_parser_global_scope_opt (parser,
14894                               /*current_scope_valid_p=*/false);
14895   /* Look for the nested-name-specifier.  */
14896   nested_name_p
14897     = (cp_parser_nested_name_specifier_opt (parser,
14898                                             /*typename_keyword_p=*/false,
14899                                             /*check_dependency_p=*/false,
14900                                             /*type_p=*/false,
14901                                             /*is_declaration=*/false)
14902        != NULL_TREE);
14903   /* Outside of a class-specifier, there must be a
14904      nested-name-specifier.  */
14905   if (!nested_name_p &&
14906       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14907        || friend_p))
14908     constructor_p = false;
14909   /* If we still think that this might be a constructor-declarator,
14910      look for a class-name.  */
14911   if (constructor_p)
14912     {
14913       /* If we have:
14914
14915            template <typename T> struct S { S(); };
14916            template <typename T> S<T>::S ();
14917
14918          we must recognize that the nested `S' names a class.
14919          Similarly, for:
14920
14921            template <typename T> S<T>::S<T> ();
14922
14923          we must recognize that the nested `S' names a template.  */
14924       type_decl = cp_parser_class_name (parser,
14925                                         /*typename_keyword_p=*/false,
14926                                         /*template_keyword_p=*/false,
14927                                         none_type,
14928                                         /*check_dependency_p=*/false,
14929                                         /*class_head_p=*/false,
14930                                         /*is_declaration=*/false);
14931       /* If there was no class-name, then this is not a constructor.  */
14932       constructor_p = !cp_parser_error_occurred (parser);
14933     }
14934
14935   /* If we're still considering a constructor, we have to see a `(',
14936      to begin the parameter-declaration-clause, followed by either a
14937      `)', an `...', or a decl-specifier.  We need to check for a
14938      type-specifier to avoid being fooled into thinking that:
14939
14940        S::S (f) (int);
14941
14942      is a constructor.  (It is actually a function named `f' that
14943      takes one parameter (of type `int') and returns a value of type
14944      `S::S'.  */
14945   if (constructor_p
14946       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14947     {
14948       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14949           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14950           /* A parameter declaration begins with a decl-specifier,
14951              which is either the "attribute" keyword, a storage class
14952              specifier, or (usually) a type-specifier.  */
14953           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14954           && !cp_parser_storage_class_specifier_opt (parser))
14955         {
14956           tree type;
14957           tree pushed_scope = NULL_TREE;
14958           unsigned saved_num_template_parameter_lists;
14959
14960           /* Names appearing in the type-specifier should be looked up
14961              in the scope of the class.  */
14962           if (current_class_type)
14963             type = NULL_TREE;
14964           else
14965             {
14966               type = TREE_TYPE (type_decl);
14967               if (TREE_CODE (type) == TYPENAME_TYPE)
14968                 {
14969                   type = resolve_typename_type (type,
14970                                                 /*only_current_p=*/false);
14971                   if (type == error_mark_node)
14972                     {
14973                       cp_parser_abort_tentative_parse (parser);
14974                       return false;
14975                     }
14976                 }
14977               pushed_scope = push_scope (type);
14978             }
14979
14980           /* Inside the constructor parameter list, surrounding
14981              template-parameter-lists do not apply.  */
14982           saved_num_template_parameter_lists
14983             = parser->num_template_parameter_lists;
14984           parser->num_template_parameter_lists = 0;
14985
14986           /* Look for the type-specifier.  */
14987           cp_parser_type_specifier (parser,
14988                                     CP_PARSER_FLAGS_NONE,
14989                                     /*decl_specs=*/NULL,
14990                                     /*is_declarator=*/true,
14991                                     /*declares_class_or_enum=*/NULL,
14992                                     /*is_cv_qualifier=*/NULL);
14993
14994           parser->num_template_parameter_lists
14995             = saved_num_template_parameter_lists;
14996
14997           /* Leave the scope of the class.  */
14998           if (pushed_scope)
14999             pop_scope (pushed_scope);
15000
15001           constructor_p = !cp_parser_error_occurred (parser);
15002         }
15003     }
15004   else
15005     constructor_p = false;
15006   /* We did not really want to consume any tokens.  */
15007   cp_parser_abort_tentative_parse (parser);
15008
15009   return constructor_p;
15010 }
15011
15012 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15013    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15014    they must be performed once we are in the scope of the function.
15015
15016    Returns the function defined.  */
15017
15018 static tree
15019 cp_parser_function_definition_from_specifiers_and_declarator
15020   (cp_parser* parser,
15021    cp_decl_specifier_seq *decl_specifiers,
15022    tree attributes,
15023    const cp_declarator *declarator)
15024 {
15025   tree fn;
15026   bool success_p;
15027
15028   /* Begin the function-definition.  */
15029   success_p = start_function (decl_specifiers, declarator, attributes);
15030
15031   /* The things we're about to see are not directly qualified by any
15032      template headers we've seen thus far.  */
15033   reset_specialization ();
15034
15035   /* If there were names looked up in the decl-specifier-seq that we
15036      did not check, check them now.  We must wait until we are in the
15037      scope of the function to perform the checks, since the function
15038      might be a friend.  */
15039   perform_deferred_access_checks ();
15040
15041   if (!success_p)
15042     {
15043       /* Skip the entire function.  */
15044       error ("invalid function declaration");
15045       cp_parser_skip_to_end_of_block_or_statement (parser);
15046       fn = error_mark_node;
15047     }
15048   else
15049     fn = cp_parser_function_definition_after_declarator (parser,
15050                                                          /*inline_p=*/false);
15051
15052   return fn;
15053 }
15054
15055 /* Parse the part of a function-definition that follows the
15056    declarator.  INLINE_P is TRUE iff this function is an inline
15057    function defined with a class-specifier.
15058
15059    Returns the function defined.  */
15060
15061 static tree
15062 cp_parser_function_definition_after_declarator (cp_parser* parser,
15063                                                 bool inline_p)
15064 {
15065   tree fn;
15066   bool ctor_initializer_p = false;
15067   bool saved_in_unbraced_linkage_specification_p;
15068   unsigned saved_num_template_parameter_lists;
15069
15070   /* If the next token is `return', then the code may be trying to
15071      make use of the "named return value" extension that G++ used to
15072      support.  */
15073   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15074     {
15075       /* Consume the `return' keyword.  */
15076       cp_lexer_consume_token (parser->lexer);
15077       /* Look for the identifier that indicates what value is to be
15078          returned.  */
15079       cp_parser_identifier (parser);
15080       /* Issue an error message.  */
15081       error ("named return values are no longer supported");
15082       /* Skip tokens until we reach the start of the function body.  */
15083       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15084              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15085         cp_lexer_consume_token (parser->lexer);
15086     }
15087   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15088      anything declared inside `f'.  */
15089   saved_in_unbraced_linkage_specification_p
15090     = parser->in_unbraced_linkage_specification_p;
15091   parser->in_unbraced_linkage_specification_p = false;
15092   /* Inside the function, surrounding template-parameter-lists do not
15093      apply.  */
15094   saved_num_template_parameter_lists
15095     = parser->num_template_parameter_lists;
15096   parser->num_template_parameter_lists = 0;
15097   /* If the next token is `try', then we are looking at a
15098      function-try-block.  */
15099   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15100     ctor_initializer_p = cp_parser_function_try_block (parser);
15101   /* A function-try-block includes the function-body, so we only do
15102      this next part if we're not processing a function-try-block.  */
15103   else
15104     ctor_initializer_p
15105       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15106
15107   /* Finish the function.  */
15108   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15109                         (inline_p ? 2 : 0));
15110   /* Generate code for it, if necessary.  */
15111   expand_or_defer_fn (fn);
15112   /* Restore the saved values.  */
15113   parser->in_unbraced_linkage_specification_p
15114     = saved_in_unbraced_linkage_specification_p;
15115   parser->num_template_parameter_lists
15116     = saved_num_template_parameter_lists;
15117
15118   return fn;
15119 }
15120
15121 /* Parse a template-declaration, assuming that the `export' (and
15122    `extern') keywords, if present, has already been scanned.  MEMBER_P
15123    is as for cp_parser_template_declaration.  */
15124
15125 static void
15126 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15127 {
15128   tree decl = NULL_TREE;
15129   tree parameter_list;
15130   bool friend_p = false;
15131
15132   /* Look for the `template' keyword.  */
15133   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15134     return;
15135
15136   /* And the `<'.  */
15137   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15138     return;
15139
15140   /* If the next token is `>', then we have an invalid
15141      specialization.  Rather than complain about an invalid template
15142      parameter, issue an error message here.  */
15143   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15144     {
15145       cp_parser_error (parser, "invalid explicit specialization");
15146       begin_specialization ();
15147       parameter_list = NULL_TREE;
15148     }
15149   else
15150     {
15151       /* Parse the template parameters.  */
15152       begin_template_parm_list ();
15153       parameter_list = cp_parser_template_parameter_list (parser);
15154       parameter_list = end_template_parm_list (parameter_list);
15155     }
15156
15157   /* Look for the `>'.  */
15158   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15159   /* We just processed one more parameter list.  */
15160   ++parser->num_template_parameter_lists;
15161   /* If the next token is `template', there are more template
15162      parameters.  */
15163   if (cp_lexer_next_token_is_keyword (parser->lexer,
15164                                       RID_TEMPLATE))
15165     cp_parser_template_declaration_after_export (parser, member_p);
15166   else
15167     {
15168       /* There are no access checks when parsing a template, as we do not
15169          know if a specialization will be a friend.  */
15170       push_deferring_access_checks (dk_no_check);
15171
15172       decl = cp_parser_single_declaration (parser,
15173                                            member_p,
15174                                            &friend_p);
15175
15176       pop_deferring_access_checks ();
15177
15178       /* If this is a member template declaration, let the front
15179          end know.  */
15180       if (member_p && !friend_p && decl)
15181         {
15182           if (TREE_CODE (decl) == TYPE_DECL)
15183             cp_parser_check_access_in_redeclaration (decl);
15184
15185           decl = finish_member_template_decl (decl);
15186         }
15187       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15188         make_friend_class (current_class_type, TREE_TYPE (decl),
15189                            /*complain=*/true);
15190     }
15191   /* We are done with the current parameter list.  */
15192   --parser->num_template_parameter_lists;
15193
15194   /* Finish up.  */
15195   finish_template_decl (parameter_list);
15196
15197   /* Register member declarations.  */
15198   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15199     finish_member_declaration (decl);
15200
15201   /* If DECL is a function template, we must return to parse it later.
15202      (Even though there is no definition, there might be default
15203      arguments that need handling.)  */
15204   if (member_p && decl
15205       && (TREE_CODE (decl) == FUNCTION_DECL
15206           || DECL_FUNCTION_TEMPLATE_P (decl)))
15207     TREE_VALUE (parser->unparsed_functions_queues)
15208       = tree_cons (NULL_TREE, decl,
15209                    TREE_VALUE (parser->unparsed_functions_queues));
15210 }
15211
15212 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15213    `function-definition' sequence.  MEMBER_P is true, this declaration
15214    appears in a class scope.
15215
15216    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15217    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15218
15219 static tree
15220 cp_parser_single_declaration (cp_parser* parser,
15221                               bool member_p,
15222                               bool* friend_p)
15223 {
15224   int declares_class_or_enum;
15225   tree decl = NULL_TREE;
15226   cp_decl_specifier_seq decl_specifiers;
15227   bool function_definition_p = false;
15228
15229   /* This function is only used when processing a template
15230      declaration.  */
15231   gcc_assert (innermost_scope_kind () == sk_template_parms
15232               || innermost_scope_kind () == sk_template_spec);
15233
15234   /* Defer access checks until we know what is being declared.  */
15235   push_deferring_access_checks (dk_deferred);
15236
15237   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15238      alternative.  */
15239   cp_parser_decl_specifier_seq (parser,
15240                                 CP_PARSER_FLAGS_OPTIONAL,
15241                                 &decl_specifiers,
15242                                 &declares_class_or_enum);
15243   if (friend_p)
15244     *friend_p = cp_parser_friend_p (&decl_specifiers);
15245
15246   /* There are no template typedefs.  */
15247   if (decl_specifiers.specs[(int) ds_typedef])
15248     {
15249       error ("template declaration of %qs", "typedef");
15250       decl = error_mark_node;
15251     }
15252
15253   /* Gather up the access checks that occurred the
15254      decl-specifier-seq.  */
15255   stop_deferring_access_checks ();
15256
15257   /* Check for the declaration of a template class.  */
15258   if (declares_class_or_enum)
15259     {
15260       if (cp_parser_declares_only_class_p (parser))
15261         {
15262           decl = shadow_tag (&decl_specifiers);
15263
15264           /* In this case:
15265
15266                struct C {
15267                  friend template <typename T> struct A<T>::B;
15268                };
15269
15270              A<T>::B will be represented by a TYPENAME_TYPE, and
15271              therefore not recognized by shadow_tag.  */
15272           if (friend_p && *friend_p
15273               && !decl
15274               && decl_specifiers.type
15275               && TYPE_P (decl_specifiers.type))
15276             decl = decl_specifiers.type;
15277
15278           if (decl && decl != error_mark_node)
15279             decl = TYPE_NAME (decl);
15280           else
15281             decl = error_mark_node;
15282         }
15283     }
15284   /* If it's not a template class, try for a template function.  If
15285      the next token is a `;', then this declaration does not declare
15286      anything.  But, if there were errors in the decl-specifiers, then
15287      the error might well have come from an attempted class-specifier.
15288      In that case, there's no need to warn about a missing declarator.  */
15289   if (!decl
15290       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15291           || decl_specifiers.type != error_mark_node))
15292     decl = cp_parser_init_declarator (parser,
15293                                       &decl_specifiers,
15294                                       /*function_definition_allowed_p=*/true,
15295                                       member_p,
15296                                       declares_class_or_enum,
15297                                       &function_definition_p);
15298
15299   pop_deferring_access_checks ();
15300
15301   /* Clear any current qualification; whatever comes next is the start
15302      of something new.  */
15303   parser->scope = NULL_TREE;
15304   parser->qualifying_scope = NULL_TREE;
15305   parser->object_scope = NULL_TREE;
15306   /* Look for a trailing `;' after the declaration.  */
15307   if (!function_definition_p
15308       && (decl == error_mark_node
15309           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15310     cp_parser_skip_to_end_of_block_or_statement (parser);
15311
15312   return decl;
15313 }
15314
15315 /* Parse a cast-expression that is not the operand of a unary "&".  */
15316
15317 static tree
15318 cp_parser_simple_cast_expression (cp_parser *parser)
15319 {
15320   return cp_parser_cast_expression (parser, /*address_p=*/false,
15321                                     /*cast_p=*/false);
15322 }
15323
15324 /* Parse a functional cast to TYPE.  Returns an expression
15325    representing the cast.  */
15326
15327 static tree
15328 cp_parser_functional_cast (cp_parser* parser, tree type)
15329 {
15330   tree expression_list;
15331   tree cast;
15332
15333   expression_list
15334     = cp_parser_parenthesized_expression_list (parser, false,
15335                                                /*cast_p=*/true,
15336                                                /*non_constant_p=*/NULL);
15337
15338   cast = build_functional_cast (type, expression_list);
15339   /* [expr.const]/1: In an integral constant expression "only type
15340      conversions to integral or enumeration type can be used".  */
15341   if (cast != error_mark_node && !type_dependent_expression_p (type)
15342       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15343     {
15344       if (cp_parser_non_integral_constant_expression
15345           (parser, "a call to a constructor"))
15346         return error_mark_node;
15347     }
15348   return cast;
15349 }
15350
15351 /* Save the tokens that make up the body of a member function defined
15352    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15353    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15354    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15355    for the member function.  */
15356
15357 static tree
15358 cp_parser_save_member_function_body (cp_parser* parser,
15359                                      cp_decl_specifier_seq *decl_specifiers,
15360                                      cp_declarator *declarator,
15361                                      tree attributes)
15362 {
15363   cp_token *first;
15364   cp_token *last;
15365   tree fn;
15366
15367   /* Create the function-declaration.  */
15368   fn = start_method (decl_specifiers, declarator, attributes);
15369   /* If something went badly wrong, bail out now.  */
15370   if (fn == error_mark_node)
15371     {
15372       /* If there's a function-body, skip it.  */
15373       if (cp_parser_token_starts_function_definition_p
15374           (cp_lexer_peek_token (parser->lexer)))
15375         cp_parser_skip_to_end_of_block_or_statement (parser);
15376       return error_mark_node;
15377     }
15378
15379   /* Remember it, if there default args to post process.  */
15380   cp_parser_save_default_args (parser, fn);
15381
15382   /* Save away the tokens that make up the body of the
15383      function.  */
15384   first = parser->lexer->next_token;
15385   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15386   /* Handle function try blocks.  */
15387   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15388     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15389   last = parser->lexer->next_token;
15390
15391   /* Save away the inline definition; we will process it when the
15392      class is complete.  */
15393   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15394   DECL_PENDING_INLINE_P (fn) = 1;
15395
15396   /* We need to know that this was defined in the class, so that
15397      friend templates are handled correctly.  */
15398   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15399
15400   /* We're done with the inline definition.  */
15401   finish_method (fn);
15402
15403   /* Add FN to the queue of functions to be parsed later.  */
15404   TREE_VALUE (parser->unparsed_functions_queues)
15405     = tree_cons (NULL_TREE, fn,
15406                  TREE_VALUE (parser->unparsed_functions_queues));
15407
15408   return fn;
15409 }
15410
15411 /* Parse a template-argument-list, as well as the trailing ">" (but
15412    not the opening ">").  See cp_parser_template_argument_list for the
15413    return value.  */
15414
15415 static tree
15416 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15417 {
15418   tree arguments;
15419   tree saved_scope;
15420   tree saved_qualifying_scope;
15421   tree saved_object_scope;
15422   bool saved_greater_than_is_operator_p;
15423   bool saved_skip_evaluation;
15424
15425   /* [temp.names]
15426
15427      When parsing a template-id, the first non-nested `>' is taken as
15428      the end of the template-argument-list rather than a greater-than
15429      operator.  */
15430   saved_greater_than_is_operator_p
15431     = parser->greater_than_is_operator_p;
15432   parser->greater_than_is_operator_p = false;
15433   /* Parsing the argument list may modify SCOPE, so we save it
15434      here.  */
15435   saved_scope = parser->scope;
15436   saved_qualifying_scope = parser->qualifying_scope;
15437   saved_object_scope = parser->object_scope;
15438   /* We need to evaluate the template arguments, even though this
15439      template-id may be nested within a "sizeof".  */
15440   saved_skip_evaluation = skip_evaluation;
15441   skip_evaluation = false;
15442   /* Parse the template-argument-list itself.  */
15443   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15444     arguments = NULL_TREE;
15445   else
15446     arguments = cp_parser_template_argument_list (parser);
15447   /* Look for the `>' that ends the template-argument-list. If we find
15448      a '>>' instead, it's probably just a typo.  */
15449   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15450     {
15451       if (!saved_greater_than_is_operator_p)
15452         {
15453           /* If we're in a nested template argument list, the '>>' has
15454             to be a typo for '> >'. We emit the error message, but we
15455             continue parsing and we push a '>' as next token, so that
15456             the argument list will be parsed correctly.  Note that the
15457             global source location is still on the token before the
15458             '>>', so we need to say explicitly where we want it.  */
15459           cp_token *token = cp_lexer_peek_token (parser->lexer);
15460           error ("%H%<>>%> should be %<> >%> "
15461                  "within a nested template argument list",
15462                  &token->location);
15463
15464           /* ??? Proper recovery should terminate two levels of
15465              template argument list here.  */
15466           token->type = CPP_GREATER;
15467         }
15468       else
15469         {
15470           /* If this is not a nested template argument list, the '>>'
15471             is a typo for '>'. Emit an error message and continue.
15472             Same deal about the token location, but here we can get it
15473             right by consuming the '>>' before issuing the diagnostic.  */
15474           cp_lexer_consume_token (parser->lexer);
15475           error ("spurious %<>>%>, use %<>%> to terminate "
15476                  "a template argument list");
15477         }
15478     }
15479   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15480     error ("missing %<>%> to terminate the template argument list");
15481   else
15482     /* It's what we want, a '>'; consume it.  */
15483     cp_lexer_consume_token (parser->lexer);
15484   /* The `>' token might be a greater-than operator again now.  */
15485   parser->greater_than_is_operator_p
15486     = saved_greater_than_is_operator_p;
15487   /* Restore the SAVED_SCOPE.  */
15488   parser->scope = saved_scope;
15489   parser->qualifying_scope = saved_qualifying_scope;
15490   parser->object_scope = saved_object_scope;
15491   skip_evaluation = saved_skip_evaluation;
15492
15493   return arguments;
15494 }
15495
15496 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15497    arguments, or the body of the function have not yet been parsed,
15498    parse them now.  */
15499
15500 static void
15501 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15502 {
15503   /* If this member is a template, get the underlying
15504      FUNCTION_DECL.  */
15505   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15506     member_function = DECL_TEMPLATE_RESULT (member_function);
15507
15508   /* There should not be any class definitions in progress at this
15509      point; the bodies of members are only parsed outside of all class
15510      definitions.  */
15511   gcc_assert (parser->num_classes_being_defined == 0);
15512   /* While we're parsing the member functions we might encounter more
15513      classes.  We want to handle them right away, but we don't want
15514      them getting mixed up with functions that are currently in the
15515      queue.  */
15516   parser->unparsed_functions_queues
15517     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15518
15519   /* Make sure that any template parameters are in scope.  */
15520   maybe_begin_member_template_processing (member_function);
15521
15522   /* If the body of the function has not yet been parsed, parse it
15523      now.  */
15524   if (DECL_PENDING_INLINE_P (member_function))
15525     {
15526       tree function_scope;
15527       cp_token_cache *tokens;
15528
15529       /* The function is no longer pending; we are processing it.  */
15530       tokens = DECL_PENDING_INLINE_INFO (member_function);
15531       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15532       DECL_PENDING_INLINE_P (member_function) = 0;
15533
15534       /* If this is a local class, enter the scope of the containing
15535          function.  */
15536       function_scope = current_function_decl;
15537       if (function_scope)
15538         push_function_context_to (function_scope);
15539
15540
15541       /* Push the body of the function onto the lexer stack.  */
15542       cp_parser_push_lexer_for_tokens (parser, tokens);
15543
15544       /* Let the front end know that we going to be defining this
15545          function.  */
15546       start_preparsed_function (member_function, NULL_TREE,
15547                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15548
15549       /* Don't do access checking if it is a templated function.  */
15550       if (processing_template_decl)
15551         push_deferring_access_checks (dk_no_check);
15552
15553       /* Now, parse the body of the function.  */
15554       cp_parser_function_definition_after_declarator (parser,
15555                                                       /*inline_p=*/true);
15556
15557       if (processing_template_decl)
15558         pop_deferring_access_checks ();
15559
15560       /* Leave the scope of the containing function.  */
15561       if (function_scope)
15562         pop_function_context_from (function_scope);
15563       cp_parser_pop_lexer (parser);
15564     }
15565
15566   /* Remove any template parameters from the symbol table.  */
15567   maybe_end_member_template_processing ();
15568
15569   /* Restore the queue.  */
15570   parser->unparsed_functions_queues
15571     = TREE_CHAIN (parser->unparsed_functions_queues);
15572 }
15573
15574 /* If DECL contains any default args, remember it on the unparsed
15575    functions queue.  */
15576
15577 static void
15578 cp_parser_save_default_args (cp_parser* parser, tree decl)
15579 {
15580   tree probe;
15581
15582   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15583        probe;
15584        probe = TREE_CHAIN (probe))
15585     if (TREE_PURPOSE (probe))
15586       {
15587         TREE_PURPOSE (parser->unparsed_functions_queues)
15588           = tree_cons (current_class_type, decl,
15589                        TREE_PURPOSE (parser->unparsed_functions_queues));
15590         break;
15591       }
15592   return;
15593 }
15594
15595 /* FN is a FUNCTION_DECL which may contains a parameter with an
15596    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15597    assumes that the current scope is the scope in which the default
15598    argument should be processed.  */
15599
15600 static void
15601 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15602 {
15603   bool saved_local_variables_forbidden_p;
15604   tree parm;
15605
15606   /* While we're parsing the default args, we might (due to the
15607      statement expression extension) encounter more classes.  We want
15608      to handle them right away, but we don't want them getting mixed
15609      up with default args that are currently in the queue.  */
15610   parser->unparsed_functions_queues
15611     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15612
15613   /* Local variable names (and the `this' keyword) may not appear
15614      in a default argument.  */
15615   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15616   parser->local_variables_forbidden_p = true;
15617
15618   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15619        parm;
15620        parm = TREE_CHAIN (parm))
15621     {
15622       cp_token_cache *tokens;
15623       tree default_arg = TREE_PURPOSE (parm);
15624       tree parsed_arg;
15625       VEC(tree,gc) *insts;
15626       tree copy;
15627       unsigned ix;
15628
15629       if (!default_arg)
15630         continue;
15631
15632       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15633         /* This can happen for a friend declaration for a function
15634            already declared with default arguments.  */
15635         continue;
15636
15637        /* Push the saved tokens for the default argument onto the parser's
15638           lexer stack.  */
15639       tokens = DEFARG_TOKENS (default_arg);
15640       cp_parser_push_lexer_for_tokens (parser, tokens);
15641
15642       /* Parse the assignment-expression.  */
15643       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15644
15645       TREE_PURPOSE (parm) = parsed_arg;
15646
15647       /* Update any instantiations we've already created.  */
15648       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15649            VEC_iterate (tree, insts, ix, copy); ix++)
15650         TREE_PURPOSE (copy) = parsed_arg;
15651
15652       /* If the token stream has not been completely used up, then
15653          there was extra junk after the end of the default
15654          argument.  */
15655       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15656         cp_parser_error (parser, "expected %<,%>");
15657
15658       /* Revert to the main lexer.  */
15659       cp_parser_pop_lexer (parser);
15660     }
15661
15662   /* Restore the state of local_variables_forbidden_p.  */
15663   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15664
15665   /* Restore the queue.  */
15666   parser->unparsed_functions_queues
15667     = TREE_CHAIN (parser->unparsed_functions_queues);
15668 }
15669
15670 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15671    either a TYPE or an expression, depending on the form of the
15672    input.  The KEYWORD indicates which kind of expression we have
15673    encountered.  */
15674
15675 static tree
15676 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15677 {
15678   static const char *format;
15679   tree expr = NULL_TREE;
15680   const char *saved_message;
15681   bool saved_integral_constant_expression_p;
15682   bool saved_non_integral_constant_expression_p;
15683
15684   /* Initialize FORMAT the first time we get here.  */
15685   if (!format)
15686     format = "types may not be defined in '%s' expressions";
15687
15688   /* Types cannot be defined in a `sizeof' expression.  Save away the
15689      old message.  */
15690   saved_message = parser->type_definition_forbidden_message;
15691   /* And create the new one.  */
15692   parser->type_definition_forbidden_message
15693     = xmalloc (strlen (format)
15694                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15695                + 1 /* `\0' */);
15696   sprintf ((char *) parser->type_definition_forbidden_message,
15697            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15698
15699   /* The restrictions on constant-expressions do not apply inside
15700      sizeof expressions.  */
15701   saved_integral_constant_expression_p
15702     = parser->integral_constant_expression_p;
15703   saved_non_integral_constant_expression_p
15704     = parser->non_integral_constant_expression_p;
15705   parser->integral_constant_expression_p = false;
15706
15707   /* Do not actually evaluate the expression.  */
15708   ++skip_evaluation;
15709   /* If it's a `(', then we might be looking at the type-id
15710      construction.  */
15711   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15712     {
15713       tree type;
15714       bool saved_in_type_id_in_expr_p;
15715
15716       /* We can't be sure yet whether we're looking at a type-id or an
15717          expression.  */
15718       cp_parser_parse_tentatively (parser);
15719       /* Consume the `('.  */
15720       cp_lexer_consume_token (parser->lexer);
15721       /* Parse the type-id.  */
15722       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15723       parser->in_type_id_in_expr_p = true;
15724       type = cp_parser_type_id (parser);
15725       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15726       /* Now, look for the trailing `)'.  */
15727       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15728       /* If all went well, then we're done.  */
15729       if (cp_parser_parse_definitely (parser))
15730         {
15731           cp_decl_specifier_seq decl_specs;
15732
15733           /* Build a trivial decl-specifier-seq.  */
15734           clear_decl_specs (&decl_specs);
15735           decl_specs.type = type;
15736
15737           /* Call grokdeclarator to figure out what type this is.  */
15738           expr = grokdeclarator (NULL,
15739                                  &decl_specs,
15740                                  TYPENAME,
15741                                  /*initialized=*/0,
15742                                  /*attrlist=*/NULL);
15743         }
15744     }
15745
15746   /* If the type-id production did not work out, then we must be
15747      looking at the unary-expression production.  */
15748   if (!expr)
15749     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15750                                        /*cast_p=*/false);
15751   /* Go back to evaluating expressions.  */
15752   --skip_evaluation;
15753
15754   /* Free the message we created.  */
15755   free ((char *) parser->type_definition_forbidden_message);
15756   /* And restore the old one.  */
15757   parser->type_definition_forbidden_message = saved_message;
15758   parser->integral_constant_expression_p
15759     = saved_integral_constant_expression_p;
15760   parser->non_integral_constant_expression_p
15761     = saved_non_integral_constant_expression_p;
15762
15763   return expr;
15764 }
15765
15766 /* If the current declaration has no declarator, return true.  */
15767
15768 static bool
15769 cp_parser_declares_only_class_p (cp_parser *parser)
15770 {
15771   /* If the next token is a `;' or a `,' then there is no
15772      declarator.  */
15773   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15774           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15775 }
15776
15777 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15778
15779 static void
15780 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15781                              cp_storage_class storage_class)
15782 {
15783   if (decl_specs->storage_class != sc_none)
15784     decl_specs->multiple_storage_classes_p = true;
15785   else
15786     decl_specs->storage_class = storage_class;
15787 }
15788
15789 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15790    is true, the type is a user-defined type; otherwise it is a
15791    built-in type specified by a keyword.  */
15792
15793 static void
15794 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15795                               tree type_spec,
15796                               bool user_defined_p)
15797 {
15798   decl_specs->any_specifiers_p = true;
15799
15800   /* If the user tries to redeclare bool or wchar_t (with, for
15801      example, in "typedef int wchar_t;") we remember that this is what
15802      happened.  In system headers, we ignore these declarations so
15803      that G++ can work with system headers that are not C++-safe.  */
15804   if (decl_specs->specs[(int) ds_typedef]
15805       && !user_defined_p
15806       && (type_spec == boolean_type_node
15807           || type_spec == wchar_type_node)
15808       && (decl_specs->type
15809           || decl_specs->specs[(int) ds_long]
15810           || decl_specs->specs[(int) ds_short]
15811           || decl_specs->specs[(int) ds_unsigned]
15812           || decl_specs->specs[(int) ds_signed]))
15813     {
15814       decl_specs->redefined_builtin_type = type_spec;
15815       if (!decl_specs->type)
15816         {
15817           decl_specs->type = type_spec;
15818           decl_specs->user_defined_type_p = false;
15819         }
15820     }
15821   else if (decl_specs->type)
15822     decl_specs->multiple_types_p = true;
15823   else
15824     {
15825       decl_specs->type = type_spec;
15826       decl_specs->user_defined_type_p = user_defined_p;
15827       decl_specs->redefined_builtin_type = NULL_TREE;
15828     }
15829 }
15830
15831 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15832    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15833
15834 static bool
15835 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15836 {
15837   return decl_specifiers->specs[(int) ds_friend] != 0;
15838 }
15839
15840 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15841    issue an error message indicating that TOKEN_DESC was expected.
15842
15843    Returns the token consumed, if the token had the appropriate type.
15844    Otherwise, returns NULL.  */
15845
15846 static cp_token *
15847 cp_parser_require (cp_parser* parser,
15848                    enum cpp_ttype type,
15849                    const char* token_desc)
15850 {
15851   if (cp_lexer_next_token_is (parser->lexer, type))
15852     return cp_lexer_consume_token (parser->lexer);
15853   else
15854     {
15855       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15856       if (!cp_parser_simulate_error (parser))
15857         {
15858           char *message = concat ("expected ", token_desc, NULL);
15859           cp_parser_error (parser, message);
15860           free (message);
15861         }
15862       return NULL;
15863     }
15864 }
15865
15866 /* Like cp_parser_require, except that tokens will be skipped until
15867    the desired token is found.  An error message is still produced if
15868    the next token is not as expected.  */
15869
15870 static void
15871 cp_parser_skip_until_found (cp_parser* parser,
15872                             enum cpp_ttype type,
15873                             const char* token_desc)
15874 {
15875   cp_token *token;
15876   unsigned nesting_depth = 0;
15877
15878   if (cp_parser_require (parser, type, token_desc))
15879     return;
15880
15881   /* Skip tokens until the desired token is found.  */
15882   while (true)
15883     {
15884       /* Peek at the next token.  */
15885       token = cp_lexer_peek_token (parser->lexer);
15886       /* If we've reached the token we want, consume it and
15887          stop.  */
15888       if (token->type == type && !nesting_depth)
15889         {
15890           cp_lexer_consume_token (parser->lexer);
15891           return;
15892         }
15893       /* If we've run out of tokens, stop.  */
15894       if (token->type == CPP_EOF)
15895         return;
15896       if (token->type == CPP_OPEN_BRACE
15897           || token->type == CPP_OPEN_PAREN
15898           || token->type == CPP_OPEN_SQUARE)
15899         ++nesting_depth;
15900       else if (token->type == CPP_CLOSE_BRACE
15901                || token->type == CPP_CLOSE_PAREN
15902                || token->type == CPP_CLOSE_SQUARE)
15903         {
15904           if (nesting_depth-- == 0)
15905             return;
15906         }
15907       /* Consume this token.  */
15908       cp_lexer_consume_token (parser->lexer);
15909     }
15910 }
15911
15912 /* If the next token is the indicated keyword, consume it.  Otherwise,
15913    issue an error message indicating that TOKEN_DESC was expected.
15914
15915    Returns the token consumed, if the token had the appropriate type.
15916    Otherwise, returns NULL.  */
15917
15918 static cp_token *
15919 cp_parser_require_keyword (cp_parser* parser,
15920                            enum rid keyword,
15921                            const char* token_desc)
15922 {
15923   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15924
15925   if (token && token->keyword != keyword)
15926     {
15927       dyn_string_t error_msg;
15928
15929       /* Format the error message.  */
15930       error_msg = dyn_string_new (0);
15931       dyn_string_append_cstr (error_msg, "expected ");
15932       dyn_string_append_cstr (error_msg, token_desc);
15933       cp_parser_error (parser, error_msg->s);
15934       dyn_string_delete (error_msg);
15935       return NULL;
15936     }
15937
15938   return token;
15939 }
15940
15941 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15942    function-definition.  */
15943
15944 static bool
15945 cp_parser_token_starts_function_definition_p (cp_token* token)
15946 {
15947   return (/* An ordinary function-body begins with an `{'.  */
15948           token->type == CPP_OPEN_BRACE
15949           /* A ctor-initializer begins with a `:'.  */
15950           || token->type == CPP_COLON
15951           /* A function-try-block begins with `try'.  */
15952           || token->keyword == RID_TRY
15953           /* The named return value extension begins with `return'.  */
15954           || token->keyword == RID_RETURN);
15955 }
15956
15957 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15958    definition.  */
15959
15960 static bool
15961 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15962 {
15963   cp_token *token;
15964
15965   token = cp_lexer_peek_token (parser->lexer);
15966   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15967 }
15968
15969 /* Returns TRUE iff the next token is the "," or ">" ending a
15970    template-argument.  */
15971
15972 static bool
15973 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15974 {
15975   cp_token *token;
15976
15977   token = cp_lexer_peek_token (parser->lexer);
15978   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15979 }
15980
15981 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
15982    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15983
15984 static bool
15985 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15986                                                      size_t n)
15987 {
15988   cp_token *token;
15989
15990   token = cp_lexer_peek_nth_token (parser->lexer, n);
15991   if (token->type == CPP_LESS)
15992     return true;
15993   /* Check for the sequence `<::' in the original code. It would be lexed as
15994      `[:', where `[' is a digraph, and there is no whitespace before
15995      `:'.  */
15996   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15997     {
15998       cp_token *token2;
15999       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16000       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16001         return true;
16002     }
16003   return false;
16004 }
16005
16006 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16007    or none_type otherwise.  */
16008
16009 static enum tag_types
16010 cp_parser_token_is_class_key (cp_token* token)
16011 {
16012   switch (token->keyword)
16013     {
16014     case RID_CLASS:
16015       return class_type;
16016     case RID_STRUCT:
16017       return record_type;
16018     case RID_UNION:
16019       return union_type;
16020
16021     default:
16022       return none_type;
16023     }
16024 }
16025
16026 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16027
16028 static void
16029 cp_parser_check_class_key (enum tag_types class_key, tree type)
16030 {
16031   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16032     pedwarn ("%qs tag used in naming %q#T",
16033             class_key == union_type ? "union"
16034              : class_key == record_type ? "struct" : "class",
16035              type);
16036 }
16037
16038 /* Issue an error message if DECL is redeclared with different
16039    access than its original declaration [class.access.spec/3].
16040    This applies to nested classes and nested class templates.
16041    [class.mem/1].  */
16042
16043 static void
16044 cp_parser_check_access_in_redeclaration (tree decl)
16045 {
16046   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16047     return;
16048
16049   if ((TREE_PRIVATE (decl)
16050        != (current_access_specifier == access_private_node))
16051       || (TREE_PROTECTED (decl)
16052           != (current_access_specifier == access_protected_node)))
16053     error ("%qD redeclared with different access", decl);
16054 }
16055
16056 /* Look for the `template' keyword, as a syntactic disambiguator.
16057    Return TRUE iff it is present, in which case it will be
16058    consumed.  */
16059
16060 static bool
16061 cp_parser_optional_template_keyword (cp_parser *parser)
16062 {
16063   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16064     {
16065       /* The `template' keyword can only be used within templates;
16066          outside templates the parser can always figure out what is a
16067          template and what is not.  */
16068       if (!processing_template_decl)
16069         {
16070           error ("%<template%> (as a disambiguator) is only allowed "
16071                  "within templates");
16072           /* If this part of the token stream is rescanned, the same
16073              error message would be generated.  So, we purge the token
16074              from the stream.  */
16075           cp_lexer_purge_token (parser->lexer);
16076           return false;
16077         }
16078       else
16079         {
16080           /* Consume the `template' keyword.  */
16081           cp_lexer_consume_token (parser->lexer);
16082           return true;
16083         }
16084     }
16085
16086   return false;
16087 }
16088
16089 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16090    set PARSER->SCOPE, and perform other related actions.  */
16091
16092 static void
16093 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16094 {
16095   tree value;
16096   tree check;
16097
16098   /* Get the stored value.  */
16099   value = cp_lexer_consume_token (parser->lexer)->value;
16100   /* Perform any access checks that were deferred.  */
16101   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16102     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16103   /* Set the scope from the stored value.  */
16104   parser->scope = TREE_VALUE (value);
16105   parser->qualifying_scope = TREE_TYPE (value);
16106   parser->object_scope = NULL_TREE;
16107 }
16108
16109 /* Consume tokens up through a non-nested END token.  */
16110
16111 static void
16112 cp_parser_cache_group (cp_parser *parser,
16113                        enum cpp_ttype end,
16114                        unsigned depth)
16115 {
16116   while (true)
16117     {
16118       cp_token *token;
16119
16120       /* Abort a parenthesized expression if we encounter a brace.  */
16121       if ((end == CPP_CLOSE_PAREN || depth == 0)
16122           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16123         return;
16124       /* If we've reached the end of the file, stop.  */
16125       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16126         return;
16127       /* Consume the next token.  */
16128       token = cp_lexer_consume_token (parser->lexer);
16129       /* See if it starts a new group.  */
16130       if (token->type == CPP_OPEN_BRACE)
16131         {
16132           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16133           if (depth == 0)
16134             return;
16135         }
16136       else if (token->type == CPP_OPEN_PAREN)
16137         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16138       else if (token->type == end)
16139         return;
16140     }
16141 }
16142
16143 /* Begin parsing tentatively.  We always save tokens while parsing
16144    tentatively so that if the tentative parsing fails we can restore the
16145    tokens.  */
16146
16147 static void
16148 cp_parser_parse_tentatively (cp_parser* parser)
16149 {
16150   /* Enter a new parsing context.  */
16151   parser->context = cp_parser_context_new (parser->context);
16152   /* Begin saving tokens.  */
16153   cp_lexer_save_tokens (parser->lexer);
16154   /* In order to avoid repetitive access control error messages,
16155      access checks are queued up until we are no longer parsing
16156      tentatively.  */
16157   push_deferring_access_checks (dk_deferred);
16158 }
16159
16160 /* Commit to the currently active tentative parse.  */
16161
16162 static void
16163 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16164 {
16165   cp_parser_context *context;
16166   cp_lexer *lexer;
16167
16168   /* Mark all of the levels as committed.  */
16169   lexer = parser->lexer;
16170   for (context = parser->context; context->next; context = context->next)
16171     {
16172       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16173         break;
16174       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16175       while (!cp_lexer_saving_tokens (lexer))
16176         lexer = lexer->next;
16177       cp_lexer_commit_tokens (lexer);
16178     }
16179 }
16180
16181 /* Abort the currently active tentative parse.  All consumed tokens
16182    will be rolled back, and no diagnostics will be issued.  */
16183
16184 static void
16185 cp_parser_abort_tentative_parse (cp_parser* parser)
16186 {
16187   cp_parser_simulate_error (parser);
16188   /* Now, pretend that we want to see if the construct was
16189      successfully parsed.  */
16190   cp_parser_parse_definitely (parser);
16191 }
16192
16193 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16194    token stream.  Otherwise, commit to the tokens we have consumed.
16195    Returns true if no error occurred; false otherwise.  */
16196
16197 static bool
16198 cp_parser_parse_definitely (cp_parser* parser)
16199 {
16200   bool error_occurred;
16201   cp_parser_context *context;
16202
16203   /* Remember whether or not an error occurred, since we are about to
16204      destroy that information.  */
16205   error_occurred = cp_parser_error_occurred (parser);
16206   /* Remove the topmost context from the stack.  */
16207   context = parser->context;
16208   parser->context = context->next;
16209   /* If no parse errors occurred, commit to the tentative parse.  */
16210   if (!error_occurred)
16211     {
16212       /* Commit to the tokens read tentatively, unless that was
16213          already done.  */
16214       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16215         cp_lexer_commit_tokens (parser->lexer);
16216
16217       pop_to_parent_deferring_access_checks ();
16218     }
16219   /* Otherwise, if errors occurred, roll back our state so that things
16220      are just as they were before we began the tentative parse.  */
16221   else
16222     {
16223       cp_lexer_rollback_tokens (parser->lexer);
16224       pop_deferring_access_checks ();
16225     }
16226   /* Add the context to the front of the free list.  */
16227   context->next = cp_parser_context_free_list;
16228   cp_parser_context_free_list = context;
16229
16230   return !error_occurred;
16231 }
16232
16233 /* Returns true if we are parsing tentatively and are not committed to
16234    this tentative parse.  */
16235
16236 static bool
16237 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16238 {
16239   return (cp_parser_parsing_tentatively (parser)
16240           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16241 }
16242
16243 /* Returns nonzero iff an error has occurred during the most recent
16244    tentative parse.  */
16245
16246 static bool
16247 cp_parser_error_occurred (cp_parser* parser)
16248 {
16249   return (cp_parser_parsing_tentatively (parser)
16250           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16251 }
16252
16253 /* Returns nonzero if GNU extensions are allowed.  */
16254
16255 static bool
16256 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16257 {
16258   return parser->allow_gnu_extensions_p;
16259 }
16260 \f
16261 /* Objective-C++ Productions */
16262
16263
16264 /* Parse an Objective-C expression, which feeds into a primary-expression
16265    above.
16266
16267    objc-expression:
16268      objc-message-expression
16269      objc-string-literal
16270      objc-encode-expression
16271      objc-protocol-expression
16272      objc-selector-expression
16273
16274   Returns a tree representation of the expression.  */
16275
16276 static tree
16277 cp_parser_objc_expression (cp_parser* parser)
16278 {
16279   /* Try to figure out what kind of declaration is present.  */
16280   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16281
16282   switch (kwd->type)
16283     {
16284     case CPP_OPEN_SQUARE:
16285       return cp_parser_objc_message_expression (parser);
16286
16287     case CPP_OBJC_STRING:
16288       kwd = cp_lexer_consume_token (parser->lexer);
16289       return objc_build_string_object (kwd->value);
16290
16291     case CPP_KEYWORD:
16292       switch (kwd->keyword)
16293         {
16294         case RID_AT_ENCODE:
16295           return cp_parser_objc_encode_expression (parser);
16296
16297         case RID_AT_PROTOCOL:
16298           return cp_parser_objc_protocol_expression (parser);
16299
16300         case RID_AT_SELECTOR:
16301           return cp_parser_objc_selector_expression (parser);
16302
16303         default:
16304           break;
16305         }
16306     default:
16307       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16308       cp_parser_skip_to_end_of_block_or_statement (parser);
16309     }
16310
16311   return error_mark_node;
16312 }
16313
16314 /* Parse an Objective-C message expression.
16315
16316    objc-message-expression:
16317      [ objc-message-receiver objc-message-args ]
16318
16319    Returns a representation of an Objective-C message.  */
16320
16321 static tree
16322 cp_parser_objc_message_expression (cp_parser* parser)
16323 {
16324   tree receiver, messageargs;
16325
16326   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16327   receiver = cp_parser_objc_message_receiver (parser);
16328   messageargs = cp_parser_objc_message_args (parser);
16329   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16330
16331   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16332 }
16333
16334 /* Parse an objc-message-receiver.
16335
16336    objc-message-receiver:
16337      expression
16338      simple-type-specifier
16339
16340   Returns a representation of the type or expression.  */
16341
16342 static tree
16343 cp_parser_objc_message_receiver (cp_parser* parser)
16344 {
16345   tree rcv;
16346
16347   /* An Objective-C message receiver may be either (1) a type
16348      or (2) an expression.  */
16349   cp_parser_parse_tentatively (parser);
16350   rcv = cp_parser_expression (parser, false);
16351
16352   if (cp_parser_parse_definitely (parser))
16353     return rcv;
16354
16355   rcv = cp_parser_simple_type_specifier (parser,
16356                                          /*decl_specs=*/NULL,
16357                                          CP_PARSER_FLAGS_NONE);
16358
16359   return objc_get_class_reference (rcv);
16360 }
16361
16362 /* Parse the arguments and selectors comprising an Objective-C message.
16363
16364    objc-message-args:
16365      objc-selector
16366      objc-selector-args
16367      objc-selector-args , objc-comma-args
16368
16369    objc-selector-args:
16370      objc-selector [opt] : assignment-expression
16371      objc-selector-args objc-selector [opt] : assignment-expression
16372
16373    objc-comma-args:
16374      assignment-expression
16375      objc-comma-args , assignment-expression
16376
16377    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16378    selector arguments and TREE_VALUE containing a list of comma
16379    arguments.  */
16380
16381 static tree
16382 cp_parser_objc_message_args (cp_parser* parser)
16383 {
16384   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16385   bool maybe_unary_selector_p = true;
16386   cp_token *token = cp_lexer_peek_token (parser->lexer);
16387
16388   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16389     {
16390       tree selector = NULL_TREE, arg;
16391
16392       if (token->type != CPP_COLON)
16393         selector = cp_parser_objc_selector (parser);
16394
16395       /* Detect if we have a unary selector.  */
16396       if (maybe_unary_selector_p
16397           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16398         return build_tree_list (selector, NULL_TREE);
16399
16400       maybe_unary_selector_p = false;
16401       cp_parser_require (parser, CPP_COLON, "`:'");
16402       arg = cp_parser_assignment_expression (parser, false);
16403
16404       sel_args
16405         = chainon (sel_args,
16406                    build_tree_list (selector, arg));
16407
16408       token = cp_lexer_peek_token (parser->lexer);
16409     }
16410
16411   /* Handle non-selector arguments, if any. */
16412   while (token->type == CPP_COMMA)
16413     {
16414       tree arg;
16415
16416       cp_lexer_consume_token (parser->lexer);
16417       arg = cp_parser_assignment_expression (parser, false);
16418
16419       addl_args
16420         = chainon (addl_args,
16421                    build_tree_list (NULL_TREE, arg));
16422
16423       token = cp_lexer_peek_token (parser->lexer);
16424     }
16425
16426   return build_tree_list (sel_args, addl_args);
16427 }
16428
16429 /* Parse an Objective-C encode expression.
16430
16431    objc-encode-expression:
16432      @encode objc-typename
16433
16434    Returns an encoded representation of the type argument.  */
16435
16436 static tree
16437 cp_parser_objc_encode_expression (cp_parser* parser)
16438 {
16439   tree type;
16440
16441   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16442   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16443   type = complete_type (cp_parser_type_id (parser));
16444   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16445
16446   if (!type)
16447     {
16448       error ("%<@encode%> must specify a type as an argument");
16449       return error_mark_node;
16450     }
16451
16452   return objc_build_encode_expr (type);
16453 }
16454
16455 /* Parse an Objective-C @defs expression.  */
16456
16457 static tree
16458 cp_parser_objc_defs_expression (cp_parser *parser)
16459 {
16460   tree name;
16461
16462   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16463   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16464   name = cp_parser_identifier (parser);
16465   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16466
16467   return objc_get_class_ivars (name);
16468 }
16469
16470 /* Parse an Objective-C protocol expression.
16471
16472   objc-protocol-expression:
16473     @protocol ( identifier )
16474
16475   Returns a representation of the protocol expression.  */
16476
16477 static tree
16478 cp_parser_objc_protocol_expression (cp_parser* parser)
16479 {
16480   tree proto;
16481
16482   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16483   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16484   proto = cp_parser_identifier (parser);
16485   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16486
16487   return objc_build_protocol_expr (proto);
16488 }
16489
16490 /* Parse an Objective-C selector expression.
16491
16492    objc-selector-expression:
16493      @selector ( objc-method-signature )
16494
16495    objc-method-signature:
16496      objc-selector
16497      objc-selector-seq
16498
16499    objc-selector-seq:
16500      objc-selector :
16501      objc-selector-seq objc-selector :
16502
16503   Returns a representation of the method selector.  */
16504
16505 static tree
16506 cp_parser_objc_selector_expression (cp_parser* parser)
16507 {
16508   tree sel_seq = NULL_TREE;
16509   bool maybe_unary_selector_p = true;
16510   cp_token *token;
16511
16512   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16513   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16514   token = cp_lexer_peek_token (parser->lexer);
16515
16516   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16517          || token->type == CPP_SCOPE)
16518     {
16519       tree selector = NULL_TREE;
16520
16521       if (token->type != CPP_COLON
16522           || token->type == CPP_SCOPE)
16523         selector = cp_parser_objc_selector (parser);
16524
16525       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16526           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16527         {
16528           /* Detect if we have a unary selector.  */
16529           if (maybe_unary_selector_p)
16530             {
16531               sel_seq = selector;
16532               goto finish_selector;
16533             }
16534           else
16535             {
16536               cp_parser_error (parser, "expected %<:%>");
16537             }
16538         }
16539       maybe_unary_selector_p = false;
16540       token = cp_lexer_consume_token (parser->lexer);
16541       
16542       if (token->type == CPP_SCOPE)
16543         {
16544           sel_seq
16545             = chainon (sel_seq,
16546                        build_tree_list (selector, NULL_TREE));
16547           sel_seq
16548             = chainon (sel_seq,
16549                        build_tree_list (NULL_TREE, NULL_TREE));
16550         }
16551       else
16552         sel_seq
16553           = chainon (sel_seq,
16554                      build_tree_list (selector, NULL_TREE));
16555
16556       token = cp_lexer_peek_token (parser->lexer);
16557     }
16558
16559  finish_selector:
16560   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16561
16562   return objc_build_selector_expr (sel_seq);
16563 }
16564
16565 /* Parse a list of identifiers.
16566
16567    objc-identifier-list:
16568      identifier
16569      objc-identifier-list , identifier
16570
16571    Returns a TREE_LIST of identifier nodes.  */
16572
16573 static tree
16574 cp_parser_objc_identifier_list (cp_parser* parser)
16575 {
16576   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16577   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16578
16579   while (sep->type == CPP_COMMA)
16580     {
16581       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16582       list = chainon (list,
16583                       build_tree_list (NULL_TREE,
16584                                        cp_parser_identifier (parser)));
16585       sep = cp_lexer_peek_token (parser->lexer);
16586     }
16587
16588   return list;
16589 }
16590
16591 /* Parse an Objective-C alias declaration.
16592
16593    objc-alias-declaration:
16594      @compatibility_alias identifier identifier ;
16595
16596    This function registers the alias mapping with the Objective-C front-end.
16597    It returns nothing.  */
16598
16599 static void
16600 cp_parser_objc_alias_declaration (cp_parser* parser)
16601 {
16602   tree alias, orig;
16603
16604   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16605   alias = cp_parser_identifier (parser);
16606   orig = cp_parser_identifier (parser);
16607   objc_declare_alias (alias, orig);
16608   cp_parser_consume_semicolon_at_end_of_statement (parser);
16609 }
16610
16611 /* Parse an Objective-C class forward-declaration.
16612
16613    objc-class-declaration:
16614      @class objc-identifier-list ;
16615
16616    The function registers the forward declarations with the Objective-C
16617    front-end.  It returns nothing.  */
16618
16619 static void
16620 cp_parser_objc_class_declaration (cp_parser* parser)
16621 {
16622   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16623   objc_declare_class (cp_parser_objc_identifier_list (parser));
16624   cp_parser_consume_semicolon_at_end_of_statement (parser);
16625 }
16626
16627 /* Parse a list of Objective-C protocol references.
16628
16629    objc-protocol-refs-opt:
16630      objc-protocol-refs [opt]
16631
16632    objc-protocol-refs:
16633      < objc-identifier-list >
16634
16635    Returns a TREE_LIST of identifiers, if any.  */
16636
16637 static tree
16638 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16639 {
16640   tree protorefs = NULL_TREE;
16641
16642   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16643     {
16644       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16645       protorefs = cp_parser_objc_identifier_list (parser);
16646       cp_parser_require (parser, CPP_GREATER, "`>'");
16647     }
16648
16649   return protorefs;
16650 }
16651
16652 /* Parse a Objective-C visibility specification.  */
16653
16654 static void
16655 cp_parser_objc_visibility_spec (cp_parser* parser)
16656 {
16657   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16658
16659   switch (vis->keyword)
16660     {
16661     case RID_AT_PRIVATE:
16662       objc_set_visibility (2);
16663       break;
16664     case RID_AT_PROTECTED:
16665       objc_set_visibility (0);
16666       break;
16667     case RID_AT_PUBLIC:
16668       objc_set_visibility (1);
16669       break;
16670     default:
16671       return;
16672     }
16673
16674   /* Eat '@private'/'@protected'/'@public'.  */
16675   cp_lexer_consume_token (parser->lexer);
16676 }
16677
16678 /* Parse an Objective-C method type.  */
16679
16680 static void
16681 cp_parser_objc_method_type (cp_parser* parser)
16682 {
16683   objc_set_method_type
16684    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16685     ? PLUS_EXPR
16686     : MINUS_EXPR);
16687 }
16688
16689 /* Parse an Objective-C protocol qualifier.  */
16690
16691 static tree
16692 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16693 {
16694   tree quals = NULL_TREE, node;
16695   cp_token *token = cp_lexer_peek_token (parser->lexer);
16696
16697   node = token->value;
16698
16699   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16700          && (node == ridpointers [(int) RID_IN]
16701              || node == ridpointers [(int) RID_OUT]
16702              || node == ridpointers [(int) RID_INOUT]
16703              || node == ridpointers [(int) RID_BYCOPY]
16704              || node == ridpointers [(int) RID_BYREF]
16705              || node == ridpointers [(int) RID_ONEWAY]))
16706     {
16707       quals = tree_cons (NULL_TREE, node, quals);
16708       cp_lexer_consume_token (parser->lexer);
16709       token = cp_lexer_peek_token (parser->lexer);
16710       node = token->value;
16711     }
16712
16713   return quals;
16714 }
16715
16716 /* Parse an Objective-C typename.  */
16717
16718 static tree
16719 cp_parser_objc_typename (cp_parser* parser)
16720 {
16721   tree typename = NULL_TREE;
16722
16723   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16724     {
16725       tree proto_quals, cp_type = NULL_TREE;
16726
16727       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16728       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16729
16730       /* An ObjC type name may consist of just protocol qualifiers, in which
16731          case the type shall default to 'id'.  */
16732       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16733         cp_type = cp_parser_type_id (parser);
16734
16735       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16736       typename = build_tree_list (proto_quals, cp_type);
16737     }
16738
16739   return typename;
16740 }
16741
16742 /* Check to see if TYPE refers to an Objective-C selector name.  */
16743
16744 static bool
16745 cp_parser_objc_selector_p (enum cpp_ttype type)
16746 {
16747   return (type == CPP_NAME || type == CPP_KEYWORD
16748           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16749           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16750           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16751           || type == CPP_XOR || type == CPP_XOR_EQ);
16752 }
16753
16754 /* Parse an Objective-C selector.  */
16755
16756 static tree
16757 cp_parser_objc_selector (cp_parser* parser)
16758 {
16759   cp_token *token = cp_lexer_consume_token (parser->lexer);
16760
16761   if (!cp_parser_objc_selector_p (token->type))
16762     {
16763       error ("invalid Objective-C++ selector name");
16764       return error_mark_node;
16765     }
16766
16767   /* C++ operator names are allowed to appear in ObjC selectors.  */
16768   switch (token->type)
16769     {
16770     case CPP_AND_AND: return get_identifier ("and");
16771     case CPP_AND_EQ: return get_identifier ("and_eq");
16772     case CPP_AND: return get_identifier ("bitand");
16773     case CPP_OR: return get_identifier ("bitor");
16774     case CPP_COMPL: return get_identifier ("compl");
16775     case CPP_NOT: return get_identifier ("not");
16776     case CPP_NOT_EQ: return get_identifier ("not_eq");
16777     case CPP_OR_OR: return get_identifier ("or");
16778     case CPP_OR_EQ: return get_identifier ("or_eq");
16779     case CPP_XOR: return get_identifier ("xor");
16780     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16781     default: return token->value;
16782     }
16783 }
16784
16785 /* Parse an Objective-C params list.  */
16786
16787 static tree
16788 cp_parser_objc_method_keyword_params (cp_parser* parser)
16789 {
16790   tree params = NULL_TREE;
16791   bool maybe_unary_selector_p = true;
16792   cp_token *token = cp_lexer_peek_token (parser->lexer);
16793
16794   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16795     {
16796       tree selector = NULL_TREE, typename, identifier;
16797
16798       if (token->type != CPP_COLON)
16799         selector = cp_parser_objc_selector (parser);
16800
16801       /* Detect if we have a unary selector.  */
16802       if (maybe_unary_selector_p
16803           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16804         return selector;
16805
16806       maybe_unary_selector_p = false;
16807       cp_parser_require (parser, CPP_COLON, "`:'");
16808       typename = cp_parser_objc_typename (parser);
16809       identifier = cp_parser_identifier (parser);
16810
16811       params
16812         = chainon (params,
16813                    objc_build_keyword_decl (selector,
16814                                             typename,
16815                                             identifier));
16816
16817       token = cp_lexer_peek_token (parser->lexer);
16818     }
16819
16820   return params;
16821 }
16822
16823 /* Parse the non-keyword Objective-C params.  */
16824
16825 static tree
16826 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16827 {
16828   tree params = make_node (TREE_LIST);
16829   cp_token *token = cp_lexer_peek_token (parser->lexer);
16830   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16831
16832   while (token->type == CPP_COMMA)
16833     {
16834       cp_parameter_declarator *parmdecl;
16835       tree parm;
16836
16837       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16838       token = cp_lexer_peek_token (parser->lexer);
16839
16840       if (token->type == CPP_ELLIPSIS)
16841         {
16842           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16843           *ellipsisp = true;
16844           break;
16845         }
16846
16847       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16848       parm = grokdeclarator (parmdecl->declarator,
16849                              &parmdecl->decl_specifiers,
16850                              PARM, /*initialized=*/0,
16851                              /*attrlist=*/NULL);
16852
16853       chainon (params, build_tree_list (NULL_TREE, parm));
16854       token = cp_lexer_peek_token (parser->lexer);
16855     }
16856
16857   return params;
16858 }
16859
16860 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16861
16862 static void
16863 cp_parser_objc_interstitial_code (cp_parser* parser)
16864 {
16865   cp_token *token = cp_lexer_peek_token (parser->lexer);
16866
16867   /* If the next token is `extern' and the following token is a string
16868      literal, then we have a linkage specification.  */
16869   if (token->keyword == RID_EXTERN
16870       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16871     cp_parser_linkage_specification (parser);
16872   /* Handle #pragma, if any.  */
16873   else if (token->type == CPP_PRAGMA)
16874     cp_lexer_handle_pragma (parser->lexer);
16875   /* Allow stray semicolons.  */
16876   else if (token->type == CPP_SEMICOLON)
16877     cp_lexer_consume_token (parser->lexer);
16878   /* Finally, try to parse a block-declaration, or a function-definition.  */
16879   else
16880     cp_parser_block_declaration (parser, /*statement_p=*/false);
16881 }
16882
16883 /* Parse a method signature.  */
16884
16885 static tree
16886 cp_parser_objc_method_signature (cp_parser* parser)
16887 {
16888   tree rettype, kwdparms, optparms;
16889   bool ellipsis = false;
16890
16891   cp_parser_objc_method_type (parser);
16892   rettype = cp_parser_objc_typename (parser);
16893   kwdparms = cp_parser_objc_method_keyword_params (parser);
16894   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16895
16896   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16897 }
16898
16899 /* Pars an Objective-C method prototype list.  */
16900
16901 static void
16902 cp_parser_objc_method_prototype_list (cp_parser* parser)
16903 {
16904   cp_token *token = cp_lexer_peek_token (parser->lexer);
16905
16906   while (token->keyword != RID_AT_END)
16907     {
16908       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16909         {
16910           objc_add_method_declaration
16911            (cp_parser_objc_method_signature (parser));
16912           cp_parser_consume_semicolon_at_end_of_statement (parser);
16913         }
16914       else
16915         /* Allow for interspersed non-ObjC++ code.  */
16916         cp_parser_objc_interstitial_code (parser);
16917
16918       token = cp_lexer_peek_token (parser->lexer);
16919     }
16920
16921   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16922   objc_finish_interface ();
16923 }
16924
16925 /* Parse an Objective-C method definition list.  */
16926
16927 static void
16928 cp_parser_objc_method_definition_list (cp_parser* parser)
16929 {
16930   cp_token *token = cp_lexer_peek_token (parser->lexer);
16931
16932   while (token->keyword != RID_AT_END)
16933     {
16934       tree meth;
16935
16936       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16937         {
16938           push_deferring_access_checks (dk_deferred);
16939           objc_start_method_definition
16940            (cp_parser_objc_method_signature (parser));
16941
16942           /* For historical reasons, we accept an optional semicolon.  */
16943           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16944             cp_lexer_consume_token (parser->lexer);
16945
16946           perform_deferred_access_checks ();
16947           stop_deferring_access_checks ();
16948           meth = cp_parser_function_definition_after_declarator (parser,
16949                                                                  false);
16950           pop_deferring_access_checks ();
16951           objc_finish_method_definition (meth);
16952         }
16953       else
16954         /* Allow for interspersed non-ObjC++ code.  */
16955         cp_parser_objc_interstitial_code (parser);
16956
16957       token = cp_lexer_peek_token (parser->lexer);
16958     }
16959
16960   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16961   objc_finish_implementation ();
16962 }
16963
16964 /* Parse Objective-C ivars.  */
16965
16966 static void
16967 cp_parser_objc_class_ivars (cp_parser* parser)
16968 {
16969   cp_token *token = cp_lexer_peek_token (parser->lexer);
16970
16971   if (token->type != CPP_OPEN_BRACE)
16972     return;     /* No ivars specified.  */
16973
16974   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
16975   token = cp_lexer_peek_token (parser->lexer);
16976
16977   while (token->type != CPP_CLOSE_BRACE)
16978     {
16979       cp_decl_specifier_seq declspecs;
16980       int decl_class_or_enum_p;
16981       tree prefix_attributes;
16982
16983       cp_parser_objc_visibility_spec (parser);
16984
16985       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16986         break;
16987
16988       cp_parser_decl_specifier_seq (parser,
16989                                     CP_PARSER_FLAGS_OPTIONAL,
16990                                     &declspecs,
16991                                     &decl_class_or_enum_p);
16992       prefix_attributes = declspecs.attributes;
16993       declspecs.attributes = NULL_TREE;
16994
16995       /* Keep going until we hit the `;' at the end of the
16996          declaration.  */
16997       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16998         {
16999           tree width = NULL_TREE, attributes, first_attribute, decl;
17000           cp_declarator *declarator = NULL;
17001           int ctor_dtor_or_conv_p;
17002
17003           /* Check for a (possibly unnamed) bitfield declaration.  */
17004           token = cp_lexer_peek_token (parser->lexer);
17005           if (token->type == CPP_COLON)
17006             goto eat_colon;
17007
17008           if (token->type == CPP_NAME
17009               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17010                   == CPP_COLON))
17011             {
17012               /* Get the name of the bitfield.  */
17013               declarator = make_id_declarator (NULL_TREE,
17014                                                cp_parser_identifier (parser));
17015
17016              eat_colon:
17017               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17018               /* Get the width of the bitfield.  */
17019               width
17020                 = cp_parser_constant_expression (parser,
17021                                                  /*allow_non_constant=*/false,
17022                                                  NULL);
17023             }
17024           else
17025             {
17026               /* Parse the declarator.  */
17027               declarator
17028                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17029                                         &ctor_dtor_or_conv_p,
17030                                         /*parenthesized_p=*/NULL,
17031                                         /*member_p=*/false);
17032             }
17033
17034           /* Look for attributes that apply to the ivar.  */
17035           attributes = cp_parser_attributes_opt (parser);
17036           /* Remember which attributes are prefix attributes and
17037              which are not.  */
17038           first_attribute = attributes;
17039           /* Combine the attributes.  */
17040           attributes = chainon (prefix_attributes, attributes);
17041
17042           if (width)
17043             {
17044               /* Create the bitfield declaration.  */
17045               decl = grokbitfield (declarator, &declspecs, width);
17046               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17047             }
17048           else
17049             decl = grokfield (declarator, &declspecs, NULL_TREE,
17050                               NULL_TREE, attributes);
17051
17052           /* Add the instance variable.  */
17053           objc_add_instance_variable (decl);
17054
17055           /* Reset PREFIX_ATTRIBUTES.  */
17056           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17057             attributes = TREE_CHAIN (attributes);
17058           if (attributes)
17059             TREE_CHAIN (attributes) = NULL_TREE;
17060
17061           token = cp_lexer_peek_token (parser->lexer);
17062
17063           if (token->type == CPP_COMMA)
17064             {
17065               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17066               continue;
17067             }
17068           break;
17069         }
17070
17071       cp_parser_consume_semicolon_at_end_of_statement (parser);
17072       token = cp_lexer_peek_token (parser->lexer);
17073     }
17074
17075   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17076   /* For historical reasons, we accept an optional semicolon.  */
17077   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17078     cp_lexer_consume_token (parser->lexer);
17079 }
17080
17081 /* Parse an Objective-C protocol declaration.  */
17082
17083 static void
17084 cp_parser_objc_protocol_declaration (cp_parser* parser)
17085 {
17086   tree proto, protorefs;
17087   cp_token *tok;
17088
17089   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17090   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17091     {
17092       error ("identifier expected after %<@protocol%>");
17093       goto finish;
17094     }
17095
17096   /* See if we have a forward declaration or a definition.  */
17097   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17098
17099   /* Try a forward declaration first.  */
17100   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17101     {
17102       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17103      finish:
17104       cp_parser_consume_semicolon_at_end_of_statement (parser);
17105     }
17106
17107   /* Ok, we got a full-fledged definition (or at least should).  */
17108   else
17109     {
17110       proto = cp_parser_identifier (parser);
17111       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17112       objc_start_protocol (proto, protorefs);
17113       cp_parser_objc_method_prototype_list (parser);
17114     }
17115 }
17116
17117 /* Parse an Objective-C superclass or category.  */
17118
17119 static void
17120 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17121                                                           tree *categ)
17122 {
17123   cp_token *next = cp_lexer_peek_token (parser->lexer);
17124
17125   *super = *categ = NULL_TREE;
17126   if (next->type == CPP_COLON)
17127     {
17128       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17129       *super = cp_parser_identifier (parser);
17130     }
17131   else if (next->type == CPP_OPEN_PAREN)
17132     {
17133       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17134       *categ = cp_parser_identifier (parser);
17135       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17136     }
17137 }
17138
17139 /* Parse an Objective-C class interface.  */
17140
17141 static void
17142 cp_parser_objc_class_interface (cp_parser* parser)
17143 {
17144   tree name, super, categ, protos;
17145
17146   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17147   name = cp_parser_identifier (parser);
17148   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17149   protos = cp_parser_objc_protocol_refs_opt (parser);
17150
17151   /* We have either a class or a category on our hands.  */
17152   if (categ)
17153     objc_start_category_interface (name, categ, protos);
17154   else
17155     {
17156       objc_start_class_interface (name, super, protos);
17157       /* Handle instance variable declarations, if any.  */
17158       cp_parser_objc_class_ivars (parser);
17159       objc_continue_interface ();
17160     }
17161
17162   cp_parser_objc_method_prototype_list (parser);
17163 }
17164
17165 /* Parse an Objective-C class implementation.  */
17166
17167 static void
17168 cp_parser_objc_class_implementation (cp_parser* parser)
17169 {
17170   tree name, super, categ;
17171
17172   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17173   name = cp_parser_identifier (parser);
17174   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17175
17176   /* We have either a class or a category on our hands.  */
17177   if (categ)
17178     objc_start_category_implementation (name, categ);
17179   else
17180     {
17181       objc_start_class_implementation (name, super);
17182       /* Handle instance variable declarations, if any.  */
17183       cp_parser_objc_class_ivars (parser);
17184       objc_continue_implementation ();
17185     }
17186
17187   cp_parser_objc_method_definition_list (parser);
17188 }
17189
17190 /* Consume the @end token and finish off the implementation.  */
17191
17192 static void
17193 cp_parser_objc_end_implementation (cp_parser* parser)
17194 {
17195   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17196   objc_finish_implementation ();
17197 }
17198
17199 /* Parse an Objective-C declaration.  */
17200
17201 static void
17202 cp_parser_objc_declaration (cp_parser* parser)
17203 {
17204   /* Try to figure out what kind of declaration is present.  */
17205   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17206
17207   switch (kwd->keyword)
17208     {
17209     case RID_AT_ALIAS:
17210       cp_parser_objc_alias_declaration (parser);
17211       break;
17212     case RID_AT_CLASS:
17213       cp_parser_objc_class_declaration (parser);
17214       break;
17215     case RID_AT_PROTOCOL:
17216       cp_parser_objc_protocol_declaration (parser);
17217       break;
17218     case RID_AT_INTERFACE:
17219       cp_parser_objc_class_interface (parser);
17220       break;
17221     case RID_AT_IMPLEMENTATION:
17222       cp_parser_objc_class_implementation (parser);
17223       break;
17224     case RID_AT_END:
17225       cp_parser_objc_end_implementation (parser);
17226       break;
17227     default:
17228       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17229       cp_parser_skip_to_end_of_block_or_statement (parser);
17230     }
17231 }
17232
17233 /* Parse an Objective-C try-catch-finally statement.
17234
17235    objc-try-catch-finally-stmt:
17236      @try compound-statement objc-catch-clause-seq [opt]
17237        objc-finally-clause [opt]
17238
17239    objc-catch-clause-seq:
17240      objc-catch-clause objc-catch-clause-seq [opt]
17241
17242    objc-catch-clause:
17243      @catch ( exception-declaration ) compound-statement
17244
17245    objc-finally-clause
17246      @finally compound-statement
17247
17248    Returns NULL_TREE.  */
17249
17250 static tree
17251 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17252   location_t location;
17253   tree stmt;
17254
17255   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17256   location = cp_lexer_peek_token (parser->lexer)->location;
17257   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17258      node, lest it get absorbed into the surrounding block.  */
17259   stmt = push_stmt_list ();
17260   cp_parser_compound_statement (parser, NULL, false);
17261   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17262
17263   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17264     {
17265       cp_parameter_declarator *parmdecl;
17266       tree parm;
17267
17268       cp_lexer_consume_token (parser->lexer);
17269       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17270       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17271       parm = grokdeclarator (parmdecl->declarator,
17272                              &parmdecl->decl_specifiers,
17273                              PARM, /*initialized=*/0,
17274                              /*attrlist=*/NULL);
17275       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17276       objc_begin_catch_clause (parm);
17277       cp_parser_compound_statement (parser, NULL, false);
17278       objc_finish_catch_clause ();
17279     }
17280
17281   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17282     {
17283       cp_lexer_consume_token (parser->lexer);
17284       location = cp_lexer_peek_token (parser->lexer)->location;
17285       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17286          node, lest it get absorbed into the surrounding block.  */
17287       stmt = push_stmt_list ();
17288       cp_parser_compound_statement (parser, NULL, false);
17289       objc_build_finally_clause (location, pop_stmt_list (stmt));
17290     }
17291
17292   return objc_finish_try_stmt ();
17293 }
17294
17295 /* Parse an Objective-C synchronized statement.
17296
17297    objc-synchronized-stmt:
17298      @synchronized ( expression ) compound-statement
17299
17300    Returns NULL_TREE.  */
17301
17302 static tree
17303 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17304   location_t location;
17305   tree lock, stmt;
17306
17307   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17308
17309   location = cp_lexer_peek_token (parser->lexer)->location;
17310   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17311   lock = cp_parser_expression (parser, false);
17312   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17313
17314   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17315      node, lest it get absorbed into the surrounding block.  */
17316   stmt = push_stmt_list ();
17317   cp_parser_compound_statement (parser, NULL, false);
17318
17319   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17320 }
17321
17322 /* Parse an Objective-C throw statement.
17323
17324    objc-throw-stmt:
17325      @throw assignment-expression [opt] ;
17326
17327    Returns a constructed '@throw' statement.  */
17328
17329 static tree
17330 cp_parser_objc_throw_statement (cp_parser *parser) {
17331   tree expr = NULL_TREE;
17332
17333   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17334
17335   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17336     expr = cp_parser_assignment_expression (parser, false);
17337
17338   cp_parser_consume_semicolon_at_end_of_statement (parser);
17339
17340   return objc_build_throw_stmt (expr);
17341 }
17342
17343 /* Parse an Objective-C statement.  */
17344
17345 static tree
17346 cp_parser_objc_statement (cp_parser * parser) {
17347   /* Try to figure out what kind of declaration is present.  */
17348   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17349
17350   switch (kwd->keyword)
17351     {
17352     case RID_AT_TRY:
17353       return cp_parser_objc_try_catch_finally_statement (parser);
17354     case RID_AT_SYNCHRONIZED:
17355       return cp_parser_objc_synchronized_statement (parser);
17356     case RID_AT_THROW:
17357       return cp_parser_objc_throw_statement (parser);
17358     default:
17359       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17360       cp_parser_skip_to_end_of_block_or_statement (parser);
17361     }
17362
17363   return error_mark_node;
17364 }
17365 \f
17366 /* The parser.  */
17367
17368 static GTY (()) cp_parser *the_parser;
17369
17370 /* External interface.  */
17371
17372 /* Parse one entire translation unit.  */
17373
17374 void
17375 c_parse_file (void)
17376 {
17377   bool error_occurred;
17378   static bool already_called = false;
17379
17380   if (already_called)
17381     {
17382       sorry ("inter-module optimizations not implemented for C++");
17383       return;
17384     }
17385   already_called = true;
17386
17387   the_parser = cp_parser_new ();
17388   push_deferring_access_checks (flag_access_control
17389                                 ? dk_no_deferred : dk_no_check);
17390   error_occurred = cp_parser_translation_unit (the_parser);
17391   the_parser = NULL;
17392 }
17393
17394 /* This variable must be provided by every front end.  */
17395
17396 int yydebug;
17397
17398 #include "gt-cp-parser.h"