OSDN Git Service

PR c++/23440
[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, bool, bool, cp_id_kind *);
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    ADDRESS_P is true iff this expression was immediately preceded by
2720    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2721    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2722    true iff this expression is a tempalte argument.
2723
2724    Returns a representation of the expression.  Upon return, *IDK
2725    indicates what kind of id-expression (if any) was present.  */
2726
2727 static tree
2728 cp_parser_primary_expression (cp_parser *parser,
2729                               bool address_p,
2730                               bool cast_p,
2731                               bool template_arg_p,
2732                               cp_id_kind *idk)
2733 {
2734   cp_token *token;
2735
2736   /* Assume the primary expression is not an id-expression.  */
2737   *idk = CP_ID_KIND_NONE;
2738
2739   /* Peek at the next token.  */
2740   token = cp_lexer_peek_token (parser->lexer);
2741   switch (token->type)
2742     {
2743       /* literal:
2744            integer-literal
2745            character-literal
2746            floating-literal
2747            string-literal
2748            boolean-literal  */
2749     case CPP_CHAR:
2750     case CPP_WCHAR:
2751     case CPP_NUMBER:
2752       token = cp_lexer_consume_token (parser->lexer);
2753       /* Floating-point literals are only allowed in an integral
2754          constant expression if they are cast to an integral or
2755          enumeration type.  */
2756       if (TREE_CODE (token->value) == REAL_CST
2757           && parser->integral_constant_expression_p
2758           && pedantic)
2759         {
2760           /* CAST_P will be set even in invalid code like "int(2.7 +
2761              ...)".   Therefore, we have to check that the next token
2762              is sure to end the cast.  */
2763           if (cast_p)
2764             {
2765               cp_token *next_token;
2766
2767               next_token = cp_lexer_peek_token (parser->lexer);
2768               if (/* The comma at the end of an
2769                      enumerator-definition.  */
2770                   next_token->type != CPP_COMMA
2771                   /* The curly brace at the end of an enum-specifier.  */
2772                   && next_token->type != CPP_CLOSE_BRACE
2773                   /* The end of a statement.  */
2774                   && next_token->type != CPP_SEMICOLON
2775                   /* The end of the cast-expression.  */
2776                   && next_token->type != CPP_CLOSE_PAREN
2777                   /* The end of an array bound.  */
2778                   && next_token->type != CPP_CLOSE_SQUARE
2779                   /* The closing ">" in a template-argument-list.  */
2780                   && (next_token->type != CPP_GREATER
2781                       || parser->greater_than_is_operator_p))
2782                 cast_p = false;
2783             }
2784
2785           /* If we are within a cast, then the constraint that the
2786              cast is to an integral or enumeration type will be
2787              checked at that point.  If we are not within a cast, then
2788              this code is invalid.  */
2789           if (!cast_p)
2790             cp_parser_non_integral_constant_expression
2791               (parser, "floating-point literal");
2792         }
2793       return token->value;
2794
2795     case CPP_STRING:
2796     case CPP_WSTRING:
2797       /* ??? Should wide strings be allowed when parser->translate_strings_p
2798          is false (i.e. in attributes)?  If not, we can kill the third
2799          argument to cp_parser_string_literal.  */
2800       return cp_parser_string_literal (parser,
2801                                        parser->translate_strings_p,
2802                                        true);
2803
2804     case CPP_OPEN_PAREN:
2805       {
2806         tree expr;
2807         bool saved_greater_than_is_operator_p;
2808
2809         /* Consume the `('.  */
2810         cp_lexer_consume_token (parser->lexer);
2811         /* Within a parenthesized expression, a `>' token is always
2812            the greater-than operator.  */
2813         saved_greater_than_is_operator_p
2814           = parser->greater_than_is_operator_p;
2815         parser->greater_than_is_operator_p = true;
2816         /* If we see `( { ' then we are looking at the beginning of
2817            a GNU statement-expression.  */
2818         if (cp_parser_allow_gnu_extensions_p (parser)
2819             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2820           {
2821             /* Statement-expressions are not allowed by the standard.  */
2822             if (pedantic)
2823               pedwarn ("ISO C++ forbids braced-groups within expressions");
2824
2825             /* And they're not allowed outside of a function-body; you
2826                cannot, for example, write:
2827
2828                  int i = ({ int j = 3; j + 1; });
2829
2830                at class or namespace scope.  */
2831             if (!at_function_scope_p ())
2832               error ("statement-expressions are allowed only inside functions");
2833             /* Start the statement-expression.  */
2834             expr = begin_stmt_expr ();
2835             /* Parse the compound-statement.  */
2836             cp_parser_compound_statement (parser, expr, false);
2837             /* Finish up.  */
2838             expr = finish_stmt_expr (expr, false);
2839           }
2840         else
2841           {
2842             /* Parse the parenthesized expression.  */
2843             expr = cp_parser_expression (parser, cast_p);
2844             /* Let the front end know that this expression was
2845                enclosed in parentheses. This matters in case, for
2846                example, the expression is of the form `A::B', since
2847                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2848                not.  */
2849             finish_parenthesized_expr (expr);
2850           }
2851         /* The `>' token might be the end of a template-id or
2852            template-parameter-list now.  */
2853         parser->greater_than_is_operator_p
2854           = saved_greater_than_is_operator_p;
2855         /* Consume the `)'.  */
2856         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2857           cp_parser_skip_to_end_of_statement (parser);
2858
2859         return expr;
2860       }
2861
2862     case CPP_KEYWORD:
2863       switch (token->keyword)
2864         {
2865           /* These two are the boolean literals.  */
2866         case RID_TRUE:
2867           cp_lexer_consume_token (parser->lexer);
2868           return boolean_true_node;
2869         case RID_FALSE:
2870           cp_lexer_consume_token (parser->lexer);
2871           return boolean_false_node;
2872
2873           /* The `__null' literal.  */
2874         case RID_NULL:
2875           cp_lexer_consume_token (parser->lexer);
2876           return null_node;
2877
2878           /* Recognize the `this' keyword.  */
2879         case RID_THIS:
2880           cp_lexer_consume_token (parser->lexer);
2881           if (parser->local_variables_forbidden_p)
2882             {
2883               error ("%<this%> may not be used in this context");
2884               return error_mark_node;
2885             }
2886           /* Pointers cannot appear in constant-expressions.  */
2887           if (cp_parser_non_integral_constant_expression (parser,
2888                                                           "`this'"))
2889             return error_mark_node;
2890           return finish_this_expr ();
2891
2892           /* The `operator' keyword can be the beginning of an
2893              id-expression.  */
2894         case RID_OPERATOR:
2895           goto id_expression;
2896
2897         case RID_FUNCTION_NAME:
2898         case RID_PRETTY_FUNCTION_NAME:
2899         case RID_C99_FUNCTION_NAME:
2900           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2901              __func__ are the names of variables -- but they are
2902              treated specially.  Therefore, they are handled here,
2903              rather than relying on the generic id-expression logic
2904              below.  Grammatically, these names are id-expressions.
2905
2906              Consume the token.  */
2907           token = cp_lexer_consume_token (parser->lexer);
2908           /* Look up the name.  */
2909           return finish_fname (token->value);
2910
2911         case RID_VA_ARG:
2912           {
2913             tree expression;
2914             tree type;
2915
2916             /* The `__builtin_va_arg' construct is used to handle
2917                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2918             cp_lexer_consume_token (parser->lexer);
2919             /* Look for the opening `('.  */
2920             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2921             /* Now, parse the assignment-expression.  */
2922             expression = cp_parser_assignment_expression (parser,
2923                                                           /*cast_p=*/false);
2924             /* Look for the `,'.  */
2925             cp_parser_require (parser, CPP_COMMA, "`,'");
2926             /* Parse the type-id.  */
2927             type = cp_parser_type_id (parser);
2928             /* Look for the closing `)'.  */
2929             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2930             /* Using `va_arg' in a constant-expression is not
2931                allowed.  */
2932             if (cp_parser_non_integral_constant_expression (parser,
2933                                                             "`va_arg'"))
2934               return error_mark_node;
2935             return build_x_va_arg (expression, type);
2936           }
2937
2938         case RID_OFFSETOF:
2939           return cp_parser_builtin_offsetof (parser);
2940
2941           /* Objective-C++ expressions.  */
2942         case RID_AT_ENCODE:
2943         case RID_AT_PROTOCOL:
2944         case RID_AT_SELECTOR:
2945           return cp_parser_objc_expression (parser);
2946
2947         default:
2948           cp_parser_error (parser, "expected primary-expression");
2949           return error_mark_node;
2950         }
2951
2952       /* An id-expression can start with either an identifier, a
2953          `::' as the beginning of a qualified-id, or the "operator"
2954          keyword.  */
2955     case CPP_NAME:
2956     case CPP_SCOPE:
2957     case CPP_TEMPLATE_ID:
2958     case CPP_NESTED_NAME_SPECIFIER:
2959       {
2960         tree id_expression;
2961         tree decl;
2962         const char *error_msg;
2963         bool template_p;
2964         bool done;
2965
2966       id_expression:
2967         /* Parse the id-expression.  */
2968         id_expression
2969           = cp_parser_id_expression (parser,
2970                                      /*template_keyword_p=*/false,
2971                                      /*check_dependency_p=*/true,
2972                                      &template_p,
2973                                      /*declarator_p=*/false);
2974         if (id_expression == error_mark_node)
2975           return error_mark_node;
2976         token = cp_lexer_peek_token (parser->lexer);
2977         done = (token->type != CPP_OPEN_SQUARE
2978                 && token->type != CPP_OPEN_PAREN
2979                 && token->type != CPP_DOT
2980                 && token->type != CPP_DEREF
2981                 && token->type != CPP_PLUS_PLUS
2982                 && token->type != CPP_MINUS_MINUS);
2983         /* If we have a template-id, then no further lookup is
2984            required.  If the template-id was for a template-class, we
2985            will sometimes have a TYPE_DECL at this point.  */
2986         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2987                  || TREE_CODE (id_expression) == TYPE_DECL)
2988           decl = id_expression;
2989         /* Look up the name.  */
2990         else
2991           {
2992             bool ambiguous_p;
2993
2994             decl = cp_parser_lookup_name (parser, id_expression,
2995                                           none_type,
2996                                           template_p,
2997                                           /*is_namespace=*/false,
2998                                           /*check_dependency=*/true,
2999                                           &ambiguous_p);
3000             /* If the lookup was ambiguous, an error will already have
3001                been issued.  */
3002             if (ambiguous_p)
3003               return error_mark_node;
3004
3005             /* In Objective-C++, an instance variable (ivar) may be preferred
3006                to whatever cp_parser_lookup_name() found.  */
3007             decl = objc_lookup_ivar (decl, id_expression);
3008
3009             /* If name lookup gives us a SCOPE_REF, then the
3010                qualifying scope was dependent.  */
3011             if (TREE_CODE (decl) == SCOPE_REF)
3012               return decl;
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 
3042                 (id_expression, decl, parser->scope,
3043                  idk,
3044                  parser->integral_constant_expression_p,
3045                  parser->allow_non_integral_constant_expression_p,
3046                  &parser->non_integral_constant_expression_p,
3047                  template_p, done, address_p,
3048                  template_arg_p,
3049                  &error_msg));
3050         if (error_msg)
3051           cp_parser_error (parser, error_msg);
3052         return decl;
3053       }
3054
3055       /* Anything else is an error.  */
3056     default:
3057       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3058       if (c_dialect_objc ()
3059           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3060         return cp_parser_objc_expression (parser);
3061
3062       cp_parser_error (parser, "expected primary-expression");
3063       return error_mark_node;
3064     }
3065 }
3066
3067 /* Parse an id-expression.
3068
3069    id-expression:
3070      unqualified-id
3071      qualified-id
3072
3073    qualified-id:
3074      :: [opt] nested-name-specifier template [opt] unqualified-id
3075      :: identifier
3076      :: operator-function-id
3077      :: template-id
3078
3079    Return a representation of the unqualified portion of the
3080    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3081    a `::' or nested-name-specifier.
3082
3083    Often, if the id-expression was a qualified-id, the caller will
3084    want to make a SCOPE_REF to represent the qualified-id.  This
3085    function does not do this in order to avoid wastefully creating
3086    SCOPE_REFs when they are not required.
3087
3088    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3089    `template' keyword.
3090
3091    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3092    uninstantiated templates.
3093
3094    If *TEMPLATE_P is non-NULL, it is set to true iff the
3095    `template' keyword is used to explicitly indicate that the entity
3096    named is a template.
3097
3098    If DECLARATOR_P is true, the id-expression is appearing as part of
3099    a declarator, rather than as part of an expression.  */
3100
3101 static tree
3102 cp_parser_id_expression (cp_parser *parser,
3103                          bool template_keyword_p,
3104                          bool check_dependency_p,
3105                          bool *template_p,
3106                          bool declarator_p)
3107 {
3108   bool global_scope_p;
3109   bool nested_name_specifier_p;
3110
3111   /* Assume the `template' keyword was not used.  */
3112   if (template_p)
3113     *template_p = template_keyword_p;
3114
3115   /* Look for the optional `::' operator.  */
3116   global_scope_p
3117     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3118        != NULL_TREE);
3119   /* Look for the optional nested-name-specifier.  */
3120   nested_name_specifier_p
3121     = (cp_parser_nested_name_specifier_opt (parser,
3122                                             /*typename_keyword_p=*/false,
3123                                             check_dependency_p,
3124                                             /*type_p=*/false,
3125                                             declarator_p)
3126        != NULL_TREE);
3127   /* If there is a nested-name-specifier, then we are looking at
3128      the first qualified-id production.  */
3129   if (nested_name_specifier_p)
3130     {
3131       tree saved_scope;
3132       tree saved_object_scope;
3133       tree saved_qualifying_scope;
3134       tree unqualified_id;
3135       bool is_template;
3136
3137       /* See if the next token is the `template' keyword.  */
3138       if (!template_p)
3139         template_p = &is_template;
3140       *template_p = cp_parser_optional_template_keyword (parser);
3141       /* Name lookup we do during the processing of the
3142          unqualified-id might obliterate SCOPE.  */
3143       saved_scope = parser->scope;
3144       saved_object_scope = parser->object_scope;
3145       saved_qualifying_scope = parser->qualifying_scope;
3146       /* Process the final unqualified-id.  */
3147       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3148                                                  check_dependency_p,
3149                                                  declarator_p);
3150       /* Restore the SAVED_SCOPE for our caller.  */
3151       parser->scope = saved_scope;
3152       parser->object_scope = saved_object_scope;
3153       parser->qualifying_scope = saved_qualifying_scope;
3154
3155       return unqualified_id;
3156     }
3157   /* Otherwise, if we are in global scope, then we are looking at one
3158      of the other qualified-id productions.  */
3159   else if (global_scope_p)
3160     {
3161       cp_token *token;
3162       tree id;
3163
3164       /* Peek at the next token.  */
3165       token = cp_lexer_peek_token (parser->lexer);
3166
3167       /* If it's an identifier, and the next token is not a "<", then
3168          we can avoid the template-id case.  This is an optimization
3169          for this common case.  */
3170       if (token->type == CPP_NAME
3171           && !cp_parser_nth_token_starts_template_argument_list_p
3172                (parser, 2))
3173         return cp_parser_identifier (parser);
3174
3175       cp_parser_parse_tentatively (parser);
3176       /* Try a template-id.  */
3177       id = cp_parser_template_id (parser,
3178                                   /*template_keyword_p=*/false,
3179                                   /*check_dependency_p=*/true,
3180                                   declarator_p);
3181       /* If that worked, we're done.  */
3182       if (cp_parser_parse_definitely (parser))
3183         return id;
3184
3185       /* Peek at the next token.  (Changes in the token buffer may
3186          have invalidated the pointer obtained above.)  */
3187       token = cp_lexer_peek_token (parser->lexer);
3188
3189       switch (token->type)
3190         {
3191         case CPP_NAME:
3192           return cp_parser_identifier (parser);
3193
3194         case CPP_KEYWORD:
3195           if (token->keyword == RID_OPERATOR)
3196             return cp_parser_operator_function_id (parser);
3197           /* Fall through.  */
3198
3199         default:
3200           cp_parser_error (parser, "expected id-expression");
3201           return error_mark_node;
3202         }
3203     }
3204   else
3205     return cp_parser_unqualified_id (parser, template_keyword_p,
3206                                      /*check_dependency_p=*/true,
3207                                      declarator_p);
3208 }
3209
3210 /* Parse an unqualified-id.
3211
3212    unqualified-id:
3213      identifier
3214      operator-function-id
3215      conversion-function-id
3216      ~ class-name
3217      template-id
3218
3219    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3220    keyword, in a construct like `A::template ...'.
3221
3222    Returns a representation of unqualified-id.  For the `identifier'
3223    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3224    production a BIT_NOT_EXPR is returned; the operand of the
3225    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3226    other productions, see the documentation accompanying the
3227    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3228    names are looked up in uninstantiated templates.  If DECLARATOR_P
3229    is true, the unqualified-id is appearing as part of a declarator,
3230    rather than as part of an expression.  */
3231
3232 static tree
3233 cp_parser_unqualified_id (cp_parser* parser,
3234                           bool template_keyword_p,
3235                           bool check_dependency_p,
3236                           bool declarator_p)
3237 {
3238   cp_token *token;
3239
3240   /* Peek at the next token.  */
3241   token = cp_lexer_peek_token (parser->lexer);
3242
3243   switch (token->type)
3244     {
3245     case CPP_NAME:
3246       {
3247         tree id;
3248
3249         /* We don't know yet whether or not this will be a
3250            template-id.  */
3251         cp_parser_parse_tentatively (parser);
3252         /* Try a template-id.  */
3253         id = cp_parser_template_id (parser, template_keyword_p,
3254                                     check_dependency_p,
3255                                     declarator_p);
3256         /* If it worked, we're done.  */
3257         if (cp_parser_parse_definitely (parser))
3258           return id;
3259         /* Otherwise, it's an ordinary identifier.  */
3260         return cp_parser_identifier (parser);
3261       }
3262
3263     case CPP_TEMPLATE_ID:
3264       return cp_parser_template_id (parser, template_keyword_p,
3265                                     check_dependency_p,
3266                                     declarator_p);
3267
3268     case CPP_COMPL:
3269       {
3270         tree type_decl;
3271         tree qualifying_scope;
3272         tree object_scope;
3273         tree scope;
3274         bool done;
3275
3276         /* Consume the `~' token.  */
3277         cp_lexer_consume_token (parser->lexer);
3278         /* Parse the class-name.  The standard, as written, seems to
3279            say that:
3280
3281              template <typename T> struct S { ~S (); };
3282              template <typename T> S<T>::~S() {}
3283
3284            is invalid, since `~' must be followed by a class-name, but
3285            `S<T>' is dependent, and so not known to be a class.
3286            That's not right; we need to look in uninstantiated
3287            templates.  A further complication arises from:
3288
3289              template <typename T> void f(T t) {
3290                t.T::~T();
3291              }
3292
3293            Here, it is not possible to look up `T' in the scope of `T'
3294            itself.  We must look in both the current scope, and the
3295            scope of the containing complete expression.
3296
3297            Yet another issue is:
3298
3299              struct S {
3300                int S;
3301                ~S();
3302              };
3303
3304              S::~S() {}
3305
3306            The standard does not seem to say that the `S' in `~S'
3307            should refer to the type `S' and not the data member
3308            `S::S'.  */
3309
3310         /* DR 244 says that we look up the name after the "~" in the
3311            same scope as we looked up the qualifying name.  That idea
3312            isn't fully worked out; it's more complicated than that.  */
3313         scope = parser->scope;
3314         object_scope = parser->object_scope;
3315         qualifying_scope = parser->qualifying_scope;
3316
3317         /* If the name is of the form "X::~X" it's OK.  */
3318         if (scope && TYPE_P (scope)
3319             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3320             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3321                 == CPP_OPEN_PAREN)
3322             && (cp_lexer_peek_token (parser->lexer)->value
3323                 == TYPE_IDENTIFIER (scope)))
3324           {
3325             cp_lexer_consume_token (parser->lexer);
3326             return build_nt (BIT_NOT_EXPR, scope);
3327           }
3328
3329         /* If there was an explicit qualification (S::~T), first look
3330            in the scope given by the qualification (i.e., S).  */
3331         done = false;
3332         type_decl = NULL_TREE;
3333         if (scope)
3334           {
3335             cp_parser_parse_tentatively (parser);
3336             type_decl = cp_parser_class_name (parser,
3337                                               /*typename_keyword_p=*/false,
3338                                               /*template_keyword_p=*/false,
3339                                               none_type,
3340                                               /*check_dependency=*/false,
3341                                               /*class_head_p=*/false,
3342                                               declarator_p);
3343             if (cp_parser_parse_definitely (parser))
3344               done = true;
3345           }
3346         /* In "N::S::~S", look in "N" as well.  */
3347         if (!done && scope && qualifying_scope)
3348           {
3349             cp_parser_parse_tentatively (parser);
3350             parser->scope = qualifying_scope;
3351             parser->object_scope = NULL_TREE;
3352             parser->qualifying_scope = NULL_TREE;
3353             type_decl
3354               = cp_parser_class_name (parser,
3355                                       /*typename_keyword_p=*/false,
3356                                       /*template_keyword_p=*/false,
3357                                       none_type,
3358                                       /*check_dependency=*/false,
3359                                       /*class_head_p=*/false,
3360                                       declarator_p);
3361             if (cp_parser_parse_definitely (parser))
3362               done = true;
3363           }
3364         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3365         else if (!done && object_scope)
3366           {
3367             cp_parser_parse_tentatively (parser);
3368             parser->scope = object_scope;
3369             parser->object_scope = NULL_TREE;
3370             parser->qualifying_scope = NULL_TREE;
3371             type_decl
3372               = cp_parser_class_name (parser,
3373                                       /*typename_keyword_p=*/false,
3374                                       /*template_keyword_p=*/false,
3375                                       none_type,
3376                                       /*check_dependency=*/false,
3377                                       /*class_head_p=*/false,
3378                                       declarator_p);
3379             if (cp_parser_parse_definitely (parser))
3380               done = true;
3381           }
3382         /* Look in the surrounding context.  */
3383         if (!done)
3384           {
3385             parser->scope = NULL_TREE;
3386             parser->object_scope = NULL_TREE;
3387             parser->qualifying_scope = NULL_TREE;
3388             type_decl
3389               = cp_parser_class_name (parser,
3390                                       /*typename_keyword_p=*/false,
3391                                       /*template_keyword_p=*/false,
3392                                       none_type,
3393                                       /*check_dependency=*/false,
3394                                       /*class_head_p=*/false,
3395                                       declarator_p);
3396           }
3397         /* If an error occurred, assume that the name of the
3398            destructor is the same as the name of the qualifying
3399            class.  That allows us to keep parsing after running
3400            into ill-formed destructor names.  */
3401         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3402           return build_nt (BIT_NOT_EXPR, scope);
3403         else if (type_decl == error_mark_node)
3404           return error_mark_node;
3405
3406         /* [class.dtor]
3407
3408            A typedef-name that names a class shall not be used as the
3409            identifier in the declarator for a destructor declaration.  */
3410         if (declarator_p
3411             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3412             && !DECL_SELF_REFERENCE_P (type_decl)
3413             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3414           error ("typedef-name %qD used as destructor declarator",
3415                  type_decl);
3416
3417         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3418       }
3419
3420     case CPP_KEYWORD:
3421       if (token->keyword == RID_OPERATOR)
3422         {
3423           tree id;
3424
3425           /* This could be a template-id, so we try that first.  */
3426           cp_parser_parse_tentatively (parser);
3427           /* Try a template-id.  */
3428           id = cp_parser_template_id (parser, template_keyword_p,
3429                                       /*check_dependency_p=*/true,
3430                                       declarator_p);
3431           /* If that worked, we're done.  */
3432           if (cp_parser_parse_definitely (parser))
3433             return id;
3434           /* We still don't know whether we're looking at an
3435              operator-function-id or a conversion-function-id.  */
3436           cp_parser_parse_tentatively (parser);
3437           /* Try an operator-function-id.  */
3438           id = cp_parser_operator_function_id (parser);
3439           /* If that didn't work, try a conversion-function-id.  */
3440           if (!cp_parser_parse_definitely (parser))
3441             id = cp_parser_conversion_function_id (parser);
3442
3443           return id;
3444         }
3445       /* Fall through.  */
3446
3447     default:
3448       cp_parser_error (parser, "expected unqualified-id");
3449       return error_mark_node;
3450     }
3451 }
3452
3453 /* Parse an (optional) nested-name-specifier.
3454
3455    nested-name-specifier:
3456      class-or-namespace-name :: nested-name-specifier [opt]
3457      class-or-namespace-name :: template nested-name-specifier [opt]
3458
3459    PARSER->SCOPE should be set appropriately before this function is
3460    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3461    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3462    in name lookups.
3463
3464    Sets PARSER->SCOPE to the class (TYPE) or namespace
3465    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3466    it unchanged if there is no nested-name-specifier.  Returns the new
3467    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3468
3469    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3470    part of a declaration and/or decl-specifier.  */
3471
3472 static tree
3473 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3474                                      bool typename_keyword_p,
3475                                      bool check_dependency_p,
3476                                      bool type_p,
3477                                      bool is_declaration)
3478 {
3479   bool success = false;
3480   tree access_check = NULL_TREE;
3481   cp_token_position start = 0;
3482   cp_token *token;
3483
3484   /* If the next token corresponds to a nested name specifier, there
3485      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3486      false, it may have been true before, in which case something
3487      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3488      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3489      CHECK_DEPENDENCY_P is false, we have to fall through into the
3490      main loop.  */
3491   if (check_dependency_p
3492       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3493     {
3494       cp_parser_pre_parsed_nested_name_specifier (parser);
3495       return parser->scope;
3496     }
3497
3498   /* Remember where the nested-name-specifier starts.  */
3499   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3500     start = cp_lexer_token_position (parser->lexer, false);
3501
3502   push_deferring_access_checks (dk_deferred);
3503
3504   while (true)
3505     {
3506       tree new_scope;
3507       tree old_scope;
3508       tree saved_qualifying_scope;
3509       bool template_keyword_p;
3510
3511       /* Spot cases that cannot be the beginning of a
3512          nested-name-specifier.  */
3513       token = cp_lexer_peek_token (parser->lexer);
3514
3515       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3516          the already parsed nested-name-specifier.  */
3517       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3518         {
3519           /* Grab the nested-name-specifier and continue the loop.  */
3520           cp_parser_pre_parsed_nested_name_specifier (parser);
3521           success = true;
3522           continue;
3523         }
3524
3525       /* Spot cases that cannot be the beginning of a
3526          nested-name-specifier.  On the second and subsequent times
3527          through the loop, we look for the `template' keyword.  */
3528       if (success && token->keyword == RID_TEMPLATE)
3529         ;
3530       /* A template-id can start a nested-name-specifier.  */
3531       else if (token->type == CPP_TEMPLATE_ID)
3532         ;
3533       else
3534         {
3535           /* If the next token is not an identifier, then it is
3536              definitely not a class-or-namespace-name.  */
3537           if (token->type != CPP_NAME)
3538             break;
3539           /* If the following token is neither a `<' (to begin a
3540              template-id), nor a `::', then we are not looking at a
3541              nested-name-specifier.  */
3542           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3543           if (token->type != CPP_SCOPE
3544               && !cp_parser_nth_token_starts_template_argument_list_p
3545                   (parser, 2))
3546             break;
3547         }
3548
3549       /* The nested-name-specifier is optional, so we parse
3550          tentatively.  */
3551       cp_parser_parse_tentatively (parser);
3552
3553       /* Look for the optional `template' keyword, if this isn't the
3554          first time through the loop.  */
3555       if (success)
3556         template_keyword_p = cp_parser_optional_template_keyword (parser);
3557       else
3558         template_keyword_p = false;
3559
3560       /* Save the old scope since the name lookup we are about to do
3561          might destroy it.  */
3562       old_scope = parser->scope;
3563       saved_qualifying_scope = parser->qualifying_scope;
3564       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3565          look up names in "X<T>::I" in order to determine that "Y" is
3566          a template.  So, if we have a typename at this point, we make
3567          an effort to look through it.  */
3568       if (is_declaration
3569           && !typename_keyword_p
3570           && parser->scope
3571           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3572         parser->scope = resolve_typename_type (parser->scope,
3573                                                /*only_current_p=*/false);
3574       /* Parse the qualifying entity.  */
3575       new_scope
3576         = cp_parser_class_or_namespace_name (parser,
3577                                              typename_keyword_p,
3578                                              template_keyword_p,
3579                                              check_dependency_p,
3580                                              type_p,
3581                                              is_declaration);
3582       /* Look for the `::' token.  */
3583       cp_parser_require (parser, CPP_SCOPE, "`::'");
3584
3585       /* If we found what we wanted, we keep going; otherwise, we're
3586          done.  */
3587       if (!cp_parser_parse_definitely (parser))
3588         {
3589           bool error_p = false;
3590
3591           /* Restore the OLD_SCOPE since it was valid before the
3592              failed attempt at finding the last
3593              class-or-namespace-name.  */
3594           parser->scope = old_scope;
3595           parser->qualifying_scope = saved_qualifying_scope;
3596           /* If the next token is an identifier, and the one after
3597              that is a `::', then any valid interpretation would have
3598              found a class-or-namespace-name.  */
3599           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3600                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3601                      == CPP_SCOPE)
3602                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3603                      != CPP_COMPL))
3604             {
3605               token = cp_lexer_consume_token (parser->lexer);
3606               if (!error_p)
3607                 {
3608                   tree decl;
3609
3610                   decl = cp_parser_lookup_name_simple (parser, token->value);
3611                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3612                     error ("%qD used without template parameters", decl);
3613                   else
3614                     cp_parser_name_lookup_error
3615                       (parser, token->value, decl,
3616                        "is not a class or namespace");
3617                   parser->scope = NULL_TREE;
3618                   error_p = true;
3619                   /* Treat this as a successful nested-name-specifier
3620                      due to:
3621
3622                      [basic.lookup.qual]
3623
3624                      If the name found is not a class-name (clause
3625                      _class_) or namespace-name (_namespace.def_), the
3626                      program is ill-formed.  */
3627                   success = true;
3628                 }
3629               cp_lexer_consume_token (parser->lexer);
3630             }
3631           break;
3632         }
3633       /* We've found one valid nested-name-specifier.  */
3634       success = true;
3635       /* Name lookup always gives us a DECL.  */
3636       if (TREE_CODE (new_scope) == TYPE_DECL)
3637         new_scope = TREE_TYPE (new_scope);
3638       /* Uses of "template" must be followed by actual templates.  */
3639       if (template_keyword_p
3640           && !(CLASS_TYPE_P (new_scope)
3641                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3642                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3643                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3644           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3645                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3646                    == TEMPLATE_ID_EXPR)))
3647         pedwarn (TYPE_P (new_scope)
3648                  ? "%qT is not a template"
3649                  : "%qD is not a template",
3650                  new_scope);
3651       /* If it is a class scope, try to complete it; we are about to
3652          be looking up names inside the class.  */
3653       if (TYPE_P (new_scope)
3654           /* Since checking types for dependency can be expensive,
3655              avoid doing it if the type is already complete.  */
3656           && !COMPLETE_TYPE_P (new_scope)
3657           /* Do not try to complete dependent types.  */
3658           && !dependent_type_p (new_scope))
3659         new_scope = complete_type (new_scope);
3660       /* Make sure we look in the right scope the next time through
3661          the loop.  */
3662       parser->scope = new_scope;
3663     }
3664
3665   /* Retrieve any deferred checks.  Do not pop this access checks yet
3666      so the memory will not be reclaimed during token replacing below.  */
3667   access_check = get_deferred_access_checks ();
3668
3669   /* If parsing tentatively, replace the sequence of tokens that makes
3670      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3671      token.  That way, should we re-parse the token stream, we will
3672      not have to repeat the effort required to do the parse, nor will
3673      we issue duplicate error messages.  */
3674   if (success && start)
3675     {
3676       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3677
3678       /* Reset the contents of the START token.  */
3679       token->type = CPP_NESTED_NAME_SPECIFIER;
3680       token->value = build_tree_list (access_check, parser->scope);
3681       TREE_TYPE (token->value) = parser->qualifying_scope;
3682       token->keyword = RID_MAX;
3683
3684       /* Purge all subsequent tokens.  */
3685       cp_lexer_purge_tokens_after (parser->lexer, start);
3686     }
3687
3688   pop_deferring_access_checks ();
3689   return success ? parser->scope : NULL_TREE;
3690 }
3691
3692 /* Parse a nested-name-specifier.  See
3693    cp_parser_nested_name_specifier_opt for details.  This function
3694    behaves identically, except that it will an issue an error if no
3695    nested-name-specifier is present.  */
3696
3697 static tree
3698 cp_parser_nested_name_specifier (cp_parser *parser,
3699                                  bool typename_keyword_p,
3700                                  bool check_dependency_p,
3701                                  bool type_p,
3702                                  bool is_declaration)
3703 {
3704   tree scope;
3705
3706   /* Look for the nested-name-specifier.  */
3707   scope = cp_parser_nested_name_specifier_opt (parser,
3708                                                typename_keyword_p,
3709                                                check_dependency_p,
3710                                                type_p,
3711                                                is_declaration);
3712   /* If it was not present, issue an error message.  */
3713   if (!scope)
3714     {
3715       cp_parser_error (parser, "expected nested-name-specifier");
3716       parser->scope = NULL_TREE;
3717     }
3718
3719   return scope;
3720 }
3721
3722 /* Parse a class-or-namespace-name.
3723
3724    class-or-namespace-name:
3725      class-name
3726      namespace-name
3727
3728    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3729    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3730    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3731    TYPE_P is TRUE iff the next name should be taken as a class-name,
3732    even the same name is declared to be another entity in the same
3733    scope.
3734
3735    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3736    specified by the class-or-namespace-name.  If neither is found the
3737    ERROR_MARK_NODE is returned.  */
3738
3739 static tree
3740 cp_parser_class_or_namespace_name (cp_parser *parser,
3741                                    bool typename_keyword_p,
3742                                    bool template_keyword_p,
3743                                    bool check_dependency_p,
3744                                    bool type_p,
3745                                    bool is_declaration)
3746 {
3747   tree saved_scope;
3748   tree saved_qualifying_scope;
3749   tree saved_object_scope;
3750   tree scope;
3751   bool only_class_p;
3752
3753   /* Before we try to parse the class-name, we must save away the
3754      current PARSER->SCOPE since cp_parser_class_name will destroy
3755      it.  */
3756   saved_scope = parser->scope;
3757   saved_qualifying_scope = parser->qualifying_scope;
3758   saved_object_scope = parser->object_scope;
3759   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3760      there is no need to look for a namespace-name.  */
3761   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3762   if (!only_class_p)
3763     cp_parser_parse_tentatively (parser);
3764   scope = cp_parser_class_name (parser,
3765                                 typename_keyword_p,
3766                                 template_keyword_p,
3767                                 type_p ? class_type : none_type,
3768                                 check_dependency_p,
3769                                 /*class_head_p=*/false,
3770                                 is_declaration);
3771   /* If that didn't work, try for a namespace-name.  */
3772   if (!only_class_p && !cp_parser_parse_definitely (parser))
3773     {
3774       /* Restore the saved scope.  */
3775       parser->scope = saved_scope;
3776       parser->qualifying_scope = saved_qualifying_scope;
3777       parser->object_scope = saved_object_scope;
3778       /* If we are not looking at an identifier followed by the scope
3779          resolution operator, then this is not part of a
3780          nested-name-specifier.  (Note that this function is only used
3781          to parse the components of a nested-name-specifier.)  */
3782       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3783           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3784         return error_mark_node;
3785       scope = cp_parser_namespace_name (parser);
3786     }
3787
3788   return scope;
3789 }
3790
3791 /* Parse a postfix-expression.
3792
3793    postfix-expression:
3794      primary-expression
3795      postfix-expression [ expression ]
3796      postfix-expression ( expression-list [opt] )
3797      simple-type-specifier ( expression-list [opt] )
3798      typename :: [opt] nested-name-specifier identifier
3799        ( expression-list [opt] )
3800      typename :: [opt] nested-name-specifier template [opt] template-id
3801        ( expression-list [opt] )
3802      postfix-expression . template [opt] id-expression
3803      postfix-expression -> template [opt] id-expression
3804      postfix-expression . pseudo-destructor-name
3805      postfix-expression -> pseudo-destructor-name
3806      postfix-expression ++
3807      postfix-expression --
3808      dynamic_cast < type-id > ( expression )
3809      static_cast < type-id > ( expression )
3810      reinterpret_cast < type-id > ( expression )
3811      const_cast < type-id > ( expression )
3812      typeid ( expression )
3813      typeid ( type-id )
3814
3815    GNU Extension:
3816
3817    postfix-expression:
3818      ( type-id ) { initializer-list , [opt] }
3819
3820    This extension is a GNU version of the C99 compound-literal
3821    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3822    but they are essentially the same concept.)
3823
3824    If ADDRESS_P is true, the postfix expression is the operand of the
3825    `&' operator.  CAST_P is true if this expression is the target of a
3826    cast.
3827
3828    Returns a representation of the expression.  */
3829
3830 static tree
3831 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3832 {
3833   cp_token *token;
3834   enum rid keyword;
3835   cp_id_kind idk = CP_ID_KIND_NONE;
3836   tree postfix_expression = NULL_TREE;
3837
3838   /* Peek at the next token.  */
3839   token = cp_lexer_peek_token (parser->lexer);
3840   /* Some of the productions are determined by keywords.  */
3841   keyword = token->keyword;
3842   switch (keyword)
3843     {
3844     case RID_DYNCAST:
3845     case RID_STATCAST:
3846     case RID_REINTCAST:
3847     case RID_CONSTCAST:
3848       {
3849         tree type;
3850         tree expression;
3851         const char *saved_message;
3852
3853         /* All of these can be handled in the same way from the point
3854            of view of parsing.  Begin by consuming the token
3855            identifying the cast.  */
3856         cp_lexer_consume_token (parser->lexer);
3857
3858         /* New types cannot be defined in the cast.  */
3859         saved_message = parser->type_definition_forbidden_message;
3860         parser->type_definition_forbidden_message
3861           = "types may not be defined in casts";
3862
3863         /* Look for the opening `<'.  */
3864         cp_parser_require (parser, CPP_LESS, "`<'");
3865         /* Parse the type to which we are casting.  */
3866         type = cp_parser_type_id (parser);
3867         /* Look for the closing `>'.  */
3868         cp_parser_require (parser, CPP_GREATER, "`>'");
3869         /* Restore the old message.  */
3870         parser->type_definition_forbidden_message = saved_message;
3871
3872         /* And the expression which is being cast.  */
3873         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3874         expression = cp_parser_expression (parser, /*cast_p=*/true);
3875         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3876
3877         /* Only type conversions to integral or enumeration types
3878            can be used in constant-expressions.  */
3879         if (parser->integral_constant_expression_p
3880             && !dependent_type_p (type)
3881             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3882             && (cp_parser_non_integral_constant_expression
3883                 (parser,
3884                  "a cast to a type other than an integral or "
3885                  "enumeration type")))
3886           return error_mark_node;
3887
3888         switch (keyword)
3889           {
3890           case RID_DYNCAST:
3891             postfix_expression
3892               = build_dynamic_cast (type, expression);
3893             break;
3894           case RID_STATCAST:
3895             postfix_expression
3896               = build_static_cast (type, expression);
3897             break;
3898           case RID_REINTCAST:
3899             postfix_expression
3900               = build_reinterpret_cast (type, expression);
3901             break;
3902           case RID_CONSTCAST:
3903             postfix_expression
3904               = build_const_cast (type, expression);
3905             break;
3906           default:
3907             gcc_unreachable ();
3908           }
3909       }
3910       break;
3911
3912     case RID_TYPEID:
3913       {
3914         tree type;
3915         const char *saved_message;
3916         bool saved_in_type_id_in_expr_p;
3917
3918         /* Consume the `typeid' token.  */
3919         cp_lexer_consume_token (parser->lexer);
3920         /* Look for the `(' token.  */
3921         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3922         /* Types cannot be defined in a `typeid' expression.  */
3923         saved_message = parser->type_definition_forbidden_message;
3924         parser->type_definition_forbidden_message
3925           = "types may not be defined in a `typeid\' expression";
3926         /* We can't be sure yet whether we're looking at a type-id or an
3927            expression.  */
3928         cp_parser_parse_tentatively (parser);
3929         /* Try a type-id first.  */
3930         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3931         parser->in_type_id_in_expr_p = true;
3932         type = cp_parser_type_id (parser);
3933         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3934         /* Look for the `)' token.  Otherwise, we can't be sure that
3935            we're not looking at an expression: consider `typeid (int
3936            (3))', for example.  */
3937         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3938         /* If all went well, simply lookup the type-id.  */
3939         if (cp_parser_parse_definitely (parser))
3940           postfix_expression = get_typeid (type);
3941         /* Otherwise, fall back to the expression variant.  */
3942         else
3943           {
3944             tree expression;
3945
3946             /* Look for an expression.  */
3947             expression = cp_parser_expression (parser, /*cast_p=*/false);
3948             /* Compute its typeid.  */
3949             postfix_expression = build_typeid (expression);
3950             /* Look for the `)' token.  */
3951             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3952           }
3953         /* `typeid' may not appear in an integral constant expression.  */
3954         if (cp_parser_non_integral_constant_expression(parser,
3955                                                        "`typeid' operator"))
3956           return error_mark_node;
3957         /* Restore the saved message.  */
3958         parser->type_definition_forbidden_message = saved_message;
3959       }
3960       break;
3961
3962     case RID_TYPENAME:
3963       {
3964         bool template_p = false;
3965         tree id;
3966         tree type;
3967         tree scope;
3968
3969         /* Consume the `typename' token.  */
3970         cp_lexer_consume_token (parser->lexer);
3971
3972         /* Look for the optional `::' operator.  */
3973         cp_parser_global_scope_opt (parser,
3974                                     /*current_scope_valid_p=*/false);
3975         /* Look for the nested-name-specifier.  In case of error here,
3976            consume the trailing id to avoid subsequent error messages
3977            for usual cases.  */
3978         scope = cp_parser_nested_name_specifier (parser,
3979                                                  /*typename_keyword_p=*/true,
3980                                                  /*check_dependency_p=*/true,
3981                                                  /*type_p=*/true,
3982                                                  /*is_declaration=*/true);
3983
3984         /* Look for the optional `template' keyword.  */
3985         template_p = cp_parser_optional_template_keyword (parser);
3986         /* We don't know whether we're looking at a template-id or an
3987            identifier.  */
3988         cp_parser_parse_tentatively (parser);
3989         /* Try a template-id.  */
3990         id = cp_parser_template_id (parser, template_p,
3991                                     /*check_dependency_p=*/true,
3992                                     /*is_declaration=*/true);
3993         /* If that didn't work, try an identifier.  */
3994         if (!cp_parser_parse_definitely (parser))
3995           id = cp_parser_identifier (parser);
3996
3997         /* Don't process id if nested name specifier is invalid.  */
3998         if (!scope || scope == error_mark_node)
3999           return error_mark_node;
4000         /* If we look up a template-id in a non-dependent qualifying
4001            scope, there's no need to create a dependent type.  */
4002         if (TREE_CODE (id) == TYPE_DECL
4003             && (!TYPE_P (scope)
4004                 || !dependent_type_p (parser->scope)))
4005           type = TREE_TYPE (id);
4006         /* Create a TYPENAME_TYPE to represent the type to which the
4007            functional cast is being performed.  */
4008         else
4009           type = make_typename_type (parser->scope, id,
4010                                      typename_type,
4011                                      /*complain=*/1);
4012
4013         postfix_expression = cp_parser_functional_cast (parser, type);
4014       }
4015       break;
4016
4017     default:
4018       {
4019         tree type;
4020
4021         /* If the next thing is a simple-type-specifier, we may be
4022            looking at a functional cast.  We could also be looking at
4023            an id-expression.  So, we try the functional cast, and if
4024            that doesn't work we fall back to the primary-expression.  */
4025         cp_parser_parse_tentatively (parser);
4026         /* Look for the simple-type-specifier.  */
4027         type = cp_parser_simple_type_specifier (parser,
4028                                                 /*decl_specs=*/NULL,
4029                                                 CP_PARSER_FLAGS_NONE);
4030         /* Parse the cast itself.  */
4031         if (!cp_parser_error_occurred (parser))
4032           postfix_expression
4033             = cp_parser_functional_cast (parser, type);
4034         /* If that worked, we're done.  */
4035         if (cp_parser_parse_definitely (parser))
4036           break;
4037
4038         /* If the functional-cast didn't work out, try a
4039            compound-literal.  */
4040         if (cp_parser_allow_gnu_extensions_p (parser)
4041             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4042           {
4043             VEC(constructor_elt,gc) *initializer_list = NULL;
4044             bool saved_in_type_id_in_expr_p;
4045
4046             cp_parser_parse_tentatively (parser);
4047             /* Consume the `('.  */
4048             cp_lexer_consume_token (parser->lexer);
4049             /* Parse the type.  */
4050             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4051             parser->in_type_id_in_expr_p = true;
4052             type = cp_parser_type_id (parser);
4053             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4054             /* Look for the `)'.  */
4055             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4056             /* Look for the `{'.  */
4057             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4058             /* If things aren't going well, there's no need to
4059                keep going.  */
4060             if (!cp_parser_error_occurred (parser))
4061               {
4062                 bool non_constant_p;
4063                 /* Parse the initializer-list.  */
4064                 initializer_list
4065                   = cp_parser_initializer_list (parser, &non_constant_p);
4066                 /* Allow a trailing `,'.  */
4067                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4068                   cp_lexer_consume_token (parser->lexer);
4069                 /* Look for the final `}'.  */
4070                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4071               }
4072             /* If that worked, we're definitely looking at a
4073                compound-literal expression.  */
4074             if (cp_parser_parse_definitely (parser))
4075               {
4076                 /* Warn the user that a compound literal is not
4077                    allowed in standard C++.  */
4078                 if (pedantic)
4079                   pedwarn ("ISO C++ forbids compound-literals");
4080                 /* Form the representation of the compound-literal.  */
4081                 postfix_expression
4082                   = finish_compound_literal (type, initializer_list);
4083                 break;
4084               }
4085           }
4086
4087         /* It must be a primary-expression.  */
4088         postfix_expression 
4089           = cp_parser_primary_expression (parser, address_p, cast_p, 
4090                                           /*template_arg_p=*/false,
4091                                           &idk);
4092       }
4093       break;
4094     }
4095
4096   /* Keep looping until the postfix-expression is complete.  */
4097   while (true)
4098     {
4099       if (idk == CP_ID_KIND_UNQUALIFIED
4100           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4101           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4102         /* It is not a Koenig lookup function call.  */
4103         postfix_expression
4104           = unqualified_name_lookup_error (postfix_expression);
4105
4106       /* Peek at the next token.  */
4107       token = cp_lexer_peek_token (parser->lexer);
4108
4109       switch (token->type)
4110         {
4111         case CPP_OPEN_SQUARE:
4112           postfix_expression
4113             = cp_parser_postfix_open_square_expression (parser,
4114                                                         postfix_expression,
4115                                                         false);
4116           idk = CP_ID_KIND_NONE;
4117           break;
4118
4119         case CPP_OPEN_PAREN:
4120           /* postfix-expression ( expression-list [opt] ) */
4121           {
4122             bool koenig_p;
4123             bool is_builtin_constant_p;
4124             bool saved_integral_constant_expression_p = false;
4125             bool saved_non_integral_constant_expression_p = false;
4126             tree args;
4127
4128             is_builtin_constant_p
4129               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4130             if (is_builtin_constant_p)
4131               {
4132                 /* The whole point of __builtin_constant_p is to allow
4133                    non-constant expressions to appear as arguments.  */
4134                 saved_integral_constant_expression_p
4135                   = parser->integral_constant_expression_p;
4136                 saved_non_integral_constant_expression_p
4137                   = parser->non_integral_constant_expression_p;
4138                 parser->integral_constant_expression_p = false;
4139               }
4140             args = (cp_parser_parenthesized_expression_list
4141                     (parser, /*is_attribute_list=*/false,
4142                      /*cast_p=*/false,
4143                      /*non_constant_p=*/NULL));
4144             if (is_builtin_constant_p)
4145               {
4146                 parser->integral_constant_expression_p
4147                   = saved_integral_constant_expression_p;
4148                 parser->non_integral_constant_expression_p
4149                   = saved_non_integral_constant_expression_p;
4150               }
4151
4152             if (args == error_mark_node)
4153               {
4154                 postfix_expression = error_mark_node;
4155                 break;
4156               }
4157
4158             /* Function calls are not permitted in
4159                constant-expressions.  */
4160             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4161                 && cp_parser_non_integral_constant_expression (parser,
4162                                                                "a function call"))
4163               {
4164                 postfix_expression = error_mark_node;
4165                 break;
4166               }
4167
4168             koenig_p = false;
4169             if (idk == CP_ID_KIND_UNQUALIFIED)
4170               {
4171                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4172                   {
4173                     if (args)
4174                       {
4175                         koenig_p = true;
4176                         postfix_expression
4177                           = perform_koenig_lookup (postfix_expression, args);
4178                       }
4179                     else
4180                       postfix_expression
4181                         = unqualified_fn_lookup_error (postfix_expression);
4182                   }
4183                 /* We do not perform argument-dependent lookup if
4184                    normal lookup finds a non-function, in accordance
4185                    with the expected resolution of DR 218.  */
4186                 else if (args && is_overloaded_fn (postfix_expression))
4187                   {
4188                     tree fn = get_first_fn (postfix_expression);
4189
4190                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4191                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4192
4193                     /* Only do argument dependent lookup if regular
4194                        lookup does not find a set of member functions.
4195                        [basic.lookup.koenig]/2a  */
4196                     if (!DECL_FUNCTION_MEMBER_P (fn))
4197                       {
4198                         koenig_p = true;
4199                         postfix_expression
4200                           = perform_koenig_lookup (postfix_expression, args);
4201                       }
4202                   }
4203               }
4204
4205             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4206               {
4207                 tree instance = TREE_OPERAND (postfix_expression, 0);
4208                 tree fn = TREE_OPERAND (postfix_expression, 1);
4209
4210                 if (processing_template_decl
4211                     && (type_dependent_expression_p (instance)
4212                         || (!BASELINK_P (fn)
4213                             && TREE_CODE (fn) != FIELD_DECL)
4214                         || type_dependent_expression_p (fn)
4215                         || any_type_dependent_arguments_p (args)))
4216                   {
4217                     postfix_expression
4218                       = build_min_nt (CALL_EXPR, postfix_expression,
4219                                       args, NULL_TREE);
4220                     break;
4221                   }
4222
4223                 if (BASELINK_P (fn))
4224                   postfix_expression
4225                     = (build_new_method_call
4226                        (instance, fn, args, NULL_TREE,
4227                         (idk == CP_ID_KIND_QUALIFIED
4228                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4229                 else
4230                   postfix_expression
4231                     = finish_call_expr (postfix_expression, args,
4232                                         /*disallow_virtual=*/false,
4233                                         /*koenig_p=*/false);
4234               }
4235             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4236                      || TREE_CODE (postfix_expression) == MEMBER_REF
4237                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4238               postfix_expression = (build_offset_ref_call_from_tree
4239                                     (postfix_expression, args));
4240             else if (idk == CP_ID_KIND_QUALIFIED)
4241               /* A call to a static class member, or a namespace-scope
4242                  function.  */
4243               postfix_expression
4244                 = finish_call_expr (postfix_expression, args,
4245                                     /*disallow_virtual=*/true,
4246                                     koenig_p);
4247             else
4248               /* All other function calls.  */
4249               postfix_expression
4250                 = finish_call_expr (postfix_expression, args,
4251                                     /*disallow_virtual=*/false,
4252                                     koenig_p);
4253
4254             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4255             idk = CP_ID_KIND_NONE;
4256           }
4257           break;
4258
4259         case CPP_DOT:
4260         case CPP_DEREF:
4261           /* postfix-expression . template [opt] id-expression
4262              postfix-expression . pseudo-destructor-name
4263              postfix-expression -> template [opt] id-expression
4264              postfix-expression -> pseudo-destructor-name */
4265
4266           /* Consume the `.' or `->' operator.  */
4267           cp_lexer_consume_token (parser->lexer);
4268
4269           postfix_expression
4270             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4271                                                       postfix_expression,
4272                                                       false, &idk);
4273           break;
4274
4275         case CPP_PLUS_PLUS:
4276           /* postfix-expression ++  */
4277           /* Consume the `++' token.  */
4278           cp_lexer_consume_token (parser->lexer);
4279           /* Generate a representation for the complete expression.  */
4280           postfix_expression
4281             = finish_increment_expr (postfix_expression,
4282                                      POSTINCREMENT_EXPR);
4283           /* Increments may not appear in constant-expressions.  */
4284           if (cp_parser_non_integral_constant_expression (parser,
4285                                                           "an increment"))
4286             postfix_expression = error_mark_node;
4287           idk = CP_ID_KIND_NONE;
4288           break;
4289
4290         case CPP_MINUS_MINUS:
4291           /* postfix-expression -- */
4292           /* Consume the `--' token.  */
4293           cp_lexer_consume_token (parser->lexer);
4294           /* Generate a representation for the complete expression.  */
4295           postfix_expression
4296             = finish_increment_expr (postfix_expression,
4297                                      POSTDECREMENT_EXPR);
4298           /* Decrements may not appear in constant-expressions.  */
4299           if (cp_parser_non_integral_constant_expression (parser,
4300                                                           "a decrement"))
4301             postfix_expression = error_mark_node;
4302           idk = CP_ID_KIND_NONE;
4303           break;
4304
4305         default:
4306           return postfix_expression;
4307         }
4308     }
4309
4310   /* We should never get here.  */
4311   gcc_unreachable ();
4312   return error_mark_node;
4313 }
4314
4315 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4316    by cp_parser_builtin_offsetof.  We're looking for
4317
4318      postfix-expression [ expression ]
4319
4320    FOR_OFFSETOF is set if we're being called in that context, which
4321    changes how we deal with integer constant expressions.  */
4322
4323 static tree
4324 cp_parser_postfix_open_square_expression (cp_parser *parser,
4325                                           tree postfix_expression,
4326                                           bool for_offsetof)
4327 {
4328   tree index;
4329
4330   /* Consume the `[' token.  */
4331   cp_lexer_consume_token (parser->lexer);
4332
4333   /* Parse the index expression.  */
4334   /* ??? For offsetof, there is a question of what to allow here.  If
4335      offsetof is not being used in an integral constant expression context,
4336      then we *could* get the right answer by computing the value at runtime.
4337      If we are in an integral constant expression context, then we might
4338      could accept any constant expression; hard to say without analysis.
4339      Rather than open the barn door too wide right away, allow only integer
4340      constant expressions here.  */
4341   if (for_offsetof)
4342     index = cp_parser_constant_expression (parser, false, NULL);
4343   else
4344     index = cp_parser_expression (parser, /*cast_p=*/false);
4345
4346   /* Look for the closing `]'.  */
4347   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4348
4349   /* Build the ARRAY_REF.  */
4350   postfix_expression = grok_array_decl (postfix_expression, index);
4351
4352   /* When not doing offsetof, array references are not permitted in
4353      constant-expressions.  */
4354   if (!for_offsetof
4355       && (cp_parser_non_integral_constant_expression
4356           (parser, "an array reference")))
4357     postfix_expression = error_mark_node;
4358
4359   return postfix_expression;
4360 }
4361
4362 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4363    by cp_parser_builtin_offsetof.  We're looking for
4364
4365      postfix-expression . template [opt] id-expression
4366      postfix-expression . pseudo-destructor-name
4367      postfix-expression -> template [opt] id-expression
4368      postfix-expression -> pseudo-destructor-name
4369
4370    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4371    limits what of the above we'll actually accept, but nevermind.
4372    TOKEN_TYPE is the "." or "->" token, which will already have been
4373    removed from the stream.  */
4374
4375 static tree
4376 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4377                                         enum cpp_ttype token_type,
4378                                         tree postfix_expression,
4379                                         bool for_offsetof, cp_id_kind *idk)
4380 {
4381   tree name;
4382   bool dependent_p;
4383   bool pseudo_destructor_p;
4384   tree scope = NULL_TREE;
4385
4386   /* If this is a `->' operator, dereference the pointer.  */
4387   if (token_type == CPP_DEREF)
4388     postfix_expression = build_x_arrow (postfix_expression);
4389   /* Check to see whether or not the expression is type-dependent.  */
4390   dependent_p = type_dependent_expression_p (postfix_expression);
4391   /* The identifier following the `->' or `.' is not qualified.  */
4392   parser->scope = NULL_TREE;
4393   parser->qualifying_scope = NULL_TREE;
4394   parser->object_scope = NULL_TREE;
4395   *idk = CP_ID_KIND_NONE;
4396   /* Enter the scope corresponding to the type of the object
4397      given by the POSTFIX_EXPRESSION.  */
4398   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4399     {
4400       scope = TREE_TYPE (postfix_expression);
4401       /* According to the standard, no expression should ever have
4402          reference type.  Unfortunately, we do not currently match
4403          the standard in this respect in that our internal representation
4404          of an expression may have reference type even when the standard
4405          says it does not.  Therefore, we have to manually obtain the
4406          underlying type here.  */
4407       scope = non_reference (scope);
4408       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4409       scope = complete_type_or_else (scope, NULL_TREE);
4410       /* Let the name lookup machinery know that we are processing a
4411          class member access expression.  */
4412       parser->context->object_type = scope;
4413       /* If something went wrong, we want to be able to discern that case,
4414          as opposed to the case where there was no SCOPE due to the type
4415          of expression being dependent.  */
4416       if (!scope)
4417         scope = error_mark_node;
4418       /* If the SCOPE was erroneous, make the various semantic analysis
4419          functions exit quickly -- and without issuing additional error
4420          messages.  */
4421       if (scope == error_mark_node)
4422         postfix_expression = error_mark_node;
4423     }
4424
4425   /* Assume this expression is not a pseudo-destructor access.  */
4426   pseudo_destructor_p = false;
4427
4428   /* If the SCOPE is a scalar type, then, if this is a valid program,
4429      we must be looking at a pseudo-destructor-name.  */
4430   if (scope && SCALAR_TYPE_P (scope))
4431     {
4432       tree s;
4433       tree type;
4434
4435       cp_parser_parse_tentatively (parser);
4436       /* Parse the pseudo-destructor-name.  */
4437       s = NULL_TREE;
4438       cp_parser_pseudo_destructor_name (parser, &s, &type);
4439       if (cp_parser_parse_definitely (parser))
4440         {
4441           pseudo_destructor_p = true;
4442           postfix_expression
4443             = finish_pseudo_destructor_expr (postfix_expression,
4444                                              s, TREE_TYPE (type));
4445         }
4446     }
4447
4448   if (!pseudo_destructor_p)
4449     {
4450       /* If the SCOPE is not a scalar type, we are looking at an
4451          ordinary class member access expression, rather than a
4452          pseudo-destructor-name.  */
4453       bool template_p;
4454       /* Parse the id-expression.  */
4455       name = (cp_parser_id_expression 
4456               (parser, 
4457                cp_parser_optional_template_keyword (parser),
4458                /*check_dependency_p=*/true,
4459                &template_p,
4460                /*declarator_p=*/false));
4461       /* In general, build a SCOPE_REF if the member name is qualified.
4462          However, if the name was not dependent and has already been
4463          resolved; there is no need to build the SCOPE_REF.  For example;
4464
4465              struct X { void f(); };
4466              template <typename T> void f(T* t) { t->X::f(); }
4467
4468          Even though "t" is dependent, "X::f" is not and has been resolved
4469          to a BASELINK; there is no need to include scope information.  */
4470
4471       /* But we do need to remember that there was an explicit scope for
4472          virtual function calls.  */
4473       if (parser->scope)
4474         *idk = CP_ID_KIND_QUALIFIED;
4475
4476       /* If the name is a template-id that names a type, we will get a
4477          TYPE_DECL here.  That is invalid code.  */
4478       if (TREE_CODE (name) == TYPE_DECL)
4479         {
4480           error ("invalid use of %qD", name);
4481           postfix_expression = error_mark_node;
4482         }
4483       else
4484         {
4485           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4486             {
4487               name = build_qualified_name (/*type=*/NULL_TREE,
4488                                            parser->scope,
4489                                            name,
4490                                            template_p);
4491               parser->scope = NULL_TREE;
4492               parser->qualifying_scope = NULL_TREE;
4493               parser->object_scope = NULL_TREE;
4494             }
4495           if (scope && name && BASELINK_P (name))
4496             adjust_result_of_qualified_name_lookup
4497               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4498           postfix_expression
4499             = finish_class_member_access_expr (postfix_expression, name,
4500                                                template_p);
4501         }
4502     }
4503
4504   /* We no longer need to look up names in the scope of the object on
4505      the left-hand side of the `.' or `->' operator.  */
4506   parser->context->object_type = NULL_TREE;
4507
4508   /* Outside of offsetof, these operators may not appear in
4509      constant-expressions.  */
4510   if (!for_offsetof
4511       && (cp_parser_non_integral_constant_expression
4512           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4513     postfix_expression = error_mark_node;
4514
4515   return postfix_expression;
4516 }
4517
4518 /* Parse a parenthesized expression-list.
4519
4520    expression-list:
4521      assignment-expression
4522      expression-list, assignment-expression
4523
4524    attribute-list:
4525      expression-list
4526      identifier
4527      identifier, expression-list
4528
4529    CAST_P is true if this expression is the target of a cast.
4530
4531    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4532    representation of an assignment-expression.  Note that a TREE_LIST
4533    is returned even if there is only a single expression in the list.
4534    error_mark_node is returned if the ( and or ) are
4535    missing. NULL_TREE is returned on no expressions. The parentheses
4536    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4537    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4538    indicates whether or not all of the expressions in the list were
4539    constant.  */
4540
4541 static tree
4542 cp_parser_parenthesized_expression_list (cp_parser* parser,
4543                                          bool is_attribute_list,
4544                                          bool cast_p,
4545                                          bool *non_constant_p)
4546 {
4547   tree expression_list = NULL_TREE;
4548   bool fold_expr_p = is_attribute_list;
4549   tree identifier = NULL_TREE;
4550
4551   /* Assume all the expressions will be constant.  */
4552   if (non_constant_p)
4553     *non_constant_p = false;
4554
4555   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4556     return error_mark_node;
4557
4558   /* Consume expressions until there are no more.  */
4559   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4560     while (true)
4561       {
4562         tree expr;
4563
4564         /* At the beginning of attribute lists, check to see if the
4565            next token is an identifier.  */
4566         if (is_attribute_list
4567             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4568           {
4569             cp_token *token;
4570
4571             /* Consume the identifier.  */
4572             token = cp_lexer_consume_token (parser->lexer);
4573             /* Save the identifier.  */
4574             identifier = token->value;
4575           }
4576         else
4577           {
4578             /* Parse the next assignment-expression.  */
4579             if (non_constant_p)
4580               {
4581                 bool expr_non_constant_p;
4582                 expr = (cp_parser_constant_expression
4583                         (parser, /*allow_non_constant_p=*/true,
4584                          &expr_non_constant_p));
4585                 if (expr_non_constant_p)
4586                   *non_constant_p = true;
4587               }
4588             else
4589               expr = cp_parser_assignment_expression (parser, cast_p);
4590
4591             if (fold_expr_p)
4592               expr = fold_non_dependent_expr (expr);
4593
4594              /* Add it to the list.  We add error_mark_node
4595                 expressions to the list, so that we can still tell if
4596                 the correct form for a parenthesized expression-list
4597                 is found. That gives better errors.  */
4598             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4599
4600             if (expr == error_mark_node)
4601               goto skip_comma;
4602           }
4603
4604         /* After the first item, attribute lists look the same as
4605            expression lists.  */
4606         is_attribute_list = false;
4607
4608       get_comma:;
4609         /* If the next token isn't a `,', then we are done.  */
4610         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4611           break;
4612
4613         /* Otherwise, consume the `,' and keep going.  */
4614         cp_lexer_consume_token (parser->lexer);
4615       }
4616
4617   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4618     {
4619       int ending;
4620
4621     skip_comma:;
4622       /* We try and resync to an unnested comma, as that will give the
4623          user better diagnostics.  */
4624       ending = cp_parser_skip_to_closing_parenthesis (parser,
4625                                                       /*recovering=*/true,
4626                                                       /*or_comma=*/true,
4627                                                       /*consume_paren=*/true);
4628       if (ending < 0)
4629         goto get_comma;
4630       if (!ending)
4631         return error_mark_node;
4632     }
4633
4634   /* We built up the list in reverse order so we must reverse it now.  */
4635   expression_list = nreverse (expression_list);
4636   if (identifier)
4637     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4638
4639   return expression_list;
4640 }
4641
4642 /* Parse a pseudo-destructor-name.
4643
4644    pseudo-destructor-name:
4645      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4646      :: [opt] nested-name-specifier template template-id :: ~ type-name
4647      :: [opt] nested-name-specifier [opt] ~ type-name
4648
4649    If either of the first two productions is used, sets *SCOPE to the
4650    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4651    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4652    or ERROR_MARK_NODE if the parse fails.  */
4653
4654 static void
4655 cp_parser_pseudo_destructor_name (cp_parser* parser,
4656                                   tree* scope,
4657                                   tree* type)
4658 {
4659   bool nested_name_specifier_p;
4660
4661   /* Assume that things will not work out.  */
4662   *type = error_mark_node;
4663
4664   /* Look for the optional `::' operator.  */
4665   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4666   /* Look for the optional nested-name-specifier.  */
4667   nested_name_specifier_p
4668     = (cp_parser_nested_name_specifier_opt (parser,
4669                                             /*typename_keyword_p=*/false,
4670                                             /*check_dependency_p=*/true,
4671                                             /*type_p=*/false,
4672                                             /*is_declaration=*/true)
4673        != NULL_TREE);
4674   /* Now, if we saw a nested-name-specifier, we might be doing the
4675      second production.  */
4676   if (nested_name_specifier_p
4677       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4678     {
4679       /* Consume the `template' keyword.  */
4680       cp_lexer_consume_token (parser->lexer);
4681       /* Parse the template-id.  */
4682       cp_parser_template_id (parser,
4683                              /*template_keyword_p=*/true,
4684                              /*check_dependency_p=*/false,
4685                              /*is_declaration=*/true);
4686       /* Look for the `::' token.  */
4687       cp_parser_require (parser, CPP_SCOPE, "`::'");
4688     }
4689   /* If the next token is not a `~', then there might be some
4690      additional qualification.  */
4691   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4692     {
4693       /* Look for the type-name.  */
4694       *scope = TREE_TYPE (cp_parser_type_name (parser));
4695
4696       if (*scope == error_mark_node)
4697         return;
4698
4699       /* If we don't have ::~, then something has gone wrong.  Since
4700          the only caller of this function is looking for something
4701          after `.' or `->' after a scalar type, most likely the
4702          program is trying to get a member of a non-aggregate
4703          type.  */
4704       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4705           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4706         {
4707           cp_parser_error (parser, "request for member of non-aggregate type");
4708           return;
4709         }
4710
4711       /* Look for the `::' token.  */
4712       cp_parser_require (parser, CPP_SCOPE, "`::'");
4713     }
4714   else
4715     *scope = NULL_TREE;
4716
4717   /* Look for the `~'.  */
4718   cp_parser_require (parser, CPP_COMPL, "`~'");
4719   /* Look for the type-name again.  We are not responsible for
4720      checking that it matches the first type-name.  */
4721   *type = cp_parser_type_name (parser);
4722 }
4723
4724 /* Parse a unary-expression.
4725
4726    unary-expression:
4727      postfix-expression
4728      ++ cast-expression
4729      -- cast-expression
4730      unary-operator cast-expression
4731      sizeof unary-expression
4732      sizeof ( type-id )
4733      new-expression
4734      delete-expression
4735
4736    GNU Extensions:
4737
4738    unary-expression:
4739      __extension__ cast-expression
4740      __alignof__ unary-expression
4741      __alignof__ ( type-id )
4742      __real__ cast-expression
4743      __imag__ cast-expression
4744      && identifier
4745
4746    ADDRESS_P is true iff the unary-expression is appearing as the
4747    operand of the `&' operator.   CAST_P is true if this expression is
4748    the target of a cast.
4749
4750    Returns a representation of the expression.  */
4751
4752 static tree
4753 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4754 {
4755   cp_token *token;
4756   enum tree_code unary_operator;
4757
4758   /* Peek at the next token.  */
4759   token = cp_lexer_peek_token (parser->lexer);
4760   /* Some keywords give away the kind of expression.  */
4761   if (token->type == CPP_KEYWORD)
4762     {
4763       enum rid keyword = token->keyword;
4764
4765       switch (keyword)
4766         {
4767         case RID_ALIGNOF:
4768         case RID_SIZEOF:
4769           {
4770             tree operand;
4771             enum tree_code op;
4772
4773             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4774             /* Consume the token.  */
4775             cp_lexer_consume_token (parser->lexer);
4776             /* Parse the operand.  */
4777             operand = cp_parser_sizeof_operand (parser, keyword);
4778
4779             if (TYPE_P (operand))
4780               return cxx_sizeof_or_alignof_type (operand, op, true);
4781             else
4782               return cxx_sizeof_or_alignof_expr (operand, op);
4783           }
4784
4785         case RID_NEW:
4786           return cp_parser_new_expression (parser);
4787
4788         case RID_DELETE:
4789           return cp_parser_delete_expression (parser);
4790
4791         case RID_EXTENSION:
4792           {
4793             /* The saved value of the PEDANTIC flag.  */
4794             int saved_pedantic;
4795             tree expr;
4796
4797             /* Save away the PEDANTIC flag.  */
4798             cp_parser_extension_opt (parser, &saved_pedantic);
4799             /* Parse the cast-expression.  */
4800             expr = cp_parser_simple_cast_expression (parser);
4801             /* Restore the PEDANTIC flag.  */
4802             pedantic = saved_pedantic;
4803
4804             return expr;
4805           }
4806
4807         case RID_REALPART:
4808         case RID_IMAGPART:
4809           {
4810             tree expression;
4811
4812             /* Consume the `__real__' or `__imag__' token.  */
4813             cp_lexer_consume_token (parser->lexer);
4814             /* Parse the cast-expression.  */
4815             expression = cp_parser_simple_cast_expression (parser);
4816             /* Create the complete representation.  */
4817             return build_x_unary_op ((keyword == RID_REALPART
4818                                       ? REALPART_EXPR : IMAGPART_EXPR),
4819                                      expression);
4820           }
4821           break;
4822
4823         default:
4824           break;
4825         }
4826     }
4827
4828   /* Look for the `:: new' and `:: delete', which also signal the
4829      beginning of a new-expression, or delete-expression,
4830      respectively.  If the next token is `::', then it might be one of
4831      these.  */
4832   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4833     {
4834       enum rid keyword;
4835
4836       /* See if the token after the `::' is one of the keywords in
4837          which we're interested.  */
4838       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4839       /* If it's `new', we have a new-expression.  */
4840       if (keyword == RID_NEW)
4841         return cp_parser_new_expression (parser);
4842       /* Similarly, for `delete'.  */
4843       else if (keyword == RID_DELETE)
4844         return cp_parser_delete_expression (parser);
4845     }
4846
4847   /* Look for a unary operator.  */
4848   unary_operator = cp_parser_unary_operator (token);
4849   /* The `++' and `--' operators can be handled similarly, even though
4850      they are not technically unary-operators in the grammar.  */
4851   if (unary_operator == ERROR_MARK)
4852     {
4853       if (token->type == CPP_PLUS_PLUS)
4854         unary_operator = PREINCREMENT_EXPR;
4855       else if (token->type == CPP_MINUS_MINUS)
4856         unary_operator = PREDECREMENT_EXPR;
4857       /* Handle the GNU address-of-label extension.  */
4858       else if (cp_parser_allow_gnu_extensions_p (parser)
4859                && token->type == CPP_AND_AND)
4860         {
4861           tree identifier;
4862
4863           /* Consume the '&&' token.  */
4864           cp_lexer_consume_token (parser->lexer);
4865           /* Look for the identifier.  */
4866           identifier = cp_parser_identifier (parser);
4867           /* Create an expression representing the address.  */
4868           return finish_label_address_expr (identifier);
4869         }
4870     }
4871   if (unary_operator != ERROR_MARK)
4872     {
4873       tree cast_expression;
4874       tree expression = error_mark_node;
4875       const char *non_constant_p = NULL;
4876
4877       /* Consume the operator token.  */
4878       token = cp_lexer_consume_token (parser->lexer);
4879       /* Parse the cast-expression.  */
4880       cast_expression
4881         = cp_parser_cast_expression (parser,
4882                                      unary_operator == ADDR_EXPR,
4883                                      /*cast_p=*/false);
4884       /* Now, build an appropriate representation.  */
4885       switch (unary_operator)
4886         {
4887         case INDIRECT_REF:
4888           non_constant_p = "`*'";
4889           expression = build_x_indirect_ref (cast_expression, "unary *");
4890           break;
4891
4892         case ADDR_EXPR:
4893           non_constant_p = "`&'";
4894           /* Fall through.  */
4895         case BIT_NOT_EXPR:
4896           expression = build_x_unary_op (unary_operator, cast_expression);
4897           break;
4898
4899         case PREINCREMENT_EXPR:
4900         case PREDECREMENT_EXPR:
4901           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4902                             ? "`++'" : "`--'");
4903           /* Fall through.  */
4904         case UNARY_PLUS_EXPR:
4905         case NEGATE_EXPR:
4906         case TRUTH_NOT_EXPR:
4907           expression = finish_unary_op_expr (unary_operator, cast_expression);
4908           break;
4909
4910         default:
4911           gcc_unreachable ();
4912         }
4913
4914       if (non_constant_p
4915           && cp_parser_non_integral_constant_expression (parser,
4916                                                          non_constant_p))
4917         expression = error_mark_node;
4918
4919       return expression;
4920     }
4921
4922   return cp_parser_postfix_expression (parser, address_p, cast_p);
4923 }
4924
4925 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4926    unary-operator, the corresponding tree code is returned.  */
4927
4928 static enum tree_code
4929 cp_parser_unary_operator (cp_token* token)
4930 {
4931   switch (token->type)
4932     {
4933     case CPP_MULT:
4934       return INDIRECT_REF;
4935
4936     case CPP_AND:
4937       return ADDR_EXPR;
4938
4939     case CPP_PLUS:
4940       return UNARY_PLUS_EXPR;
4941
4942     case CPP_MINUS:
4943       return NEGATE_EXPR;
4944
4945     case CPP_NOT:
4946       return TRUTH_NOT_EXPR;
4947
4948     case CPP_COMPL:
4949       return BIT_NOT_EXPR;
4950
4951     default:
4952       return ERROR_MARK;
4953     }
4954 }
4955
4956 /* Parse a new-expression.
4957
4958    new-expression:
4959      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4960      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4961
4962    Returns a representation of the expression.  */
4963
4964 static tree
4965 cp_parser_new_expression (cp_parser* parser)
4966 {
4967   bool global_scope_p;
4968   tree placement;
4969   tree type;
4970   tree initializer;
4971   tree nelts;
4972
4973   /* Look for the optional `::' operator.  */
4974   global_scope_p
4975     = (cp_parser_global_scope_opt (parser,
4976                                    /*current_scope_valid_p=*/false)
4977        != NULL_TREE);
4978   /* Look for the `new' operator.  */
4979   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4980   /* There's no easy way to tell a new-placement from the
4981      `( type-id )' construct.  */
4982   cp_parser_parse_tentatively (parser);
4983   /* Look for a new-placement.  */
4984   placement = cp_parser_new_placement (parser);
4985   /* If that didn't work out, there's no new-placement.  */
4986   if (!cp_parser_parse_definitely (parser))
4987     placement = NULL_TREE;
4988
4989   /* If the next token is a `(', then we have a parenthesized
4990      type-id.  */
4991   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4992     {
4993       /* Consume the `('.  */
4994       cp_lexer_consume_token (parser->lexer);
4995       /* Parse the type-id.  */
4996       type = cp_parser_type_id (parser);
4997       /* Look for the closing `)'.  */
4998       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4999       /* There should not be a direct-new-declarator in this production,
5000          but GCC used to allowed this, so we check and emit a sensible error
5001          message for this case.  */
5002       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5003         {
5004           error ("array bound forbidden after parenthesized type-id");
5005           inform ("try removing the parentheses around the type-id");
5006           cp_parser_direct_new_declarator (parser);
5007         }
5008       nelts = NULL_TREE;
5009     }
5010   /* Otherwise, there must be a new-type-id.  */
5011   else
5012     type = cp_parser_new_type_id (parser, &nelts);
5013
5014   /* If the next token is a `(', then we have a new-initializer.  */
5015   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5016     initializer = cp_parser_new_initializer (parser);
5017   else
5018     initializer = NULL_TREE;
5019
5020   /* A new-expression may not appear in an integral constant
5021      expression.  */
5022   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5023     return error_mark_node;
5024
5025   /* Create a representation of the new-expression.  */
5026   return build_new (placement, type, nelts, initializer, global_scope_p);
5027 }
5028
5029 /* Parse a new-placement.
5030
5031    new-placement:
5032      ( expression-list )
5033
5034    Returns the same representation as for an expression-list.  */
5035
5036 static tree
5037 cp_parser_new_placement (cp_parser* parser)
5038 {
5039   tree expression_list;
5040
5041   /* Parse the expression-list.  */
5042   expression_list = (cp_parser_parenthesized_expression_list
5043                      (parser, false, /*cast_p=*/false,
5044                       /*non_constant_p=*/NULL));
5045
5046   return expression_list;
5047 }
5048
5049 /* Parse a new-type-id.
5050
5051    new-type-id:
5052      type-specifier-seq new-declarator [opt]
5053
5054    Returns the TYPE allocated.  If the new-type-id indicates an array
5055    type, *NELTS is set to the number of elements in the last array
5056    bound; the TYPE will not include the last array bound.  */
5057
5058 static tree
5059 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5060 {
5061   cp_decl_specifier_seq type_specifier_seq;
5062   cp_declarator *new_declarator;
5063   cp_declarator *declarator;
5064   cp_declarator *outer_declarator;
5065   const char *saved_message;
5066   tree type;
5067
5068   /* The type-specifier sequence must not contain type definitions.
5069      (It cannot contain declarations of new types either, but if they
5070      are not definitions we will catch that because they are not
5071      complete.)  */
5072   saved_message = parser->type_definition_forbidden_message;
5073   parser->type_definition_forbidden_message
5074     = "types may not be defined in a new-type-id";
5075   /* Parse the type-specifier-seq.  */
5076   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5077                                 &type_specifier_seq);
5078   /* Restore the old message.  */
5079   parser->type_definition_forbidden_message = saved_message;
5080   /* Parse the new-declarator.  */
5081   new_declarator = cp_parser_new_declarator_opt (parser);
5082
5083   /* Determine the number of elements in the last array dimension, if
5084      any.  */
5085   *nelts = NULL_TREE;
5086   /* Skip down to the last array dimension.  */
5087   declarator = new_declarator;
5088   outer_declarator = NULL;
5089   while (declarator && (declarator->kind == cdk_pointer
5090                         || declarator->kind == cdk_ptrmem))
5091     {
5092       outer_declarator = declarator;
5093       declarator = declarator->declarator;
5094     }
5095   while (declarator
5096          && declarator->kind == cdk_array
5097          && declarator->declarator
5098          && declarator->declarator->kind == cdk_array)
5099     {
5100       outer_declarator = declarator;
5101       declarator = declarator->declarator;
5102     }
5103
5104   if (declarator && declarator->kind == cdk_array)
5105     {
5106       *nelts = declarator->u.array.bounds;
5107       if (*nelts == error_mark_node)
5108         *nelts = integer_one_node;
5109
5110       if (outer_declarator)
5111         outer_declarator->declarator = declarator->declarator;
5112       else
5113         new_declarator = NULL;
5114     }
5115
5116   type = groktypename (&type_specifier_seq, new_declarator);
5117   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5118     {
5119       *nelts = array_type_nelts_top (type);
5120       type = TREE_TYPE (type);
5121     }
5122   return type;
5123 }
5124
5125 /* Parse an (optional) new-declarator.
5126
5127    new-declarator:
5128      ptr-operator new-declarator [opt]
5129      direct-new-declarator
5130
5131    Returns the declarator.  */
5132
5133 static cp_declarator *
5134 cp_parser_new_declarator_opt (cp_parser* parser)
5135 {
5136   enum tree_code code;
5137   tree type;
5138   cp_cv_quals cv_quals;
5139
5140   /* We don't know if there's a ptr-operator next, or not.  */
5141   cp_parser_parse_tentatively (parser);
5142   /* Look for a ptr-operator.  */
5143   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5144   /* If that worked, look for more new-declarators.  */
5145   if (cp_parser_parse_definitely (parser))
5146     {
5147       cp_declarator *declarator;
5148
5149       /* Parse another optional declarator.  */
5150       declarator = cp_parser_new_declarator_opt (parser);
5151
5152       /* Create the representation of the declarator.  */
5153       if (type)
5154         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5155       else if (code == INDIRECT_REF)
5156         declarator = make_pointer_declarator (cv_quals, declarator);
5157       else
5158         declarator = make_reference_declarator (cv_quals, declarator);
5159
5160       return declarator;
5161     }
5162
5163   /* If the next token is a `[', there is a direct-new-declarator.  */
5164   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5165     return cp_parser_direct_new_declarator (parser);
5166
5167   return NULL;
5168 }
5169
5170 /* Parse a direct-new-declarator.
5171
5172    direct-new-declarator:
5173      [ expression ]
5174      direct-new-declarator [constant-expression]
5175
5176    */
5177
5178 static cp_declarator *
5179 cp_parser_direct_new_declarator (cp_parser* parser)
5180 {
5181   cp_declarator *declarator = NULL;
5182
5183   while (true)
5184     {
5185       tree expression;
5186
5187       /* Look for the opening `['.  */
5188       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5189       /* The first expression is not required to be constant.  */
5190       if (!declarator)
5191         {
5192           expression = cp_parser_expression (parser, /*cast_p=*/false);
5193           /* The standard requires that the expression have integral
5194              type.  DR 74 adds enumeration types.  We believe that the
5195              real intent is that these expressions be handled like the
5196              expression in a `switch' condition, which also allows
5197              classes with a single conversion to integral or
5198              enumeration type.  */
5199           if (!processing_template_decl)
5200             {
5201               expression
5202                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5203                                               expression,
5204                                               /*complain=*/true);
5205               if (!expression)
5206                 {
5207                   error ("expression in new-declarator must have integral "
5208                          "or enumeration type");
5209                   expression = error_mark_node;
5210                 }
5211             }
5212         }
5213       /* But all the other expressions must be.  */
5214       else
5215         expression
5216           = cp_parser_constant_expression (parser,
5217                                            /*allow_non_constant=*/false,
5218                                            NULL);
5219       /* Look for the closing `]'.  */
5220       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5221
5222       /* Add this bound to the declarator.  */
5223       declarator = make_array_declarator (declarator, expression);
5224
5225       /* If the next token is not a `[', then there are no more
5226          bounds.  */
5227       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5228         break;
5229     }
5230
5231   return declarator;
5232 }
5233
5234 /* Parse a new-initializer.
5235
5236    new-initializer:
5237      ( expression-list [opt] )
5238
5239    Returns a representation of the expression-list.  If there is no
5240    expression-list, VOID_ZERO_NODE is returned.  */
5241
5242 static tree
5243 cp_parser_new_initializer (cp_parser* parser)
5244 {
5245   tree expression_list;
5246
5247   expression_list = (cp_parser_parenthesized_expression_list
5248                      (parser, false, /*cast_p=*/false,
5249                       /*non_constant_p=*/NULL));
5250   if (!expression_list)
5251     expression_list = void_zero_node;
5252
5253   return expression_list;
5254 }
5255
5256 /* Parse a delete-expression.
5257
5258    delete-expression:
5259      :: [opt] delete cast-expression
5260      :: [opt] delete [ ] cast-expression
5261
5262    Returns a representation of the expression.  */
5263
5264 static tree
5265 cp_parser_delete_expression (cp_parser* parser)
5266 {
5267   bool global_scope_p;
5268   bool array_p;
5269   tree expression;
5270
5271   /* Look for the optional `::' operator.  */
5272   global_scope_p
5273     = (cp_parser_global_scope_opt (parser,
5274                                    /*current_scope_valid_p=*/false)
5275        != NULL_TREE);
5276   /* Look for the `delete' keyword.  */
5277   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5278   /* See if the array syntax is in use.  */
5279   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5280     {
5281       /* Consume the `[' token.  */
5282       cp_lexer_consume_token (parser->lexer);
5283       /* Look for the `]' token.  */
5284       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5285       /* Remember that this is the `[]' construct.  */
5286       array_p = true;
5287     }
5288   else
5289     array_p = false;
5290
5291   /* Parse the cast-expression.  */
5292   expression = cp_parser_simple_cast_expression (parser);
5293
5294   /* A delete-expression may not appear in an integral constant
5295      expression.  */
5296   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5297     return error_mark_node;
5298
5299   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5300 }
5301
5302 /* Parse a cast-expression.
5303
5304    cast-expression:
5305      unary-expression
5306      ( type-id ) cast-expression
5307
5308    ADDRESS_P is true iff the unary-expression is appearing as the
5309    operand of the `&' operator.   CAST_P is true if this expression is
5310    the target of a cast.
5311
5312    Returns a representation of the expression.  */
5313
5314 static tree
5315 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5316 {
5317   /* If it's a `(', then we might be looking at a cast.  */
5318   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5319     {
5320       tree type = NULL_TREE;
5321       tree expr = NULL_TREE;
5322       bool compound_literal_p;
5323       const char *saved_message;
5324
5325       /* There's no way to know yet whether or not this is a cast.
5326          For example, `(int (3))' is a unary-expression, while `(int)
5327          3' is a cast.  So, we resort to parsing tentatively.  */
5328       cp_parser_parse_tentatively (parser);
5329       /* Types may not be defined in a cast.  */
5330       saved_message = parser->type_definition_forbidden_message;
5331       parser->type_definition_forbidden_message
5332         = "types may not be defined in casts";
5333       /* Consume the `('.  */
5334       cp_lexer_consume_token (parser->lexer);
5335       /* A very tricky bit is that `(struct S) { 3 }' is a
5336          compound-literal (which we permit in C++ as an extension).
5337          But, that construct is not a cast-expression -- it is a
5338          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5339          is legal; if the compound-literal were a cast-expression,
5340          you'd need an extra set of parentheses.)  But, if we parse
5341          the type-id, and it happens to be a class-specifier, then we
5342          will commit to the parse at that point, because we cannot
5343          undo the action that is done when creating a new class.  So,
5344          then we cannot back up and do a postfix-expression.
5345
5346          Therefore, we scan ahead to the closing `)', and check to see
5347          if the token after the `)' is a `{'.  If so, we are not
5348          looking at a cast-expression.
5349
5350          Save tokens so that we can put them back.  */
5351       cp_lexer_save_tokens (parser->lexer);
5352       /* Skip tokens until the next token is a closing parenthesis.
5353          If we find the closing `)', and the next token is a `{', then
5354          we are looking at a compound-literal.  */
5355       compound_literal_p
5356         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5357                                                   /*consume_paren=*/true)
5358            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5359       /* Roll back the tokens we skipped.  */
5360       cp_lexer_rollback_tokens (parser->lexer);
5361       /* If we were looking at a compound-literal, simulate an error
5362          so that the call to cp_parser_parse_definitely below will
5363          fail.  */
5364       if (compound_literal_p)
5365         cp_parser_simulate_error (parser);
5366       else
5367         {
5368           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5369           parser->in_type_id_in_expr_p = true;
5370           /* Look for the type-id.  */
5371           type = cp_parser_type_id (parser);
5372           /* Look for the closing `)'.  */
5373           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5374           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5375         }
5376
5377       /* Restore the saved message.  */
5378       parser->type_definition_forbidden_message = saved_message;
5379
5380       /* If ok so far, parse the dependent expression. We cannot be
5381          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5382          ctor of T, but looks like a cast to function returning T
5383          without a dependent expression.  */
5384       if (!cp_parser_error_occurred (parser))
5385         expr = cp_parser_cast_expression (parser,
5386                                           /*address_p=*/false,
5387                                           /*cast_p=*/true);
5388
5389       if (cp_parser_parse_definitely (parser))
5390         {
5391           /* Warn about old-style casts, if so requested.  */
5392           if (warn_old_style_cast
5393               && !in_system_header
5394               && !VOID_TYPE_P (type)
5395               && current_lang_name != lang_name_c)
5396             warning (0, "use of old-style cast");
5397
5398           /* Only type conversions to integral or enumeration types
5399              can be used in constant-expressions.  */
5400           if (parser->integral_constant_expression_p
5401               && !dependent_type_p (type)
5402               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5403               && (cp_parser_non_integral_constant_expression
5404                   (parser,
5405                    "a cast to a type other than an integral or "
5406                    "enumeration type")))
5407             return error_mark_node;
5408
5409           /* Perform the cast.  */
5410           expr = build_c_cast (type, expr);
5411           return expr;
5412         }
5413     }
5414
5415   /* If we get here, then it's not a cast, so it must be a
5416      unary-expression.  */
5417   return cp_parser_unary_expression (parser, address_p, cast_p);
5418 }
5419
5420 /* Parse a binary expression of the general form:
5421
5422    pm-expression:
5423      cast-expression
5424      pm-expression .* cast-expression
5425      pm-expression ->* cast-expression
5426
5427    multiplicative-expression:
5428      pm-expression
5429      multiplicative-expression * pm-expression
5430      multiplicative-expression / pm-expression
5431      multiplicative-expression % pm-expression
5432
5433    additive-expression:
5434      multiplicative-expression
5435      additive-expression + multiplicative-expression
5436      additive-expression - multiplicative-expression
5437
5438    shift-expression:
5439      additive-expression
5440      shift-expression << additive-expression
5441      shift-expression >> additive-expression
5442
5443    relational-expression:
5444      shift-expression
5445      relational-expression < shift-expression
5446      relational-expression > shift-expression
5447      relational-expression <= shift-expression
5448      relational-expression >= shift-expression
5449
5450   GNU Extension:
5451
5452    relational-expression:
5453      relational-expression <? shift-expression
5454      relational-expression >? shift-expression
5455
5456    equality-expression:
5457      relational-expression
5458      equality-expression == relational-expression
5459      equality-expression != relational-expression
5460
5461    and-expression:
5462      equality-expression
5463      and-expression & equality-expression
5464
5465    exclusive-or-expression:
5466      and-expression
5467      exclusive-or-expression ^ and-expression
5468
5469    inclusive-or-expression:
5470      exclusive-or-expression
5471      inclusive-or-expression | exclusive-or-expression
5472
5473    logical-and-expression:
5474      inclusive-or-expression
5475      logical-and-expression && inclusive-or-expression
5476
5477    logical-or-expression:
5478      logical-and-expression
5479      logical-or-expression || logical-and-expression
5480
5481    All these are implemented with a single function like:
5482
5483    binary-expression:
5484      simple-cast-expression
5485      binary-expression <token> binary-expression
5486
5487    CAST_P is true if this expression is the target of a cast.
5488
5489    The binops_by_token map is used to get the tree codes for each <token> type.
5490    binary-expressions are associated according to a precedence table.  */
5491
5492 #define TOKEN_PRECEDENCE(token) \
5493   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5494    ? PREC_NOT_OPERATOR \
5495    : binops_by_token[token->type].prec)
5496
5497 static tree
5498 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5499 {
5500   cp_parser_expression_stack stack;
5501   cp_parser_expression_stack_entry *sp = &stack[0];
5502   tree lhs, rhs;
5503   cp_token *token;
5504   enum tree_code tree_type;
5505   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5506   bool overloaded_p;
5507
5508   /* Parse the first expression.  */
5509   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5510
5511   for (;;)
5512     {
5513       /* Get an operator token.  */
5514       token = cp_lexer_peek_token (parser->lexer);
5515       if (token->type == CPP_MIN || token->type == CPP_MAX)
5516         cp_parser_warn_min_max ();
5517
5518       new_prec = TOKEN_PRECEDENCE (token);
5519
5520       /* Popping an entry off the stack means we completed a subexpression:
5521          - either we found a token which is not an operator (`>' where it is not
5522            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5523            will happen repeatedly;
5524          - or, we found an operator which has lower priority.  This is the case
5525            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5526            parsing `3 * 4'.  */
5527       if (new_prec <= prec)
5528         {
5529           if (sp == stack)
5530             break;
5531           else
5532             goto pop;
5533         }
5534
5535      get_rhs:
5536       tree_type = binops_by_token[token->type].tree_type;
5537
5538       /* We used the operator token.  */
5539       cp_lexer_consume_token (parser->lexer);
5540
5541       /* Extract another operand.  It may be the RHS of this expression
5542          or the LHS of a new, higher priority expression.  */
5543       rhs = cp_parser_simple_cast_expression (parser);
5544
5545       /* Get another operator token.  Look up its precedence to avoid
5546          building a useless (immediately popped) stack entry for common
5547          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5548       token = cp_lexer_peek_token (parser->lexer);
5549       lookahead_prec = TOKEN_PRECEDENCE (token);
5550       if (lookahead_prec > new_prec)
5551         {
5552           /* ... and prepare to parse the RHS of the new, higher priority
5553              expression.  Since precedence levels on the stack are
5554              monotonically increasing, we do not have to care about
5555              stack overflows.  */
5556           sp->prec = prec;
5557           sp->tree_type = tree_type;
5558           sp->lhs = lhs;
5559           sp++;
5560           lhs = rhs;
5561           prec = new_prec;
5562           new_prec = lookahead_prec;
5563           goto get_rhs;
5564
5565          pop:
5566           /* If the stack is not empty, we have parsed into LHS the right side
5567              (`4' in the example above) of an expression we had suspended.
5568              We can use the information on the stack to recover the LHS (`3')
5569              from the stack together with the tree code (`MULT_EXPR'), and
5570              the precedence of the higher level subexpression
5571              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5572              which will be used to actually build the additive expression.  */
5573           --sp;
5574           prec = sp->prec;
5575           tree_type = sp->tree_type;
5576           rhs = lhs;
5577           lhs = sp->lhs;
5578         }
5579
5580       overloaded_p = false;
5581       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5582
5583       /* If the binary operator required the use of an overloaded operator,
5584          then this expression cannot be an integral constant-expression.
5585          An overloaded operator can be used even if both operands are
5586          otherwise permissible in an integral constant-expression if at
5587          least one of the operands is of enumeration type.  */
5588
5589       if (overloaded_p
5590           && (cp_parser_non_integral_constant_expression
5591               (parser, "calls to overloaded operators")))
5592         return error_mark_node;
5593     }
5594
5595   return lhs;
5596 }
5597
5598
5599 /* Parse the `? expression : assignment-expression' part of a
5600    conditional-expression.  The LOGICAL_OR_EXPR is the
5601    logical-or-expression that started the conditional-expression.
5602    Returns a representation of the entire conditional-expression.
5603
5604    This routine is used by cp_parser_assignment_expression.
5605
5606      ? expression : assignment-expression
5607
5608    GNU Extensions:
5609
5610      ? : assignment-expression */
5611
5612 static tree
5613 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5614 {
5615   tree expr;
5616   tree assignment_expr;
5617
5618   /* Consume the `?' token.  */
5619   cp_lexer_consume_token (parser->lexer);
5620   if (cp_parser_allow_gnu_extensions_p (parser)
5621       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5622     /* Implicit true clause.  */
5623     expr = NULL_TREE;
5624   else
5625     /* Parse the expression.  */
5626     expr = cp_parser_expression (parser, /*cast_p=*/false);
5627
5628   /* The next token should be a `:'.  */
5629   cp_parser_require (parser, CPP_COLON, "`:'");
5630   /* Parse the assignment-expression.  */
5631   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5632
5633   /* Build the conditional-expression.  */
5634   return build_x_conditional_expr (logical_or_expr,
5635                                    expr,
5636                                    assignment_expr);
5637 }
5638
5639 /* Parse an assignment-expression.
5640
5641    assignment-expression:
5642      conditional-expression
5643      logical-or-expression assignment-operator assignment_expression
5644      throw-expression
5645
5646    CAST_P is true if this expression is the target of a cast.
5647
5648    Returns a representation for the expression.  */
5649
5650 static tree
5651 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5652 {
5653   tree expr;
5654
5655   /* If the next token is the `throw' keyword, then we're looking at
5656      a throw-expression.  */
5657   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5658     expr = cp_parser_throw_expression (parser);
5659   /* Otherwise, it must be that we are looking at a
5660      logical-or-expression.  */
5661   else
5662     {
5663       /* Parse the binary expressions (logical-or-expression).  */
5664       expr = cp_parser_binary_expression (parser, cast_p);
5665       /* If the next token is a `?' then we're actually looking at a
5666          conditional-expression.  */
5667       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5668         return cp_parser_question_colon_clause (parser, expr);
5669       else
5670         {
5671           enum tree_code assignment_operator;
5672
5673           /* If it's an assignment-operator, we're using the second
5674              production.  */
5675           assignment_operator
5676             = cp_parser_assignment_operator_opt (parser);
5677           if (assignment_operator != ERROR_MARK)
5678             {
5679               tree rhs;
5680
5681               /* Parse the right-hand side of the assignment.  */
5682               rhs = cp_parser_assignment_expression (parser, cast_p);
5683               /* An assignment may not appear in a
5684                  constant-expression.  */
5685               if (cp_parser_non_integral_constant_expression (parser,
5686                                                               "an assignment"))
5687                 return error_mark_node;
5688               /* Build the assignment expression.  */
5689               expr = build_x_modify_expr (expr,
5690                                           assignment_operator,
5691                                           rhs);
5692             }
5693         }
5694     }
5695
5696   return expr;
5697 }
5698
5699 /* Parse an (optional) assignment-operator.
5700
5701    assignment-operator: one of
5702      = *= /= %= += -= >>= <<= &= ^= |=
5703
5704    GNU Extension:
5705
5706    assignment-operator: one of
5707      <?= >?=
5708
5709    If the next token is an assignment operator, the corresponding tree
5710    code is returned, and the token is consumed.  For example, for
5711    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5712    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5713    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5714    operator, ERROR_MARK is returned.  */
5715
5716 static enum tree_code
5717 cp_parser_assignment_operator_opt (cp_parser* parser)
5718 {
5719   enum tree_code op;
5720   cp_token *token;
5721
5722   /* Peek at the next toen.  */
5723   token = cp_lexer_peek_token (parser->lexer);
5724
5725   switch (token->type)
5726     {
5727     case CPP_EQ:
5728       op = NOP_EXPR;
5729       break;
5730
5731     case CPP_MULT_EQ:
5732       op = MULT_EXPR;
5733       break;
5734
5735     case CPP_DIV_EQ:
5736       op = TRUNC_DIV_EXPR;
5737       break;
5738
5739     case CPP_MOD_EQ:
5740       op = TRUNC_MOD_EXPR;
5741       break;
5742
5743     case CPP_PLUS_EQ:
5744       op = PLUS_EXPR;
5745       break;
5746
5747     case CPP_MINUS_EQ:
5748       op = MINUS_EXPR;
5749       break;
5750
5751     case CPP_RSHIFT_EQ:
5752       op = RSHIFT_EXPR;
5753       break;
5754
5755     case CPP_LSHIFT_EQ:
5756       op = LSHIFT_EXPR;
5757       break;
5758
5759     case CPP_AND_EQ:
5760       op = BIT_AND_EXPR;
5761       break;
5762
5763     case CPP_XOR_EQ:
5764       op = BIT_XOR_EXPR;
5765       break;
5766
5767     case CPP_OR_EQ:
5768       op = BIT_IOR_EXPR;
5769       break;
5770
5771     case CPP_MIN_EQ:
5772       op = MIN_EXPR;
5773       cp_parser_warn_min_max ();
5774       break;
5775
5776     case CPP_MAX_EQ:
5777       op = MAX_EXPR;
5778       cp_parser_warn_min_max ();
5779       break;
5780
5781     default:
5782       /* Nothing else is an assignment operator.  */
5783       op = ERROR_MARK;
5784     }
5785
5786   /* If it was an assignment operator, consume it.  */
5787   if (op != ERROR_MARK)
5788     cp_lexer_consume_token (parser->lexer);
5789
5790   return op;
5791 }
5792
5793 /* Parse an expression.
5794
5795    expression:
5796      assignment-expression
5797      expression , assignment-expression
5798
5799    CAST_P is true if this expression is the target of a cast.
5800
5801    Returns a representation of the expression.  */
5802
5803 static tree
5804 cp_parser_expression (cp_parser* parser, bool cast_p)
5805 {
5806   tree expression = NULL_TREE;
5807
5808   while (true)
5809     {
5810       tree assignment_expression;
5811
5812       /* Parse the next assignment-expression.  */
5813       assignment_expression
5814         = cp_parser_assignment_expression (parser, cast_p);
5815       /* If this is the first assignment-expression, we can just
5816          save it away.  */
5817       if (!expression)
5818         expression = assignment_expression;
5819       else
5820         expression = build_x_compound_expr (expression,
5821                                             assignment_expression);
5822       /* If the next token is not a comma, then we are done with the
5823          expression.  */
5824       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5825         break;
5826       /* Consume the `,'.  */
5827       cp_lexer_consume_token (parser->lexer);
5828       /* A comma operator cannot appear in a constant-expression.  */
5829       if (cp_parser_non_integral_constant_expression (parser,
5830                                                       "a comma operator"))
5831         expression = error_mark_node;
5832     }
5833
5834   return expression;
5835 }
5836
5837 /* Parse a constant-expression.
5838
5839    constant-expression:
5840      conditional-expression
5841
5842   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5843   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5844   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5845   is false, NON_CONSTANT_P should be NULL.  */
5846
5847 static tree
5848 cp_parser_constant_expression (cp_parser* parser,
5849                                bool allow_non_constant_p,
5850                                bool *non_constant_p)
5851 {
5852   bool saved_integral_constant_expression_p;
5853   bool saved_allow_non_integral_constant_expression_p;
5854   bool saved_non_integral_constant_expression_p;
5855   tree expression;
5856
5857   /* It might seem that we could simply parse the
5858      conditional-expression, and then check to see if it were
5859      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5860      one that the compiler can figure out is constant, possibly after
5861      doing some simplifications or optimizations.  The standard has a
5862      precise definition of constant-expression, and we must honor
5863      that, even though it is somewhat more restrictive.
5864
5865      For example:
5866
5867        int i[(2, 3)];
5868
5869      is not a legal declaration, because `(2, 3)' is not a
5870      constant-expression.  The `,' operator is forbidden in a
5871      constant-expression.  However, GCC's constant-folding machinery
5872      will fold this operation to an INTEGER_CST for `3'.  */
5873
5874   /* Save the old settings.  */
5875   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5876   saved_allow_non_integral_constant_expression_p
5877     = parser->allow_non_integral_constant_expression_p;
5878   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5879   /* We are now parsing a constant-expression.  */
5880   parser->integral_constant_expression_p = true;
5881   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5882   parser->non_integral_constant_expression_p = false;
5883   /* Although the grammar says "conditional-expression", we parse an
5884      "assignment-expression", which also permits "throw-expression"
5885      and the use of assignment operators.  In the case that
5886      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5887      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5888      actually essential that we look for an assignment-expression.
5889      For example, cp_parser_initializer_clauses uses this function to
5890      determine whether a particular assignment-expression is in fact
5891      constant.  */
5892   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5893   /* Restore the old settings.  */
5894   parser->integral_constant_expression_p
5895     = saved_integral_constant_expression_p;
5896   parser->allow_non_integral_constant_expression_p
5897     = saved_allow_non_integral_constant_expression_p;
5898   if (allow_non_constant_p)
5899     *non_constant_p = parser->non_integral_constant_expression_p;
5900   else if (parser->non_integral_constant_expression_p)
5901     expression = error_mark_node;
5902   parser->non_integral_constant_expression_p
5903     = saved_non_integral_constant_expression_p;
5904
5905   return expression;
5906 }
5907
5908 /* Parse __builtin_offsetof.
5909
5910    offsetof-expression:
5911      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5912
5913    offsetof-member-designator:
5914      id-expression
5915      | offsetof-member-designator "." id-expression
5916      | offsetof-member-designator "[" expression "]"
5917 */
5918
5919 static tree
5920 cp_parser_builtin_offsetof (cp_parser *parser)
5921 {
5922   int save_ice_p, save_non_ice_p;
5923   tree type, expr;
5924   cp_id_kind dummy;
5925
5926   /* We're about to accept non-integral-constant things, but will
5927      definitely yield an integral constant expression.  Save and
5928      restore these values around our local parsing.  */
5929   save_ice_p = parser->integral_constant_expression_p;
5930   save_non_ice_p = parser->non_integral_constant_expression_p;
5931
5932   /* Consume the "__builtin_offsetof" token.  */
5933   cp_lexer_consume_token (parser->lexer);
5934   /* Consume the opening `('.  */
5935   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5936   /* Parse the type-id.  */
5937   type = cp_parser_type_id (parser);
5938   /* Look for the `,'.  */
5939   cp_parser_require (parser, CPP_COMMA, "`,'");
5940
5941   /* Build the (type *)null that begins the traditional offsetof macro.  */
5942   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5943
5944   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5945   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5946                                                  true, &dummy);
5947   while (true)
5948     {
5949       cp_token *token = cp_lexer_peek_token (parser->lexer);
5950       switch (token->type)
5951         {
5952         case CPP_OPEN_SQUARE:
5953           /* offsetof-member-designator "[" expression "]" */
5954           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5955           break;
5956
5957         case CPP_DOT:
5958           /* offsetof-member-designator "." identifier */
5959           cp_lexer_consume_token (parser->lexer);
5960           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5961                                                          true, &dummy);
5962           break;
5963
5964         case CPP_CLOSE_PAREN:
5965           /* Consume the ")" token.  */
5966           cp_lexer_consume_token (parser->lexer);
5967           goto success;
5968
5969         default:
5970           /* Error.  We know the following require will fail, but
5971              that gives the proper error message.  */
5972           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5973           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5974           expr = error_mark_node;
5975           goto failure;
5976         }
5977     }
5978
5979  success:
5980   /* If we're processing a template, we can't finish the semantics yet.
5981      Otherwise we can fold the entire expression now.  */
5982   if (processing_template_decl)
5983     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5984   else
5985     expr = fold_offsetof (expr);
5986
5987  failure:
5988   parser->integral_constant_expression_p = save_ice_p;
5989   parser->non_integral_constant_expression_p = save_non_ice_p;
5990
5991   return expr;
5992 }
5993
5994 /* Statements [gram.stmt.stmt]  */
5995
5996 /* Parse a statement.
5997
5998    statement:
5999      labeled-statement
6000      expression-statement
6001      compound-statement
6002      selection-statement
6003      iteration-statement
6004      jump-statement
6005      declaration-statement
6006      try-block  */
6007
6008 static void
6009 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6010 {
6011   tree statement;
6012   cp_token *token;
6013   location_t statement_location;
6014
6015   /* There is no statement yet.  */
6016   statement = NULL_TREE;
6017   /* Peek at the next token.  */
6018   token = cp_lexer_peek_token (parser->lexer);
6019   /* Remember the location of the first token in the statement.  */
6020   statement_location = token->location;
6021   /* If this is a keyword, then that will often determine what kind of
6022      statement we have.  */
6023   if (token->type == CPP_KEYWORD)
6024     {
6025       enum rid keyword = token->keyword;
6026
6027       switch (keyword)
6028         {
6029         case RID_CASE:
6030         case RID_DEFAULT:
6031           statement = cp_parser_labeled_statement (parser,
6032                                                    in_statement_expr);
6033           break;
6034
6035         case RID_IF:
6036         case RID_SWITCH:
6037           statement = cp_parser_selection_statement (parser);
6038           break;
6039
6040         case RID_WHILE:
6041         case RID_DO:
6042         case RID_FOR:
6043           statement = cp_parser_iteration_statement (parser);
6044           break;
6045
6046         case RID_BREAK:
6047         case RID_CONTINUE:
6048         case RID_RETURN:
6049         case RID_GOTO:
6050           statement = cp_parser_jump_statement (parser);
6051           break;
6052
6053           /* Objective-C++ exception-handling constructs.  */
6054         case RID_AT_TRY:
6055         case RID_AT_CATCH:
6056         case RID_AT_FINALLY:
6057         case RID_AT_SYNCHRONIZED:
6058         case RID_AT_THROW:
6059           statement = cp_parser_objc_statement (parser);
6060           break;
6061
6062         case RID_TRY:
6063           statement = cp_parser_try_block (parser);
6064           break;
6065
6066         default:
6067           /* It might be a keyword like `int' that can start a
6068              declaration-statement.  */
6069           break;
6070         }
6071     }
6072   else if (token->type == CPP_NAME)
6073     {
6074       /* If the next token is a `:', then we are looking at a
6075          labeled-statement.  */
6076       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6077       if (token->type == CPP_COLON)
6078         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6079     }
6080   /* Anything that starts with a `{' must be a compound-statement.  */
6081   else if (token->type == CPP_OPEN_BRACE)
6082     statement = cp_parser_compound_statement (parser, NULL, false);
6083   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6084      a statement all its own.  */
6085   else if (token->type == CPP_PRAGMA)
6086     {
6087       cp_lexer_handle_pragma (parser->lexer);
6088       return;
6089     }
6090   else if (token->type == CPP_EOF)
6091     {
6092       cp_parser_error (parser, "expected statement");
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   bool saved_ice_p;
8907   bool saved_non_ice_p;
8908
8909   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8910   parser->in_template_argument_list_p = true;
8911   /* Even if the template-id appears in an integral
8912      constant-expression, the contents of the argument list do 
8913      not.  */ 
8914   saved_ice_p = parser->integral_constant_expression_p;
8915   parser->integral_constant_expression_p = false;
8916   saved_non_ice_p = parser->non_integral_constant_expression_p;
8917   parser->non_integral_constant_expression_p = false;
8918   /* Parse the arguments.  */
8919   do
8920     {
8921       tree argument;
8922
8923       if (n_args)
8924         /* Consume the comma.  */
8925         cp_lexer_consume_token (parser->lexer);
8926
8927       /* Parse the template-argument.  */
8928       argument = cp_parser_template_argument (parser);
8929       if (n_args == alloced)
8930         {
8931           alloced *= 2;
8932
8933           if (arg_ary == fixed_args)
8934             {
8935               arg_ary = xmalloc (sizeof (tree) * alloced);
8936               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8937             }
8938           else
8939             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8940         }
8941       arg_ary[n_args++] = argument;
8942     }
8943   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8944
8945   vec = make_tree_vec (n_args);
8946
8947   while (n_args--)
8948     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8949
8950   if (arg_ary != fixed_args)
8951     free (arg_ary);
8952   parser->non_integral_constant_expression_p = saved_non_ice_p;
8953   parser->integral_constant_expression_p = saved_ice_p;
8954   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8955   return vec;
8956 }
8957
8958 /* Parse a template-argument.
8959
8960    template-argument:
8961      assignment-expression
8962      type-id
8963      id-expression
8964
8965    The representation is that of an assignment-expression, type-id, or
8966    id-expression -- except that the qualified id-expression is
8967    evaluated, so that the value returned is either a DECL or an
8968    OVERLOAD.
8969
8970    Although the standard says "assignment-expression", it forbids
8971    throw-expressions or assignments in the template argument.
8972    Therefore, we use "conditional-expression" instead.  */
8973
8974 static tree
8975 cp_parser_template_argument (cp_parser* parser)
8976 {
8977   tree argument;
8978   bool template_p;
8979   bool address_p;
8980   bool maybe_type_id = false;
8981   cp_token *token;
8982   cp_id_kind idk;
8983
8984   /* There's really no way to know what we're looking at, so we just
8985      try each alternative in order.
8986
8987        [temp.arg]
8988
8989        In a template-argument, an ambiguity between a type-id and an
8990        expression is resolved to a type-id, regardless of the form of
8991        the corresponding template-parameter.
8992
8993      Therefore, we try a type-id first.  */
8994   cp_parser_parse_tentatively (parser);
8995   argument = cp_parser_type_id (parser);
8996   /* If there was no error parsing the type-id but the next token is a '>>',
8997      we probably found a typo for '> >'. But there are type-id which are
8998      also valid expressions. For instance:
8999
9000      struct X { int operator >> (int); };
9001      template <int V> struct Foo {};
9002      Foo<X () >> 5> r;
9003
9004      Here 'X()' is a valid type-id of a function type, but the user just
9005      wanted to write the expression "X() >> 5". Thus, we remember that we
9006      found a valid type-id, but we still try to parse the argument as an
9007      expression to see what happens.  */
9008   if (!cp_parser_error_occurred (parser)
9009       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9010     {
9011       maybe_type_id = true;
9012       cp_parser_abort_tentative_parse (parser);
9013     }
9014   else
9015     {
9016       /* If the next token isn't a `,' or a `>', then this argument wasn't
9017       really finished. This means that the argument is not a valid
9018       type-id.  */
9019       if (!cp_parser_next_token_ends_template_argument_p (parser))
9020         cp_parser_error (parser, "expected template-argument");
9021       /* If that worked, we're done.  */
9022       if (cp_parser_parse_definitely (parser))
9023         return argument;
9024     }
9025   /* We're still not sure what the argument will be.  */
9026   cp_parser_parse_tentatively (parser);
9027   /* Try a template.  */
9028   argument = cp_parser_id_expression (parser,
9029                                       /*template_keyword_p=*/false,
9030                                       /*check_dependency_p=*/true,
9031                                       &template_p,
9032                                       /*declarator_p=*/false);
9033   /* If the next token isn't a `,' or a `>', then this argument wasn't
9034      really finished.  */
9035   if (!cp_parser_next_token_ends_template_argument_p (parser))
9036     cp_parser_error (parser, "expected template-argument");
9037   if (!cp_parser_error_occurred (parser))
9038     {
9039       /* Figure out what is being referred to.  If the id-expression
9040          was for a class template specialization, then we will have a
9041          TYPE_DECL at this point.  There is no need to do name lookup
9042          at this point in that case.  */
9043       if (TREE_CODE (argument) != TYPE_DECL)
9044         argument = cp_parser_lookup_name (parser, argument,
9045                                           none_type,
9046                                           /*is_template=*/template_p,
9047                                           /*is_namespace=*/false,
9048                                           /*check_dependency=*/true,
9049                                           /*ambiguous_p=*/NULL);
9050       if (TREE_CODE (argument) != TEMPLATE_DECL
9051           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9052         cp_parser_error (parser, "expected template-name");
9053     }
9054   if (cp_parser_parse_definitely (parser))
9055     return argument;
9056   /* It must be a non-type argument.  There permitted cases are given
9057      in [temp.arg.nontype]:
9058
9059      -- an integral constant-expression of integral or enumeration
9060         type; or
9061
9062      -- the name of a non-type template-parameter; or
9063
9064      -- the name of an object or function with external linkage...
9065
9066      -- the address of an object or function with external linkage...
9067
9068      -- a pointer to member...  */
9069   /* Look for a non-type template parameter.  */
9070   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9071     {
9072       cp_parser_parse_tentatively (parser);
9073       argument = cp_parser_primary_expression (parser,
9074                                                /*adress_p=*/false,
9075                                                /*cast_p=*/false,
9076                                                /*template_arg_p=*/true,
9077                                                &idk);
9078       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9079           || !cp_parser_next_token_ends_template_argument_p (parser))
9080         cp_parser_simulate_error (parser);
9081       if (cp_parser_parse_definitely (parser))
9082         return argument;
9083     }
9084
9085   /* If the next token is "&", the argument must be the address of an
9086      object or function with external linkage.  */
9087   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9088   if (address_p)
9089     cp_lexer_consume_token (parser->lexer);
9090   /* See if we might have an id-expression.  */
9091   token = cp_lexer_peek_token (parser->lexer);
9092   if (token->type == CPP_NAME
9093       || token->keyword == RID_OPERATOR
9094       || token->type == CPP_SCOPE
9095       || token->type == CPP_TEMPLATE_ID
9096       || token->type == CPP_NESTED_NAME_SPECIFIER)
9097     {
9098       cp_parser_parse_tentatively (parser);
9099       argument = cp_parser_primary_expression (parser,
9100                                                address_p,
9101                                                /*cast_p=*/false,
9102                                                /*template_arg_p=*/true,
9103                                                &idk);
9104       if (cp_parser_error_occurred (parser)
9105           || !cp_parser_next_token_ends_template_argument_p (parser))
9106         cp_parser_abort_tentative_parse (parser);
9107       else
9108         {
9109           if (TREE_CODE (argument) == INDIRECT_REF)
9110             {
9111               gcc_assert (REFERENCE_REF_P (argument));
9112               argument = TREE_OPERAND (argument, 0);
9113             }
9114
9115           if (TREE_CODE (argument) == BASELINK)
9116             /* We don't need the information about what class was used
9117                to name the overloaded functions.  */  
9118             argument = BASELINK_FUNCTIONS (argument);
9119
9120           if (TREE_CODE (argument) == VAR_DECL)
9121             {
9122               /* A variable without external linkage might still be a
9123                  valid constant-expression, so no error is issued here
9124                  if the external-linkage check fails.  */
9125               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9126                 cp_parser_simulate_error (parser);
9127             }
9128           else if (is_overloaded_fn (argument))
9129             /* All overloaded functions are allowed; if the external
9130                linkage test does not pass, an error will be issued
9131                later.  */
9132             ;
9133           else if (address_p
9134                    && (TREE_CODE (argument) == OFFSET_REF
9135                        || TREE_CODE (argument) == SCOPE_REF))
9136             /* A pointer-to-member.  */
9137             ;
9138           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9139             ;
9140           else
9141             cp_parser_simulate_error (parser);
9142
9143           if (cp_parser_parse_definitely (parser))
9144             {
9145               if (address_p)
9146                 argument = build_x_unary_op (ADDR_EXPR, argument);
9147               return argument;
9148             }
9149         }
9150     }
9151   /* If the argument started with "&", there are no other valid
9152      alternatives at this point.  */
9153   if (address_p)
9154     {
9155       cp_parser_error (parser, "invalid non-type template argument");
9156       return error_mark_node;
9157     }
9158
9159   /* If the argument wasn't successfully parsed as a type-id followed
9160      by '>>', the argument can only be a constant expression now.
9161      Otherwise, we try parsing the constant-expression tentatively,
9162      because the argument could really be a type-id.  */
9163   if (maybe_type_id)
9164     cp_parser_parse_tentatively (parser);
9165   argument = cp_parser_constant_expression (parser,
9166                                             /*allow_non_constant_p=*/false,
9167                                             /*non_constant_p=*/NULL);
9168   argument = fold_non_dependent_expr (argument);
9169   if (!maybe_type_id)
9170     return argument;
9171   if (!cp_parser_next_token_ends_template_argument_p (parser))
9172     cp_parser_error (parser, "expected template-argument");
9173   if (cp_parser_parse_definitely (parser))
9174     return argument;
9175   /* We did our best to parse the argument as a non type-id, but that
9176      was the only alternative that matched (albeit with a '>' after
9177      it). We can assume it's just a typo from the user, and a
9178      diagnostic will then be issued.  */
9179   return cp_parser_type_id (parser);
9180 }
9181
9182 /* Parse an explicit-instantiation.
9183
9184    explicit-instantiation:
9185      template declaration
9186
9187    Although the standard says `declaration', what it really means is:
9188
9189    explicit-instantiation:
9190      template decl-specifier-seq [opt] declarator [opt] ;
9191
9192    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9193    supposed to be allowed.  A defect report has been filed about this
9194    issue.
9195
9196    GNU Extension:
9197
9198    explicit-instantiation:
9199      storage-class-specifier template
9200        decl-specifier-seq [opt] declarator [opt] ;
9201      function-specifier template
9202        decl-specifier-seq [opt] declarator [opt] ;  */
9203
9204 static void
9205 cp_parser_explicit_instantiation (cp_parser* parser)
9206 {
9207   int declares_class_or_enum;
9208   cp_decl_specifier_seq decl_specifiers;
9209   tree extension_specifier = NULL_TREE;
9210
9211   /* Look for an (optional) storage-class-specifier or
9212      function-specifier.  */
9213   if (cp_parser_allow_gnu_extensions_p (parser))
9214     {
9215       extension_specifier
9216         = cp_parser_storage_class_specifier_opt (parser);
9217       if (!extension_specifier)
9218         extension_specifier
9219           = cp_parser_function_specifier_opt (parser,
9220                                               /*decl_specs=*/NULL);
9221     }
9222
9223   /* Look for the `template' keyword.  */
9224   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9225   /* Let the front end know that we are processing an explicit
9226      instantiation.  */
9227   begin_explicit_instantiation ();
9228   /* [temp.explicit] says that we are supposed to ignore access
9229      control while processing explicit instantiation directives.  */
9230   push_deferring_access_checks (dk_no_check);
9231   /* Parse a decl-specifier-seq.  */
9232   cp_parser_decl_specifier_seq (parser,
9233                                 CP_PARSER_FLAGS_OPTIONAL,
9234                                 &decl_specifiers,
9235                                 &declares_class_or_enum);
9236   /* If there was exactly one decl-specifier, and it declared a class,
9237      and there's no declarator, then we have an explicit type
9238      instantiation.  */
9239   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9240     {
9241       tree type;
9242
9243       type = check_tag_decl (&decl_specifiers);
9244       /* Turn access control back on for names used during
9245          template instantiation.  */
9246       pop_deferring_access_checks ();
9247       if (type)
9248         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9249     }
9250   else
9251     {
9252       cp_declarator *declarator;
9253       tree decl;
9254
9255       /* Parse the declarator.  */
9256       declarator
9257         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9258                                 /*ctor_dtor_or_conv_p=*/NULL,
9259                                 /*parenthesized_p=*/NULL,
9260                                 /*member_p=*/false);
9261       if (declares_class_or_enum & 2)
9262         cp_parser_check_for_definition_in_return_type (declarator,
9263                                                        decl_specifiers.type);
9264       if (declarator != cp_error_declarator)
9265         {
9266           decl = grokdeclarator (declarator, &decl_specifiers,
9267                                  NORMAL, 0, NULL);
9268           /* Turn access control back on for names used during
9269              template instantiation.  */
9270           pop_deferring_access_checks ();
9271           /* Do the explicit instantiation.  */
9272           do_decl_instantiation (decl, extension_specifier);
9273         }
9274       else
9275         {
9276           pop_deferring_access_checks ();
9277           /* Skip the body of the explicit instantiation.  */
9278           cp_parser_skip_to_end_of_statement (parser);
9279         }
9280     }
9281   /* We're done with the instantiation.  */
9282   end_explicit_instantiation ();
9283
9284   cp_parser_consume_semicolon_at_end_of_statement (parser);
9285 }
9286
9287 /* Parse an explicit-specialization.
9288
9289    explicit-specialization:
9290      template < > declaration
9291
9292    Although the standard says `declaration', what it really means is:
9293
9294    explicit-specialization:
9295      template <> decl-specifier [opt] init-declarator [opt] ;
9296      template <> function-definition
9297      template <> explicit-specialization
9298      template <> template-declaration  */
9299
9300 static void
9301 cp_parser_explicit_specialization (cp_parser* parser)
9302 {
9303   /* Look for the `template' keyword.  */
9304   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9305   /* Look for the `<'.  */
9306   cp_parser_require (parser, CPP_LESS, "`<'");
9307   /* Look for the `>'.  */
9308   cp_parser_require (parser, CPP_GREATER, "`>'");
9309   /* We have processed another parameter list.  */
9310   ++parser->num_template_parameter_lists;
9311   /* Let the front end know that we are beginning a specialization.  */
9312   begin_specialization ();
9313
9314   /* If the next keyword is `template', we need to figure out whether
9315      or not we're looking a template-declaration.  */
9316   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9317     {
9318       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9319           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9320         cp_parser_template_declaration_after_export (parser,
9321                                                      /*member_p=*/false);
9322       else
9323         cp_parser_explicit_specialization (parser);
9324     }
9325   else
9326     /* Parse the dependent declaration.  */
9327     cp_parser_single_declaration (parser,
9328                                   /*member_p=*/false,
9329                                   /*friend_p=*/NULL);
9330
9331   /* We're done with the specialization.  */
9332   end_specialization ();
9333   /* We're done with this parameter list.  */
9334   --parser->num_template_parameter_lists;
9335 }
9336
9337 /* Parse a type-specifier.
9338
9339    type-specifier:
9340      simple-type-specifier
9341      class-specifier
9342      enum-specifier
9343      elaborated-type-specifier
9344      cv-qualifier
9345
9346    GNU Extension:
9347
9348    type-specifier:
9349      __complex__
9350
9351    Returns a representation of the type-specifier.  For a
9352    class-specifier, enum-specifier, or elaborated-type-specifier, a
9353    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9354
9355    The parser flags FLAGS is used to control type-specifier parsing.
9356
9357    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9358    in a decl-specifier-seq.
9359
9360    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9361    class-specifier, enum-specifier, or elaborated-type-specifier, then
9362    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9363    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9364    zero.
9365
9366    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9367    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9368    is set to FALSE.  */
9369
9370 static tree
9371 cp_parser_type_specifier (cp_parser* parser,
9372                           cp_parser_flags flags,
9373                           cp_decl_specifier_seq *decl_specs,
9374                           bool is_declaration,
9375                           int* declares_class_or_enum,
9376                           bool* is_cv_qualifier)
9377 {
9378   tree type_spec = NULL_TREE;
9379   cp_token *token;
9380   enum rid keyword;
9381   cp_decl_spec ds = ds_last;
9382
9383   /* Assume this type-specifier does not declare a new type.  */
9384   if (declares_class_or_enum)
9385     *declares_class_or_enum = 0;
9386   /* And that it does not specify a cv-qualifier.  */
9387   if (is_cv_qualifier)
9388     *is_cv_qualifier = false;
9389   /* Peek at the next token.  */
9390   token = cp_lexer_peek_token (parser->lexer);
9391
9392   /* If we're looking at a keyword, we can use that to guide the
9393      production we choose.  */
9394   keyword = token->keyword;
9395   switch (keyword)
9396     {
9397     case RID_ENUM:
9398       /* 'enum' [identifier] '{' introduces an enum-specifier;
9399          'enum' <anything else> introduces an elaborated-type-specifier.  */
9400       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9401           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9402               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9403                  == CPP_OPEN_BRACE))
9404         {
9405           if (parser->num_template_parameter_lists)
9406             {
9407               error ("template declaration of %qs", "enum");
9408               cp_parser_skip_to_end_of_block_or_statement (parser);
9409               type_spec = error_mark_node;
9410             }
9411           else
9412             type_spec = cp_parser_enum_specifier (parser);
9413
9414           if (declares_class_or_enum)
9415             *declares_class_or_enum = 2;
9416           if (decl_specs)
9417             cp_parser_set_decl_spec_type (decl_specs,
9418                                           type_spec,
9419                                           /*user_defined_p=*/true);
9420           return type_spec;
9421         }
9422       else
9423         goto elaborated_type_specifier;
9424
9425       /* Any of these indicate either a class-specifier, or an
9426          elaborated-type-specifier.  */
9427     case RID_CLASS:
9428     case RID_STRUCT:
9429     case RID_UNION:
9430       /* Parse tentatively so that we can back up if we don't find a
9431          class-specifier.  */
9432       cp_parser_parse_tentatively (parser);
9433       /* Look for the class-specifier.  */
9434       type_spec = cp_parser_class_specifier (parser);
9435       /* If that worked, we're done.  */
9436       if (cp_parser_parse_definitely (parser))
9437         {
9438           if (declares_class_or_enum)
9439             *declares_class_or_enum = 2;
9440           if (decl_specs)
9441             cp_parser_set_decl_spec_type (decl_specs,
9442                                           type_spec,
9443                                           /*user_defined_p=*/true);
9444           return type_spec;
9445         }
9446
9447       /* Fall through.  */
9448     elaborated_type_specifier:
9449       /* We're declaring (not defining) a class or enum.  */
9450       if (declares_class_or_enum)
9451         *declares_class_or_enum = 1;
9452
9453       /* Fall through.  */
9454     case RID_TYPENAME:
9455       /* Look for an elaborated-type-specifier.  */
9456       type_spec
9457         = (cp_parser_elaborated_type_specifier
9458            (parser,
9459             decl_specs && decl_specs->specs[(int) ds_friend],
9460             is_declaration));
9461       if (decl_specs)
9462         cp_parser_set_decl_spec_type (decl_specs,
9463                                       type_spec,
9464                                       /*user_defined_p=*/true);
9465       return type_spec;
9466
9467     case RID_CONST:
9468       ds = ds_const;
9469       if (is_cv_qualifier)
9470         *is_cv_qualifier = true;
9471       break;
9472
9473     case RID_VOLATILE:
9474       ds = ds_volatile;
9475       if (is_cv_qualifier)
9476         *is_cv_qualifier = true;
9477       break;
9478
9479     case RID_RESTRICT:
9480       ds = ds_restrict;
9481       if (is_cv_qualifier)
9482         *is_cv_qualifier = true;
9483       break;
9484
9485     case RID_COMPLEX:
9486       /* The `__complex__' keyword is a GNU extension.  */
9487       ds = ds_complex;
9488       break;
9489
9490     default:
9491       break;
9492     }
9493
9494   /* Handle simple keywords.  */
9495   if (ds != ds_last)
9496     {
9497       if (decl_specs)
9498         {
9499           ++decl_specs->specs[(int)ds];
9500           decl_specs->any_specifiers_p = true;
9501         }
9502       return cp_lexer_consume_token (parser->lexer)->value;
9503     }
9504
9505   /* If we do not already have a type-specifier, assume we are looking
9506      at a simple-type-specifier.  */
9507   type_spec = cp_parser_simple_type_specifier (parser,
9508                                                decl_specs,
9509                                                flags);
9510
9511   /* If we didn't find a type-specifier, and a type-specifier was not
9512      optional in this context, issue an error message.  */
9513   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9514     {
9515       cp_parser_error (parser, "expected type specifier");
9516       return error_mark_node;
9517     }
9518
9519   return type_spec;
9520 }
9521
9522 /* Parse a simple-type-specifier.
9523
9524    simple-type-specifier:
9525      :: [opt] nested-name-specifier [opt] type-name
9526      :: [opt] nested-name-specifier template template-id
9527      char
9528      wchar_t
9529      bool
9530      short
9531      int
9532      long
9533      signed
9534      unsigned
9535      float
9536      double
9537      void
9538
9539    GNU Extension:
9540
9541    simple-type-specifier:
9542      __typeof__ unary-expression
9543      __typeof__ ( type-id )
9544
9545    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9546    appropriately updated.  */
9547
9548 static tree
9549 cp_parser_simple_type_specifier (cp_parser* parser,
9550                                  cp_decl_specifier_seq *decl_specs,
9551                                  cp_parser_flags flags)
9552 {
9553   tree type = NULL_TREE;
9554   cp_token *token;
9555
9556   /* Peek at the next token.  */
9557   token = cp_lexer_peek_token (parser->lexer);
9558
9559   /* If we're looking at a keyword, things are easy.  */
9560   switch (token->keyword)
9561     {
9562     case RID_CHAR:
9563       if (decl_specs)
9564         decl_specs->explicit_char_p = true;
9565       type = char_type_node;
9566       break;
9567     case RID_WCHAR:
9568       type = wchar_type_node;
9569       break;
9570     case RID_BOOL:
9571       type = boolean_type_node;
9572       break;
9573     case RID_SHORT:
9574       if (decl_specs)
9575         ++decl_specs->specs[(int) ds_short];
9576       type = short_integer_type_node;
9577       break;
9578     case RID_INT:
9579       if (decl_specs)
9580         decl_specs->explicit_int_p = true;
9581       type = integer_type_node;
9582       break;
9583     case RID_LONG:
9584       if (decl_specs)
9585         ++decl_specs->specs[(int) ds_long];
9586       type = long_integer_type_node;
9587       break;
9588     case RID_SIGNED:
9589       if (decl_specs)
9590         ++decl_specs->specs[(int) ds_signed];
9591       type = integer_type_node;
9592       break;
9593     case RID_UNSIGNED:
9594       if (decl_specs)
9595         ++decl_specs->specs[(int) ds_unsigned];
9596       type = unsigned_type_node;
9597       break;
9598     case RID_FLOAT:
9599       type = float_type_node;
9600       break;
9601     case RID_DOUBLE:
9602       type = double_type_node;
9603       break;
9604     case RID_VOID:
9605       type = void_type_node;
9606       break;
9607
9608     case RID_TYPEOF:
9609       /* Consume the `typeof' token.  */
9610       cp_lexer_consume_token (parser->lexer);
9611       /* Parse the operand to `typeof'.  */
9612       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9613       /* If it is not already a TYPE, take its type.  */
9614       if (!TYPE_P (type))
9615         type = finish_typeof (type);
9616
9617       if (decl_specs)
9618         cp_parser_set_decl_spec_type (decl_specs, type,
9619                                       /*user_defined_p=*/true);
9620
9621       return type;
9622
9623     default:
9624       break;
9625     }
9626
9627   /* If the type-specifier was for a built-in type, we're done.  */
9628   if (type)
9629     {
9630       tree id;
9631
9632       /* Record the type.  */
9633       if (decl_specs
9634           && (token->keyword != RID_SIGNED
9635               && token->keyword != RID_UNSIGNED
9636               && token->keyword != RID_SHORT
9637               && token->keyword != RID_LONG))
9638         cp_parser_set_decl_spec_type (decl_specs,
9639                                       type,
9640                                       /*user_defined=*/false);
9641       if (decl_specs)
9642         decl_specs->any_specifiers_p = true;
9643
9644       /* Consume the token.  */
9645       id = cp_lexer_consume_token (parser->lexer)->value;
9646
9647       /* There is no valid C++ program where a non-template type is
9648          followed by a "<".  That usually indicates that the user thought
9649          that the type was a template.  */
9650       cp_parser_check_for_invalid_template_id (parser, type);
9651
9652       return TYPE_NAME (type);
9653     }
9654
9655   /* The type-specifier must be a user-defined type.  */
9656   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9657     {
9658       bool qualified_p;
9659       bool global_p;
9660
9661       /* Don't gobble tokens or issue error messages if this is an
9662          optional type-specifier.  */
9663       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9664         cp_parser_parse_tentatively (parser);
9665
9666       /* Look for the optional `::' operator.  */
9667       global_p
9668         = (cp_parser_global_scope_opt (parser,
9669                                        /*current_scope_valid_p=*/false)
9670            != NULL_TREE);
9671       /* Look for the nested-name specifier.  */
9672       qualified_p
9673         = (cp_parser_nested_name_specifier_opt (parser,
9674                                                 /*typename_keyword_p=*/false,
9675                                                 /*check_dependency_p=*/true,
9676                                                 /*type_p=*/false,
9677                                                 /*is_declaration=*/false)
9678            != NULL_TREE);
9679       /* If we have seen a nested-name-specifier, and the next token
9680          is `template', then we are using the template-id production.  */
9681       if (parser->scope
9682           && cp_parser_optional_template_keyword (parser))
9683         {
9684           /* Look for the template-id.  */
9685           type = cp_parser_template_id (parser,
9686                                         /*template_keyword_p=*/true,
9687                                         /*check_dependency_p=*/true,
9688                                         /*is_declaration=*/false);
9689           /* If the template-id did not name a type, we are out of
9690              luck.  */
9691           if (TREE_CODE (type) != TYPE_DECL)
9692             {
9693               cp_parser_error (parser, "expected template-id for type");
9694               type = NULL_TREE;
9695             }
9696         }
9697       /* Otherwise, look for a type-name.  */
9698       else
9699         type = cp_parser_type_name (parser);
9700       /* Keep track of all name-lookups performed in class scopes.  */
9701       if (type
9702           && !global_p
9703           && !qualified_p
9704           && TREE_CODE (type) == TYPE_DECL
9705           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9706         maybe_note_name_used_in_class (DECL_NAME (type), type);
9707       /* If it didn't work out, we don't have a TYPE.  */
9708       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9709           && !cp_parser_parse_definitely (parser))
9710         type = NULL_TREE;
9711       if (type && decl_specs)
9712         cp_parser_set_decl_spec_type (decl_specs, type,
9713                                       /*user_defined=*/true);
9714     }
9715
9716   /* If we didn't get a type-name, issue an error message.  */
9717   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9718     {
9719       cp_parser_error (parser, "expected type-name");
9720       return error_mark_node;
9721     }
9722
9723   /* There is no valid C++ program where a non-template type is
9724      followed by a "<".  That usually indicates that the user thought
9725      that the type was a template.  */
9726   if (type && type != error_mark_node)
9727     {
9728       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9729          If it is, then the '<'...'>' enclose protocol names rather than
9730          template arguments, and so everything is fine.  */
9731       if (c_dialect_objc ()
9732           && (objc_is_id (type) || objc_is_class_name (type)))
9733         {
9734           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9735           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9736
9737           /* Clobber the "unqualified" type previously entered into
9738              DECL_SPECS with the new, improved protocol-qualified version.  */
9739           if (decl_specs)
9740             decl_specs->type = qual_type;
9741
9742           return qual_type;
9743         }
9744
9745       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9746     }
9747
9748   return type;
9749 }
9750
9751 /* Parse a type-name.
9752
9753    type-name:
9754      class-name
9755      enum-name
9756      typedef-name
9757
9758    enum-name:
9759      identifier
9760
9761    typedef-name:
9762      identifier
9763
9764    Returns a TYPE_DECL for the type.  */
9765
9766 static tree
9767 cp_parser_type_name (cp_parser* parser)
9768 {
9769   tree type_decl;
9770   tree identifier;
9771
9772   /* We can't know yet whether it is a class-name or not.  */
9773   cp_parser_parse_tentatively (parser);
9774   /* Try a class-name.  */
9775   type_decl = cp_parser_class_name (parser,
9776                                     /*typename_keyword_p=*/false,
9777                                     /*template_keyword_p=*/false,
9778                                     none_type,
9779                                     /*check_dependency_p=*/true,
9780                                     /*class_head_p=*/false,
9781                                     /*is_declaration=*/false);
9782   /* If it's not a class-name, keep looking.  */
9783   if (!cp_parser_parse_definitely (parser))
9784     {
9785       /* It must be a typedef-name or an enum-name.  */
9786       identifier = cp_parser_identifier (parser);
9787       if (identifier == error_mark_node)
9788         return error_mark_node;
9789
9790       /* Look up the type-name.  */
9791       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9792
9793       if (TREE_CODE (type_decl) != TYPE_DECL
9794           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9795         {
9796           /* See if this is an Objective-C type.  */
9797           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9798           tree type = objc_get_protocol_qualified_type (identifier, protos);
9799           if (type)
9800             type_decl = TYPE_NAME (type);
9801         }
9802
9803       /* Issue an error if we did not find a type-name.  */
9804       if (TREE_CODE (type_decl) != TYPE_DECL)
9805         {
9806           if (!cp_parser_simulate_error (parser))
9807             cp_parser_name_lookup_error (parser, identifier, type_decl,
9808                                          "is not a type");
9809           type_decl = error_mark_node;
9810         }
9811       /* Remember that the name was used in the definition of the
9812          current class so that we can check later to see if the
9813          meaning would have been different after the class was
9814          entirely defined.  */
9815       else if (type_decl != error_mark_node
9816                && !parser->scope)
9817         maybe_note_name_used_in_class (identifier, type_decl);
9818     }
9819
9820   return type_decl;
9821 }
9822
9823
9824 /* Parse an elaborated-type-specifier.  Note that the grammar given
9825    here incorporates the resolution to DR68.
9826
9827    elaborated-type-specifier:
9828      class-key :: [opt] nested-name-specifier [opt] identifier
9829      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9830      enum :: [opt] nested-name-specifier [opt] identifier
9831      typename :: [opt] nested-name-specifier identifier
9832      typename :: [opt] nested-name-specifier template [opt]
9833        template-id
9834
9835    GNU extension:
9836
9837    elaborated-type-specifier:
9838      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9839      class-key attributes :: [opt] nested-name-specifier [opt]
9840                template [opt] template-id
9841      enum attributes :: [opt] nested-name-specifier [opt] identifier
9842
9843    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9844    declared `friend'.  If IS_DECLARATION is TRUE, then this
9845    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9846    something is being declared.
9847
9848    Returns the TYPE specified.  */
9849
9850 static tree
9851 cp_parser_elaborated_type_specifier (cp_parser* parser,
9852                                      bool is_friend,
9853                                      bool is_declaration)
9854 {
9855   enum tag_types tag_type;
9856   tree identifier;
9857   tree type = NULL_TREE;
9858   tree attributes = NULL_TREE;
9859
9860   /* See if we're looking at the `enum' keyword.  */
9861   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9862     {
9863       /* Consume the `enum' token.  */
9864       cp_lexer_consume_token (parser->lexer);
9865       /* Remember that it's an enumeration type.  */
9866       tag_type = enum_type;
9867       /* Parse the attributes.  */
9868       attributes = cp_parser_attributes_opt (parser);
9869     }
9870   /* Or, it might be `typename'.  */
9871   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9872                                            RID_TYPENAME))
9873     {
9874       /* Consume the `typename' token.  */
9875       cp_lexer_consume_token (parser->lexer);
9876       /* Remember that it's a `typename' type.  */
9877       tag_type = typename_type;
9878       /* The `typename' keyword is only allowed in templates.  */
9879       if (!processing_template_decl)
9880         pedwarn ("using %<typename%> outside of template");
9881     }
9882   /* Otherwise it must be a class-key.  */
9883   else
9884     {
9885       tag_type = cp_parser_class_key (parser);
9886       if (tag_type == none_type)
9887         return error_mark_node;
9888       /* Parse the attributes.  */
9889       attributes = cp_parser_attributes_opt (parser);
9890     }
9891
9892   /* Look for the `::' operator.  */
9893   cp_parser_global_scope_opt (parser,
9894                               /*current_scope_valid_p=*/false);
9895   /* Look for the nested-name-specifier.  */
9896   if (tag_type == typename_type)
9897     {
9898       if (!cp_parser_nested_name_specifier (parser,
9899                                            /*typename_keyword_p=*/true,
9900                                            /*check_dependency_p=*/true,
9901                                            /*type_p=*/true,
9902                                             is_declaration))
9903         return error_mark_node;
9904     }
9905   else
9906     /* Even though `typename' is not present, the proposed resolution
9907        to Core Issue 180 says that in `class A<T>::B', `B' should be
9908        considered a type-name, even if `A<T>' is dependent.  */
9909     cp_parser_nested_name_specifier_opt (parser,
9910                                          /*typename_keyword_p=*/true,
9911                                          /*check_dependency_p=*/true,
9912                                          /*type_p=*/true,
9913                                          is_declaration);
9914   /* For everything but enumeration types, consider a template-id.  */
9915   if (tag_type != enum_type)
9916     {
9917       bool template_p = false;
9918       tree decl;
9919
9920       /* Allow the `template' keyword.  */
9921       template_p = cp_parser_optional_template_keyword (parser);
9922       /* If we didn't see `template', we don't know if there's a
9923          template-id or not.  */
9924       if (!template_p)
9925         cp_parser_parse_tentatively (parser);
9926       /* Parse the template-id.  */
9927       decl = cp_parser_template_id (parser, template_p,
9928                                     /*check_dependency_p=*/true,
9929                                     is_declaration);
9930       /* If we didn't find a template-id, look for an ordinary
9931          identifier.  */
9932       if (!template_p && !cp_parser_parse_definitely (parser))
9933         ;
9934       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9935          in effect, then we must assume that, upon instantiation, the
9936          template will correspond to a class.  */
9937       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9938                && tag_type == typename_type)
9939         type = make_typename_type (parser->scope, decl,
9940                                    typename_type,
9941                                    /*complain=*/1);
9942       else
9943         type = TREE_TYPE (decl);
9944     }
9945
9946   /* For an enumeration type, consider only a plain identifier.  */
9947   if (!type)
9948     {
9949       identifier = cp_parser_identifier (parser);
9950
9951       if (identifier == error_mark_node)
9952         {
9953           parser->scope = NULL_TREE;
9954           return error_mark_node;
9955         }
9956
9957       /* For a `typename', we needn't call xref_tag.  */
9958       if (tag_type == typename_type
9959           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9960         return cp_parser_make_typename_type (parser, parser->scope,
9961                                              identifier);
9962       /* Look up a qualified name in the usual way.  */
9963       if (parser->scope)
9964         {
9965           tree decl;
9966
9967           decl = cp_parser_lookup_name (parser, identifier,
9968                                         tag_type,
9969                                         /*is_template=*/false,
9970                                         /*is_namespace=*/false,
9971                                         /*check_dependency=*/true,
9972                                         /*ambiguous_p=*/NULL);
9973
9974           /* If we are parsing friend declaration, DECL may be a
9975              TEMPLATE_DECL tree node here.  However, we need to check
9976              whether this TEMPLATE_DECL results in valid code.  Consider
9977              the following example:
9978
9979                namespace N {
9980                  template <class T> class C {};
9981                }
9982                class X {
9983                  template <class T> friend class N::C; // #1, valid code
9984                };
9985                template <class T> class Y {
9986                  friend class N::C;                    // #2, invalid code
9987                };
9988
9989              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9990              name lookup of `N::C'.  We see that friend declaration must
9991              be template for the code to be valid.  Note that
9992              processing_template_decl does not work here since it is
9993              always 1 for the above two cases.  */
9994
9995           decl = (cp_parser_maybe_treat_template_as_class
9996                   (decl, /*tag_name_p=*/is_friend
9997                          && parser->num_template_parameter_lists));
9998
9999           if (TREE_CODE (decl) != TYPE_DECL)
10000             {
10001               cp_parser_diagnose_invalid_type_name (parser,
10002                                                     parser->scope,
10003                                                     identifier);
10004               return error_mark_node;
10005             }
10006
10007           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10008             check_elaborated_type_specifier
10009               (tag_type, decl,
10010                (parser->num_template_parameter_lists
10011                 || DECL_SELF_REFERENCE_P (decl)));
10012
10013           type = TREE_TYPE (decl);
10014         }
10015       else
10016         {
10017           /* An elaborated-type-specifier sometimes introduces a new type and
10018              sometimes names an existing type.  Normally, the rule is that it
10019              introduces a new type only if there is not an existing type of
10020              the same name already in scope.  For example, given:
10021
10022                struct S {};
10023                void f() { struct S s; }
10024
10025              the `struct S' in the body of `f' is the same `struct S' as in
10026              the global scope; the existing definition is used.  However, if
10027              there were no global declaration, this would introduce a new
10028              local class named `S'.
10029
10030              An exception to this rule applies to the following code:
10031
10032                namespace N { struct S; }
10033
10034              Here, the elaborated-type-specifier names a new type
10035              unconditionally; even if there is already an `S' in the
10036              containing scope this declaration names a new type.
10037              This exception only applies if the elaborated-type-specifier
10038              forms the complete declaration:
10039
10040                [class.name]
10041
10042                A declaration consisting solely of `class-key identifier ;' is
10043                either a redeclaration of the name in the current scope or a
10044                forward declaration of the identifier as a class name.  It
10045                introduces the name into the current scope.
10046
10047              We are in this situation precisely when the next token is a `;'.
10048
10049              An exception to the exception is that a `friend' declaration does
10050              *not* name a new type; i.e., given:
10051
10052                struct S { friend struct T; };
10053
10054              `T' is not a new type in the scope of `S'.
10055
10056              Also, `new struct S' or `sizeof (struct S)' never results in the
10057              definition of a new type; a new type can only be declared in a
10058              declaration context.  */
10059
10060           tag_scope ts;
10061           bool template_p;
10062
10063           if (is_friend)
10064             /* Friends have special name lookup rules.  */
10065             ts = ts_within_enclosing_non_class;
10066           else if (is_declaration
10067                    && cp_lexer_next_token_is (parser->lexer,
10068                                               CPP_SEMICOLON))
10069             /* This is a `class-key identifier ;' */
10070             ts = ts_current;
10071           else
10072             ts = ts_global;
10073
10074           /* Warn about attributes. They are ignored.  */
10075           if (attributes)
10076             warning (OPT_Wattributes,
10077                      "type attributes are honored only at type definition");
10078
10079           template_p = 
10080             (parser->num_template_parameter_lists
10081              && (cp_parser_next_token_starts_class_definition_p (parser)
10082                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10083           type = xref_tag (tag_type, identifier, ts, template_p);
10084         }
10085     }
10086   if (tag_type != enum_type)
10087     cp_parser_check_class_key (tag_type, type);
10088
10089   /* A "<" cannot follow an elaborated type specifier.  If that
10090      happens, the user was probably trying to form a template-id.  */
10091   cp_parser_check_for_invalid_template_id (parser, type);
10092
10093   return type;
10094 }
10095
10096 /* Parse an enum-specifier.
10097
10098    enum-specifier:
10099      enum identifier [opt] { enumerator-list [opt] }
10100
10101    GNU Extensions:
10102      enum identifier [opt] { enumerator-list [opt] } attributes
10103
10104    Returns an ENUM_TYPE representing the enumeration.  */
10105
10106 static tree
10107 cp_parser_enum_specifier (cp_parser* parser)
10108 {
10109   tree identifier;
10110   tree type;
10111
10112   /* Caller guarantees that the current token is 'enum', an identifier
10113      possibly follows, and the token after that is an opening brace.
10114      If we don't have an identifier, fabricate an anonymous name for
10115      the enumeration being defined.  */
10116   cp_lexer_consume_token (parser->lexer);
10117
10118   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10119     identifier = cp_parser_identifier (parser);
10120   else
10121     identifier = make_anon_name ();
10122
10123   /* Issue an error message if type-definitions are forbidden here.  */
10124   cp_parser_check_type_definition (parser);
10125
10126   /* Create the new type.  We do this before consuming the opening brace
10127      so the enum will be recorded as being on the line of its tag (or the
10128      'enum' keyword, if there is no tag).  */
10129   type = start_enum (identifier);
10130
10131   /* Consume the opening brace.  */
10132   cp_lexer_consume_token (parser->lexer);
10133
10134   /* If the next token is not '}', then there are some enumerators.  */
10135   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10136     cp_parser_enumerator_list (parser, type);
10137
10138   /* Consume the final '}'.  */
10139   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10140
10141   /* Look for trailing attributes to apply to this enumeration, and
10142      apply them if appropriate.  */
10143   if (cp_parser_allow_gnu_extensions_p (parser))
10144     {
10145       tree trailing_attr = cp_parser_attributes_opt (parser);
10146       cplus_decl_attributes (&type,
10147                              trailing_attr,
10148                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10149     }
10150
10151   /* Finish up the enumeration.  */
10152   finish_enum (type);
10153
10154   return type;
10155 }
10156
10157 /* Parse an enumerator-list.  The enumerators all have the indicated
10158    TYPE.
10159
10160    enumerator-list:
10161      enumerator-definition
10162      enumerator-list , enumerator-definition  */
10163
10164 static void
10165 cp_parser_enumerator_list (cp_parser* parser, tree type)
10166 {
10167   while (true)
10168     {
10169       /* Parse an enumerator-definition.  */
10170       cp_parser_enumerator_definition (parser, type);
10171
10172       /* If the next token is not a ',', we've reached the end of
10173          the list.  */
10174       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10175         break;
10176       /* Otherwise, consume the `,' and keep going.  */
10177       cp_lexer_consume_token (parser->lexer);
10178       /* If the next token is a `}', there is a trailing comma.  */
10179       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10180         {
10181           if (pedantic && !in_system_header)
10182             pedwarn ("comma at end of enumerator list");
10183           break;
10184         }
10185     }
10186 }
10187
10188 /* Parse an enumerator-definition.  The enumerator has the indicated
10189    TYPE.
10190
10191    enumerator-definition:
10192      enumerator
10193      enumerator = constant-expression
10194
10195    enumerator:
10196      identifier  */
10197
10198 static void
10199 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10200 {
10201   tree identifier;
10202   tree value;
10203
10204   /* Look for the identifier.  */
10205   identifier = cp_parser_identifier (parser);
10206   if (identifier == error_mark_node)
10207     return;
10208
10209   /* If the next token is an '=', then there is an explicit value.  */
10210   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10211     {
10212       /* Consume the `=' token.  */
10213       cp_lexer_consume_token (parser->lexer);
10214       /* Parse the value.  */
10215       value = cp_parser_constant_expression (parser,
10216                                              /*allow_non_constant_p=*/false,
10217                                              NULL);
10218     }
10219   else
10220     value = NULL_TREE;
10221
10222   /* Create the enumerator.  */
10223   build_enumerator (identifier, value, type);
10224 }
10225
10226 /* Parse a namespace-name.
10227
10228    namespace-name:
10229      original-namespace-name
10230      namespace-alias
10231
10232    Returns the NAMESPACE_DECL for the namespace.  */
10233
10234 static tree
10235 cp_parser_namespace_name (cp_parser* parser)
10236 {
10237   tree identifier;
10238   tree namespace_decl;
10239
10240   /* Get the name of the namespace.  */
10241   identifier = cp_parser_identifier (parser);
10242   if (identifier == error_mark_node)
10243     return error_mark_node;
10244
10245   /* Look up the identifier in the currently active scope.  Look only
10246      for namespaces, due to:
10247
10248        [basic.lookup.udir]
10249
10250        When looking up a namespace-name in a using-directive or alias
10251        definition, only namespace names are considered.
10252
10253      And:
10254
10255        [basic.lookup.qual]
10256
10257        During the lookup of a name preceding the :: scope resolution
10258        operator, object, function, and enumerator names are ignored.
10259
10260      (Note that cp_parser_class_or_namespace_name only calls this
10261      function if the token after the name is the scope resolution
10262      operator.)  */
10263   namespace_decl = cp_parser_lookup_name (parser, identifier,
10264                                           none_type,
10265                                           /*is_template=*/false,
10266                                           /*is_namespace=*/true,
10267                                           /*check_dependency=*/true,
10268                                           /*ambiguous_p=*/NULL);
10269   /* If it's not a namespace, issue an error.  */
10270   if (namespace_decl == error_mark_node
10271       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10272     {
10273       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10274         error ("%qD is not a namespace-name", identifier);
10275       cp_parser_error (parser, "expected namespace-name");
10276       namespace_decl = error_mark_node;
10277     }
10278
10279   return namespace_decl;
10280 }
10281
10282 /* Parse a namespace-definition.
10283
10284    namespace-definition:
10285      named-namespace-definition
10286      unnamed-namespace-definition
10287
10288    named-namespace-definition:
10289      original-namespace-definition
10290      extension-namespace-definition
10291
10292    original-namespace-definition:
10293      namespace identifier { namespace-body }
10294
10295    extension-namespace-definition:
10296      namespace original-namespace-name { namespace-body }
10297
10298    unnamed-namespace-definition:
10299      namespace { namespace-body } */
10300
10301 static void
10302 cp_parser_namespace_definition (cp_parser* parser)
10303 {
10304   tree identifier;
10305
10306   /* Look for the `namespace' keyword.  */
10307   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10308
10309   /* Get the name of the namespace.  We do not attempt to distinguish
10310      between an original-namespace-definition and an
10311      extension-namespace-definition at this point.  The semantic
10312      analysis routines are responsible for that.  */
10313   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10314     identifier = cp_parser_identifier (parser);
10315   else
10316     identifier = NULL_TREE;
10317
10318   /* Look for the `{' to start the namespace.  */
10319   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10320   /* Start the namespace.  */
10321   push_namespace (identifier);
10322   /* Parse the body of the namespace.  */
10323   cp_parser_namespace_body (parser);
10324   /* Finish the namespace.  */
10325   pop_namespace ();
10326   /* Look for the final `}'.  */
10327   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10328 }
10329
10330 /* Parse a namespace-body.
10331
10332    namespace-body:
10333      declaration-seq [opt]  */
10334
10335 static void
10336 cp_parser_namespace_body (cp_parser* parser)
10337 {
10338   cp_parser_declaration_seq_opt (parser);
10339 }
10340
10341 /* Parse a namespace-alias-definition.
10342
10343    namespace-alias-definition:
10344      namespace identifier = qualified-namespace-specifier ;  */
10345
10346 static void
10347 cp_parser_namespace_alias_definition (cp_parser* parser)
10348 {
10349   tree identifier;
10350   tree namespace_specifier;
10351
10352   /* Look for the `namespace' keyword.  */
10353   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10354   /* Look for the identifier.  */
10355   identifier = cp_parser_identifier (parser);
10356   if (identifier == error_mark_node)
10357     return;
10358   /* Look for the `=' token.  */
10359   cp_parser_require (parser, CPP_EQ, "`='");
10360   /* Look for the qualified-namespace-specifier.  */
10361   namespace_specifier
10362     = cp_parser_qualified_namespace_specifier (parser);
10363   /* Look for the `;' token.  */
10364   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10365
10366   /* Register the alias in the symbol table.  */
10367   do_namespace_alias (identifier, namespace_specifier);
10368 }
10369
10370 /* Parse a qualified-namespace-specifier.
10371
10372    qualified-namespace-specifier:
10373      :: [opt] nested-name-specifier [opt] namespace-name
10374
10375    Returns a NAMESPACE_DECL corresponding to the specified
10376    namespace.  */
10377
10378 static tree
10379 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10380 {
10381   /* Look for the optional `::'.  */
10382   cp_parser_global_scope_opt (parser,
10383                               /*current_scope_valid_p=*/false);
10384
10385   /* Look for the optional nested-name-specifier.  */
10386   cp_parser_nested_name_specifier_opt (parser,
10387                                        /*typename_keyword_p=*/false,
10388                                        /*check_dependency_p=*/true,
10389                                        /*type_p=*/false,
10390                                        /*is_declaration=*/true);
10391
10392   return cp_parser_namespace_name (parser);
10393 }
10394
10395 /* Parse a using-declaration.
10396
10397    using-declaration:
10398      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10399      using :: unqualified-id ;  */
10400
10401 static void
10402 cp_parser_using_declaration (cp_parser* parser)
10403 {
10404   cp_token *token;
10405   bool typename_p = false;
10406   bool global_scope_p;
10407   tree decl;
10408   tree identifier;
10409   tree qscope;
10410
10411   /* Look for the `using' keyword.  */
10412   cp_parser_require_keyword (parser, RID_USING, "`using'");
10413
10414   /* Peek at the next token.  */
10415   token = cp_lexer_peek_token (parser->lexer);
10416   /* See if it's `typename'.  */
10417   if (token->keyword == RID_TYPENAME)
10418     {
10419       /* Remember that we've seen it.  */
10420       typename_p = true;
10421       /* Consume the `typename' token.  */
10422       cp_lexer_consume_token (parser->lexer);
10423     }
10424
10425   /* Look for the optional global scope qualification.  */
10426   global_scope_p
10427     = (cp_parser_global_scope_opt (parser,
10428                                    /*current_scope_valid_p=*/false)
10429        != NULL_TREE);
10430
10431   /* If we saw `typename', or didn't see `::', then there must be a
10432      nested-name-specifier present.  */
10433   if (typename_p || !global_scope_p)
10434     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10435                                               /*check_dependency_p=*/true,
10436                                               /*type_p=*/false,
10437                                               /*is_declaration=*/true);
10438   /* Otherwise, we could be in either of the two productions.  In that
10439      case, treat the nested-name-specifier as optional.  */
10440   else
10441     qscope = cp_parser_nested_name_specifier_opt (parser,
10442                                                   /*typename_keyword_p=*/false,
10443                                                   /*check_dependency_p=*/true,
10444                                                   /*type_p=*/false,
10445                                                   /*is_declaration=*/true);
10446   if (!qscope)
10447     qscope = global_namespace;
10448
10449   /* Parse the unqualified-id.  */
10450   identifier = cp_parser_unqualified_id (parser,
10451                                          /*template_keyword_p=*/false,
10452                                          /*check_dependency_p=*/true,
10453                                          /*declarator_p=*/true);
10454
10455   /* The function we call to handle a using-declaration is different
10456      depending on what scope we are in.  */
10457   if (identifier == error_mark_node)
10458     ;
10459   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10460            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10461     /* [namespace.udecl]
10462
10463        A using declaration shall not name a template-id.  */
10464     error ("a template-id may not appear in a using-declaration");
10465   else
10466     {
10467       if (at_class_scope_p ())
10468         {
10469           /* Create the USING_DECL.  */
10470           decl = do_class_using_decl (parser->scope, identifier);
10471           /* Add it to the list of members in this class.  */
10472           finish_member_declaration (decl);
10473         }
10474       else
10475         {
10476           decl = cp_parser_lookup_name_simple (parser, identifier);
10477           if (decl == error_mark_node)
10478             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10479           else if (!at_namespace_scope_p ())
10480             do_local_using_decl (decl, qscope, identifier);
10481           else
10482             do_toplevel_using_decl (decl, qscope, identifier);
10483         }
10484     }
10485
10486   /* Look for the final `;'.  */
10487   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10488 }
10489
10490 /* Parse a using-directive.
10491
10492    using-directive:
10493      using namespace :: [opt] nested-name-specifier [opt]
10494        namespace-name ;  */
10495
10496 static void
10497 cp_parser_using_directive (cp_parser* parser)
10498 {
10499   tree namespace_decl;
10500   tree attribs;
10501
10502   /* Look for the `using' keyword.  */
10503   cp_parser_require_keyword (parser, RID_USING, "`using'");
10504   /* And the `namespace' keyword.  */
10505   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10506   /* Look for the optional `::' operator.  */
10507   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10508   /* And the optional nested-name-specifier.  */
10509   cp_parser_nested_name_specifier_opt (parser,
10510                                        /*typename_keyword_p=*/false,
10511                                        /*check_dependency_p=*/true,
10512                                        /*type_p=*/false,
10513                                        /*is_declaration=*/true);
10514   /* Get the namespace being used.  */
10515   namespace_decl = cp_parser_namespace_name (parser);
10516   /* And any specified attributes.  */
10517   attribs = cp_parser_attributes_opt (parser);
10518   /* Update the symbol table.  */
10519   parse_using_directive (namespace_decl, attribs);
10520   /* Look for the final `;'.  */
10521   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10522 }
10523
10524 /* Parse an asm-definition.
10525
10526    asm-definition:
10527      asm ( string-literal ) ;
10528
10529    GNU Extension:
10530
10531    asm-definition:
10532      asm volatile [opt] ( string-literal ) ;
10533      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10534      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10535                           : asm-operand-list [opt] ) ;
10536      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10537                           : asm-operand-list [opt]
10538                           : asm-operand-list [opt] ) ;  */
10539
10540 static void
10541 cp_parser_asm_definition (cp_parser* parser)
10542 {
10543   tree string;
10544   tree outputs = NULL_TREE;
10545   tree inputs = NULL_TREE;
10546   tree clobbers = NULL_TREE;
10547   tree asm_stmt;
10548   bool volatile_p = false;
10549   bool extended_p = false;
10550
10551   /* Look for the `asm' keyword.  */
10552   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10553   /* See if the next token is `volatile'.  */
10554   if (cp_parser_allow_gnu_extensions_p (parser)
10555       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10556     {
10557       /* Remember that we saw the `volatile' keyword.  */
10558       volatile_p = true;
10559       /* Consume the token.  */
10560       cp_lexer_consume_token (parser->lexer);
10561     }
10562   /* Look for the opening `('.  */
10563   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10564     return;
10565   /* Look for the string.  */
10566   string = cp_parser_string_literal (parser, false, false);
10567   if (string == error_mark_node)
10568     {
10569       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10570                                              /*consume_paren=*/true);
10571       return;
10572     }
10573
10574   /* If we're allowing GNU extensions, check for the extended assembly
10575      syntax.  Unfortunately, the `:' tokens need not be separated by
10576      a space in C, and so, for compatibility, we tolerate that here
10577      too.  Doing that means that we have to treat the `::' operator as
10578      two `:' tokens.  */
10579   if (cp_parser_allow_gnu_extensions_p (parser)
10580       && at_function_scope_p ()
10581       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10582           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10583     {
10584       bool inputs_p = false;
10585       bool clobbers_p = false;
10586
10587       /* The extended syntax was used.  */
10588       extended_p = true;
10589
10590       /* Look for outputs.  */
10591       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10592         {
10593           /* Consume the `:'.  */
10594           cp_lexer_consume_token (parser->lexer);
10595           /* Parse the output-operands.  */
10596           if (cp_lexer_next_token_is_not (parser->lexer,
10597                                           CPP_COLON)
10598               && cp_lexer_next_token_is_not (parser->lexer,
10599                                              CPP_SCOPE)
10600               && cp_lexer_next_token_is_not (parser->lexer,
10601                                              CPP_CLOSE_PAREN))
10602             outputs = cp_parser_asm_operand_list (parser);
10603         }
10604       /* If the next token is `::', there are no outputs, and the
10605          next token is the beginning of the inputs.  */
10606       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10607         /* The inputs are coming next.  */
10608         inputs_p = true;
10609
10610       /* Look for inputs.  */
10611       if (inputs_p
10612           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10613         {
10614           /* Consume the `:' or `::'.  */
10615           cp_lexer_consume_token (parser->lexer);
10616           /* Parse the output-operands.  */
10617           if (cp_lexer_next_token_is_not (parser->lexer,
10618                                           CPP_COLON)
10619               && cp_lexer_next_token_is_not (parser->lexer,
10620                                              CPP_CLOSE_PAREN))
10621             inputs = cp_parser_asm_operand_list (parser);
10622         }
10623       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10624         /* The clobbers are coming next.  */
10625         clobbers_p = true;
10626
10627       /* Look for clobbers.  */
10628       if (clobbers_p
10629           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10630         {
10631           /* Consume the `:' or `::'.  */
10632           cp_lexer_consume_token (parser->lexer);
10633           /* Parse the clobbers.  */
10634           if (cp_lexer_next_token_is_not (parser->lexer,
10635                                           CPP_CLOSE_PAREN))
10636             clobbers = cp_parser_asm_clobber_list (parser);
10637         }
10638     }
10639   /* Look for the closing `)'.  */
10640   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10641     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10642                                            /*consume_paren=*/true);
10643   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10644
10645   /* Create the ASM_EXPR.  */
10646   if (at_function_scope_p ())
10647     {
10648       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10649                                   inputs, clobbers);
10650       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10651       if (!extended_p)
10652         {
10653           tree temp = asm_stmt;
10654           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10655             temp = TREE_OPERAND (temp, 0);
10656
10657           ASM_INPUT_P (temp) = 1;
10658         }
10659     }
10660   else
10661     assemble_asm (string);
10662 }
10663
10664 /* Declarators [gram.dcl.decl] */
10665
10666 /* Parse an init-declarator.
10667
10668    init-declarator:
10669      declarator initializer [opt]
10670
10671    GNU Extension:
10672
10673    init-declarator:
10674      declarator asm-specification [opt] attributes [opt] initializer [opt]
10675
10676    function-definition:
10677      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10678        function-body
10679      decl-specifier-seq [opt] declarator function-try-block
10680
10681    GNU Extension:
10682
10683    function-definition:
10684      __extension__ function-definition
10685
10686    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10687    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10688    then this declarator appears in a class scope.  The new DECL created
10689    by this declarator is returned.
10690
10691    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10692    for a function-definition here as well.  If the declarator is a
10693    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10694    be TRUE upon return.  By that point, the function-definition will
10695    have been completely parsed.
10696
10697    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10698    is FALSE.  */
10699
10700 static tree
10701 cp_parser_init_declarator (cp_parser* parser,
10702                            cp_decl_specifier_seq *decl_specifiers,
10703                            bool function_definition_allowed_p,
10704                            bool member_p,
10705                            int declares_class_or_enum,
10706                            bool* function_definition_p)
10707 {
10708   cp_token *token;
10709   cp_declarator *declarator;
10710   tree prefix_attributes;
10711   tree attributes;
10712   tree asm_specification;
10713   tree initializer;
10714   tree decl = NULL_TREE;
10715   tree scope;
10716   bool is_initialized;
10717   bool is_parenthesized_init;
10718   bool is_non_constant_init;
10719   int ctor_dtor_or_conv_p;
10720   bool friend_p;
10721   tree pushed_scope = NULL;
10722
10723   /* Gather the attributes that were provided with the
10724      decl-specifiers.  */
10725   prefix_attributes = decl_specifiers->attributes;
10726
10727   /* Assume that this is not the declarator for a function
10728      definition.  */
10729   if (function_definition_p)
10730     *function_definition_p = false;
10731
10732   /* Defer access checks while parsing the declarator; we cannot know
10733      what names are accessible until we know what is being
10734      declared.  */
10735   resume_deferring_access_checks ();
10736
10737   /* Parse the declarator.  */
10738   declarator
10739     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10740                             &ctor_dtor_or_conv_p,
10741                             /*parenthesized_p=*/NULL,
10742                             /*member_p=*/false);
10743   /* Gather up the deferred checks.  */
10744   stop_deferring_access_checks ();
10745
10746   /* If the DECLARATOR was erroneous, there's no need to go
10747      further.  */
10748   if (declarator == cp_error_declarator)
10749     return error_mark_node;
10750
10751   if (declares_class_or_enum & 2)
10752     cp_parser_check_for_definition_in_return_type (declarator,
10753                                                    decl_specifiers->type);
10754
10755   /* Figure out what scope the entity declared by the DECLARATOR is
10756      located in.  `grokdeclarator' sometimes changes the scope, so
10757      we compute it now.  */
10758   scope = get_scope_of_declarator (declarator);
10759
10760   /* If we're allowing GNU extensions, look for an asm-specification
10761      and attributes.  */
10762   if (cp_parser_allow_gnu_extensions_p (parser))
10763     {
10764       /* Look for an asm-specification.  */
10765       asm_specification = cp_parser_asm_specification_opt (parser);
10766       /* And attributes.  */
10767       attributes = cp_parser_attributes_opt (parser);
10768     }
10769   else
10770     {
10771       asm_specification = NULL_TREE;
10772       attributes = NULL_TREE;
10773     }
10774
10775   /* Peek at the next token.  */
10776   token = cp_lexer_peek_token (parser->lexer);
10777   /* Check to see if the token indicates the start of a
10778      function-definition.  */
10779   if (cp_parser_token_starts_function_definition_p (token))
10780     {
10781       if (!function_definition_allowed_p)
10782         {
10783           /* If a function-definition should not appear here, issue an
10784              error message.  */
10785           cp_parser_error (parser,
10786                            "a function-definition is not allowed here");
10787           return error_mark_node;
10788         }
10789       else
10790         {
10791           /* Neither attributes nor an asm-specification are allowed
10792              on a function-definition.  */
10793           if (asm_specification)
10794             error ("an asm-specification is not allowed on a function-definition");
10795           if (attributes)
10796             error ("attributes are not allowed on a function-definition");
10797           /* This is a function-definition.  */
10798           *function_definition_p = true;
10799
10800           /* Parse the function definition.  */
10801           if (member_p)
10802             decl = cp_parser_save_member_function_body (parser,
10803                                                         decl_specifiers,
10804                                                         declarator,
10805                                                         prefix_attributes);
10806           else
10807             decl
10808               = (cp_parser_function_definition_from_specifiers_and_declarator
10809                  (parser, decl_specifiers, prefix_attributes, declarator));
10810
10811           return decl;
10812         }
10813     }
10814
10815   /* [dcl.dcl]
10816
10817      Only in function declarations for constructors, destructors, and
10818      type conversions can the decl-specifier-seq be omitted.
10819
10820      We explicitly postpone this check past the point where we handle
10821      function-definitions because we tolerate function-definitions
10822      that are missing their return types in some modes.  */
10823   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10824     {
10825       cp_parser_error (parser,
10826                        "expected constructor, destructor, or type conversion");
10827       return error_mark_node;
10828     }
10829
10830   /* An `=' or an `(' indicates an initializer.  */
10831   is_initialized = (token->type == CPP_EQ
10832                      || token->type == CPP_OPEN_PAREN);
10833   /* If the init-declarator isn't initialized and isn't followed by a
10834      `,' or `;', it's not a valid init-declarator.  */
10835   if (!is_initialized
10836       && token->type != CPP_COMMA
10837       && token->type != CPP_SEMICOLON)
10838     {
10839       cp_parser_error (parser, "expected initializer");
10840       return error_mark_node;
10841     }
10842
10843   /* Because start_decl has side-effects, we should only call it if we
10844      know we're going ahead.  By this point, we know that we cannot
10845      possibly be looking at any other construct.  */
10846   cp_parser_commit_to_tentative_parse (parser);
10847
10848   /* If the decl specifiers were bad, issue an error now that we're
10849      sure this was intended to be a declarator.  Then continue
10850      declaring the variable(s), as int, to try to cut down on further
10851      errors.  */
10852   if (decl_specifiers->any_specifiers_p
10853       && decl_specifiers->type == error_mark_node)
10854     {
10855       cp_parser_error (parser, "invalid type in declaration");
10856       decl_specifiers->type = integer_type_node;
10857     }
10858
10859   /* Check to see whether or not this declaration is a friend.  */
10860   friend_p = cp_parser_friend_p (decl_specifiers);
10861
10862   /* Check that the number of template-parameter-lists is OK.  */
10863   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10864     return error_mark_node;
10865
10866   /* Enter the newly declared entry in the symbol table.  If we're
10867      processing a declaration in a class-specifier, we wait until
10868      after processing the initializer.  */
10869   if (!member_p)
10870     {
10871       if (parser->in_unbraced_linkage_specification_p)
10872         {
10873           decl_specifiers->storage_class = sc_extern;
10874           have_extern_spec = false;
10875         }
10876       decl = start_decl (declarator, decl_specifiers,
10877                          is_initialized, attributes, prefix_attributes,
10878                          &pushed_scope);
10879     }
10880   else if (scope)
10881     /* Enter the SCOPE.  That way unqualified names appearing in the
10882        initializer will be looked up in SCOPE.  */
10883     pushed_scope = push_scope (scope);
10884
10885   /* Perform deferred access control checks, now that we know in which
10886      SCOPE the declared entity resides.  */
10887   if (!member_p && decl)
10888     {
10889       tree saved_current_function_decl = NULL_TREE;
10890
10891       /* If the entity being declared is a function, pretend that we
10892          are in its scope.  If it is a `friend', it may have access to
10893          things that would not otherwise be accessible.  */
10894       if (TREE_CODE (decl) == FUNCTION_DECL)
10895         {
10896           saved_current_function_decl = current_function_decl;
10897           current_function_decl = decl;
10898         }
10899
10900       /* Perform the access control checks for the declarator and the
10901          the decl-specifiers.  */
10902       perform_deferred_access_checks ();
10903
10904       /* Restore the saved value.  */
10905       if (TREE_CODE (decl) == FUNCTION_DECL)
10906         current_function_decl = saved_current_function_decl;
10907     }
10908
10909   /* Parse the initializer.  */
10910   if (is_initialized)
10911     initializer = cp_parser_initializer (parser,
10912                                          &is_parenthesized_init,
10913                                          &is_non_constant_init);
10914   else
10915     {
10916       initializer = NULL_TREE;
10917       is_parenthesized_init = false;
10918       is_non_constant_init = true;
10919     }
10920
10921   /* The old parser allows attributes to appear after a parenthesized
10922      initializer.  Mark Mitchell proposed removing this functionality
10923      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10924      attributes -- but ignores them.  */
10925   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10926     if (cp_parser_attributes_opt (parser))
10927       warning (OPT_Wattributes,
10928                "attributes after parenthesized initializer ignored");
10929
10930   /* For an in-class declaration, use `grokfield' to create the
10931      declaration.  */
10932   if (member_p)
10933     {
10934       if (pushed_scope)
10935         {
10936           pop_scope (pushed_scope);
10937           pushed_scope = false;
10938         }
10939       decl = grokfield (declarator, decl_specifiers,
10940                         initializer, /*asmspec=*/NULL_TREE,
10941                         /*attributes=*/NULL_TREE);
10942       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10943         cp_parser_save_default_args (parser, decl);
10944     }
10945
10946   /* Finish processing the declaration.  But, skip friend
10947      declarations.  */
10948   if (!friend_p && decl && decl != error_mark_node)
10949     {
10950       cp_finish_decl (decl,
10951                       initializer,
10952                       asm_specification,
10953                       /* If the initializer is in parentheses, then this is
10954                          a direct-initialization, which means that an
10955                          `explicit' constructor is OK.  Otherwise, an
10956                          `explicit' constructor cannot be used.  */
10957                       ((is_parenthesized_init || !is_initialized)
10958                      ? 0 : LOOKUP_ONLYCONVERTING));
10959     }
10960   if (!friend_p && pushed_scope)
10961     pop_scope (pushed_scope);
10962
10963   /* Remember whether or not variables were initialized by
10964      constant-expressions.  */
10965   if (decl && TREE_CODE (decl) == VAR_DECL
10966       && is_initialized && !is_non_constant_init)
10967     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10968
10969   return decl;
10970 }
10971
10972 /* Parse a declarator.
10973
10974    declarator:
10975      direct-declarator
10976      ptr-operator declarator
10977
10978    abstract-declarator:
10979      ptr-operator abstract-declarator [opt]
10980      direct-abstract-declarator
10981
10982    GNU Extensions:
10983
10984    declarator:
10985      attributes [opt] direct-declarator
10986      attributes [opt] ptr-operator declarator
10987
10988    abstract-declarator:
10989      attributes [opt] ptr-operator abstract-declarator [opt]
10990      attributes [opt] direct-abstract-declarator
10991
10992    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10993    detect constructor, destructor or conversion operators. It is set
10994    to -1 if the declarator is a name, and +1 if it is a
10995    function. Otherwise it is set to zero. Usually you just want to
10996    test for >0, but internally the negative value is used.
10997
10998    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10999    a decl-specifier-seq unless it declares a constructor, destructor,
11000    or conversion.  It might seem that we could check this condition in
11001    semantic analysis, rather than parsing, but that makes it difficult
11002    to handle something like `f()'.  We want to notice that there are
11003    no decl-specifiers, and therefore realize that this is an
11004    expression, not a declaration.)
11005
11006    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11007    the declarator is a direct-declarator of the form "(...)".
11008
11009    MEMBER_P is true iff this declarator is a member-declarator.  */
11010
11011 static cp_declarator *
11012 cp_parser_declarator (cp_parser* parser,
11013                       cp_parser_declarator_kind dcl_kind,
11014                       int* ctor_dtor_or_conv_p,
11015                       bool* parenthesized_p,
11016                       bool member_p)
11017 {
11018   cp_token *token;
11019   cp_declarator *declarator;
11020   enum tree_code code;
11021   cp_cv_quals cv_quals;
11022   tree class_type;
11023   tree attributes = NULL_TREE;
11024
11025   /* Assume this is not a constructor, destructor, or type-conversion
11026      operator.  */
11027   if (ctor_dtor_or_conv_p)
11028     *ctor_dtor_or_conv_p = 0;
11029
11030   if (cp_parser_allow_gnu_extensions_p (parser))
11031     attributes = cp_parser_attributes_opt (parser);
11032
11033   /* Peek at the next token.  */
11034   token = cp_lexer_peek_token (parser->lexer);
11035
11036   /* Check for the ptr-operator production.  */
11037   cp_parser_parse_tentatively (parser);
11038   /* Parse the ptr-operator.  */
11039   code = cp_parser_ptr_operator (parser,
11040                                  &class_type,
11041                                  &cv_quals);
11042   /* If that worked, then we have a ptr-operator.  */
11043   if (cp_parser_parse_definitely (parser))
11044     {
11045       /* If a ptr-operator was found, then this declarator was not
11046          parenthesized.  */
11047       if (parenthesized_p)
11048         *parenthesized_p = true;
11049       /* The dependent declarator is optional if we are parsing an
11050          abstract-declarator.  */
11051       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11052         cp_parser_parse_tentatively (parser);
11053
11054       /* Parse the dependent declarator.  */
11055       declarator = cp_parser_declarator (parser, dcl_kind,
11056                                          /*ctor_dtor_or_conv_p=*/NULL,
11057                                          /*parenthesized_p=*/NULL,
11058                                          /*member_p=*/false);
11059
11060       /* If we are parsing an abstract-declarator, we must handle the
11061          case where the dependent declarator is absent.  */
11062       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11063           && !cp_parser_parse_definitely (parser))
11064         declarator = NULL;
11065
11066       /* Build the representation of the ptr-operator.  */
11067       if (class_type)
11068         declarator = make_ptrmem_declarator (cv_quals,
11069                                              class_type,
11070                                              declarator);
11071       else if (code == INDIRECT_REF)
11072         declarator = make_pointer_declarator (cv_quals, declarator);
11073       else
11074         declarator = make_reference_declarator (cv_quals, declarator);
11075     }
11076   /* Everything else is a direct-declarator.  */
11077   else
11078     {
11079       if (parenthesized_p)
11080         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11081                                                    CPP_OPEN_PAREN);
11082       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11083                                                 ctor_dtor_or_conv_p,
11084                                                 member_p);
11085     }
11086
11087   if (attributes && declarator != cp_error_declarator)
11088     declarator->attributes = attributes;
11089
11090   return declarator;
11091 }
11092
11093 /* Parse a direct-declarator or direct-abstract-declarator.
11094
11095    direct-declarator:
11096      declarator-id
11097      direct-declarator ( parameter-declaration-clause )
11098        cv-qualifier-seq [opt]
11099        exception-specification [opt]
11100      direct-declarator [ constant-expression [opt] ]
11101      ( declarator )
11102
11103    direct-abstract-declarator:
11104      direct-abstract-declarator [opt]
11105        ( parameter-declaration-clause )
11106        cv-qualifier-seq [opt]
11107        exception-specification [opt]
11108      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11109      ( abstract-declarator )
11110
11111    Returns a representation of the declarator.  DCL_KIND is
11112    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11113    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11114    we are parsing a direct-declarator.  It is
11115    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11116    of ambiguity we prefer an abstract declarator, as per
11117    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11118    cp_parser_declarator.  */
11119
11120 static cp_declarator *
11121 cp_parser_direct_declarator (cp_parser* parser,
11122                              cp_parser_declarator_kind dcl_kind,
11123                              int* ctor_dtor_or_conv_p,
11124                              bool member_p)
11125 {
11126   cp_token *token;
11127   cp_declarator *declarator = NULL;
11128   tree scope = NULL_TREE;
11129   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11130   bool saved_in_declarator_p = parser->in_declarator_p;
11131   bool first = true;
11132   tree pushed_scope = NULL_TREE;
11133
11134   while (true)
11135     {
11136       /* Peek at the next token.  */
11137       token = cp_lexer_peek_token (parser->lexer);
11138       if (token->type == CPP_OPEN_PAREN)
11139         {
11140           /* This is either a parameter-declaration-clause, or a
11141              parenthesized declarator. When we know we are parsing a
11142              named declarator, it must be a parenthesized declarator
11143              if FIRST is true. For instance, `(int)' is a
11144              parameter-declaration-clause, with an omitted
11145              direct-abstract-declarator. But `((*))', is a
11146              parenthesized abstract declarator. Finally, when T is a
11147              template parameter `(T)' is a
11148              parameter-declaration-clause, and not a parenthesized
11149              named declarator.
11150
11151              We first try and parse a parameter-declaration-clause,
11152              and then try a nested declarator (if FIRST is true).
11153
11154              It is not an error for it not to be a
11155              parameter-declaration-clause, even when FIRST is
11156              false. Consider,
11157
11158                int i (int);
11159                int i (3);
11160
11161              The first is the declaration of a function while the
11162              second is a the definition of a variable, including its
11163              initializer.
11164
11165              Having seen only the parenthesis, we cannot know which of
11166              these two alternatives should be selected.  Even more
11167              complex are examples like:
11168
11169                int i (int (a));
11170                int i (int (3));
11171
11172              The former is a function-declaration; the latter is a
11173              variable initialization.
11174
11175              Thus again, we try a parameter-declaration-clause, and if
11176              that fails, we back out and return.  */
11177
11178           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11179             {
11180               cp_parameter_declarator *params;
11181               unsigned saved_num_template_parameter_lists;
11182
11183               /* In a member-declarator, the only valid interpretation
11184                  of a parenthesis is the start of a
11185                  parameter-declaration-clause.  (It is invalid to
11186                  initialize a static data member with a parenthesized
11187                  initializer; only the "=" form of initialization is
11188                  permitted.)  */
11189               if (!member_p)
11190                 cp_parser_parse_tentatively (parser);
11191
11192               /* Consume the `('.  */
11193               cp_lexer_consume_token (parser->lexer);
11194               if (first)
11195                 {
11196                   /* If this is going to be an abstract declarator, we're
11197                      in a declarator and we can't have default args.  */
11198                   parser->default_arg_ok_p = false;
11199                   parser->in_declarator_p = true;
11200                 }
11201
11202               /* Inside the function parameter list, surrounding
11203                  template-parameter-lists do not apply.  */
11204               saved_num_template_parameter_lists
11205                 = parser->num_template_parameter_lists;
11206               parser->num_template_parameter_lists = 0;
11207
11208               /* Parse the parameter-declaration-clause.  */
11209               params = cp_parser_parameter_declaration_clause (parser);
11210
11211               parser->num_template_parameter_lists
11212                 = saved_num_template_parameter_lists;
11213
11214               /* If all went well, parse the cv-qualifier-seq and the
11215                  exception-specification.  */
11216               if (member_p || cp_parser_parse_definitely (parser))
11217                 {
11218                   cp_cv_quals cv_quals;
11219                   tree exception_specification;
11220
11221                   if (ctor_dtor_or_conv_p)
11222                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11223                   first = false;
11224                   /* Consume the `)'.  */
11225                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11226
11227                   /* Parse the cv-qualifier-seq.  */
11228                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11229                   /* And the exception-specification.  */
11230                   exception_specification
11231                     = cp_parser_exception_specification_opt (parser);
11232
11233                   /* Create the function-declarator.  */
11234                   declarator = make_call_declarator (declarator,
11235                                                      params,
11236                                                      cv_quals,
11237                                                      exception_specification);
11238                   /* Any subsequent parameter lists are to do with
11239                      return type, so are not those of the declared
11240                      function.  */
11241                   parser->default_arg_ok_p = false;
11242
11243                   /* Repeat the main loop.  */
11244                   continue;
11245                 }
11246             }
11247
11248           /* If this is the first, we can try a parenthesized
11249              declarator.  */
11250           if (first)
11251             {
11252               bool saved_in_type_id_in_expr_p;
11253
11254               parser->default_arg_ok_p = saved_default_arg_ok_p;
11255               parser->in_declarator_p = saved_in_declarator_p;
11256
11257               /* Consume the `('.  */
11258               cp_lexer_consume_token (parser->lexer);
11259               /* Parse the nested declarator.  */
11260               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11261               parser->in_type_id_in_expr_p = true;
11262               declarator
11263                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11264                                         /*parenthesized_p=*/NULL,
11265                                         member_p);
11266               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11267               first = false;
11268               /* Expect a `)'.  */
11269               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11270                 declarator = cp_error_declarator;
11271               if (declarator == cp_error_declarator)
11272                 break;
11273
11274               goto handle_declarator;
11275             }
11276           /* Otherwise, we must be done.  */
11277           else
11278             break;
11279         }
11280       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11281                && token->type == CPP_OPEN_SQUARE)
11282         {
11283           /* Parse an array-declarator.  */
11284           tree bounds;
11285
11286           if (ctor_dtor_or_conv_p)
11287             *ctor_dtor_or_conv_p = 0;
11288
11289           first = false;
11290           parser->default_arg_ok_p = false;
11291           parser->in_declarator_p = true;
11292           /* Consume the `['.  */
11293           cp_lexer_consume_token (parser->lexer);
11294           /* Peek at the next token.  */
11295           token = cp_lexer_peek_token (parser->lexer);
11296           /* If the next token is `]', then there is no
11297              constant-expression.  */
11298           if (token->type != CPP_CLOSE_SQUARE)
11299             {
11300               bool non_constant_p;
11301
11302               bounds
11303                 = cp_parser_constant_expression (parser,
11304                                                  /*allow_non_constant=*/true,
11305                                                  &non_constant_p);
11306               if (!non_constant_p)
11307                 bounds = fold_non_dependent_expr (bounds);
11308               /* Normally, the array bound must be an integral constant
11309                  expression.  However, as an extension, we allow VLAs
11310                  in function scopes.  */
11311               else if (!at_function_scope_p ())
11312                 {
11313                   error ("array bound is not an integer constant");
11314                   bounds = error_mark_node;
11315                 }
11316             }
11317           else
11318             bounds = NULL_TREE;
11319           /* Look for the closing `]'.  */
11320           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11321             {
11322               declarator = cp_error_declarator;
11323               break;
11324             }
11325
11326           declarator = make_array_declarator (declarator, bounds);
11327         }
11328       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11329         {
11330           tree qualifying_scope;
11331           tree unqualified_name;
11332
11333           /* Parse a declarator-id */
11334           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11335             cp_parser_parse_tentatively (parser);
11336           unqualified_name = cp_parser_declarator_id (parser);
11337           qualifying_scope = parser->scope;
11338           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11339             {
11340               if (!cp_parser_parse_definitely (parser))
11341                 unqualified_name = error_mark_node;
11342               else if (qualifying_scope
11343                        || (TREE_CODE (unqualified_name)
11344                            != IDENTIFIER_NODE))
11345                 {
11346                   cp_parser_error (parser, "expected unqualified-id");
11347                   unqualified_name = error_mark_node;
11348                 }
11349             }
11350
11351           if (unqualified_name == error_mark_node)
11352             {
11353               declarator = cp_error_declarator;
11354               break;
11355             }
11356
11357           if (qualifying_scope && at_namespace_scope_p ()
11358               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11359             {
11360               /* In the declaration of a member of a template class
11361                  outside of the class itself, the SCOPE will sometimes
11362                  be a TYPENAME_TYPE.  For example, given:
11363
11364                  template <typename T>
11365                  int S<T>::R::i = 3;
11366
11367                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11368                  this context, we must resolve S<T>::R to an ordinary
11369                  type, rather than a typename type.
11370
11371                  The reason we normally avoid resolving TYPENAME_TYPEs
11372                  is that a specialization of `S' might render
11373                  `S<T>::R' not a type.  However, if `S' is
11374                  specialized, then this `i' will not be used, so there
11375                  is no harm in resolving the types here.  */
11376               tree type;
11377
11378               /* Resolve the TYPENAME_TYPE.  */
11379               type = resolve_typename_type (qualifying_scope,
11380                                             /*only_current_p=*/false);
11381               /* If that failed, the declarator is invalid.  */
11382               if (type == error_mark_node)
11383                 error ("%<%T::%D%> is not a type",
11384                        TYPE_CONTEXT (qualifying_scope),
11385                        TYPE_IDENTIFIER (qualifying_scope));
11386               qualifying_scope = type;
11387             }
11388
11389           declarator = make_id_declarator (qualifying_scope,
11390                                            unqualified_name);
11391           declarator->id_loc = token->location;
11392           if (unqualified_name)
11393             {
11394               tree class_type;
11395
11396               if (qualifying_scope
11397                   && CLASS_TYPE_P (qualifying_scope))
11398                 class_type = qualifying_scope;
11399               else
11400                 class_type = current_class_type;
11401
11402               if (class_type)
11403                 {
11404                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11405                     declarator->u.id.sfk = sfk_destructor;
11406                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11407                     declarator->u.id.sfk = sfk_conversion;
11408                   else if (/* There's no way to declare a constructor
11409                               for an anonymous type, even if the type
11410                               got a name for linkage purposes.  */
11411                            !TYPE_WAS_ANONYMOUS (class_type)
11412                            && (constructor_name_p (unqualified_name,
11413                                                    class_type)
11414                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11415                                    && (same_type_p
11416                                        (TREE_TYPE (unqualified_name),
11417                                         class_type)))))
11418                     declarator->u.id.sfk = sfk_constructor;
11419
11420                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11421                     *ctor_dtor_or_conv_p = -1;
11422                   if (qualifying_scope
11423                       && TREE_CODE (unqualified_name) == TYPE_DECL
11424                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11425                     {
11426                       error ("invalid use of constructor as a template");
11427                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11428                               "the constructor in a qualified name",
11429                               class_type,
11430                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11431                               class_type, class_type);
11432                     }
11433                 }
11434             }
11435
11436         handle_declarator:;
11437           scope = get_scope_of_declarator (declarator);
11438           if (scope)
11439             /* Any names that appear after the declarator-id for a
11440                member are looked up in the containing scope.  */
11441             pushed_scope = push_scope (scope);
11442           parser->in_declarator_p = true;
11443           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11444               || (declarator && declarator->kind == cdk_id))
11445             /* Default args are only allowed on function
11446                declarations.  */
11447             parser->default_arg_ok_p = saved_default_arg_ok_p;
11448           else
11449             parser->default_arg_ok_p = false;
11450
11451           first = false;
11452         }
11453       /* We're done.  */
11454       else
11455         break;
11456     }
11457
11458   /* For an abstract declarator, we might wind up with nothing at this
11459      point.  That's an error; the declarator is not optional.  */
11460   if (!declarator)
11461     cp_parser_error (parser, "expected declarator");
11462
11463   /* If we entered a scope, we must exit it now.  */
11464   if (pushed_scope)
11465     pop_scope (pushed_scope);
11466
11467   parser->default_arg_ok_p = saved_default_arg_ok_p;
11468   parser->in_declarator_p = saved_in_declarator_p;
11469
11470   return declarator;
11471 }
11472
11473 /* Parse a ptr-operator.
11474
11475    ptr-operator:
11476      * cv-qualifier-seq [opt]
11477      &
11478      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11479
11480    GNU Extension:
11481
11482    ptr-operator:
11483      & cv-qualifier-seq [opt]
11484
11485    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11486    Returns ADDR_EXPR if a reference was used.  In the case of a
11487    pointer-to-member, *TYPE is filled in with the TYPE containing the
11488    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11489    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11490    ERROR_MARK if an error occurred.  */
11491
11492 static enum tree_code
11493 cp_parser_ptr_operator (cp_parser* parser,
11494                         tree* type,
11495                         cp_cv_quals *cv_quals)
11496 {
11497   enum tree_code code = ERROR_MARK;
11498   cp_token *token;
11499
11500   /* Assume that it's not a pointer-to-member.  */
11501   *type = NULL_TREE;
11502   /* And that there are no cv-qualifiers.  */
11503   *cv_quals = TYPE_UNQUALIFIED;
11504
11505   /* Peek at the next token.  */
11506   token = cp_lexer_peek_token (parser->lexer);
11507   /* If it's a `*' or `&' we have a pointer or reference.  */
11508   if (token->type == CPP_MULT || token->type == CPP_AND)
11509     {
11510       /* Remember which ptr-operator we were processing.  */
11511       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11512
11513       /* Consume the `*' or `&'.  */
11514       cp_lexer_consume_token (parser->lexer);
11515
11516       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11517          `&', if we are allowing GNU extensions.  (The only qualifier
11518          that can legally appear after `&' is `restrict', but that is
11519          enforced during semantic analysis.  */
11520       if (code == INDIRECT_REF
11521           || cp_parser_allow_gnu_extensions_p (parser))
11522         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11523     }
11524   else
11525     {
11526       /* Try the pointer-to-member case.  */
11527       cp_parser_parse_tentatively (parser);
11528       /* Look for the optional `::' operator.  */
11529       cp_parser_global_scope_opt (parser,
11530                                   /*current_scope_valid_p=*/false);
11531       /* Look for the nested-name specifier.  */
11532       cp_parser_nested_name_specifier (parser,
11533                                        /*typename_keyword_p=*/false,
11534                                        /*check_dependency_p=*/true,
11535                                        /*type_p=*/false,
11536                                        /*is_declaration=*/false);
11537       /* If we found it, and the next token is a `*', then we are
11538          indeed looking at a pointer-to-member operator.  */
11539       if (!cp_parser_error_occurred (parser)
11540           && cp_parser_require (parser, CPP_MULT, "`*'"))
11541         {
11542           /* The type of which the member is a member is given by the
11543              current SCOPE.  */
11544           *type = parser->scope;
11545           /* The next name will not be qualified.  */
11546           parser->scope = NULL_TREE;
11547           parser->qualifying_scope = NULL_TREE;
11548           parser->object_scope = NULL_TREE;
11549           /* Indicate that the `*' operator was used.  */
11550           code = INDIRECT_REF;
11551           /* Look for the optional cv-qualifier-seq.  */
11552           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11553         }
11554       /* If that didn't work we don't have a ptr-operator.  */
11555       if (!cp_parser_parse_definitely (parser))
11556         cp_parser_error (parser, "expected ptr-operator");
11557     }
11558
11559   return code;
11560 }
11561
11562 /* Parse an (optional) cv-qualifier-seq.
11563
11564    cv-qualifier-seq:
11565      cv-qualifier cv-qualifier-seq [opt]
11566
11567    cv-qualifier:
11568      const
11569      volatile
11570
11571    GNU Extension:
11572
11573    cv-qualifier:
11574      __restrict__
11575
11576    Returns a bitmask representing the cv-qualifiers.  */
11577
11578 static cp_cv_quals
11579 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11580 {
11581   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11582
11583   while (true)
11584     {
11585       cp_token *token;
11586       cp_cv_quals cv_qualifier;
11587
11588       /* Peek at the next token.  */
11589       token = cp_lexer_peek_token (parser->lexer);
11590       /* See if it's a cv-qualifier.  */
11591       switch (token->keyword)
11592         {
11593         case RID_CONST:
11594           cv_qualifier = TYPE_QUAL_CONST;
11595           break;
11596
11597         case RID_VOLATILE:
11598           cv_qualifier = TYPE_QUAL_VOLATILE;
11599           break;
11600
11601         case RID_RESTRICT:
11602           cv_qualifier = TYPE_QUAL_RESTRICT;
11603           break;
11604
11605         default:
11606           cv_qualifier = TYPE_UNQUALIFIED;
11607           break;
11608         }
11609
11610       if (!cv_qualifier)
11611         break;
11612
11613       if (cv_quals & cv_qualifier)
11614         {
11615           error ("duplicate cv-qualifier");
11616           cp_lexer_purge_token (parser->lexer);
11617         }
11618       else
11619         {
11620           cp_lexer_consume_token (parser->lexer);
11621           cv_quals |= cv_qualifier;
11622         }
11623     }
11624
11625   return cv_quals;
11626 }
11627
11628 /* Parse a declarator-id.
11629
11630    declarator-id:
11631      id-expression
11632      :: [opt] nested-name-specifier [opt] type-name
11633
11634    In the `id-expression' case, the value returned is as for
11635    cp_parser_id_expression if the id-expression was an unqualified-id.
11636    If the id-expression was a qualified-id, then a SCOPE_REF is
11637    returned.  The first operand is the scope (either a NAMESPACE_DECL
11638    or TREE_TYPE), but the second is still just a representation of an
11639    unqualified-id.  */
11640
11641 static tree
11642 cp_parser_declarator_id (cp_parser* parser)
11643 {
11644   /* The expression must be an id-expression.  Assume that qualified
11645      names are the names of types so that:
11646
11647        template <class T>
11648        int S<T>::R::i = 3;
11649
11650      will work; we must treat `S<T>::R' as the name of a type.
11651      Similarly, assume that qualified names are templates, where
11652      required, so that:
11653
11654        template <class T>
11655        int S<T>::R<T>::i = 3;
11656
11657      will work, too.  */
11658   return cp_parser_id_expression (parser,
11659                                   /*template_keyword_p=*/false,
11660                                   /*check_dependency_p=*/false,
11661                                   /*template_p=*/NULL,
11662                                   /*declarator_p=*/true);
11663 }
11664
11665 /* Parse a type-id.
11666
11667    type-id:
11668      type-specifier-seq abstract-declarator [opt]
11669
11670    Returns the TYPE specified.  */
11671
11672 static tree
11673 cp_parser_type_id (cp_parser* parser)
11674 {
11675   cp_decl_specifier_seq type_specifier_seq;
11676   cp_declarator *abstract_declarator;
11677
11678   /* Parse the type-specifier-seq.  */
11679   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11680                                 &type_specifier_seq);
11681   if (type_specifier_seq.type == error_mark_node)
11682     return error_mark_node;
11683
11684   /* There might or might not be an abstract declarator.  */
11685   cp_parser_parse_tentatively (parser);
11686   /* Look for the declarator.  */
11687   abstract_declarator
11688     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11689                             /*parenthesized_p=*/NULL,
11690                             /*member_p=*/false);
11691   /* Check to see if there really was a declarator.  */
11692   if (!cp_parser_parse_definitely (parser))
11693     abstract_declarator = NULL;
11694
11695   return groktypename (&type_specifier_seq, abstract_declarator);
11696 }
11697
11698 /* Parse a type-specifier-seq.
11699
11700    type-specifier-seq:
11701      type-specifier type-specifier-seq [opt]
11702
11703    GNU extension:
11704
11705    type-specifier-seq:
11706      attributes type-specifier-seq [opt]
11707
11708    If IS_CONDITION is true, we are at the start of a "condition",
11709    e.g., we've just seen "if (".
11710
11711    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11712
11713 static void
11714 cp_parser_type_specifier_seq (cp_parser* parser,
11715                               bool is_condition,
11716                               cp_decl_specifier_seq *type_specifier_seq)
11717 {
11718   bool seen_type_specifier = false;
11719   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11720
11721   /* Clear the TYPE_SPECIFIER_SEQ.  */
11722   clear_decl_specs (type_specifier_seq);
11723
11724   /* Parse the type-specifiers and attributes.  */
11725   while (true)
11726     {
11727       tree type_specifier;
11728       bool is_cv_qualifier;
11729
11730       /* Check for attributes first.  */
11731       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11732         {
11733           type_specifier_seq->attributes =
11734             chainon (type_specifier_seq->attributes,
11735                      cp_parser_attributes_opt (parser));
11736           continue;
11737         }
11738
11739       /* Look for the type-specifier.  */
11740       type_specifier = cp_parser_type_specifier (parser,
11741                                                  flags,
11742                                                  type_specifier_seq,
11743                                                  /*is_declaration=*/false,
11744                                                  NULL,
11745                                                  &is_cv_qualifier);
11746       if (!type_specifier)
11747         {
11748           /* If the first type-specifier could not be found, this is not a
11749              type-specifier-seq at all.  */
11750           if (!seen_type_specifier)
11751             {
11752               cp_parser_error (parser, "expected type-specifier");
11753               type_specifier_seq->type = error_mark_node;
11754               return;
11755             }
11756           /* If subsequent type-specifiers could not be found, the
11757              type-specifier-seq is complete.  */
11758           break;
11759         }
11760
11761       seen_type_specifier = true;
11762       /* The standard says that a condition can be:
11763
11764             type-specifier-seq declarator = assignment-expression
11765
11766          However, given:
11767
11768            struct S {};
11769            if (int S = ...)
11770
11771          we should treat the "S" as a declarator, not as a
11772          type-specifier.  The standard doesn't say that explicitly for
11773          type-specifier-seq, but it does say that for
11774          decl-specifier-seq in an ordinary declaration.  Perhaps it
11775          would be clearer just to allow a decl-specifier-seq here, and
11776          then add a semantic restriction that if any decl-specifiers
11777          that are not type-specifiers appear, the program is invalid.  */
11778       if (is_condition && !is_cv_qualifier)
11779         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11780     }
11781
11782   return;
11783 }
11784
11785 /* Parse a parameter-declaration-clause.
11786
11787    parameter-declaration-clause:
11788      parameter-declaration-list [opt] ... [opt]
11789      parameter-declaration-list , ...
11790
11791    Returns a representation for the parameter declarations.  A return
11792    value of NULL indicates a parameter-declaration-clause consisting
11793    only of an ellipsis.  */
11794
11795 static cp_parameter_declarator *
11796 cp_parser_parameter_declaration_clause (cp_parser* parser)
11797 {
11798   cp_parameter_declarator *parameters;
11799   cp_token *token;
11800   bool ellipsis_p;
11801   bool is_error;
11802
11803   /* Peek at the next token.  */
11804   token = cp_lexer_peek_token (parser->lexer);
11805   /* Check for trivial parameter-declaration-clauses.  */
11806   if (token->type == CPP_ELLIPSIS)
11807     {
11808       /* Consume the `...' token.  */
11809       cp_lexer_consume_token (parser->lexer);
11810       return NULL;
11811     }
11812   else if (token->type == CPP_CLOSE_PAREN)
11813     /* There are no parameters.  */
11814     {
11815 #ifndef NO_IMPLICIT_EXTERN_C
11816       if (in_system_header && current_class_type == NULL
11817           && current_lang_name == lang_name_c)
11818         return NULL;
11819       else
11820 #endif
11821         return no_parameters;
11822     }
11823   /* Check for `(void)', too, which is a special case.  */
11824   else if (token->keyword == RID_VOID
11825            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11826                == CPP_CLOSE_PAREN))
11827     {
11828       /* Consume the `void' token.  */
11829       cp_lexer_consume_token (parser->lexer);
11830       /* There are no parameters.  */
11831       return no_parameters;
11832     }
11833
11834   /* Parse the parameter-declaration-list.  */
11835   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11836   /* If a parse error occurred while parsing the
11837      parameter-declaration-list, then the entire
11838      parameter-declaration-clause is erroneous.  */
11839   if (is_error)
11840     return NULL;
11841
11842   /* Peek at the next token.  */
11843   token = cp_lexer_peek_token (parser->lexer);
11844   /* If it's a `,', the clause should terminate with an ellipsis.  */
11845   if (token->type == CPP_COMMA)
11846     {
11847       /* Consume the `,'.  */
11848       cp_lexer_consume_token (parser->lexer);
11849       /* Expect an ellipsis.  */
11850       ellipsis_p
11851         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11852     }
11853   /* It might also be `...' if the optional trailing `,' was
11854      omitted.  */
11855   else if (token->type == CPP_ELLIPSIS)
11856     {
11857       /* Consume the `...' token.  */
11858       cp_lexer_consume_token (parser->lexer);
11859       /* And remember that we saw it.  */
11860       ellipsis_p = true;
11861     }
11862   else
11863     ellipsis_p = false;
11864
11865   /* Finish the parameter list.  */
11866   if (parameters && ellipsis_p)
11867     parameters->ellipsis_p = true;
11868
11869   return parameters;
11870 }
11871
11872 /* Parse a parameter-declaration-list.
11873
11874    parameter-declaration-list:
11875      parameter-declaration
11876      parameter-declaration-list , parameter-declaration
11877
11878    Returns a representation of the parameter-declaration-list, as for
11879    cp_parser_parameter_declaration_clause.  However, the
11880    `void_list_node' is never appended to the list.  Upon return,
11881    *IS_ERROR will be true iff an error occurred.  */
11882
11883 static cp_parameter_declarator *
11884 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11885 {
11886   cp_parameter_declarator *parameters = NULL;
11887   cp_parameter_declarator **tail = &parameters;
11888
11889   /* Assume all will go well.  */
11890   *is_error = false;
11891
11892   /* Look for more parameters.  */
11893   while (true)
11894     {
11895       cp_parameter_declarator *parameter;
11896       bool parenthesized_p;
11897       /* Parse the parameter.  */
11898       parameter
11899         = cp_parser_parameter_declaration (parser,
11900                                            /*template_parm_p=*/false,
11901                                            &parenthesized_p);
11902
11903       /* If a parse error occurred parsing the parameter declaration,
11904          then the entire parameter-declaration-list is erroneous.  */
11905       if (!parameter)
11906         {
11907           *is_error = true;
11908           parameters = NULL;
11909           break;
11910         }
11911       /* Add the new parameter to the list.  */
11912       *tail = parameter;
11913       tail = &parameter->next;
11914
11915       /* Peek at the next token.  */
11916       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11917           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11918           /* These are for Objective-C++ */
11919           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11920           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11921         /* The parameter-declaration-list is complete.  */
11922         break;
11923       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11924         {
11925           cp_token *token;
11926
11927           /* Peek at the next token.  */
11928           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11929           /* If it's an ellipsis, then the list is complete.  */
11930           if (token->type == CPP_ELLIPSIS)
11931             break;
11932           /* Otherwise, there must be more parameters.  Consume the
11933              `,'.  */
11934           cp_lexer_consume_token (parser->lexer);
11935           /* When parsing something like:
11936
11937                 int i(float f, double d)
11938
11939              we can tell after seeing the declaration for "f" that we
11940              are not looking at an initialization of a variable "i",
11941              but rather at the declaration of a function "i".
11942
11943              Due to the fact that the parsing of template arguments
11944              (as specified to a template-id) requires backtracking we
11945              cannot use this technique when inside a template argument
11946              list.  */
11947           if (!parser->in_template_argument_list_p
11948               && !parser->in_type_id_in_expr_p
11949               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11950               /* However, a parameter-declaration of the form
11951                  "foat(f)" (which is a valid declaration of a
11952                  parameter "f") can also be interpreted as an
11953                  expression (the conversion of "f" to "float").  */
11954               && !parenthesized_p)
11955             cp_parser_commit_to_tentative_parse (parser);
11956         }
11957       else
11958         {
11959           cp_parser_error (parser, "expected %<,%> or %<...%>");
11960           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11961             cp_parser_skip_to_closing_parenthesis (parser,
11962                                                    /*recovering=*/true,
11963                                                    /*or_comma=*/false,
11964                                                    /*consume_paren=*/false);
11965           break;
11966         }
11967     }
11968
11969   return parameters;
11970 }
11971
11972 /* Parse a parameter declaration.
11973
11974    parameter-declaration:
11975      decl-specifier-seq declarator
11976      decl-specifier-seq declarator = assignment-expression
11977      decl-specifier-seq abstract-declarator [opt]
11978      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11979
11980    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11981    declares a template parameter.  (In that case, a non-nested `>'
11982    token encountered during the parsing of the assignment-expression
11983    is not interpreted as a greater-than operator.)
11984
11985    Returns a representation of the parameter, or NULL if an error
11986    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11987    true iff the declarator is of the form "(p)".  */
11988
11989 static cp_parameter_declarator *
11990 cp_parser_parameter_declaration (cp_parser *parser,
11991                                  bool template_parm_p,
11992                                  bool *parenthesized_p)
11993 {
11994   int declares_class_or_enum;
11995   bool greater_than_is_operator_p;
11996   cp_decl_specifier_seq decl_specifiers;
11997   cp_declarator *declarator;
11998   tree default_argument;
11999   cp_token *token;
12000   const char *saved_message;
12001
12002   /* In a template parameter, `>' is not an operator.
12003
12004      [temp.param]
12005
12006      When parsing a default template-argument for a non-type
12007      template-parameter, the first non-nested `>' is taken as the end
12008      of the template parameter-list rather than a greater-than
12009      operator.  */
12010   greater_than_is_operator_p = !template_parm_p;
12011
12012   /* Type definitions may not appear in parameter types.  */
12013   saved_message = parser->type_definition_forbidden_message;
12014   parser->type_definition_forbidden_message
12015     = "types may not be defined in parameter types";
12016
12017   /* Parse the declaration-specifiers.  */
12018   cp_parser_decl_specifier_seq (parser,
12019                                 CP_PARSER_FLAGS_NONE,
12020                                 &decl_specifiers,
12021                                 &declares_class_or_enum);
12022   /* If an error occurred, there's no reason to attempt to parse the
12023      rest of the declaration.  */
12024   if (cp_parser_error_occurred (parser))
12025     {
12026       parser->type_definition_forbidden_message = saved_message;
12027       return NULL;
12028     }
12029
12030   /* Peek at the next token.  */
12031   token = cp_lexer_peek_token (parser->lexer);
12032   /* If the next token is a `)', `,', `=', `>', or `...', then there
12033      is no declarator.  */
12034   if (token->type == CPP_CLOSE_PAREN
12035       || token->type == CPP_COMMA
12036       || token->type == CPP_EQ
12037       || token->type == CPP_ELLIPSIS
12038       || token->type == CPP_GREATER)
12039     {
12040       declarator = NULL;
12041       if (parenthesized_p)
12042         *parenthesized_p = false;
12043     }
12044   /* Otherwise, there should be a declarator.  */
12045   else
12046     {
12047       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12048       parser->default_arg_ok_p = false;
12049
12050       /* After seeing a decl-specifier-seq, if the next token is not a
12051          "(", there is no possibility that the code is a valid
12052          expression.  Therefore, if parsing tentatively, we commit at
12053          this point.  */
12054       if (!parser->in_template_argument_list_p
12055           /* In an expression context, having seen:
12056
12057                (int((char ...
12058
12059              we cannot be sure whether we are looking at a
12060              function-type (taking a "char" as a parameter) or a cast
12061              of some object of type "char" to "int".  */
12062           && !parser->in_type_id_in_expr_p
12063           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12064           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12065         cp_parser_commit_to_tentative_parse (parser);
12066       /* Parse the declarator.  */
12067       declarator = cp_parser_declarator (parser,
12068                                          CP_PARSER_DECLARATOR_EITHER,
12069                                          /*ctor_dtor_or_conv_p=*/NULL,
12070                                          parenthesized_p,
12071                                          /*member_p=*/false);
12072       parser->default_arg_ok_p = saved_default_arg_ok_p;
12073       /* After the declarator, allow more attributes.  */
12074       decl_specifiers.attributes
12075         = chainon (decl_specifiers.attributes,
12076                    cp_parser_attributes_opt (parser));
12077     }
12078
12079   /* The restriction on defining new types applies only to the type
12080      of the parameter, not to the default argument.  */
12081   parser->type_definition_forbidden_message = saved_message;
12082
12083   /* If the next token is `=', then process a default argument.  */
12084   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12085     {
12086       bool saved_greater_than_is_operator_p;
12087       /* Consume the `='.  */
12088       cp_lexer_consume_token (parser->lexer);
12089
12090       /* If we are defining a class, then the tokens that make up the
12091          default argument must be saved and processed later.  */
12092       if (!template_parm_p && at_class_scope_p ()
12093           && TYPE_BEING_DEFINED (current_class_type))
12094         {
12095           unsigned depth = 0;
12096           cp_token *first_token;
12097           cp_token *token;
12098
12099           /* Add tokens until we have processed the entire default
12100              argument.  We add the range [first_token, token).  */
12101           first_token = cp_lexer_peek_token (parser->lexer);
12102           while (true)
12103             {
12104               bool done = false;
12105
12106               /* Peek at the next token.  */
12107               token = cp_lexer_peek_token (parser->lexer);
12108               /* What we do depends on what token we have.  */
12109               switch (token->type)
12110                 {
12111                   /* In valid code, a default argument must be
12112                      immediately followed by a `,' `)', or `...'.  */
12113                 case CPP_COMMA:
12114                 case CPP_CLOSE_PAREN:
12115                 case CPP_ELLIPSIS:
12116                   /* If we run into a non-nested `;', `}', or `]',
12117                      then the code is invalid -- but the default
12118                      argument is certainly over.  */
12119                 case CPP_SEMICOLON:
12120                 case CPP_CLOSE_BRACE:
12121                 case CPP_CLOSE_SQUARE:
12122                   if (depth == 0)
12123                     done = true;
12124                   /* Update DEPTH, if necessary.  */
12125                   else if (token->type == CPP_CLOSE_PAREN
12126                            || token->type == CPP_CLOSE_BRACE
12127                            || token->type == CPP_CLOSE_SQUARE)
12128                     --depth;
12129                   break;
12130
12131                 case CPP_OPEN_PAREN:
12132                 case CPP_OPEN_SQUARE:
12133                 case CPP_OPEN_BRACE:
12134                   ++depth;
12135                   break;
12136
12137                 case CPP_GREATER:
12138                   /* If we see a non-nested `>', and `>' is not an
12139                      operator, then it marks the end of the default
12140                      argument.  */
12141                   if (!depth && !greater_than_is_operator_p)
12142                     done = true;
12143                   break;
12144
12145                   /* If we run out of tokens, issue an error message.  */
12146                 case CPP_EOF:
12147                   error ("file ends in default argument");
12148                   done = true;
12149                   break;
12150
12151                 case CPP_NAME:
12152                 case CPP_SCOPE:
12153                   /* In these cases, we should look for template-ids.
12154                      For example, if the default argument is
12155                      `X<int, double>()', we need to do name lookup to
12156                      figure out whether or not `X' is a template; if
12157                      so, the `,' does not end the default argument.
12158
12159                      That is not yet done.  */
12160                   break;
12161
12162                 default:
12163                   break;
12164                 }
12165
12166               /* If we've reached the end, stop.  */
12167               if (done)
12168                 break;
12169
12170               /* Add the token to the token block.  */
12171               token = cp_lexer_consume_token (parser->lexer);
12172             }
12173
12174           /* Create a DEFAULT_ARG to represented the unparsed default
12175              argument.  */
12176           default_argument = make_node (DEFAULT_ARG);
12177           DEFARG_TOKENS (default_argument)
12178             = cp_token_cache_new (first_token, token);
12179           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12180         }
12181       /* Outside of a class definition, we can just parse the
12182          assignment-expression.  */
12183       else
12184         {
12185           bool saved_local_variables_forbidden_p;
12186
12187           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12188              set correctly.  */
12189           saved_greater_than_is_operator_p
12190             = parser->greater_than_is_operator_p;
12191           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12192           /* Local variable names (and the `this' keyword) may not
12193              appear in a default argument.  */
12194           saved_local_variables_forbidden_p
12195             = parser->local_variables_forbidden_p;
12196           parser->local_variables_forbidden_p = true;
12197           /* Parse the assignment-expression.  */
12198           default_argument
12199             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12200           /* Restore saved state.  */
12201           parser->greater_than_is_operator_p
12202             = saved_greater_than_is_operator_p;
12203           parser->local_variables_forbidden_p
12204             = saved_local_variables_forbidden_p;
12205         }
12206       if (!parser->default_arg_ok_p)
12207         {
12208           if (!flag_pedantic_errors)
12209             warning (0, "deprecated use of default argument for parameter of non-function");
12210           else
12211             {
12212               error ("default arguments are only permitted for function parameters");
12213               default_argument = NULL_TREE;
12214             }
12215         }
12216     }
12217   else
12218     default_argument = NULL_TREE;
12219
12220   return make_parameter_declarator (&decl_specifiers,
12221                                     declarator,
12222                                     default_argument);
12223 }
12224
12225 /* Parse a function-body.
12226
12227    function-body:
12228      compound_statement  */
12229
12230 static void
12231 cp_parser_function_body (cp_parser *parser)
12232 {
12233   cp_parser_compound_statement (parser, NULL, false);
12234 }
12235
12236 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12237    true if a ctor-initializer was present.  */
12238
12239 static bool
12240 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12241 {
12242   tree body;
12243   bool ctor_initializer_p;
12244
12245   /* Begin the function body.  */
12246   body = begin_function_body ();
12247   /* Parse the optional ctor-initializer.  */
12248   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12249   /* Parse the function-body.  */
12250   cp_parser_function_body (parser);
12251   /* Finish the function body.  */
12252   finish_function_body (body);
12253
12254   return ctor_initializer_p;
12255 }
12256
12257 /* Parse an initializer.
12258
12259    initializer:
12260      = initializer-clause
12261      ( expression-list )
12262
12263    Returns an expression representing the initializer.  If no
12264    initializer is present, NULL_TREE is returned.
12265
12266    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12267    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12268    set to FALSE if there is no initializer present.  If there is an
12269    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12270    is set to true; otherwise it is set to false.  */
12271
12272 static tree
12273 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12274                        bool* non_constant_p)
12275 {
12276   cp_token *token;
12277   tree init;
12278
12279   /* Peek at the next token.  */
12280   token = cp_lexer_peek_token (parser->lexer);
12281
12282   /* Let our caller know whether or not this initializer was
12283      parenthesized.  */
12284   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12285   /* Assume that the initializer is constant.  */
12286   *non_constant_p = false;
12287
12288   if (token->type == CPP_EQ)
12289     {
12290       /* Consume the `='.  */
12291       cp_lexer_consume_token (parser->lexer);
12292       /* Parse the initializer-clause.  */
12293       init = cp_parser_initializer_clause (parser, non_constant_p);
12294     }
12295   else if (token->type == CPP_OPEN_PAREN)
12296     init = cp_parser_parenthesized_expression_list (parser, false,
12297                                                     /*cast_p=*/false,
12298                                                     non_constant_p);
12299   else
12300     {
12301       /* Anything else is an error.  */
12302       cp_parser_error (parser, "expected initializer");
12303       init = error_mark_node;
12304     }
12305
12306   return init;
12307 }
12308
12309 /* Parse an initializer-clause.
12310
12311    initializer-clause:
12312      assignment-expression
12313      { initializer-list , [opt] }
12314      { }
12315
12316    Returns an expression representing the initializer.
12317
12318    If the `assignment-expression' production is used the value
12319    returned is simply a representation for the expression.
12320
12321    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12322    the elements of the initializer-list (or NULL, if the last
12323    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12324    NULL_TREE.  There is no way to detect whether or not the optional
12325    trailing `,' was provided.  NON_CONSTANT_P is as for
12326    cp_parser_initializer.  */
12327
12328 static tree
12329 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12330 {
12331   tree initializer;
12332
12333   /* Assume the expression is constant.  */
12334   *non_constant_p = false;
12335
12336   /* If it is not a `{', then we are looking at an
12337      assignment-expression.  */
12338   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12339     {
12340       initializer
12341         = cp_parser_constant_expression (parser,
12342                                         /*allow_non_constant_p=*/true,
12343                                         non_constant_p);
12344       if (!*non_constant_p)
12345         initializer = fold_non_dependent_expr (initializer);
12346     }
12347   else
12348     {
12349       /* Consume the `{' token.  */
12350       cp_lexer_consume_token (parser->lexer);
12351       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12352       initializer = make_node (CONSTRUCTOR);
12353       /* If it's not a `}', then there is a non-trivial initializer.  */
12354       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12355         {
12356           /* Parse the initializer list.  */
12357           CONSTRUCTOR_ELTS (initializer)
12358             = cp_parser_initializer_list (parser, non_constant_p);
12359           /* A trailing `,' token is allowed.  */
12360           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12361             cp_lexer_consume_token (parser->lexer);
12362         }
12363       /* Now, there should be a trailing `}'.  */
12364       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12365     }
12366
12367   return initializer;
12368 }
12369
12370 /* Parse an initializer-list.
12371
12372    initializer-list:
12373      initializer-clause
12374      initializer-list , initializer-clause
12375
12376    GNU Extension:
12377
12378    initializer-list:
12379      identifier : initializer-clause
12380      initializer-list, identifier : initializer-clause
12381
12382    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12383    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12384    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12385    as for cp_parser_initializer.  */
12386
12387 static VEC(constructor_elt,gc) *
12388 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12389 {
12390   VEC(constructor_elt,gc) *v = NULL;
12391
12392   /* Assume all of the expressions are constant.  */
12393   *non_constant_p = false;
12394
12395   /* Parse the rest of the list.  */
12396   while (true)
12397     {
12398       cp_token *token;
12399       tree identifier;
12400       tree initializer;
12401       bool clause_non_constant_p;
12402
12403       /* If the next token is an identifier and the following one is a
12404          colon, we are looking at the GNU designated-initializer
12405          syntax.  */
12406       if (cp_parser_allow_gnu_extensions_p (parser)
12407           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12408           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12409         {
12410           /* Consume the identifier.  */
12411           identifier = cp_lexer_consume_token (parser->lexer)->value;
12412           /* Consume the `:'.  */
12413           cp_lexer_consume_token (parser->lexer);
12414         }
12415       else
12416         identifier = NULL_TREE;
12417
12418       /* Parse the initializer.  */
12419       initializer = cp_parser_initializer_clause (parser,
12420                                                   &clause_non_constant_p);
12421       /* If any clause is non-constant, so is the entire initializer.  */
12422       if (clause_non_constant_p)
12423         *non_constant_p = true;
12424
12425       /* Add it to the vector.  */
12426       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12427
12428       /* If the next token is not a comma, we have reached the end of
12429          the list.  */
12430       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12431         break;
12432
12433       /* Peek at the next token.  */
12434       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12435       /* If the next token is a `}', then we're still done.  An
12436          initializer-clause can have a trailing `,' after the
12437          initializer-list and before the closing `}'.  */
12438       if (token->type == CPP_CLOSE_BRACE)
12439         break;
12440
12441       /* Consume the `,' token.  */
12442       cp_lexer_consume_token (parser->lexer);
12443     }
12444
12445   return v;
12446 }
12447
12448 /* Classes [gram.class] */
12449
12450 /* Parse a class-name.
12451
12452    class-name:
12453      identifier
12454      template-id
12455
12456    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12457    to indicate that names looked up in dependent types should be
12458    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12459    keyword has been used to indicate that the name that appears next
12460    is a template.  TAG_TYPE indicates the explicit tag given before
12461    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12462    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12463    is the class being defined in a class-head.
12464
12465    Returns the TYPE_DECL representing the class.  */
12466
12467 static tree
12468 cp_parser_class_name (cp_parser *parser,
12469                       bool typename_keyword_p,
12470                       bool template_keyword_p,
12471                       enum tag_types tag_type,
12472                       bool check_dependency_p,
12473                       bool class_head_p,
12474                       bool is_declaration)
12475 {
12476   tree decl;
12477   tree scope;
12478   bool typename_p;
12479   cp_token *token;
12480
12481   /* All class-names start with an identifier.  */
12482   token = cp_lexer_peek_token (parser->lexer);
12483   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12484     {
12485       cp_parser_error (parser, "expected class-name");
12486       return error_mark_node;
12487     }
12488
12489   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12490      to a template-id, so we save it here.  */
12491   scope = parser->scope;
12492   if (scope == error_mark_node)
12493     return error_mark_node;
12494
12495   /* Any name names a type if we're following the `typename' keyword
12496      in a qualified name where the enclosing scope is type-dependent.  */
12497   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12498                 && dependent_type_p (scope));
12499   /* Handle the common case (an identifier, but not a template-id)
12500      efficiently.  */
12501   if (token->type == CPP_NAME
12502       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12503     {
12504       tree identifier;
12505
12506       /* Look for the identifier.  */
12507       identifier = cp_parser_identifier (parser);
12508       /* If the next token isn't an identifier, we are certainly not
12509          looking at a class-name.  */
12510       if (identifier == error_mark_node)
12511         decl = error_mark_node;
12512       /* If we know this is a type-name, there's no need to look it
12513          up.  */
12514       else if (typename_p)
12515         decl = identifier;
12516       else
12517         {
12518           /* If the next token is a `::', then the name must be a type
12519              name.
12520
12521              [basic.lookup.qual]
12522
12523              During the lookup for a name preceding the :: scope
12524              resolution operator, object, function, and enumerator
12525              names are ignored.  */
12526           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12527             tag_type = typename_type;
12528           /* Look up the name.  */
12529           decl = cp_parser_lookup_name (parser, identifier,
12530                                         tag_type,
12531                                         /*is_template=*/false,
12532                                         /*is_namespace=*/false,
12533                                         check_dependency_p,
12534                                         /*ambiguous_p=*/NULL);
12535         }
12536     }
12537   else
12538     {
12539       /* Try a template-id.  */
12540       decl = cp_parser_template_id (parser, template_keyword_p,
12541                                     check_dependency_p,
12542                                     is_declaration);
12543       if (decl == error_mark_node)
12544         return error_mark_node;
12545     }
12546
12547   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12548
12549   /* If this is a typename, create a TYPENAME_TYPE.  */
12550   if (typename_p && decl != error_mark_node)
12551     {
12552       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12553       if (decl != error_mark_node)
12554         decl = TYPE_NAME (decl);
12555     }
12556
12557   /* Check to see that it is really the name of a class.  */
12558   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12559       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12560       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12561     /* Situations like this:
12562
12563          template <typename T> struct A {
12564            typename T::template X<int>::I i;
12565          };
12566
12567        are problematic.  Is `T::template X<int>' a class-name?  The
12568        standard does not seem to be definitive, but there is no other
12569        valid interpretation of the following `::'.  Therefore, those
12570        names are considered class-names.  */
12571     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12572   else if (decl == error_mark_node
12573            || TREE_CODE (decl) != TYPE_DECL
12574            || TREE_TYPE (decl) == error_mark_node
12575            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12576     {
12577       cp_parser_error (parser, "expected class-name");
12578       return error_mark_node;
12579     }
12580
12581   return decl;
12582 }
12583
12584 /* Parse a class-specifier.
12585
12586    class-specifier:
12587      class-head { member-specification [opt] }
12588
12589    Returns the TREE_TYPE representing the class.  */
12590
12591 static tree
12592 cp_parser_class_specifier (cp_parser* parser)
12593 {
12594   cp_token *token;
12595   tree type;
12596   tree attributes = NULL_TREE;
12597   int has_trailing_semicolon;
12598   bool nested_name_specifier_p;
12599   unsigned saved_num_template_parameter_lists;
12600   tree old_scope = NULL_TREE;
12601   tree scope = NULL_TREE;
12602
12603   push_deferring_access_checks (dk_no_deferred);
12604
12605   /* Parse the class-head.  */
12606   type = cp_parser_class_head (parser,
12607                                &nested_name_specifier_p,
12608                                &attributes);
12609   /* If the class-head was a semantic disaster, skip the entire body
12610      of the class.  */
12611   if (!type)
12612     {
12613       cp_parser_skip_to_end_of_block_or_statement (parser);
12614       pop_deferring_access_checks ();
12615       return error_mark_node;
12616     }
12617
12618   /* Look for the `{'.  */
12619   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12620     {
12621       pop_deferring_access_checks ();
12622       return error_mark_node;
12623     }
12624
12625   /* Issue an error message if type-definitions are forbidden here.  */
12626   cp_parser_check_type_definition (parser);
12627   /* Remember that we are defining one more class.  */
12628   ++parser->num_classes_being_defined;
12629   /* Inside the class, surrounding template-parameter-lists do not
12630      apply.  */
12631   saved_num_template_parameter_lists
12632     = parser->num_template_parameter_lists;
12633   parser->num_template_parameter_lists = 0;
12634
12635   /* Start the class.  */
12636   if (nested_name_specifier_p)
12637     {
12638       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12639       old_scope = push_inner_scope (scope);
12640     }
12641   type = begin_class_definition (type);
12642
12643   if (type == error_mark_node)
12644     /* If the type is erroneous, skip the entire body of the class.  */
12645     cp_parser_skip_to_closing_brace (parser);
12646   else
12647     /* Parse the member-specification.  */
12648     cp_parser_member_specification_opt (parser);
12649
12650   /* Look for the trailing `}'.  */
12651   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12652   /* We get better error messages by noticing a common problem: a
12653      missing trailing `;'.  */
12654   token = cp_lexer_peek_token (parser->lexer);
12655   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12656   /* Look for trailing attributes to apply to this class.  */
12657   if (cp_parser_allow_gnu_extensions_p (parser))
12658     {
12659       tree sub_attr = cp_parser_attributes_opt (parser);
12660       attributes = chainon (attributes, sub_attr);
12661     }
12662   if (type != error_mark_node)
12663     type = finish_struct (type, attributes);
12664   if (nested_name_specifier_p)
12665     pop_inner_scope (old_scope, scope);
12666   /* If this class is not itself within the scope of another class,
12667      then we need to parse the bodies of all of the queued function
12668      definitions.  Note that the queued functions defined in a class
12669      are not always processed immediately following the
12670      class-specifier for that class.  Consider:
12671
12672        struct A {
12673          struct B { void f() { sizeof (A); } };
12674        };
12675
12676      If `f' were processed before the processing of `A' were
12677      completed, there would be no way to compute the size of `A'.
12678      Note that the nesting we are interested in here is lexical --
12679      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12680      for:
12681
12682        struct A { struct B; };
12683        struct A::B { void f() { } };
12684
12685      there is no need to delay the parsing of `A::B::f'.  */
12686   if (--parser->num_classes_being_defined == 0)
12687     {
12688       tree queue_entry;
12689       tree fn;
12690       tree class_type = NULL_TREE;
12691       tree pushed_scope = NULL_TREE;
12692  
12693       /* In a first pass, parse default arguments to the functions.
12694          Then, in a second pass, parse the bodies of the functions.
12695          This two-phased approach handles cases like:
12696
12697             struct S {
12698               void f() { g(); }
12699               void g(int i = 3);
12700             };
12701
12702          */
12703       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12704              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12705            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12706            TREE_PURPOSE (parser->unparsed_functions_queues)
12707              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12708         {
12709           fn = TREE_VALUE (queue_entry);
12710           /* If there are default arguments that have not yet been processed,
12711              take care of them now.  */
12712           if (class_type != TREE_PURPOSE (queue_entry))
12713             {
12714               if (pushed_scope)
12715                 pop_scope (pushed_scope);
12716               class_type = TREE_PURPOSE (queue_entry);
12717               pushed_scope = push_scope (class_type);
12718             }
12719           /* Make sure that any template parameters are in scope.  */
12720           maybe_begin_member_template_processing (fn);
12721           /* Parse the default argument expressions.  */
12722           cp_parser_late_parsing_default_args (parser, fn);
12723           /* Remove any template parameters from the symbol table.  */
12724           maybe_end_member_template_processing ();
12725         }
12726       if (pushed_scope)
12727         pop_scope (pushed_scope);
12728       /* Now parse the body of the functions.  */
12729       for (TREE_VALUE (parser->unparsed_functions_queues)
12730              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12731            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12732            TREE_VALUE (parser->unparsed_functions_queues)
12733              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12734         {
12735           /* Figure out which function we need to process.  */
12736           fn = TREE_VALUE (queue_entry);
12737           /* Parse the function.  */
12738           cp_parser_late_parsing_for_member (parser, fn);
12739         }
12740     }
12741
12742   /* Put back any saved access checks.  */
12743   pop_deferring_access_checks ();
12744
12745   /* Restore the count of active template-parameter-lists.  */
12746   parser->num_template_parameter_lists
12747     = saved_num_template_parameter_lists;
12748
12749   return type;
12750 }
12751
12752 /* Parse a class-head.
12753
12754    class-head:
12755      class-key identifier [opt] base-clause [opt]
12756      class-key nested-name-specifier identifier base-clause [opt]
12757      class-key nested-name-specifier [opt] template-id
12758        base-clause [opt]
12759
12760    GNU Extensions:
12761      class-key attributes identifier [opt] base-clause [opt]
12762      class-key attributes nested-name-specifier identifier base-clause [opt]
12763      class-key attributes nested-name-specifier [opt] template-id
12764        base-clause [opt]
12765
12766    Returns the TYPE of the indicated class.  Sets
12767    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12768    involving a nested-name-specifier was used, and FALSE otherwise.
12769
12770    Returns error_mark_node if this is not a class-head.
12771
12772    Returns NULL_TREE if the class-head is syntactically valid, but
12773    semantically invalid in a way that means we should skip the entire
12774    body of the class.  */
12775
12776 static tree
12777 cp_parser_class_head (cp_parser* parser,
12778                       bool* nested_name_specifier_p,
12779                       tree *attributes_p)
12780 {
12781   tree nested_name_specifier;
12782   enum tag_types class_key;
12783   tree id = NULL_TREE;
12784   tree type = NULL_TREE;
12785   tree attributes;
12786   bool template_id_p = false;
12787   bool qualified_p = false;
12788   bool invalid_nested_name_p = false;
12789   bool invalid_explicit_specialization_p = false;
12790   tree pushed_scope = NULL_TREE;
12791   unsigned num_templates;
12792   tree bases;
12793
12794   /* Assume no nested-name-specifier will be present.  */
12795   *nested_name_specifier_p = false;
12796   /* Assume no template parameter lists will be used in defining the
12797      type.  */
12798   num_templates = 0;
12799
12800   /* Look for the class-key.  */
12801   class_key = cp_parser_class_key (parser);
12802   if (class_key == none_type)
12803     return error_mark_node;
12804
12805   /* Parse the attributes.  */
12806   attributes = cp_parser_attributes_opt (parser);
12807
12808   /* If the next token is `::', that is invalid -- but sometimes
12809      people do try to write:
12810
12811        struct ::S {};
12812
12813      Handle this gracefully by accepting the extra qualifier, and then
12814      issuing an error about it later if this really is a
12815      class-head.  If it turns out just to be an elaborated type
12816      specifier, remain silent.  */
12817   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12818     qualified_p = true;
12819
12820   push_deferring_access_checks (dk_no_check);
12821
12822   /* Determine the name of the class.  Begin by looking for an
12823      optional nested-name-specifier.  */
12824   nested_name_specifier
12825     = cp_parser_nested_name_specifier_opt (parser,
12826                                            /*typename_keyword_p=*/false,
12827                                            /*check_dependency_p=*/false,
12828                                            /*type_p=*/false,
12829                                            /*is_declaration=*/false);
12830   /* If there was a nested-name-specifier, then there *must* be an
12831      identifier.  */
12832   if (nested_name_specifier)
12833     {
12834       /* Although the grammar says `identifier', it really means
12835          `class-name' or `template-name'.  You are only allowed to
12836          define a class that has already been declared with this
12837          syntax.
12838
12839          The proposed resolution for Core Issue 180 says that whever
12840          you see `class T::X' you should treat `X' as a type-name.
12841
12842          It is OK to define an inaccessible class; for example:
12843
12844            class A { class B; };
12845            class A::B {};
12846
12847          We do not know if we will see a class-name, or a
12848          template-name.  We look for a class-name first, in case the
12849          class-name is a template-id; if we looked for the
12850          template-name first we would stop after the template-name.  */
12851       cp_parser_parse_tentatively (parser);
12852       type = cp_parser_class_name (parser,
12853                                    /*typename_keyword_p=*/false,
12854                                    /*template_keyword_p=*/false,
12855                                    class_type,
12856                                    /*check_dependency_p=*/false,
12857                                    /*class_head_p=*/true,
12858                                    /*is_declaration=*/false);
12859       /* If that didn't work, ignore the nested-name-specifier.  */
12860       if (!cp_parser_parse_definitely (parser))
12861         {
12862           invalid_nested_name_p = true;
12863           id = cp_parser_identifier (parser);
12864           if (id == error_mark_node)
12865             id = NULL_TREE;
12866         }
12867       /* If we could not find a corresponding TYPE, treat this
12868          declaration like an unqualified declaration.  */
12869       if (type == error_mark_node)
12870         nested_name_specifier = NULL_TREE;
12871       /* Otherwise, count the number of templates used in TYPE and its
12872          containing scopes.  */
12873       else
12874         {
12875           tree scope;
12876
12877           for (scope = TREE_TYPE (type);
12878                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12879                scope = (TYPE_P (scope)
12880                         ? TYPE_CONTEXT (scope)
12881                         : DECL_CONTEXT (scope)))
12882             if (TYPE_P (scope)
12883                 && CLASS_TYPE_P (scope)
12884                 && CLASSTYPE_TEMPLATE_INFO (scope)
12885                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12886                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12887               ++num_templates;
12888         }
12889     }
12890   /* Otherwise, the identifier is optional.  */
12891   else
12892     {
12893       /* We don't know whether what comes next is a template-id,
12894          an identifier, or nothing at all.  */
12895       cp_parser_parse_tentatively (parser);
12896       /* Check for a template-id.  */
12897       id = cp_parser_template_id (parser,
12898                                   /*template_keyword_p=*/false,
12899                                   /*check_dependency_p=*/true,
12900                                   /*is_declaration=*/true);
12901       /* If that didn't work, it could still be an identifier.  */
12902       if (!cp_parser_parse_definitely (parser))
12903         {
12904           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12905             id = cp_parser_identifier (parser);
12906           else
12907             id = NULL_TREE;
12908         }
12909       else
12910         {
12911           template_id_p = true;
12912           ++num_templates;
12913         }
12914     }
12915
12916   pop_deferring_access_checks ();
12917
12918   if (id)
12919     cp_parser_check_for_invalid_template_id (parser, id);
12920
12921   /* If it's not a `:' or a `{' then we can't really be looking at a
12922      class-head, since a class-head only appears as part of a
12923      class-specifier.  We have to detect this situation before calling
12924      xref_tag, since that has irreversible side-effects.  */
12925   if (!cp_parser_next_token_starts_class_definition_p (parser))
12926     {
12927       cp_parser_error (parser, "expected %<{%> or %<:%>");
12928       return error_mark_node;
12929     }
12930
12931   /* At this point, we're going ahead with the class-specifier, even
12932      if some other problem occurs.  */
12933   cp_parser_commit_to_tentative_parse (parser);
12934   /* Issue the error about the overly-qualified name now.  */
12935   if (qualified_p)
12936     cp_parser_error (parser,
12937                      "global qualification of class name is invalid");
12938   else if (invalid_nested_name_p)
12939     cp_parser_error (parser,
12940                      "qualified name does not name a class");
12941   else if (nested_name_specifier)
12942     {
12943       tree scope;
12944
12945       /* Reject typedef-names in class heads.  */
12946       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12947         {
12948           error ("invalid class name in declaration of %qD", type);
12949           type = NULL_TREE;
12950           goto done;
12951         }
12952
12953       /* Figure out in what scope the declaration is being placed.  */
12954       scope = current_scope ();
12955       /* If that scope does not contain the scope in which the
12956          class was originally declared, the program is invalid.  */
12957       if (scope && !is_ancestor (scope, nested_name_specifier))
12958         {
12959           error ("declaration of %qD in %qD which does not enclose %qD",
12960                  type, scope, nested_name_specifier);
12961           type = NULL_TREE;
12962           goto done;
12963         }
12964       /* [dcl.meaning]
12965
12966          A declarator-id shall not be qualified exception of the
12967          definition of a ... nested class outside of its class
12968          ... [or] a the definition or explicit instantiation of a
12969          class member of a namespace outside of its namespace.  */
12970       if (scope == nested_name_specifier)
12971         {
12972           pedwarn ("extra qualification ignored");
12973           nested_name_specifier = NULL_TREE;
12974           num_templates = 0;
12975         }
12976     }
12977   /* An explicit-specialization must be preceded by "template <>".  If
12978      it is not, try to recover gracefully.  */
12979   if (at_namespace_scope_p ()
12980       && parser->num_template_parameter_lists == 0
12981       && template_id_p)
12982     {
12983       error ("an explicit specialization must be preceded by %<template <>%>");
12984       invalid_explicit_specialization_p = true;
12985       /* Take the same action that would have been taken by
12986          cp_parser_explicit_specialization.  */
12987       ++parser->num_template_parameter_lists;
12988       begin_specialization ();
12989     }
12990   /* There must be no "return" statements between this point and the
12991      end of this function; set "type "to the correct return value and
12992      use "goto done;" to return.  */
12993   /* Make sure that the right number of template parameters were
12994      present.  */
12995   if (!cp_parser_check_template_parameters (parser, num_templates))
12996     {
12997       /* If something went wrong, there is no point in even trying to
12998          process the class-definition.  */
12999       type = NULL_TREE;
13000       goto done;
13001     }
13002
13003   /* Look up the type.  */
13004   if (template_id_p)
13005     {
13006       type = TREE_TYPE (id);
13007       maybe_process_partial_specialization (type);
13008       if (nested_name_specifier)
13009         pushed_scope = push_scope (nested_name_specifier);
13010     }
13011   else if (nested_name_specifier)
13012     {
13013       tree class_type;
13014
13015       /* Given:
13016
13017             template <typename T> struct S { struct T };
13018             template <typename T> struct S<T>::T { };
13019
13020          we will get a TYPENAME_TYPE when processing the definition of
13021          `S::T'.  We need to resolve it to the actual type before we
13022          try to define it.  */
13023       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13024         {
13025           class_type = resolve_typename_type (TREE_TYPE (type),
13026                                               /*only_current_p=*/false);
13027           if (class_type != error_mark_node)
13028             type = TYPE_NAME (class_type);
13029           else
13030             {
13031               cp_parser_error (parser, "could not resolve typename type");
13032               type = error_mark_node;
13033             }
13034         }
13035
13036       maybe_process_partial_specialization (TREE_TYPE (type));
13037       class_type = current_class_type;
13038       /* Enter the scope indicated by the nested-name-specifier.  */
13039       pushed_scope = push_scope (nested_name_specifier);
13040       /* Get the canonical version of this type.  */
13041       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13042       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13043           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13044         {
13045           type = push_template_decl (type);
13046           if (type == error_mark_node)
13047             {
13048               type = NULL_TREE;
13049               goto done;
13050             }
13051         }
13052
13053       type = TREE_TYPE (type);
13054       *nested_name_specifier_p = true;
13055     }
13056   else      /* The name is not a nested name.  */
13057     {
13058       /* If the class was unnamed, create a dummy name.  */
13059       if (!id)
13060         id = make_anon_name ();
13061       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13062                        parser->num_template_parameter_lists);
13063     }
13064
13065   /* Indicate whether this class was declared as a `class' or as a
13066      `struct'.  */
13067   if (TREE_CODE (type) == RECORD_TYPE)
13068     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13069   cp_parser_check_class_key (class_key, type);
13070
13071   /* If this type was already complete, and we see another definition,
13072      that's an error.  */
13073   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13074     {
13075       error ("redefinition of %q#T", type);
13076       error ("previous definition of %q+#T", type);
13077       type = NULL_TREE;
13078       goto done;
13079     }
13080
13081   /* We will have entered the scope containing the class; the names of
13082      base classes should be looked up in that context.  For example:
13083
13084        struct A { struct B {}; struct C; };
13085        struct A::C : B {};
13086
13087      is valid.  */
13088   bases = NULL_TREE;
13089
13090   /* Get the list of base-classes, if there is one.  */
13091   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13092     bases = cp_parser_base_clause (parser);
13093
13094   /* Process the base classes.  */
13095   xref_basetypes (type, bases);
13096
13097  done:
13098   /* Leave the scope given by the nested-name-specifier.  We will
13099      enter the class scope itself while processing the members.  */
13100   if (pushed_scope)
13101     pop_scope (pushed_scope);
13102
13103   if (invalid_explicit_specialization_p)
13104     {
13105       end_specialization ();
13106       --parser->num_template_parameter_lists;
13107     }
13108   *attributes_p = attributes;
13109   return type;
13110 }
13111
13112 /* Parse a class-key.
13113
13114    class-key:
13115      class
13116      struct
13117      union
13118
13119    Returns the kind of class-key specified, or none_type to indicate
13120    error.  */
13121
13122 static enum tag_types
13123 cp_parser_class_key (cp_parser* parser)
13124 {
13125   cp_token *token;
13126   enum tag_types tag_type;
13127
13128   /* Look for the class-key.  */
13129   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13130   if (!token)
13131     return none_type;
13132
13133   /* Check to see if the TOKEN is a class-key.  */
13134   tag_type = cp_parser_token_is_class_key (token);
13135   if (!tag_type)
13136     cp_parser_error (parser, "expected class-key");
13137   return tag_type;
13138 }
13139
13140 /* Parse an (optional) member-specification.
13141
13142    member-specification:
13143      member-declaration member-specification [opt]
13144      access-specifier : member-specification [opt]  */
13145
13146 static void
13147 cp_parser_member_specification_opt (cp_parser* parser)
13148 {
13149   while (true)
13150     {
13151       cp_token *token;
13152       enum rid keyword;
13153
13154       /* Peek at the next token.  */
13155       token = cp_lexer_peek_token (parser->lexer);
13156       /* If it's a `}', or EOF then we've seen all the members.  */
13157       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13158         break;
13159
13160       /* See if this token is a keyword.  */
13161       keyword = token->keyword;
13162       switch (keyword)
13163         {
13164         case RID_PUBLIC:
13165         case RID_PROTECTED:
13166         case RID_PRIVATE:
13167           /* Consume the access-specifier.  */
13168           cp_lexer_consume_token (parser->lexer);
13169           /* Remember which access-specifier is active.  */
13170           current_access_specifier = token->value;
13171           /* Look for the `:'.  */
13172           cp_parser_require (parser, CPP_COLON, "`:'");
13173           break;
13174
13175         default:
13176           /* Accept #pragmas at class scope.  */
13177           if (token->type == CPP_PRAGMA)
13178             {
13179               cp_lexer_handle_pragma (parser->lexer);
13180               break;
13181             }
13182
13183           /* Otherwise, the next construction must be a
13184              member-declaration.  */
13185           cp_parser_member_declaration (parser);
13186         }
13187     }
13188 }
13189
13190 /* Parse a member-declaration.
13191
13192    member-declaration:
13193      decl-specifier-seq [opt] member-declarator-list [opt] ;
13194      function-definition ; [opt]
13195      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13196      using-declaration
13197      template-declaration
13198
13199    member-declarator-list:
13200      member-declarator
13201      member-declarator-list , member-declarator
13202
13203    member-declarator:
13204      declarator pure-specifier [opt]
13205      declarator constant-initializer [opt]
13206      identifier [opt] : constant-expression
13207
13208    GNU Extensions:
13209
13210    member-declaration:
13211      __extension__ member-declaration
13212
13213    member-declarator:
13214      declarator attributes [opt] pure-specifier [opt]
13215      declarator attributes [opt] constant-initializer [opt]
13216      identifier [opt] attributes [opt] : constant-expression  */
13217
13218 static void
13219 cp_parser_member_declaration (cp_parser* parser)
13220 {
13221   cp_decl_specifier_seq decl_specifiers;
13222   tree prefix_attributes;
13223   tree decl;
13224   int declares_class_or_enum;
13225   bool friend_p;
13226   cp_token *token;
13227   int saved_pedantic;
13228
13229   /* Check for the `__extension__' keyword.  */
13230   if (cp_parser_extension_opt (parser, &saved_pedantic))
13231     {
13232       /* Recurse.  */
13233       cp_parser_member_declaration (parser);
13234       /* Restore the old value of the PEDANTIC flag.  */
13235       pedantic = saved_pedantic;
13236
13237       return;
13238     }
13239
13240   /* Check for a template-declaration.  */
13241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13242     {
13243       /* Parse the template-declaration.  */
13244       cp_parser_template_declaration (parser, /*member_p=*/true);
13245
13246       return;
13247     }
13248
13249   /* Check for a using-declaration.  */
13250   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13251     {
13252       /* Parse the using-declaration.  */
13253       cp_parser_using_declaration (parser);
13254
13255       return;
13256     }
13257
13258   /* Check for @defs.  */
13259   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13260     {
13261       tree ivar, member;
13262       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13263       ivar = ivar_chains;
13264       while (ivar)
13265         {
13266           member = ivar;
13267           ivar = TREE_CHAIN (member);
13268           TREE_CHAIN (member) = NULL_TREE;
13269           finish_member_declaration (member);
13270         }
13271       return;
13272     }
13273
13274   /* Parse the decl-specifier-seq.  */
13275   cp_parser_decl_specifier_seq (parser,
13276                                 CP_PARSER_FLAGS_OPTIONAL,
13277                                 &decl_specifiers,
13278                                 &declares_class_or_enum);
13279   prefix_attributes = decl_specifiers.attributes;
13280   decl_specifiers.attributes = NULL_TREE;
13281   /* Check for an invalid type-name.  */
13282   if (!decl_specifiers.type
13283       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13284     return;
13285   /* If there is no declarator, then the decl-specifier-seq should
13286      specify a type.  */
13287   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13288     {
13289       /* If there was no decl-specifier-seq, and the next token is a
13290          `;', then we have something like:
13291
13292            struct S { ; };
13293
13294          [class.mem]
13295
13296          Each member-declaration shall declare at least one member
13297          name of the class.  */
13298       if (!decl_specifiers.any_specifiers_p)
13299         {
13300           cp_token *token = cp_lexer_peek_token (parser->lexer);
13301           if (pedantic && !token->in_system_header)
13302             pedwarn ("%Hextra %<;%>", &token->location);
13303         }
13304       else
13305         {
13306           tree type;
13307
13308           /* See if this declaration is a friend.  */
13309           friend_p = cp_parser_friend_p (&decl_specifiers);
13310           /* If there were decl-specifiers, check to see if there was
13311              a class-declaration.  */
13312           type = check_tag_decl (&decl_specifiers);
13313           /* Nested classes have already been added to the class, but
13314              a `friend' needs to be explicitly registered.  */
13315           if (friend_p)
13316             {
13317               /* If the `friend' keyword was present, the friend must
13318                  be introduced with a class-key.  */
13319                if (!declares_class_or_enum)
13320                  error ("a class-key must be used when declaring a friend");
13321                /* In this case:
13322
13323                     template <typename T> struct A {
13324                       friend struct A<T>::B;
13325                     };
13326
13327                   A<T>::B will be represented by a TYPENAME_TYPE, and
13328                   therefore not recognized by check_tag_decl.  */
13329                if (!type
13330                    && decl_specifiers.type
13331                    && TYPE_P (decl_specifiers.type))
13332                  type = decl_specifiers.type;
13333                if (!type || !TYPE_P (type))
13334                  error ("friend declaration does not name a class or "
13335                         "function");
13336                else
13337                  make_friend_class (current_class_type, type,
13338                                     /*complain=*/true);
13339             }
13340           /* If there is no TYPE, an error message will already have
13341              been issued.  */
13342           else if (!type || type == error_mark_node)
13343             ;
13344           /* An anonymous aggregate has to be handled specially; such
13345              a declaration really declares a data member (with a
13346              particular type), as opposed to a nested class.  */
13347           else if (ANON_AGGR_TYPE_P (type))
13348             {
13349               /* Remove constructors and such from TYPE, now that we
13350                  know it is an anonymous aggregate.  */
13351               fixup_anonymous_aggr (type);
13352               /* And make the corresponding data member.  */
13353               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13354               /* Add it to the class.  */
13355               finish_member_declaration (decl);
13356             }
13357           else
13358             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13359         }
13360     }
13361   else
13362     {
13363       /* See if these declarations will be friends.  */
13364       friend_p = cp_parser_friend_p (&decl_specifiers);
13365
13366       /* Keep going until we hit the `;' at the end of the
13367          declaration.  */
13368       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13369         {
13370           tree attributes = NULL_TREE;
13371           tree first_attribute;
13372
13373           /* Peek at the next token.  */
13374           token = cp_lexer_peek_token (parser->lexer);
13375
13376           /* Check for a bitfield declaration.  */
13377           if (token->type == CPP_COLON
13378               || (token->type == CPP_NAME
13379                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13380                   == CPP_COLON))
13381             {
13382               tree identifier;
13383               tree width;
13384
13385               /* Get the name of the bitfield.  Note that we cannot just
13386                  check TOKEN here because it may have been invalidated by
13387                  the call to cp_lexer_peek_nth_token above.  */
13388               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13389                 identifier = cp_parser_identifier (parser);
13390               else
13391                 identifier = NULL_TREE;
13392
13393               /* Consume the `:' token.  */
13394               cp_lexer_consume_token (parser->lexer);
13395               /* Get the width of the bitfield.  */
13396               width
13397                 = cp_parser_constant_expression (parser,
13398                                                  /*allow_non_constant=*/false,
13399                                                  NULL);
13400
13401               /* Look for attributes that apply to the bitfield.  */
13402               attributes = cp_parser_attributes_opt (parser);
13403               /* Remember which attributes are prefix attributes and
13404                  which are not.  */
13405               first_attribute = attributes;
13406               /* Combine the attributes.  */
13407               attributes = chainon (prefix_attributes, attributes);
13408
13409               /* Create the bitfield declaration.  */
13410               decl = grokbitfield (identifier
13411                                    ? make_id_declarator (NULL_TREE,
13412                                                          identifier)
13413                                    : NULL,
13414                                    &decl_specifiers,
13415                                    width);
13416               /* Apply the attributes.  */
13417               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13418             }
13419           else
13420             {
13421               cp_declarator *declarator;
13422               tree initializer;
13423               tree asm_specification;
13424               int ctor_dtor_or_conv_p;
13425
13426               /* Parse the declarator.  */
13427               declarator
13428                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13429                                         &ctor_dtor_or_conv_p,
13430                                         /*parenthesized_p=*/NULL,
13431                                         /*member_p=*/true);
13432
13433               /* If something went wrong parsing the declarator, make sure
13434                  that we at least consume some tokens.  */
13435               if (declarator == cp_error_declarator)
13436                 {
13437                   /* Skip to the end of the statement.  */
13438                   cp_parser_skip_to_end_of_statement (parser);
13439                   /* If the next token is not a semicolon, that is
13440                      probably because we just skipped over the body of
13441                      a function.  So, we consume a semicolon if
13442                      present, but do not issue an error message if it
13443                      is not present.  */
13444                   if (cp_lexer_next_token_is (parser->lexer,
13445                                               CPP_SEMICOLON))
13446                     cp_lexer_consume_token (parser->lexer);
13447                   return;
13448                 }
13449
13450               if (declares_class_or_enum & 2)
13451                 cp_parser_check_for_definition_in_return_type
13452                   (declarator, decl_specifiers.type);
13453
13454               /* Look for an asm-specification.  */
13455               asm_specification = cp_parser_asm_specification_opt (parser);
13456               /* Look for attributes that apply to the declaration.  */
13457               attributes = cp_parser_attributes_opt (parser);
13458               /* Remember which attributes are prefix attributes and
13459                  which are not.  */
13460               first_attribute = attributes;
13461               /* Combine the attributes.  */
13462               attributes = chainon (prefix_attributes, attributes);
13463
13464               /* If it's an `=', then we have a constant-initializer or a
13465                  pure-specifier.  It is not correct to parse the
13466                  initializer before registering the member declaration
13467                  since the member declaration should be in scope while
13468                  its initializer is processed.  However, the rest of the
13469                  front end does not yet provide an interface that allows
13470                  us to handle this correctly.  */
13471               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13472                 {
13473                   /* In [class.mem]:
13474
13475                      A pure-specifier shall be used only in the declaration of
13476                      a virtual function.
13477
13478                      A member-declarator can contain a constant-initializer
13479                      only if it declares a static member of integral or
13480                      enumeration type.
13481
13482                      Therefore, if the DECLARATOR is for a function, we look
13483                      for a pure-specifier; otherwise, we look for a
13484                      constant-initializer.  When we call `grokfield', it will
13485                      perform more stringent semantics checks.  */
13486                   if (declarator->kind == cdk_function)
13487                     initializer = cp_parser_pure_specifier (parser);
13488                   else
13489                     /* Parse the initializer.  */
13490                     initializer = cp_parser_constant_initializer (parser);
13491                 }
13492               /* Otherwise, there is no initializer.  */
13493               else
13494                 initializer = NULL_TREE;
13495
13496               /* See if we are probably looking at a function
13497                  definition.  We are certainly not looking at a
13498                  member-declarator.  Calling `grokfield' has
13499                  side-effects, so we must not do it unless we are sure
13500                  that we are looking at a member-declarator.  */
13501               if (cp_parser_token_starts_function_definition_p
13502                   (cp_lexer_peek_token (parser->lexer)))
13503                 {
13504                   /* The grammar does not allow a pure-specifier to be
13505                      used when a member function is defined.  (It is
13506                      possible that this fact is an oversight in the
13507                      standard, since a pure function may be defined
13508                      outside of the class-specifier.  */
13509                   if (initializer)
13510                     error ("pure-specifier on function-definition");
13511                   decl = cp_parser_save_member_function_body (parser,
13512                                                               &decl_specifiers,
13513                                                               declarator,
13514                                                               attributes);
13515                   /* If the member was not a friend, declare it here.  */
13516                   if (!friend_p)
13517                     finish_member_declaration (decl);
13518                   /* Peek at the next token.  */
13519                   token = cp_lexer_peek_token (parser->lexer);
13520                   /* If the next token is a semicolon, consume it.  */
13521                   if (token->type == CPP_SEMICOLON)
13522                     cp_lexer_consume_token (parser->lexer);
13523                   return;
13524                 }
13525               else
13526                 {
13527                   /* Create the declaration.  */
13528                   decl = grokfield (declarator, &decl_specifiers,
13529                                     initializer, asm_specification,
13530                                     attributes);
13531                   /* Any initialization must have been from a
13532                      constant-expression.  */
13533                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13534                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13535                 }
13536             }
13537
13538           /* Reset PREFIX_ATTRIBUTES.  */
13539           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13540             attributes = TREE_CHAIN (attributes);
13541           if (attributes)
13542             TREE_CHAIN (attributes) = NULL_TREE;
13543
13544           /* If there is any qualification still in effect, clear it
13545              now; we will be starting fresh with the next declarator.  */
13546           parser->scope = NULL_TREE;
13547           parser->qualifying_scope = NULL_TREE;
13548           parser->object_scope = NULL_TREE;
13549           /* If it's a `,', then there are more declarators.  */
13550           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13551             cp_lexer_consume_token (parser->lexer);
13552           /* If the next token isn't a `;', then we have a parse error.  */
13553           else if (cp_lexer_next_token_is_not (parser->lexer,
13554                                                CPP_SEMICOLON))
13555             {
13556               cp_parser_error (parser, "expected %<;%>");
13557               /* Skip tokens until we find a `;'.  */
13558               cp_parser_skip_to_end_of_statement (parser);
13559
13560               break;
13561             }
13562
13563           if (decl)
13564             {
13565               /* Add DECL to the list of members.  */
13566               if (!friend_p)
13567                 finish_member_declaration (decl);
13568
13569               if (TREE_CODE (decl) == FUNCTION_DECL)
13570                 cp_parser_save_default_args (parser, decl);
13571             }
13572         }
13573     }
13574
13575   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13576 }
13577
13578 /* Parse a pure-specifier.
13579
13580    pure-specifier:
13581      = 0
13582
13583    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13584    Otherwise, ERROR_MARK_NODE is returned.  */
13585
13586 static tree
13587 cp_parser_pure_specifier (cp_parser* parser)
13588 {
13589   cp_token *token;
13590
13591   /* Look for the `=' token.  */
13592   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13593     return error_mark_node;
13594   /* Look for the `0' token.  */
13595   token = cp_lexer_consume_token (parser->lexer);
13596   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13597     {
13598       cp_parser_error (parser,
13599                        "invalid pure specifier (only `= 0' is allowed)");
13600       cp_parser_skip_to_end_of_statement (parser);
13601       return error_mark_node;
13602     }
13603
13604   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13605      We need to get information from the lexer about how the number
13606      was spelled in order to fix this problem.  */
13607   return integer_zero_node;
13608 }
13609
13610 /* Parse a constant-initializer.
13611
13612    constant-initializer:
13613      = constant-expression
13614
13615    Returns a representation of the constant-expression.  */
13616
13617 static tree
13618 cp_parser_constant_initializer (cp_parser* parser)
13619 {
13620   /* Look for the `=' token.  */
13621   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13622     return error_mark_node;
13623
13624   /* It is invalid to write:
13625
13626        struct S { static const int i = { 7 }; };
13627
13628      */
13629   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13630     {
13631       cp_parser_error (parser,
13632                        "a brace-enclosed initializer is not allowed here");
13633       /* Consume the opening brace.  */
13634       cp_lexer_consume_token (parser->lexer);
13635       /* Skip the initializer.  */
13636       cp_parser_skip_to_closing_brace (parser);
13637       /* Look for the trailing `}'.  */
13638       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13639
13640       return error_mark_node;
13641     }
13642
13643   return cp_parser_constant_expression (parser,
13644                                         /*allow_non_constant=*/false,
13645                                         NULL);
13646 }
13647
13648 /* Derived classes [gram.class.derived] */
13649
13650 /* Parse a base-clause.
13651
13652    base-clause:
13653      : base-specifier-list
13654
13655    base-specifier-list:
13656      base-specifier
13657      base-specifier-list , base-specifier
13658
13659    Returns a TREE_LIST representing the base-classes, in the order in
13660    which they were declared.  The representation of each node is as
13661    described by cp_parser_base_specifier.
13662
13663    In the case that no bases are specified, this function will return
13664    NULL_TREE, not ERROR_MARK_NODE.  */
13665
13666 static tree
13667 cp_parser_base_clause (cp_parser* parser)
13668 {
13669   tree bases = NULL_TREE;
13670
13671   /* Look for the `:' that begins the list.  */
13672   cp_parser_require (parser, CPP_COLON, "`:'");
13673
13674   /* Scan the base-specifier-list.  */
13675   while (true)
13676     {
13677       cp_token *token;
13678       tree base;
13679
13680       /* Look for the base-specifier.  */
13681       base = cp_parser_base_specifier (parser);
13682       /* Add BASE to the front of the list.  */
13683       if (base != error_mark_node)
13684         {
13685           TREE_CHAIN (base) = bases;
13686           bases = base;
13687         }
13688       /* Peek at the next token.  */
13689       token = cp_lexer_peek_token (parser->lexer);
13690       /* If it's not a comma, then the list is complete.  */
13691       if (token->type != CPP_COMMA)
13692         break;
13693       /* Consume the `,'.  */
13694       cp_lexer_consume_token (parser->lexer);
13695     }
13696
13697   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13698      base class had a qualified name.  However, the next name that
13699      appears is certainly not qualified.  */
13700   parser->scope = NULL_TREE;
13701   parser->qualifying_scope = NULL_TREE;
13702   parser->object_scope = NULL_TREE;
13703
13704   return nreverse (bases);
13705 }
13706
13707 /* Parse a base-specifier.
13708
13709    base-specifier:
13710      :: [opt] nested-name-specifier [opt] class-name
13711      virtual access-specifier [opt] :: [opt] nested-name-specifier
13712        [opt] class-name
13713      access-specifier virtual [opt] :: [opt] nested-name-specifier
13714        [opt] class-name
13715
13716    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13717    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13718    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13719    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13720
13721 static tree
13722 cp_parser_base_specifier (cp_parser* parser)
13723 {
13724   cp_token *token;
13725   bool done = false;
13726   bool virtual_p = false;
13727   bool duplicate_virtual_error_issued_p = false;
13728   bool duplicate_access_error_issued_p = false;
13729   bool class_scope_p, template_p;
13730   tree access = access_default_node;
13731   tree type;
13732
13733   /* Process the optional `virtual' and `access-specifier'.  */
13734   while (!done)
13735     {
13736       /* Peek at the next token.  */
13737       token = cp_lexer_peek_token (parser->lexer);
13738       /* Process `virtual'.  */
13739       switch (token->keyword)
13740         {
13741         case RID_VIRTUAL:
13742           /* If `virtual' appears more than once, issue an error.  */
13743           if (virtual_p && !duplicate_virtual_error_issued_p)
13744             {
13745               cp_parser_error (parser,
13746                                "%<virtual%> specified more than once in base-specified");
13747               duplicate_virtual_error_issued_p = true;
13748             }
13749
13750           virtual_p = true;
13751
13752           /* Consume the `virtual' token.  */
13753           cp_lexer_consume_token (parser->lexer);
13754
13755           break;
13756
13757         case RID_PUBLIC:
13758         case RID_PROTECTED:
13759         case RID_PRIVATE:
13760           /* If more than one access specifier appears, issue an
13761              error.  */
13762           if (access != access_default_node
13763               && !duplicate_access_error_issued_p)
13764             {
13765               cp_parser_error (parser,
13766                                "more than one access specifier in base-specified");
13767               duplicate_access_error_issued_p = true;
13768             }
13769
13770           access = ridpointers[(int) token->keyword];
13771
13772           /* Consume the access-specifier.  */
13773           cp_lexer_consume_token (parser->lexer);
13774
13775           break;
13776
13777         default:
13778           done = true;
13779           break;
13780         }
13781     }
13782   /* It is not uncommon to see programs mechanically, erroneously, use
13783      the 'typename' keyword to denote (dependent) qualified types
13784      as base classes.  */
13785   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13786     {
13787       if (!processing_template_decl)
13788         error ("keyword %<typename%> not allowed outside of templates");
13789       else
13790         error ("keyword %<typename%> not allowed in this context "
13791                "(the base class is implicitly a type)");
13792       cp_lexer_consume_token (parser->lexer);
13793     }
13794
13795   /* Look for the optional `::' operator.  */
13796   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13797   /* Look for the nested-name-specifier.  The simplest way to
13798      implement:
13799
13800        [temp.res]
13801
13802        The keyword `typename' is not permitted in a base-specifier or
13803        mem-initializer; in these contexts a qualified name that
13804        depends on a template-parameter is implicitly assumed to be a
13805        type name.
13806
13807      is to pretend that we have seen the `typename' keyword at this
13808      point.  */
13809   cp_parser_nested_name_specifier_opt (parser,
13810                                        /*typename_keyword_p=*/true,
13811                                        /*check_dependency_p=*/true,
13812                                        typename_type,
13813                                        /*is_declaration=*/true);
13814   /* If the base class is given by a qualified name, assume that names
13815      we see are type names or templates, as appropriate.  */
13816   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13817   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13818
13819   /* Finally, look for the class-name.  */
13820   type = cp_parser_class_name (parser,
13821                                class_scope_p,
13822                                template_p,
13823                                typename_type,
13824                                /*check_dependency_p=*/true,
13825                                /*class_head_p=*/false,
13826                                /*is_declaration=*/true);
13827
13828   if (type == error_mark_node)
13829     return error_mark_node;
13830
13831   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13832 }
13833
13834 /* Exception handling [gram.exception] */
13835
13836 /* Parse an (optional) exception-specification.
13837
13838    exception-specification:
13839      throw ( type-id-list [opt] )
13840
13841    Returns a TREE_LIST representing the exception-specification.  The
13842    TREE_VALUE of each node is a type.  */
13843
13844 static tree
13845 cp_parser_exception_specification_opt (cp_parser* parser)
13846 {
13847   cp_token *token;
13848   tree type_id_list;
13849
13850   /* Peek at the next token.  */
13851   token = cp_lexer_peek_token (parser->lexer);
13852   /* If it's not `throw', then there's no exception-specification.  */
13853   if (!cp_parser_is_keyword (token, RID_THROW))
13854     return NULL_TREE;
13855
13856   /* Consume the `throw'.  */
13857   cp_lexer_consume_token (parser->lexer);
13858
13859   /* Look for the `('.  */
13860   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13861
13862   /* Peek at the next token.  */
13863   token = cp_lexer_peek_token (parser->lexer);
13864   /* If it's not a `)', then there is a type-id-list.  */
13865   if (token->type != CPP_CLOSE_PAREN)
13866     {
13867       const char *saved_message;
13868
13869       /* Types may not be defined in an exception-specification.  */
13870       saved_message = parser->type_definition_forbidden_message;
13871       parser->type_definition_forbidden_message
13872         = "types may not be defined in an exception-specification";
13873       /* Parse the type-id-list.  */
13874       type_id_list = cp_parser_type_id_list (parser);
13875       /* Restore the saved message.  */
13876       parser->type_definition_forbidden_message = saved_message;
13877     }
13878   else
13879     type_id_list = empty_except_spec;
13880
13881   /* Look for the `)'.  */
13882   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13883
13884   return type_id_list;
13885 }
13886
13887 /* Parse an (optional) type-id-list.
13888
13889    type-id-list:
13890      type-id
13891      type-id-list , type-id
13892
13893    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13894    in the order that the types were presented.  */
13895
13896 static tree
13897 cp_parser_type_id_list (cp_parser* parser)
13898 {
13899   tree types = NULL_TREE;
13900
13901   while (true)
13902     {
13903       cp_token *token;
13904       tree type;
13905
13906       /* Get the next type-id.  */
13907       type = cp_parser_type_id (parser);
13908       /* Add it to the list.  */
13909       types = add_exception_specifier (types, type, /*complain=*/1);
13910       /* Peek at the next token.  */
13911       token = cp_lexer_peek_token (parser->lexer);
13912       /* If it is not a `,', we are done.  */
13913       if (token->type != CPP_COMMA)
13914         break;
13915       /* Consume the `,'.  */
13916       cp_lexer_consume_token (parser->lexer);
13917     }
13918
13919   return nreverse (types);
13920 }
13921
13922 /* Parse a try-block.
13923
13924    try-block:
13925      try compound-statement handler-seq  */
13926
13927 static tree
13928 cp_parser_try_block (cp_parser* parser)
13929 {
13930   tree try_block;
13931
13932   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13933   try_block = begin_try_block ();
13934   cp_parser_compound_statement (parser, NULL, true);
13935   finish_try_block (try_block);
13936   cp_parser_handler_seq (parser);
13937   finish_handler_sequence (try_block);
13938
13939   return try_block;
13940 }
13941
13942 /* Parse a function-try-block.
13943
13944    function-try-block:
13945      try ctor-initializer [opt] function-body handler-seq  */
13946
13947 static bool
13948 cp_parser_function_try_block (cp_parser* parser)
13949 {
13950   tree try_block;
13951   bool ctor_initializer_p;
13952
13953   /* Look for the `try' keyword.  */
13954   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13955     return false;
13956   /* Let the rest of the front-end know where we are.  */
13957   try_block = begin_function_try_block ();
13958   /* Parse the function-body.  */
13959   ctor_initializer_p
13960     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13961   /* We're done with the `try' part.  */
13962   finish_function_try_block (try_block);
13963   /* Parse the handlers.  */
13964   cp_parser_handler_seq (parser);
13965   /* We're done with the handlers.  */
13966   finish_function_handler_sequence (try_block);
13967
13968   return ctor_initializer_p;
13969 }
13970
13971 /* Parse a handler-seq.
13972
13973    handler-seq:
13974      handler handler-seq [opt]  */
13975
13976 static void
13977 cp_parser_handler_seq (cp_parser* parser)
13978 {
13979   while (true)
13980     {
13981       cp_token *token;
13982
13983       /* Parse the handler.  */
13984       cp_parser_handler (parser);
13985       /* Peek at the next token.  */
13986       token = cp_lexer_peek_token (parser->lexer);
13987       /* If it's not `catch' then there are no more handlers.  */
13988       if (!cp_parser_is_keyword (token, RID_CATCH))
13989         break;
13990     }
13991 }
13992
13993 /* Parse a handler.
13994
13995    handler:
13996      catch ( exception-declaration ) compound-statement  */
13997
13998 static void
13999 cp_parser_handler (cp_parser* parser)
14000 {
14001   tree handler;
14002   tree declaration;
14003
14004   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14005   handler = begin_handler ();
14006   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14007   declaration = cp_parser_exception_declaration (parser);
14008   finish_handler_parms (declaration, handler);
14009   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14010   cp_parser_compound_statement (parser, NULL, false);
14011   finish_handler (handler);
14012 }
14013
14014 /* Parse an exception-declaration.
14015
14016    exception-declaration:
14017      type-specifier-seq declarator
14018      type-specifier-seq abstract-declarator
14019      type-specifier-seq
14020      ...
14021
14022    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14023    ellipsis variant is used.  */
14024
14025 static tree
14026 cp_parser_exception_declaration (cp_parser* parser)
14027 {
14028   tree decl;
14029   cp_decl_specifier_seq type_specifiers;
14030   cp_declarator *declarator;
14031   const char *saved_message;
14032
14033   /* If it's an ellipsis, it's easy to handle.  */
14034   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14035     {
14036       /* Consume the `...' token.  */
14037       cp_lexer_consume_token (parser->lexer);
14038       return NULL_TREE;
14039     }
14040
14041   /* Types may not be defined in exception-declarations.  */
14042   saved_message = parser->type_definition_forbidden_message;
14043   parser->type_definition_forbidden_message
14044     = "types may not be defined in exception-declarations";
14045
14046   /* Parse the type-specifier-seq.  */
14047   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14048                                 &type_specifiers);
14049   /* If it's a `)', then there is no declarator.  */
14050   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14051     declarator = NULL;
14052   else
14053     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14054                                        /*ctor_dtor_or_conv_p=*/NULL,
14055                                        /*parenthesized_p=*/NULL,
14056                                        /*member_p=*/false);
14057
14058   /* Restore the saved message.  */
14059   parser->type_definition_forbidden_message = saved_message;
14060
14061   if (type_specifiers.any_specifiers_p)
14062     {
14063       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14064       if (decl == NULL_TREE)
14065         error ("invalid catch parameter");
14066     }
14067   else
14068     decl = NULL_TREE;
14069
14070   return decl;
14071 }
14072
14073 /* Parse a throw-expression.
14074
14075    throw-expression:
14076      throw assignment-expression [opt]
14077
14078    Returns a THROW_EXPR representing the throw-expression.  */
14079
14080 static tree
14081 cp_parser_throw_expression (cp_parser* parser)
14082 {
14083   tree expression;
14084   cp_token* token;
14085
14086   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14087   token = cp_lexer_peek_token (parser->lexer);
14088   /* Figure out whether or not there is an assignment-expression
14089      following the "throw" keyword.  */
14090   if (token->type == CPP_COMMA
14091       || token->type == CPP_SEMICOLON
14092       || token->type == CPP_CLOSE_PAREN
14093       || token->type == CPP_CLOSE_SQUARE
14094       || token->type == CPP_CLOSE_BRACE
14095       || token->type == CPP_COLON)
14096     expression = NULL_TREE;
14097   else
14098     expression = cp_parser_assignment_expression (parser,
14099                                                   /*cast_p=*/false);
14100
14101   return build_throw (expression);
14102 }
14103
14104 /* GNU Extensions */
14105
14106 /* Parse an (optional) asm-specification.
14107
14108    asm-specification:
14109      asm ( string-literal )
14110
14111    If the asm-specification is present, returns a STRING_CST
14112    corresponding to the string-literal.  Otherwise, returns
14113    NULL_TREE.  */
14114
14115 static tree
14116 cp_parser_asm_specification_opt (cp_parser* parser)
14117 {
14118   cp_token *token;
14119   tree asm_specification;
14120
14121   /* Peek at the next token.  */
14122   token = cp_lexer_peek_token (parser->lexer);
14123   /* If the next token isn't the `asm' keyword, then there's no
14124      asm-specification.  */
14125   if (!cp_parser_is_keyword (token, RID_ASM))
14126     return NULL_TREE;
14127
14128   /* Consume the `asm' token.  */
14129   cp_lexer_consume_token (parser->lexer);
14130   /* Look for the `('.  */
14131   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14132
14133   /* Look for the string-literal.  */
14134   asm_specification = cp_parser_string_literal (parser, false, false);
14135
14136   /* Look for the `)'.  */
14137   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14138
14139   return asm_specification;
14140 }
14141
14142 /* Parse an asm-operand-list.
14143
14144    asm-operand-list:
14145      asm-operand
14146      asm-operand-list , asm-operand
14147
14148    asm-operand:
14149      string-literal ( expression )
14150      [ string-literal ] string-literal ( expression )
14151
14152    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14153    each node is the expression.  The TREE_PURPOSE is itself a
14154    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14155    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14156    is a STRING_CST for the string literal before the parenthesis.  */
14157
14158 static tree
14159 cp_parser_asm_operand_list (cp_parser* parser)
14160 {
14161   tree asm_operands = NULL_TREE;
14162
14163   while (true)
14164     {
14165       tree string_literal;
14166       tree expression;
14167       tree name;
14168
14169       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14170         {
14171           /* Consume the `[' token.  */
14172           cp_lexer_consume_token (parser->lexer);
14173           /* Read the operand name.  */
14174           name = cp_parser_identifier (parser);
14175           if (name != error_mark_node)
14176             name = build_string (IDENTIFIER_LENGTH (name),
14177                                  IDENTIFIER_POINTER (name));
14178           /* Look for the closing `]'.  */
14179           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14180         }
14181       else
14182         name = NULL_TREE;
14183       /* Look for the string-literal.  */
14184       string_literal = cp_parser_string_literal (parser, false, false);
14185
14186       /* Look for the `('.  */
14187       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14188       /* Parse the expression.  */
14189       expression = cp_parser_expression (parser, /*cast_p=*/false);
14190       /* Look for the `)'.  */
14191       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14192
14193       /* Add this operand to the list.  */
14194       asm_operands = tree_cons (build_tree_list (name, string_literal),
14195                                 expression,
14196                                 asm_operands);
14197       /* If the next token is not a `,', there are no more
14198          operands.  */
14199       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14200         break;
14201       /* Consume the `,'.  */
14202       cp_lexer_consume_token (parser->lexer);
14203     }
14204
14205   return nreverse (asm_operands);
14206 }
14207
14208 /* Parse an asm-clobber-list.
14209
14210    asm-clobber-list:
14211      string-literal
14212      asm-clobber-list , string-literal
14213
14214    Returns a TREE_LIST, indicating the clobbers in the order that they
14215    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14216
14217 static tree
14218 cp_parser_asm_clobber_list (cp_parser* parser)
14219 {
14220   tree clobbers = NULL_TREE;
14221
14222   while (true)
14223     {
14224       tree string_literal;
14225
14226       /* Look for the string literal.  */
14227       string_literal = cp_parser_string_literal (parser, false, false);
14228       /* Add it to the list.  */
14229       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14230       /* If the next token is not a `,', then the list is
14231          complete.  */
14232       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14233         break;
14234       /* Consume the `,' token.  */
14235       cp_lexer_consume_token (parser->lexer);
14236     }
14237
14238   return clobbers;
14239 }
14240
14241 /* Parse an (optional) series of attributes.
14242
14243    attributes:
14244      attributes attribute
14245
14246    attribute:
14247      __attribute__ (( attribute-list [opt] ))
14248
14249    The return value is as for cp_parser_attribute_list.  */
14250
14251 static tree
14252 cp_parser_attributes_opt (cp_parser* parser)
14253 {
14254   tree attributes = NULL_TREE;
14255
14256   while (true)
14257     {
14258       cp_token *token;
14259       tree attribute_list;
14260
14261       /* Peek at the next token.  */
14262       token = cp_lexer_peek_token (parser->lexer);
14263       /* If it's not `__attribute__', then we're done.  */
14264       if (token->keyword != RID_ATTRIBUTE)
14265         break;
14266
14267       /* Consume the `__attribute__' keyword.  */
14268       cp_lexer_consume_token (parser->lexer);
14269       /* Look for the two `(' tokens.  */
14270       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14271       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14272
14273       /* Peek at the next token.  */
14274       token = cp_lexer_peek_token (parser->lexer);
14275       if (token->type != CPP_CLOSE_PAREN)
14276         /* Parse the attribute-list.  */
14277         attribute_list = cp_parser_attribute_list (parser);
14278       else
14279         /* If the next token is a `)', then there is no attribute
14280            list.  */
14281         attribute_list = NULL;
14282
14283       /* Look for the two `)' tokens.  */
14284       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14285       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14286
14287       /* Add these new attributes to the list.  */
14288       attributes = chainon (attributes, attribute_list);
14289     }
14290
14291   return attributes;
14292 }
14293
14294 /* Parse an attribute-list.
14295
14296    attribute-list:
14297      attribute
14298      attribute-list , attribute
14299
14300    attribute:
14301      identifier
14302      identifier ( identifier )
14303      identifier ( identifier , expression-list )
14304      identifier ( expression-list )
14305
14306    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14307    to an attribute.  The TREE_PURPOSE of each node is the identifier
14308    indicating which attribute is in use.  The TREE_VALUE represents
14309    the arguments, if any.  */
14310
14311 static tree
14312 cp_parser_attribute_list (cp_parser* parser)
14313 {
14314   tree attribute_list = NULL_TREE;
14315   bool save_translate_strings_p = parser->translate_strings_p;
14316
14317   parser->translate_strings_p = false;
14318   while (true)
14319     {
14320       cp_token *token;
14321       tree identifier;
14322       tree attribute;
14323
14324       /* Look for the identifier.  We also allow keywords here; for
14325          example `__attribute__ ((const))' is legal.  */
14326       token = cp_lexer_peek_token (parser->lexer);
14327       if (token->type == CPP_NAME
14328           || token->type == CPP_KEYWORD)
14329         {
14330           /* Consume the token.  */
14331           token = cp_lexer_consume_token (parser->lexer);
14332
14333           /* Save away the identifier that indicates which attribute
14334              this is.  */
14335           identifier = token->value;
14336           attribute = build_tree_list (identifier, NULL_TREE);
14337
14338           /* Peek at the next token.  */
14339           token = cp_lexer_peek_token (parser->lexer);
14340           /* If it's an `(', then parse the attribute arguments.  */
14341           if (token->type == CPP_OPEN_PAREN)
14342             {
14343               tree arguments;
14344
14345               arguments = (cp_parser_parenthesized_expression_list
14346                            (parser, true, /*cast_p=*/false,
14347                             /*non_constant_p=*/NULL));
14348               /* Save the identifier and arguments away.  */
14349               TREE_VALUE (attribute) = arguments;
14350             }
14351
14352           /* Add this attribute to the list.  */
14353           TREE_CHAIN (attribute) = attribute_list;
14354           attribute_list = attribute;
14355
14356           token = cp_lexer_peek_token (parser->lexer);
14357         }
14358       /* Now, look for more attributes.  If the next token isn't a
14359          `,', we're done.  */
14360       if (token->type != CPP_COMMA)
14361         break;
14362
14363       /* Consume the comma and keep going.  */
14364       cp_lexer_consume_token (parser->lexer);
14365     }
14366   parser->translate_strings_p = save_translate_strings_p;
14367
14368   /* We built up the list in reverse order.  */
14369   return nreverse (attribute_list);
14370 }
14371
14372 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14373    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14374    current value of the PEDANTIC flag, regardless of whether or not
14375    the `__extension__' keyword is present.  The caller is responsible
14376    for restoring the value of the PEDANTIC flag.  */
14377
14378 static bool
14379 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14380 {
14381   /* Save the old value of the PEDANTIC flag.  */
14382   *saved_pedantic = pedantic;
14383
14384   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14385     {
14386       /* Consume the `__extension__' token.  */
14387       cp_lexer_consume_token (parser->lexer);
14388       /* We're not being pedantic while the `__extension__' keyword is
14389          in effect.  */
14390       pedantic = 0;
14391
14392       return true;
14393     }
14394
14395   return false;
14396 }
14397
14398 /* Parse a label declaration.
14399
14400    label-declaration:
14401      __label__ label-declarator-seq ;
14402
14403    label-declarator-seq:
14404      identifier , label-declarator-seq
14405      identifier  */
14406
14407 static void
14408 cp_parser_label_declaration (cp_parser* parser)
14409 {
14410   /* Look for the `__label__' keyword.  */
14411   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14412
14413   while (true)
14414     {
14415       tree identifier;
14416
14417       /* Look for an identifier.  */
14418       identifier = cp_parser_identifier (parser);
14419       /* If we failed, stop.  */
14420       if (identifier == error_mark_node)
14421         break;
14422       /* Declare it as a label.  */
14423       finish_label_decl (identifier);
14424       /* If the next token is a `;', stop.  */
14425       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14426         break;
14427       /* Look for the `,' separating the label declarations.  */
14428       cp_parser_require (parser, CPP_COMMA, "`,'");
14429     }
14430
14431   /* Look for the final `;'.  */
14432   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14433 }
14434
14435 /* Support Functions */
14436
14437 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14438    NAME should have one of the representations used for an
14439    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14440    is returned.  If PARSER->SCOPE is a dependent type, then a
14441    SCOPE_REF is returned.
14442
14443    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14444    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14445    was formed.  Abstractly, such entities should not be passed to this
14446    function, because they do not need to be looked up, but it is
14447    simpler to check for this special case here, rather than at the
14448    call-sites.
14449
14450    In cases not explicitly covered above, this function returns a
14451    DECL, OVERLOAD, or baselink representing the result of the lookup.
14452    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14453    is returned.
14454
14455    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14456    (e.g., "struct") that was used.  In that case bindings that do not
14457    refer to types are ignored.
14458
14459    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14460    ignored.
14461
14462    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14463    are ignored.
14464
14465    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14466    types.
14467
14468    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14469    results in an ambiguity, and false otherwise.  */
14470
14471 static tree
14472 cp_parser_lookup_name (cp_parser *parser, tree name,
14473                        enum tag_types tag_type,
14474                        bool is_template, 
14475                        bool is_namespace,
14476                        bool check_dependency,
14477                        bool *ambiguous_p)
14478 {
14479   int flags = 0;
14480   tree decl;
14481   tree object_type = parser->context->object_type;
14482
14483   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14484     flags |= LOOKUP_COMPLAIN;
14485
14486   /* Assume that the lookup will be unambiguous.  */
14487   if (ambiguous_p)
14488     *ambiguous_p = false;
14489
14490   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14491      no longer valid.  Note that if we are parsing tentatively, and
14492      the parse fails, OBJECT_TYPE will be automatically restored.  */
14493   parser->context->object_type = NULL_TREE;
14494
14495   if (name == error_mark_node)
14496     return error_mark_node;
14497
14498   /* A template-id has already been resolved; there is no lookup to
14499      do.  */
14500   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14501     return name;
14502   if (BASELINK_P (name))
14503     {
14504       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14505                   == TEMPLATE_ID_EXPR);
14506       return name;
14507     }
14508
14509   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14510      it should already have been checked to make sure that the name
14511      used matches the type being destroyed.  */
14512   if (TREE_CODE (name) == BIT_NOT_EXPR)
14513     {
14514       tree type;
14515
14516       /* Figure out to which type this destructor applies.  */
14517       if (parser->scope)
14518         type = parser->scope;
14519       else if (object_type)
14520         type = object_type;
14521       else
14522         type = current_class_type;
14523       /* If that's not a class type, there is no destructor.  */
14524       if (!type || !CLASS_TYPE_P (type))
14525         return error_mark_node;
14526       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14527         lazily_declare_fn (sfk_destructor, type);
14528       if (!CLASSTYPE_DESTRUCTORS (type))
14529           return error_mark_node;
14530       /* If it was a class type, return the destructor.  */
14531       return CLASSTYPE_DESTRUCTORS (type);
14532     }
14533
14534   /* By this point, the NAME should be an ordinary identifier.  If
14535      the id-expression was a qualified name, the qualifying scope is
14536      stored in PARSER->SCOPE at this point.  */
14537   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14538
14539   /* Perform the lookup.  */
14540   if (parser->scope)
14541     {
14542       bool dependent_p;
14543
14544       if (parser->scope == error_mark_node)
14545         return error_mark_node;
14546
14547       /* If the SCOPE is dependent, the lookup must be deferred until
14548          the template is instantiated -- unless we are explicitly
14549          looking up names in uninstantiated templates.  Even then, we
14550          cannot look up the name if the scope is not a class type; it
14551          might, for example, be a template type parameter.  */
14552       dependent_p = (TYPE_P (parser->scope)
14553                      && !(parser->in_declarator_p
14554                           && currently_open_class (parser->scope))
14555                      && dependent_type_p (parser->scope));
14556       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14557            && dependent_p)
14558         {
14559           if (tag_type)
14560             {
14561               tree type;
14562
14563               /* The resolution to Core Issue 180 says that `struct
14564                  A::B' should be considered a type-name, even if `A'
14565                  is dependent.  */
14566               type = make_typename_type (parser->scope, name, tag_type,
14567                                          /*complain=*/1);
14568               decl = TYPE_NAME (type);
14569             }
14570           else if (is_template
14571                    && (cp_parser_next_token_ends_template_argument_p (parser)
14572                        || cp_lexer_next_token_is (parser->lexer,
14573                                                   CPP_CLOSE_PAREN)))
14574             decl = make_unbound_class_template (parser->scope,
14575                                                 name, NULL_TREE,
14576                                                 /*complain=*/1);
14577           else
14578             decl = build_qualified_name (/*type=*/NULL_TREE,
14579                                          parser->scope, name,
14580                                          is_template);
14581         }
14582       else
14583         {
14584           tree pushed_scope = NULL_TREE;
14585
14586           /* If PARSER->SCOPE is a dependent type, then it must be a
14587              class type, and we must not be checking dependencies;
14588              otherwise, we would have processed this lookup above.  So
14589              that PARSER->SCOPE is not considered a dependent base by
14590              lookup_member, we must enter the scope here.  */
14591           if (dependent_p)
14592             pushed_scope = push_scope (parser->scope);
14593           /* If the PARSER->SCOPE is a template specialization, it
14594              may be instantiated during name lookup.  In that case,
14595              errors may be issued.  Even if we rollback the current
14596              tentative parse, those errors are valid.  */
14597           decl = lookup_qualified_name (parser->scope, name,
14598                                         tag_type != none_type,
14599                                         /*complain=*/true);
14600           if (pushed_scope)
14601             pop_scope (pushed_scope);
14602         }
14603       parser->qualifying_scope = parser->scope;
14604       parser->object_scope = NULL_TREE;
14605     }
14606   else if (object_type)
14607     {
14608       tree object_decl = NULL_TREE;
14609       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14610          OBJECT_TYPE is not a class.  */
14611       if (CLASS_TYPE_P (object_type))
14612         /* If the OBJECT_TYPE is a template specialization, it may
14613            be instantiated during name lookup.  In that case, errors
14614            may be issued.  Even if we rollback the current tentative
14615            parse, those errors are valid.  */
14616         object_decl = lookup_member (object_type,
14617                                      name,
14618                                      /*protect=*/0,
14619                                      tag_type != none_type);
14620       /* Look it up in the enclosing context, too.  */
14621       decl = lookup_name_real (name, tag_type != none_type,
14622                                /*nonclass=*/0,
14623                                /*block_p=*/true, is_namespace, flags);
14624       parser->object_scope = object_type;
14625       parser->qualifying_scope = NULL_TREE;
14626       if (object_decl)
14627         decl = object_decl;
14628     }
14629   else
14630     {
14631       decl = lookup_name_real (name, tag_type != none_type,
14632                                /*nonclass=*/0,
14633                                /*block_p=*/true, is_namespace, flags);
14634       parser->qualifying_scope = NULL_TREE;
14635       parser->object_scope = NULL_TREE;
14636     }
14637
14638   /* If the lookup failed, let our caller know.  */
14639   if (!decl || decl == error_mark_node)
14640     return error_mark_node;
14641
14642   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14643   if (TREE_CODE (decl) == TREE_LIST)
14644     {
14645       if (ambiguous_p)
14646         *ambiguous_p = true;
14647       /* The error message we have to print is too complicated for
14648          cp_parser_error, so we incorporate its actions directly.  */
14649       if (!cp_parser_simulate_error (parser))
14650         {
14651           error ("reference to %qD is ambiguous", name);
14652           print_candidates (decl);
14653         }
14654       return error_mark_node;
14655     }
14656
14657   gcc_assert (DECL_P (decl)
14658               || TREE_CODE (decl) == OVERLOAD
14659               || TREE_CODE (decl) == SCOPE_REF
14660               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14661               || BASELINK_P (decl));
14662
14663   /* If we have resolved the name of a member declaration, check to
14664      see if the declaration is accessible.  When the name resolves to
14665      set of overloaded functions, accessibility is checked when
14666      overload resolution is done.
14667
14668      During an explicit instantiation, access is not checked at all,
14669      as per [temp.explicit].  */
14670   if (DECL_P (decl))
14671     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14672
14673   return decl;
14674 }
14675
14676 /* Like cp_parser_lookup_name, but for use in the typical case where
14677    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14678    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14679
14680 static tree
14681 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14682 {
14683   return cp_parser_lookup_name (parser, name,
14684                                 none_type,
14685                                 /*is_template=*/false,
14686                                 /*is_namespace=*/false,
14687                                 /*check_dependency=*/true,
14688                                 /*ambiguous_p=*/NULL);
14689 }
14690
14691 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14692    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14693    true, the DECL indicates the class being defined in a class-head,
14694    or declared in an elaborated-type-specifier.
14695
14696    Otherwise, return DECL.  */
14697
14698 static tree
14699 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14700 {
14701   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14702      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14703
14704        struct A {
14705          template <typename T> struct B;
14706        };
14707
14708        template <typename T> struct A::B {};
14709
14710      Similarly, in an elaborated-type-specifier:
14711
14712        namespace N { struct X{}; }
14713
14714        struct A {
14715          template <typename T> friend struct N::X;
14716        };
14717
14718      However, if the DECL refers to a class type, and we are in
14719      the scope of the class, then the name lookup automatically
14720      finds the TYPE_DECL created by build_self_reference rather
14721      than a TEMPLATE_DECL.  For example, in:
14722
14723        template <class T> struct S {
14724          S s;
14725        };
14726
14727      there is no need to handle such case.  */
14728
14729   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14730     return DECL_TEMPLATE_RESULT (decl);
14731
14732   return decl;
14733 }
14734
14735 /* If too many, or too few, template-parameter lists apply to the
14736    declarator, issue an error message.  Returns TRUE if all went well,
14737    and FALSE otherwise.  */
14738
14739 static bool
14740 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14741                                                 cp_declarator *declarator)
14742 {
14743   unsigned num_templates;
14744
14745   /* We haven't seen any classes that involve template parameters yet.  */
14746   num_templates = 0;
14747
14748   switch (declarator->kind)
14749     {
14750     case cdk_id:
14751       if (declarator->u.id.qualifying_scope)
14752         {
14753           tree scope;
14754           tree member;
14755
14756           scope = declarator->u.id.qualifying_scope;
14757           member = declarator->u.id.unqualified_name;
14758
14759           while (scope && CLASS_TYPE_P (scope))
14760             {
14761               /* You're supposed to have one `template <...>'
14762                  for every template class, but you don't need one
14763                  for a full specialization.  For example:
14764
14765                  template <class T> struct S{};
14766                  template <> struct S<int> { void f(); };
14767                  void S<int>::f () {}
14768
14769                  is correct; there shouldn't be a `template <>' for
14770                  the definition of `S<int>::f'.  */
14771               if (CLASSTYPE_TEMPLATE_INFO (scope)
14772                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14773                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14774                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14775                 ++num_templates;
14776
14777               scope = TYPE_CONTEXT (scope);
14778             }
14779         }
14780       else if (TREE_CODE (declarator->u.id.unqualified_name)
14781                == TEMPLATE_ID_EXPR)
14782         /* If the DECLARATOR has the form `X<y>' then it uses one
14783            additional level of template parameters.  */
14784         ++num_templates;
14785
14786       return cp_parser_check_template_parameters (parser,
14787                                                   num_templates);
14788
14789     case cdk_function:
14790     case cdk_array:
14791     case cdk_pointer:
14792     case cdk_reference:
14793     case cdk_ptrmem:
14794       return (cp_parser_check_declarator_template_parameters
14795               (parser, declarator->declarator));
14796
14797     case cdk_error:
14798       return true;
14799
14800     default:
14801       gcc_unreachable ();
14802     }
14803   return false;
14804 }
14805
14806 /* NUM_TEMPLATES were used in the current declaration.  If that is
14807    invalid, return FALSE and issue an error messages.  Otherwise,
14808    return TRUE.  */
14809
14810 static bool
14811 cp_parser_check_template_parameters (cp_parser* parser,
14812                                      unsigned num_templates)
14813 {
14814   /* If there are more template classes than parameter lists, we have
14815      something like:
14816
14817        template <class T> void S<T>::R<T>::f ();  */
14818   if (parser->num_template_parameter_lists < num_templates)
14819     {
14820       error ("too few template-parameter-lists");
14821       return false;
14822     }
14823   /* If there are the same number of template classes and parameter
14824      lists, that's OK.  */
14825   if (parser->num_template_parameter_lists == num_templates)
14826     return true;
14827   /* If there are more, but only one more, then we are referring to a
14828      member template.  That's OK too.  */
14829   if (parser->num_template_parameter_lists == num_templates + 1)
14830       return true;
14831   /* Otherwise, there are too many template parameter lists.  We have
14832      something like:
14833
14834      template <class T> template <class U> void S::f();  */
14835   error ("too many template-parameter-lists");
14836   return false;
14837 }
14838
14839 /* Parse an optional `::' token indicating that the following name is
14840    from the global namespace.  If so, PARSER->SCOPE is set to the
14841    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14842    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14843    Returns the new value of PARSER->SCOPE, if the `::' token is
14844    present, and NULL_TREE otherwise.  */
14845
14846 static tree
14847 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14848 {
14849   cp_token *token;
14850
14851   /* Peek at the next token.  */
14852   token = cp_lexer_peek_token (parser->lexer);
14853   /* If we're looking at a `::' token then we're starting from the
14854      global namespace, not our current location.  */
14855   if (token->type == CPP_SCOPE)
14856     {
14857       /* Consume the `::' token.  */
14858       cp_lexer_consume_token (parser->lexer);
14859       /* Set the SCOPE so that we know where to start the lookup.  */
14860       parser->scope = global_namespace;
14861       parser->qualifying_scope = global_namespace;
14862       parser->object_scope = NULL_TREE;
14863
14864       return parser->scope;
14865     }
14866   else if (!current_scope_valid_p)
14867     {
14868       parser->scope = NULL_TREE;
14869       parser->qualifying_scope = NULL_TREE;
14870       parser->object_scope = NULL_TREE;
14871     }
14872
14873   return NULL_TREE;
14874 }
14875
14876 /* Returns TRUE if the upcoming token sequence is the start of a
14877    constructor declarator.  If FRIEND_P is true, the declarator is
14878    preceded by the `friend' specifier.  */
14879
14880 static bool
14881 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14882 {
14883   bool constructor_p;
14884   tree type_decl = NULL_TREE;
14885   bool nested_name_p;
14886   cp_token *next_token;
14887
14888   /* The common case is that this is not a constructor declarator, so
14889      try to avoid doing lots of work if at all possible.  It's not
14890      valid declare a constructor at function scope.  */
14891   if (at_function_scope_p ())
14892     return false;
14893   /* And only certain tokens can begin a constructor declarator.  */
14894   next_token = cp_lexer_peek_token (parser->lexer);
14895   if (next_token->type != CPP_NAME
14896       && next_token->type != CPP_SCOPE
14897       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14898       && next_token->type != CPP_TEMPLATE_ID)
14899     return false;
14900
14901   /* Parse tentatively; we are going to roll back all of the tokens
14902      consumed here.  */
14903   cp_parser_parse_tentatively (parser);
14904   /* Assume that we are looking at a constructor declarator.  */
14905   constructor_p = true;
14906
14907   /* Look for the optional `::' operator.  */
14908   cp_parser_global_scope_opt (parser,
14909                               /*current_scope_valid_p=*/false);
14910   /* Look for the nested-name-specifier.  */
14911   nested_name_p
14912     = (cp_parser_nested_name_specifier_opt (parser,
14913                                             /*typename_keyword_p=*/false,
14914                                             /*check_dependency_p=*/false,
14915                                             /*type_p=*/false,
14916                                             /*is_declaration=*/false)
14917        != NULL_TREE);
14918   /* Outside of a class-specifier, there must be a
14919      nested-name-specifier.  */
14920   if (!nested_name_p &&
14921       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14922        || friend_p))
14923     constructor_p = false;
14924   /* If we still think that this might be a constructor-declarator,
14925      look for a class-name.  */
14926   if (constructor_p)
14927     {
14928       /* If we have:
14929
14930            template <typename T> struct S { S(); };
14931            template <typename T> S<T>::S ();
14932
14933          we must recognize that the nested `S' names a class.
14934          Similarly, for:
14935
14936            template <typename T> S<T>::S<T> ();
14937
14938          we must recognize that the nested `S' names a template.  */
14939       type_decl = cp_parser_class_name (parser,
14940                                         /*typename_keyword_p=*/false,
14941                                         /*template_keyword_p=*/false,
14942                                         none_type,
14943                                         /*check_dependency_p=*/false,
14944                                         /*class_head_p=*/false,
14945                                         /*is_declaration=*/false);
14946       /* If there was no class-name, then this is not a constructor.  */
14947       constructor_p = !cp_parser_error_occurred (parser);
14948     }
14949
14950   /* If we're still considering a constructor, we have to see a `(',
14951      to begin the parameter-declaration-clause, followed by either a
14952      `)', an `...', or a decl-specifier.  We need to check for a
14953      type-specifier to avoid being fooled into thinking that:
14954
14955        S::S (f) (int);
14956
14957      is a constructor.  (It is actually a function named `f' that
14958      takes one parameter (of type `int') and returns a value of type
14959      `S::S'.  */
14960   if (constructor_p
14961       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14962     {
14963       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14964           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14965           /* A parameter declaration begins with a decl-specifier,
14966              which is either the "attribute" keyword, a storage class
14967              specifier, or (usually) a type-specifier.  */
14968           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14969           && !cp_parser_storage_class_specifier_opt (parser))
14970         {
14971           tree type;
14972           tree pushed_scope = NULL_TREE;
14973           unsigned saved_num_template_parameter_lists;
14974
14975           /* Names appearing in the type-specifier should be looked up
14976              in the scope of the class.  */
14977           if (current_class_type)
14978             type = NULL_TREE;
14979           else
14980             {
14981               type = TREE_TYPE (type_decl);
14982               if (TREE_CODE (type) == TYPENAME_TYPE)
14983                 {
14984                   type = resolve_typename_type (type,
14985                                                 /*only_current_p=*/false);
14986                   if (type == error_mark_node)
14987                     {
14988                       cp_parser_abort_tentative_parse (parser);
14989                       return false;
14990                     }
14991                 }
14992               pushed_scope = push_scope (type);
14993             }
14994
14995           /* Inside the constructor parameter list, surrounding
14996              template-parameter-lists do not apply.  */
14997           saved_num_template_parameter_lists
14998             = parser->num_template_parameter_lists;
14999           parser->num_template_parameter_lists = 0;
15000
15001           /* Look for the type-specifier.  */
15002           cp_parser_type_specifier (parser,
15003                                     CP_PARSER_FLAGS_NONE,
15004                                     /*decl_specs=*/NULL,
15005                                     /*is_declarator=*/true,
15006                                     /*declares_class_or_enum=*/NULL,
15007                                     /*is_cv_qualifier=*/NULL);
15008
15009           parser->num_template_parameter_lists
15010             = saved_num_template_parameter_lists;
15011
15012           /* Leave the scope of the class.  */
15013           if (pushed_scope)
15014             pop_scope (pushed_scope);
15015
15016           constructor_p = !cp_parser_error_occurred (parser);
15017         }
15018     }
15019   else
15020     constructor_p = false;
15021   /* We did not really want to consume any tokens.  */
15022   cp_parser_abort_tentative_parse (parser);
15023
15024   return constructor_p;
15025 }
15026
15027 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15028    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15029    they must be performed once we are in the scope of the function.
15030
15031    Returns the function defined.  */
15032
15033 static tree
15034 cp_parser_function_definition_from_specifiers_and_declarator
15035   (cp_parser* parser,
15036    cp_decl_specifier_seq *decl_specifiers,
15037    tree attributes,
15038    const cp_declarator *declarator)
15039 {
15040   tree fn;
15041   bool success_p;
15042
15043   /* Begin the function-definition.  */
15044   success_p = start_function (decl_specifiers, declarator, attributes);
15045
15046   /* The things we're about to see are not directly qualified by any
15047      template headers we've seen thus far.  */
15048   reset_specialization ();
15049
15050   /* If there were names looked up in the decl-specifier-seq that we
15051      did not check, check them now.  We must wait until we are in the
15052      scope of the function to perform the checks, since the function
15053      might be a friend.  */
15054   perform_deferred_access_checks ();
15055
15056   if (!success_p)
15057     {
15058       /* Skip the entire function.  */
15059       error ("invalid function declaration");
15060       cp_parser_skip_to_end_of_block_or_statement (parser);
15061       fn = error_mark_node;
15062     }
15063   else
15064     fn = cp_parser_function_definition_after_declarator (parser,
15065                                                          /*inline_p=*/false);
15066
15067   return fn;
15068 }
15069
15070 /* Parse the part of a function-definition that follows the
15071    declarator.  INLINE_P is TRUE iff this function is an inline
15072    function defined with a class-specifier.
15073
15074    Returns the function defined.  */
15075
15076 static tree
15077 cp_parser_function_definition_after_declarator (cp_parser* parser,
15078                                                 bool inline_p)
15079 {
15080   tree fn;
15081   bool ctor_initializer_p = false;
15082   bool saved_in_unbraced_linkage_specification_p;
15083   unsigned saved_num_template_parameter_lists;
15084
15085   /* If the next token is `return', then the code may be trying to
15086      make use of the "named return value" extension that G++ used to
15087      support.  */
15088   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15089     {
15090       /* Consume the `return' keyword.  */
15091       cp_lexer_consume_token (parser->lexer);
15092       /* Look for the identifier that indicates what value is to be
15093          returned.  */
15094       cp_parser_identifier (parser);
15095       /* Issue an error message.  */
15096       error ("named return values are no longer supported");
15097       /* Skip tokens until we reach the start of the function body.  */
15098       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15099              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15100         cp_lexer_consume_token (parser->lexer);
15101     }
15102   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15103      anything declared inside `f'.  */
15104   saved_in_unbraced_linkage_specification_p
15105     = parser->in_unbraced_linkage_specification_p;
15106   parser->in_unbraced_linkage_specification_p = false;
15107   /* Inside the function, surrounding template-parameter-lists do not
15108      apply.  */
15109   saved_num_template_parameter_lists
15110     = parser->num_template_parameter_lists;
15111   parser->num_template_parameter_lists = 0;
15112   /* If the next token is `try', then we are looking at a
15113      function-try-block.  */
15114   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15115     ctor_initializer_p = cp_parser_function_try_block (parser);
15116   /* A function-try-block includes the function-body, so we only do
15117      this next part if we're not processing a function-try-block.  */
15118   else
15119     ctor_initializer_p
15120       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15121
15122   /* Finish the function.  */
15123   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15124                         (inline_p ? 2 : 0));
15125   /* Generate code for it, if necessary.  */
15126   expand_or_defer_fn (fn);
15127   /* Restore the saved values.  */
15128   parser->in_unbraced_linkage_specification_p
15129     = saved_in_unbraced_linkage_specification_p;
15130   parser->num_template_parameter_lists
15131     = saved_num_template_parameter_lists;
15132
15133   return fn;
15134 }
15135
15136 /* Parse a template-declaration, assuming that the `export' (and
15137    `extern') keywords, if present, has already been scanned.  MEMBER_P
15138    is as for cp_parser_template_declaration.  */
15139
15140 static void
15141 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15142 {
15143   tree decl = NULL_TREE;
15144   tree parameter_list;
15145   bool friend_p = false;
15146
15147   /* Look for the `template' keyword.  */
15148   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15149     return;
15150
15151   /* And the `<'.  */
15152   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15153     return;
15154
15155   /* If the next token is `>', then we have an invalid
15156      specialization.  Rather than complain about an invalid template
15157      parameter, issue an error message here.  */
15158   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15159     {
15160       cp_parser_error (parser, "invalid explicit specialization");
15161       begin_specialization ();
15162       parameter_list = NULL_TREE;
15163     }
15164   else
15165     {
15166       /* Parse the template parameters.  */
15167       begin_template_parm_list ();
15168       parameter_list = cp_parser_template_parameter_list (parser);
15169       parameter_list = end_template_parm_list (parameter_list);
15170     }
15171
15172   /* Look for the `>'.  */
15173   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15174   /* We just processed one more parameter list.  */
15175   ++parser->num_template_parameter_lists;
15176   /* If the next token is `template', there are more template
15177      parameters.  */
15178   if (cp_lexer_next_token_is_keyword (parser->lexer,
15179                                       RID_TEMPLATE))
15180     cp_parser_template_declaration_after_export (parser, member_p);
15181   else
15182     {
15183       /* There are no access checks when parsing a template, as we do not
15184          know if a specialization will be a friend.  */
15185       push_deferring_access_checks (dk_no_check);
15186
15187       decl = cp_parser_single_declaration (parser,
15188                                            member_p,
15189                                            &friend_p);
15190
15191       pop_deferring_access_checks ();
15192
15193       /* If this is a member template declaration, let the front
15194          end know.  */
15195       if (member_p && !friend_p && decl)
15196         {
15197           if (TREE_CODE (decl) == TYPE_DECL)
15198             cp_parser_check_access_in_redeclaration (decl);
15199
15200           decl = finish_member_template_decl (decl);
15201         }
15202       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15203         make_friend_class (current_class_type, TREE_TYPE (decl),
15204                            /*complain=*/true);
15205     }
15206   /* We are done with the current parameter list.  */
15207   --parser->num_template_parameter_lists;
15208
15209   /* Finish up.  */
15210   finish_template_decl (parameter_list);
15211
15212   /* Register member declarations.  */
15213   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15214     finish_member_declaration (decl);
15215
15216   /* If DECL is a function template, we must return to parse it later.
15217      (Even though there is no definition, there might be default
15218      arguments that need handling.)  */
15219   if (member_p && decl
15220       && (TREE_CODE (decl) == FUNCTION_DECL
15221           || DECL_FUNCTION_TEMPLATE_P (decl)))
15222     TREE_VALUE (parser->unparsed_functions_queues)
15223       = tree_cons (NULL_TREE, decl,
15224                    TREE_VALUE (parser->unparsed_functions_queues));
15225 }
15226
15227 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15228    `function-definition' sequence.  MEMBER_P is true, this declaration
15229    appears in a class scope.
15230
15231    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15232    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15233
15234 static tree
15235 cp_parser_single_declaration (cp_parser* parser,
15236                               bool member_p,
15237                               bool* friend_p)
15238 {
15239   int declares_class_or_enum;
15240   tree decl = NULL_TREE;
15241   cp_decl_specifier_seq decl_specifiers;
15242   bool function_definition_p = false;
15243
15244   /* This function is only used when processing a template
15245      declaration.  */
15246   gcc_assert (innermost_scope_kind () == sk_template_parms
15247               || innermost_scope_kind () == sk_template_spec);
15248
15249   /* Defer access checks until we know what is being declared.  */
15250   push_deferring_access_checks (dk_deferred);
15251
15252   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15253      alternative.  */
15254   cp_parser_decl_specifier_seq (parser,
15255                                 CP_PARSER_FLAGS_OPTIONAL,
15256                                 &decl_specifiers,
15257                                 &declares_class_or_enum);
15258   if (friend_p)
15259     *friend_p = cp_parser_friend_p (&decl_specifiers);
15260
15261   /* There are no template typedefs.  */
15262   if (decl_specifiers.specs[(int) ds_typedef])
15263     {
15264       error ("template declaration of %qs", "typedef");
15265       decl = error_mark_node;
15266     }
15267
15268   /* Gather up the access checks that occurred the
15269      decl-specifier-seq.  */
15270   stop_deferring_access_checks ();
15271
15272   /* Check for the declaration of a template class.  */
15273   if (declares_class_or_enum)
15274     {
15275       if (cp_parser_declares_only_class_p (parser))
15276         {
15277           decl = shadow_tag (&decl_specifiers);
15278
15279           /* In this case:
15280
15281                struct C {
15282                  friend template <typename T> struct A<T>::B;
15283                };
15284
15285              A<T>::B will be represented by a TYPENAME_TYPE, and
15286              therefore not recognized by shadow_tag.  */
15287           if (friend_p && *friend_p
15288               && !decl
15289               && decl_specifiers.type
15290               && TYPE_P (decl_specifiers.type))
15291             decl = decl_specifiers.type;
15292
15293           if (decl && decl != error_mark_node)
15294             decl = TYPE_NAME (decl);
15295           else
15296             decl = error_mark_node;
15297         }
15298     }
15299   /* If it's not a template class, try for a template function.  If
15300      the next token is a `;', then this declaration does not declare
15301      anything.  But, if there were errors in the decl-specifiers, then
15302      the error might well have come from an attempted class-specifier.
15303      In that case, there's no need to warn about a missing declarator.  */
15304   if (!decl
15305       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15306           || decl_specifiers.type != error_mark_node))
15307     decl = cp_parser_init_declarator (parser,
15308                                       &decl_specifiers,
15309                                       /*function_definition_allowed_p=*/true,
15310                                       member_p,
15311                                       declares_class_or_enum,
15312                                       &function_definition_p);
15313
15314   pop_deferring_access_checks ();
15315
15316   /* Clear any current qualification; whatever comes next is the start
15317      of something new.  */
15318   parser->scope = NULL_TREE;
15319   parser->qualifying_scope = NULL_TREE;
15320   parser->object_scope = NULL_TREE;
15321   /* Look for a trailing `;' after the declaration.  */
15322   if (!function_definition_p
15323       && (decl == error_mark_node
15324           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15325     cp_parser_skip_to_end_of_block_or_statement (parser);
15326
15327   return decl;
15328 }
15329
15330 /* Parse a cast-expression that is not the operand of a unary "&".  */
15331
15332 static tree
15333 cp_parser_simple_cast_expression (cp_parser *parser)
15334 {
15335   return cp_parser_cast_expression (parser, /*address_p=*/false,
15336                                     /*cast_p=*/false);
15337 }
15338
15339 /* Parse a functional cast to TYPE.  Returns an expression
15340    representing the cast.  */
15341
15342 static tree
15343 cp_parser_functional_cast (cp_parser* parser, tree type)
15344 {
15345   tree expression_list;
15346   tree cast;
15347
15348   expression_list
15349     = cp_parser_parenthesized_expression_list (parser, false,
15350                                                /*cast_p=*/true,
15351                                                /*non_constant_p=*/NULL);
15352
15353   cast = build_functional_cast (type, expression_list);
15354   /* [expr.const]/1: In an integral constant expression "only type
15355      conversions to integral or enumeration type can be used".  */
15356   if (TREE_CODE (type) == TYPE_DECL)
15357     type = TREE_TYPE (type);
15358   if (cast != error_mark_node && !dependent_type_p (type)
15359       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15360     {
15361       if (cp_parser_non_integral_constant_expression
15362           (parser, "a call to a constructor"))
15363         return error_mark_node;
15364     }
15365   return cast;
15366 }
15367
15368 /* Save the tokens that make up the body of a member function defined
15369    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15370    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15371    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15372    for the member function.  */
15373
15374 static tree
15375 cp_parser_save_member_function_body (cp_parser* parser,
15376                                      cp_decl_specifier_seq *decl_specifiers,
15377                                      cp_declarator *declarator,
15378                                      tree attributes)
15379 {
15380   cp_token *first;
15381   cp_token *last;
15382   tree fn;
15383
15384   /* Create the function-declaration.  */
15385   fn = start_method (decl_specifiers, declarator, attributes);
15386   /* If something went badly wrong, bail out now.  */
15387   if (fn == error_mark_node)
15388     {
15389       /* If there's a function-body, skip it.  */
15390       if (cp_parser_token_starts_function_definition_p
15391           (cp_lexer_peek_token (parser->lexer)))
15392         cp_parser_skip_to_end_of_block_or_statement (parser);
15393       return error_mark_node;
15394     }
15395
15396   /* Remember it, if there default args to post process.  */
15397   cp_parser_save_default_args (parser, fn);
15398
15399   /* Save away the tokens that make up the body of the
15400      function.  */
15401   first = parser->lexer->next_token;
15402   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15403   /* Handle function try blocks.  */
15404   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15405     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15406   last = parser->lexer->next_token;
15407
15408   /* Save away the inline definition; we will process it when the
15409      class is complete.  */
15410   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15411   DECL_PENDING_INLINE_P (fn) = 1;
15412
15413   /* We need to know that this was defined in the class, so that
15414      friend templates are handled correctly.  */
15415   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15416
15417   /* We're done with the inline definition.  */
15418   finish_method (fn);
15419
15420   /* Add FN to the queue of functions to be parsed later.  */
15421   TREE_VALUE (parser->unparsed_functions_queues)
15422     = tree_cons (NULL_TREE, fn,
15423                  TREE_VALUE (parser->unparsed_functions_queues));
15424
15425   return fn;
15426 }
15427
15428 /* Parse a template-argument-list, as well as the trailing ">" (but
15429    not the opening ">").  See cp_parser_template_argument_list for the
15430    return value.  */
15431
15432 static tree
15433 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15434 {
15435   tree arguments;
15436   tree saved_scope;
15437   tree saved_qualifying_scope;
15438   tree saved_object_scope;
15439   bool saved_greater_than_is_operator_p;
15440   bool saved_skip_evaluation;
15441
15442   /* [temp.names]
15443
15444      When parsing a template-id, the first non-nested `>' is taken as
15445      the end of the template-argument-list rather than a greater-than
15446      operator.  */
15447   saved_greater_than_is_operator_p
15448     = parser->greater_than_is_operator_p;
15449   parser->greater_than_is_operator_p = false;
15450   /* Parsing the argument list may modify SCOPE, so we save it
15451      here.  */
15452   saved_scope = parser->scope;
15453   saved_qualifying_scope = parser->qualifying_scope;
15454   saved_object_scope = parser->object_scope;
15455   /* We need to evaluate the template arguments, even though this
15456      template-id may be nested within a "sizeof".  */
15457   saved_skip_evaluation = skip_evaluation;
15458   skip_evaluation = false;
15459   /* Parse the template-argument-list itself.  */
15460   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15461     arguments = NULL_TREE;
15462   else
15463     arguments = cp_parser_template_argument_list (parser);
15464   /* Look for the `>' that ends the template-argument-list. If we find
15465      a '>>' instead, it's probably just a typo.  */
15466   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15467     {
15468       if (!saved_greater_than_is_operator_p)
15469         {
15470           /* If we're in a nested template argument list, the '>>' has
15471             to be a typo for '> >'. We emit the error message, but we
15472             continue parsing and we push a '>' as next token, so that
15473             the argument list will be parsed correctly.  Note that the
15474             global source location is still on the token before the
15475             '>>', so we need to say explicitly where we want it.  */
15476           cp_token *token = cp_lexer_peek_token (parser->lexer);
15477           error ("%H%<>>%> should be %<> >%> "
15478                  "within a nested template argument list",
15479                  &token->location);
15480
15481           /* ??? Proper recovery should terminate two levels of
15482              template argument list here.  */
15483           token->type = CPP_GREATER;
15484         }
15485       else
15486         {
15487           /* If this is not a nested template argument list, the '>>'
15488             is a typo for '>'. Emit an error message and continue.
15489             Same deal about the token location, but here we can get it
15490             right by consuming the '>>' before issuing the diagnostic.  */
15491           cp_lexer_consume_token (parser->lexer);
15492           error ("spurious %<>>%>, use %<>%> to terminate "
15493                  "a template argument list");
15494         }
15495     }
15496   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15497     error ("missing %<>%> to terminate the template argument list");
15498   else
15499     /* It's what we want, a '>'; consume it.  */
15500     cp_lexer_consume_token (parser->lexer);
15501   /* The `>' token might be a greater-than operator again now.  */
15502   parser->greater_than_is_operator_p
15503     = saved_greater_than_is_operator_p;
15504   /* Restore the SAVED_SCOPE.  */
15505   parser->scope = saved_scope;
15506   parser->qualifying_scope = saved_qualifying_scope;
15507   parser->object_scope = saved_object_scope;
15508   skip_evaluation = saved_skip_evaluation;
15509
15510   return arguments;
15511 }
15512
15513 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15514    arguments, or the body of the function have not yet been parsed,
15515    parse them now.  */
15516
15517 static void
15518 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15519 {
15520   /* If this member is a template, get the underlying
15521      FUNCTION_DECL.  */
15522   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15523     member_function = DECL_TEMPLATE_RESULT (member_function);
15524
15525   /* There should not be any class definitions in progress at this
15526      point; the bodies of members are only parsed outside of all class
15527      definitions.  */
15528   gcc_assert (parser->num_classes_being_defined == 0);
15529   /* While we're parsing the member functions we might encounter more
15530      classes.  We want to handle them right away, but we don't want
15531      them getting mixed up with functions that are currently in the
15532      queue.  */
15533   parser->unparsed_functions_queues
15534     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15535
15536   /* Make sure that any template parameters are in scope.  */
15537   maybe_begin_member_template_processing (member_function);
15538
15539   /* If the body of the function has not yet been parsed, parse it
15540      now.  */
15541   if (DECL_PENDING_INLINE_P (member_function))
15542     {
15543       tree function_scope;
15544       cp_token_cache *tokens;
15545
15546       /* The function is no longer pending; we are processing it.  */
15547       tokens = DECL_PENDING_INLINE_INFO (member_function);
15548       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15549       DECL_PENDING_INLINE_P (member_function) = 0;
15550
15551       /* If this is a local class, enter the scope of the containing
15552          function.  */
15553       function_scope = current_function_decl;
15554       if (function_scope)
15555         push_function_context_to (function_scope);
15556
15557
15558       /* Push the body of the function onto the lexer stack.  */
15559       cp_parser_push_lexer_for_tokens (parser, tokens);
15560
15561       /* Let the front end know that we going to be defining this
15562          function.  */
15563       start_preparsed_function (member_function, NULL_TREE,
15564                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15565
15566       /* Don't do access checking if it is a templated function.  */
15567       if (processing_template_decl)
15568         push_deferring_access_checks (dk_no_check);
15569
15570       /* Now, parse the body of the function.  */
15571       cp_parser_function_definition_after_declarator (parser,
15572                                                       /*inline_p=*/true);
15573
15574       if (processing_template_decl)
15575         pop_deferring_access_checks ();
15576
15577       /* Leave the scope of the containing function.  */
15578       if (function_scope)
15579         pop_function_context_from (function_scope);
15580       cp_parser_pop_lexer (parser);
15581     }
15582
15583   /* Remove any template parameters from the symbol table.  */
15584   maybe_end_member_template_processing ();
15585
15586   /* Restore the queue.  */
15587   parser->unparsed_functions_queues
15588     = TREE_CHAIN (parser->unparsed_functions_queues);
15589 }
15590
15591 /* If DECL contains any default args, remember it on the unparsed
15592    functions queue.  */
15593
15594 static void
15595 cp_parser_save_default_args (cp_parser* parser, tree decl)
15596 {
15597   tree probe;
15598
15599   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15600        probe;
15601        probe = TREE_CHAIN (probe))
15602     if (TREE_PURPOSE (probe))
15603       {
15604         TREE_PURPOSE (parser->unparsed_functions_queues)
15605           = tree_cons (current_class_type, decl,
15606                        TREE_PURPOSE (parser->unparsed_functions_queues));
15607         break;
15608       }
15609   return;
15610 }
15611
15612 /* FN is a FUNCTION_DECL which may contains a parameter with an
15613    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15614    assumes that the current scope is the scope in which the default
15615    argument should be processed.  */
15616
15617 static void
15618 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15619 {
15620   bool saved_local_variables_forbidden_p;
15621   tree parm;
15622
15623   /* While we're parsing the default args, we might (due to the
15624      statement expression extension) encounter more classes.  We want
15625      to handle them right away, but we don't want them getting mixed
15626      up with default args that are currently in the queue.  */
15627   parser->unparsed_functions_queues
15628     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15629
15630   /* Local variable names (and the `this' keyword) may not appear
15631      in a default argument.  */
15632   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15633   parser->local_variables_forbidden_p = true;
15634
15635   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15636        parm;
15637        parm = TREE_CHAIN (parm))
15638     {
15639       cp_token_cache *tokens;
15640       tree default_arg = TREE_PURPOSE (parm);
15641       tree parsed_arg;
15642       VEC(tree,gc) *insts;
15643       tree copy;
15644       unsigned ix;
15645
15646       if (!default_arg)
15647         continue;
15648
15649       if (TREE_CODE (default_arg) != DEFAULT_ARG)
15650         /* This can happen for a friend declaration for a function
15651            already declared with default arguments.  */
15652         continue;
15653
15654        /* Push the saved tokens for the default argument onto the parser's
15655           lexer stack.  */
15656       tokens = DEFARG_TOKENS (default_arg);
15657       cp_parser_push_lexer_for_tokens (parser, tokens);
15658
15659       /* Parse the assignment-expression.  */
15660       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15661
15662       if (!processing_template_decl)
15663         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15664       
15665       TREE_PURPOSE (parm) = parsed_arg;
15666
15667       /* Update any instantiations we've already created.  */
15668       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15669            VEC_iterate (tree, insts, ix, copy); ix++)
15670         TREE_PURPOSE (copy) = parsed_arg;
15671
15672       /* If the token stream has not been completely used up, then
15673          there was extra junk after the end of the default
15674          argument.  */
15675       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15676         cp_parser_error (parser, "expected %<,%>");
15677
15678       /* Revert to the main lexer.  */
15679       cp_parser_pop_lexer (parser);
15680     }
15681
15682   /* Restore the state of local_variables_forbidden_p.  */
15683   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15684
15685   /* Restore the queue.  */
15686   parser->unparsed_functions_queues
15687     = TREE_CHAIN (parser->unparsed_functions_queues);
15688 }
15689
15690 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15691    either a TYPE or an expression, depending on the form of the
15692    input.  The KEYWORD indicates which kind of expression we have
15693    encountered.  */
15694
15695 static tree
15696 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15697 {
15698   static const char *format;
15699   tree expr = NULL_TREE;
15700   const char *saved_message;
15701   bool saved_integral_constant_expression_p;
15702   bool saved_non_integral_constant_expression_p;
15703
15704   /* Initialize FORMAT the first time we get here.  */
15705   if (!format)
15706     format = "types may not be defined in '%s' expressions";
15707
15708   /* Types cannot be defined in a `sizeof' expression.  Save away the
15709      old message.  */
15710   saved_message = parser->type_definition_forbidden_message;
15711   /* And create the new one.  */
15712   parser->type_definition_forbidden_message
15713     = xmalloc (strlen (format)
15714                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15715                + 1 /* `\0' */);
15716   sprintf ((char *) parser->type_definition_forbidden_message,
15717            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15718
15719   /* The restrictions on constant-expressions do not apply inside
15720      sizeof expressions.  */
15721   saved_integral_constant_expression_p
15722     = parser->integral_constant_expression_p;
15723   saved_non_integral_constant_expression_p
15724     = parser->non_integral_constant_expression_p;
15725   parser->integral_constant_expression_p = false;
15726
15727   /* Do not actually evaluate the expression.  */
15728   ++skip_evaluation;
15729   /* If it's a `(', then we might be looking at the type-id
15730      construction.  */
15731   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15732     {
15733       tree type;
15734       bool saved_in_type_id_in_expr_p;
15735
15736       /* We can't be sure yet whether we're looking at a type-id or an
15737          expression.  */
15738       cp_parser_parse_tentatively (parser);
15739       /* Consume the `('.  */
15740       cp_lexer_consume_token (parser->lexer);
15741       /* Parse the type-id.  */
15742       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15743       parser->in_type_id_in_expr_p = true;
15744       type = cp_parser_type_id (parser);
15745       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15746       /* Now, look for the trailing `)'.  */
15747       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15748       /* If all went well, then we're done.  */
15749       if (cp_parser_parse_definitely (parser))
15750         {
15751           cp_decl_specifier_seq decl_specs;
15752
15753           /* Build a trivial decl-specifier-seq.  */
15754           clear_decl_specs (&decl_specs);
15755           decl_specs.type = type;
15756
15757           /* Call grokdeclarator to figure out what type this is.  */
15758           expr = grokdeclarator (NULL,
15759                                  &decl_specs,
15760                                  TYPENAME,
15761                                  /*initialized=*/0,
15762                                  /*attrlist=*/NULL);
15763         }
15764     }
15765
15766   /* If the type-id production did not work out, then we must be
15767      looking at the unary-expression production.  */
15768   if (!expr)
15769     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15770                                        /*cast_p=*/false);
15771   /* Go back to evaluating expressions.  */
15772   --skip_evaluation;
15773
15774   /* Free the message we created.  */
15775   free ((char *) parser->type_definition_forbidden_message);
15776   /* And restore the old one.  */
15777   parser->type_definition_forbidden_message = saved_message;
15778   parser->integral_constant_expression_p
15779     = saved_integral_constant_expression_p;
15780   parser->non_integral_constant_expression_p
15781     = saved_non_integral_constant_expression_p;
15782
15783   return expr;
15784 }
15785
15786 /* If the current declaration has no declarator, return true.  */
15787
15788 static bool
15789 cp_parser_declares_only_class_p (cp_parser *parser)
15790 {
15791   /* If the next token is a `;' or a `,' then there is no
15792      declarator.  */
15793   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15794           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15795 }
15796
15797 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15798
15799 static void
15800 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15801                              cp_storage_class storage_class)
15802 {
15803   if (decl_specs->storage_class != sc_none)
15804     decl_specs->multiple_storage_classes_p = true;
15805   else
15806     decl_specs->storage_class = storage_class;
15807 }
15808
15809 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15810    is true, the type is a user-defined type; otherwise it is a
15811    built-in type specified by a keyword.  */
15812
15813 static void
15814 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15815                               tree type_spec,
15816                               bool user_defined_p)
15817 {
15818   decl_specs->any_specifiers_p = true;
15819
15820   /* If the user tries to redeclare bool or wchar_t (with, for
15821      example, in "typedef int wchar_t;") we remember that this is what
15822      happened.  In system headers, we ignore these declarations so
15823      that G++ can work with system headers that are not C++-safe.  */
15824   if (decl_specs->specs[(int) ds_typedef]
15825       && !user_defined_p
15826       && (type_spec == boolean_type_node
15827           || type_spec == wchar_type_node)
15828       && (decl_specs->type
15829           || decl_specs->specs[(int) ds_long]
15830           || decl_specs->specs[(int) ds_short]
15831           || decl_specs->specs[(int) ds_unsigned]
15832           || decl_specs->specs[(int) ds_signed]))
15833     {
15834       decl_specs->redefined_builtin_type = type_spec;
15835       if (!decl_specs->type)
15836         {
15837           decl_specs->type = type_spec;
15838           decl_specs->user_defined_type_p = false;
15839         }
15840     }
15841   else if (decl_specs->type)
15842     decl_specs->multiple_types_p = true;
15843   else
15844     {
15845       decl_specs->type = type_spec;
15846       decl_specs->user_defined_type_p = user_defined_p;
15847       decl_specs->redefined_builtin_type = NULL_TREE;
15848     }
15849 }
15850
15851 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15852    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15853
15854 static bool
15855 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15856 {
15857   return decl_specifiers->specs[(int) ds_friend] != 0;
15858 }
15859
15860 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15861    issue an error message indicating that TOKEN_DESC was expected.
15862
15863    Returns the token consumed, if the token had the appropriate type.
15864    Otherwise, returns NULL.  */
15865
15866 static cp_token *
15867 cp_parser_require (cp_parser* parser,
15868                    enum cpp_ttype type,
15869                    const char* token_desc)
15870 {
15871   if (cp_lexer_next_token_is (parser->lexer, type))
15872     return cp_lexer_consume_token (parser->lexer);
15873   else
15874     {
15875       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15876       if (!cp_parser_simulate_error (parser))
15877         {
15878           char *message = concat ("expected ", token_desc, NULL);
15879           cp_parser_error (parser, message);
15880           free (message);
15881         }
15882       return NULL;
15883     }
15884 }
15885
15886 /* Like cp_parser_require, except that tokens will be skipped until
15887    the desired token is found.  An error message is still produced if
15888    the next token is not as expected.  */
15889
15890 static void
15891 cp_parser_skip_until_found (cp_parser* parser,
15892                             enum cpp_ttype type,
15893                             const char* token_desc)
15894 {
15895   cp_token *token;
15896   unsigned nesting_depth = 0;
15897
15898   if (cp_parser_require (parser, type, token_desc))
15899     return;
15900
15901   /* Skip tokens until the desired token is found.  */
15902   while (true)
15903     {
15904       /* Peek at the next token.  */
15905       token = cp_lexer_peek_token (parser->lexer);
15906       /* If we've reached the token we want, consume it and
15907          stop.  */
15908       if (token->type == type && !nesting_depth)
15909         {
15910           cp_lexer_consume_token (parser->lexer);
15911           return;
15912         }
15913       /* If we've run out of tokens, stop.  */
15914       if (token->type == CPP_EOF)
15915         return;
15916       if (token->type == CPP_OPEN_BRACE
15917           || token->type == CPP_OPEN_PAREN
15918           || token->type == CPP_OPEN_SQUARE)
15919         ++nesting_depth;
15920       else if (token->type == CPP_CLOSE_BRACE
15921                || token->type == CPP_CLOSE_PAREN
15922                || token->type == CPP_CLOSE_SQUARE)
15923         {
15924           if (nesting_depth-- == 0)
15925             return;
15926         }
15927       /* Consume this token.  */
15928       cp_lexer_consume_token (parser->lexer);
15929     }
15930 }
15931
15932 /* If the next token is the indicated keyword, consume it.  Otherwise,
15933    issue an error message indicating that TOKEN_DESC was expected.
15934
15935    Returns the token consumed, if the token had the appropriate type.
15936    Otherwise, returns NULL.  */
15937
15938 static cp_token *
15939 cp_parser_require_keyword (cp_parser* parser,
15940                            enum rid keyword,
15941                            const char* token_desc)
15942 {
15943   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15944
15945   if (token && token->keyword != keyword)
15946     {
15947       dyn_string_t error_msg;
15948
15949       /* Format the error message.  */
15950       error_msg = dyn_string_new (0);
15951       dyn_string_append_cstr (error_msg, "expected ");
15952       dyn_string_append_cstr (error_msg, token_desc);
15953       cp_parser_error (parser, error_msg->s);
15954       dyn_string_delete (error_msg);
15955       return NULL;
15956     }
15957
15958   return token;
15959 }
15960
15961 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15962    function-definition.  */
15963
15964 static bool
15965 cp_parser_token_starts_function_definition_p (cp_token* token)
15966 {
15967   return (/* An ordinary function-body begins with an `{'.  */
15968           token->type == CPP_OPEN_BRACE
15969           /* A ctor-initializer begins with a `:'.  */
15970           || token->type == CPP_COLON
15971           /* A function-try-block begins with `try'.  */
15972           || token->keyword == RID_TRY
15973           /* The named return value extension begins with `return'.  */
15974           || token->keyword == RID_RETURN);
15975 }
15976
15977 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15978    definition.  */
15979
15980 static bool
15981 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15982 {
15983   cp_token *token;
15984
15985   token = cp_lexer_peek_token (parser->lexer);
15986   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15987 }
15988
15989 /* Returns TRUE iff the next token is the "," or ">" ending a
15990    template-argument.  */
15991
15992 static bool
15993 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15994 {
15995   cp_token *token;
15996
15997   token = cp_lexer_peek_token (parser->lexer);
15998   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15999 }
16000
16001 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16002    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16003
16004 static bool
16005 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16006                                                      size_t n)
16007 {
16008   cp_token *token;
16009
16010   token = cp_lexer_peek_nth_token (parser->lexer, n);
16011   if (token->type == CPP_LESS)
16012     return true;
16013   /* Check for the sequence `<::' in the original code. It would be lexed as
16014      `[:', where `[' is a digraph, and there is no whitespace before
16015      `:'.  */
16016   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16017     {
16018       cp_token *token2;
16019       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16020       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16021         return true;
16022     }
16023   return false;
16024 }
16025
16026 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16027    or none_type otherwise.  */
16028
16029 static enum tag_types
16030 cp_parser_token_is_class_key (cp_token* token)
16031 {
16032   switch (token->keyword)
16033     {
16034     case RID_CLASS:
16035       return class_type;
16036     case RID_STRUCT:
16037       return record_type;
16038     case RID_UNION:
16039       return union_type;
16040
16041     default:
16042       return none_type;
16043     }
16044 }
16045
16046 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16047
16048 static void
16049 cp_parser_check_class_key (enum tag_types class_key, tree type)
16050 {
16051   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16052     pedwarn ("%qs tag used in naming %q#T",
16053             class_key == union_type ? "union"
16054              : class_key == record_type ? "struct" : "class",
16055              type);
16056 }
16057
16058 /* Issue an error message if DECL is redeclared with different
16059    access than its original declaration [class.access.spec/3].
16060    This applies to nested classes and nested class templates.
16061    [class.mem/1].  */
16062
16063 static void
16064 cp_parser_check_access_in_redeclaration (tree decl)
16065 {
16066   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16067     return;
16068
16069   if ((TREE_PRIVATE (decl)
16070        != (current_access_specifier == access_private_node))
16071       || (TREE_PROTECTED (decl)
16072           != (current_access_specifier == access_protected_node)))
16073     error ("%qD redeclared with different access", decl);
16074 }
16075
16076 /* Look for the `template' keyword, as a syntactic disambiguator.
16077    Return TRUE iff it is present, in which case it will be
16078    consumed.  */
16079
16080 static bool
16081 cp_parser_optional_template_keyword (cp_parser *parser)
16082 {
16083   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16084     {
16085       /* The `template' keyword can only be used within templates;
16086          outside templates the parser can always figure out what is a
16087          template and what is not.  */
16088       if (!processing_template_decl)
16089         {
16090           error ("%<template%> (as a disambiguator) is only allowed "
16091                  "within templates");
16092           /* If this part of the token stream is rescanned, the same
16093              error message would be generated.  So, we purge the token
16094              from the stream.  */
16095           cp_lexer_purge_token (parser->lexer);
16096           return false;
16097         }
16098       else
16099         {
16100           /* Consume the `template' keyword.  */
16101           cp_lexer_consume_token (parser->lexer);
16102           return true;
16103         }
16104     }
16105
16106   return false;
16107 }
16108
16109 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16110    set PARSER->SCOPE, and perform other related actions.  */
16111
16112 static void
16113 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16114 {
16115   tree value;
16116   tree check;
16117
16118   /* Get the stored value.  */
16119   value = cp_lexer_consume_token (parser->lexer)->value;
16120   /* Perform any access checks that were deferred.  */
16121   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16122     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16123   /* Set the scope from the stored value.  */
16124   parser->scope = TREE_VALUE (value);
16125   parser->qualifying_scope = TREE_TYPE (value);
16126   parser->object_scope = NULL_TREE;
16127 }
16128
16129 /* Consume tokens up through a non-nested END token.  */
16130
16131 static void
16132 cp_parser_cache_group (cp_parser *parser,
16133                        enum cpp_ttype end,
16134                        unsigned depth)
16135 {
16136   while (true)
16137     {
16138       cp_token *token;
16139
16140       /* Abort a parenthesized expression if we encounter a brace.  */
16141       if ((end == CPP_CLOSE_PAREN || depth == 0)
16142           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16143         return;
16144       /* If we've reached the end of the file, stop.  */
16145       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16146         return;
16147       /* Consume the next token.  */
16148       token = cp_lexer_consume_token (parser->lexer);
16149       /* See if it starts a new group.  */
16150       if (token->type == CPP_OPEN_BRACE)
16151         {
16152           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16153           if (depth == 0)
16154             return;
16155         }
16156       else if (token->type == CPP_OPEN_PAREN)
16157         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16158       else if (token->type == end)
16159         return;
16160     }
16161 }
16162
16163 /* Begin parsing tentatively.  We always save tokens while parsing
16164    tentatively so that if the tentative parsing fails we can restore the
16165    tokens.  */
16166
16167 static void
16168 cp_parser_parse_tentatively (cp_parser* parser)
16169 {
16170   /* Enter a new parsing context.  */
16171   parser->context = cp_parser_context_new (parser->context);
16172   /* Begin saving tokens.  */
16173   cp_lexer_save_tokens (parser->lexer);
16174   /* In order to avoid repetitive access control error messages,
16175      access checks are queued up until we are no longer parsing
16176      tentatively.  */
16177   push_deferring_access_checks (dk_deferred);
16178 }
16179
16180 /* Commit to the currently active tentative parse.  */
16181
16182 static void
16183 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16184 {
16185   cp_parser_context *context;
16186   cp_lexer *lexer;
16187
16188   /* Mark all of the levels as committed.  */
16189   lexer = parser->lexer;
16190   for (context = parser->context; context->next; context = context->next)
16191     {
16192       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16193         break;
16194       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16195       while (!cp_lexer_saving_tokens (lexer))
16196         lexer = lexer->next;
16197       cp_lexer_commit_tokens (lexer);
16198     }
16199 }
16200
16201 /* Abort the currently active tentative parse.  All consumed tokens
16202    will be rolled back, and no diagnostics will be issued.  */
16203
16204 static void
16205 cp_parser_abort_tentative_parse (cp_parser* parser)
16206 {
16207   cp_parser_simulate_error (parser);
16208   /* Now, pretend that we want to see if the construct was
16209      successfully parsed.  */
16210   cp_parser_parse_definitely (parser);
16211 }
16212
16213 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16214    token stream.  Otherwise, commit to the tokens we have consumed.
16215    Returns true if no error occurred; false otherwise.  */
16216
16217 static bool
16218 cp_parser_parse_definitely (cp_parser* parser)
16219 {
16220   bool error_occurred;
16221   cp_parser_context *context;
16222
16223   /* Remember whether or not an error occurred, since we are about to
16224      destroy that information.  */
16225   error_occurred = cp_parser_error_occurred (parser);
16226   /* Remove the topmost context from the stack.  */
16227   context = parser->context;
16228   parser->context = context->next;
16229   /* If no parse errors occurred, commit to the tentative parse.  */
16230   if (!error_occurred)
16231     {
16232       /* Commit to the tokens read tentatively, unless that was
16233          already done.  */
16234       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16235         cp_lexer_commit_tokens (parser->lexer);
16236
16237       pop_to_parent_deferring_access_checks ();
16238     }
16239   /* Otherwise, if errors occurred, roll back our state so that things
16240      are just as they were before we began the tentative parse.  */
16241   else
16242     {
16243       cp_lexer_rollback_tokens (parser->lexer);
16244       pop_deferring_access_checks ();
16245     }
16246   /* Add the context to the front of the free list.  */
16247   context->next = cp_parser_context_free_list;
16248   cp_parser_context_free_list = context;
16249
16250   return !error_occurred;
16251 }
16252
16253 /* Returns true if we are parsing tentatively and are not committed to
16254    this tentative parse.  */
16255
16256 static bool
16257 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16258 {
16259   return (cp_parser_parsing_tentatively (parser)
16260           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16261 }
16262
16263 /* Returns nonzero iff an error has occurred during the most recent
16264    tentative parse.  */
16265
16266 static bool
16267 cp_parser_error_occurred (cp_parser* parser)
16268 {
16269   return (cp_parser_parsing_tentatively (parser)
16270           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16271 }
16272
16273 /* Returns nonzero if GNU extensions are allowed.  */
16274
16275 static bool
16276 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16277 {
16278   return parser->allow_gnu_extensions_p;
16279 }
16280 \f
16281 /* Objective-C++ Productions */
16282
16283
16284 /* Parse an Objective-C expression, which feeds into a primary-expression
16285    above.
16286
16287    objc-expression:
16288      objc-message-expression
16289      objc-string-literal
16290      objc-encode-expression
16291      objc-protocol-expression
16292      objc-selector-expression
16293
16294   Returns a tree representation of the expression.  */
16295
16296 static tree
16297 cp_parser_objc_expression (cp_parser* parser)
16298 {
16299   /* Try to figure out what kind of declaration is present.  */
16300   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16301
16302   switch (kwd->type)
16303     {
16304     case CPP_OPEN_SQUARE:
16305       return cp_parser_objc_message_expression (parser);
16306
16307     case CPP_OBJC_STRING:
16308       kwd = cp_lexer_consume_token (parser->lexer);
16309       return objc_build_string_object (kwd->value);
16310
16311     case CPP_KEYWORD:
16312       switch (kwd->keyword)
16313         {
16314         case RID_AT_ENCODE:
16315           return cp_parser_objc_encode_expression (parser);
16316
16317         case RID_AT_PROTOCOL:
16318           return cp_parser_objc_protocol_expression (parser);
16319
16320         case RID_AT_SELECTOR:
16321           return cp_parser_objc_selector_expression (parser);
16322
16323         default:
16324           break;
16325         }
16326     default:
16327       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16328       cp_parser_skip_to_end_of_block_or_statement (parser);
16329     }
16330
16331   return error_mark_node;
16332 }
16333
16334 /* Parse an Objective-C message expression.
16335
16336    objc-message-expression:
16337      [ objc-message-receiver objc-message-args ]
16338
16339    Returns a representation of an Objective-C message.  */
16340
16341 static tree
16342 cp_parser_objc_message_expression (cp_parser* parser)
16343 {
16344   tree receiver, messageargs;
16345
16346   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16347   receiver = cp_parser_objc_message_receiver (parser);
16348   messageargs = cp_parser_objc_message_args (parser);
16349   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16350
16351   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16352 }
16353
16354 /* Parse an objc-message-receiver.
16355
16356    objc-message-receiver:
16357      expression
16358      simple-type-specifier
16359
16360   Returns a representation of the type or expression.  */
16361
16362 static tree
16363 cp_parser_objc_message_receiver (cp_parser* parser)
16364 {
16365   tree rcv;
16366
16367   /* An Objective-C message receiver may be either (1) a type
16368      or (2) an expression.  */
16369   cp_parser_parse_tentatively (parser);
16370   rcv = cp_parser_expression (parser, false);
16371
16372   if (cp_parser_parse_definitely (parser))
16373     return rcv;
16374
16375   rcv = cp_parser_simple_type_specifier (parser,
16376                                          /*decl_specs=*/NULL,
16377                                          CP_PARSER_FLAGS_NONE);
16378
16379   return objc_get_class_reference (rcv);
16380 }
16381
16382 /* Parse the arguments and selectors comprising an Objective-C message.
16383
16384    objc-message-args:
16385      objc-selector
16386      objc-selector-args
16387      objc-selector-args , objc-comma-args
16388
16389    objc-selector-args:
16390      objc-selector [opt] : assignment-expression
16391      objc-selector-args objc-selector [opt] : assignment-expression
16392
16393    objc-comma-args:
16394      assignment-expression
16395      objc-comma-args , assignment-expression
16396
16397    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16398    selector arguments and TREE_VALUE containing a list of comma
16399    arguments.  */
16400
16401 static tree
16402 cp_parser_objc_message_args (cp_parser* parser)
16403 {
16404   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16405   bool maybe_unary_selector_p = true;
16406   cp_token *token = cp_lexer_peek_token (parser->lexer);
16407
16408   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16409     {
16410       tree selector = NULL_TREE, arg;
16411
16412       if (token->type != CPP_COLON)
16413         selector = cp_parser_objc_selector (parser);
16414
16415       /* Detect if we have a unary selector.  */
16416       if (maybe_unary_selector_p
16417           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16418         return build_tree_list (selector, NULL_TREE);
16419
16420       maybe_unary_selector_p = false;
16421       cp_parser_require (parser, CPP_COLON, "`:'");
16422       arg = cp_parser_assignment_expression (parser, false);
16423
16424       sel_args
16425         = chainon (sel_args,
16426                    build_tree_list (selector, arg));
16427
16428       token = cp_lexer_peek_token (parser->lexer);
16429     }
16430
16431   /* Handle non-selector arguments, if any. */
16432   while (token->type == CPP_COMMA)
16433     {
16434       tree arg;
16435
16436       cp_lexer_consume_token (parser->lexer);
16437       arg = cp_parser_assignment_expression (parser, false);
16438
16439       addl_args
16440         = chainon (addl_args,
16441                    build_tree_list (NULL_TREE, arg));
16442
16443       token = cp_lexer_peek_token (parser->lexer);
16444     }
16445
16446   return build_tree_list (sel_args, addl_args);
16447 }
16448
16449 /* Parse an Objective-C encode expression.
16450
16451    objc-encode-expression:
16452      @encode objc-typename
16453
16454    Returns an encoded representation of the type argument.  */
16455
16456 static tree
16457 cp_parser_objc_encode_expression (cp_parser* parser)
16458 {
16459   tree type;
16460
16461   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16462   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16463   type = complete_type (cp_parser_type_id (parser));
16464   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16465
16466   if (!type)
16467     {
16468       error ("%<@encode%> must specify a type as an argument");
16469       return error_mark_node;
16470     }
16471
16472   return objc_build_encode_expr (type);
16473 }
16474
16475 /* Parse an Objective-C @defs expression.  */
16476
16477 static tree
16478 cp_parser_objc_defs_expression (cp_parser *parser)
16479 {
16480   tree name;
16481
16482   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16483   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16484   name = cp_parser_identifier (parser);
16485   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16486
16487   return objc_get_class_ivars (name);
16488 }
16489
16490 /* Parse an Objective-C protocol expression.
16491
16492   objc-protocol-expression:
16493     @protocol ( identifier )
16494
16495   Returns a representation of the protocol expression.  */
16496
16497 static tree
16498 cp_parser_objc_protocol_expression (cp_parser* parser)
16499 {
16500   tree proto;
16501
16502   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16503   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16504   proto = cp_parser_identifier (parser);
16505   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16506
16507   return objc_build_protocol_expr (proto);
16508 }
16509
16510 /* Parse an Objective-C selector expression.
16511
16512    objc-selector-expression:
16513      @selector ( objc-method-signature )
16514
16515    objc-method-signature:
16516      objc-selector
16517      objc-selector-seq
16518
16519    objc-selector-seq:
16520      objc-selector :
16521      objc-selector-seq objc-selector :
16522
16523   Returns a representation of the method selector.  */
16524
16525 static tree
16526 cp_parser_objc_selector_expression (cp_parser* parser)
16527 {
16528   tree sel_seq = NULL_TREE;
16529   bool maybe_unary_selector_p = true;
16530   cp_token *token;
16531
16532   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16533   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16534   token = cp_lexer_peek_token (parser->lexer);
16535
16536   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16537          || token->type == CPP_SCOPE)
16538     {
16539       tree selector = NULL_TREE;
16540
16541       if (token->type != CPP_COLON
16542           || token->type == CPP_SCOPE)
16543         selector = cp_parser_objc_selector (parser);
16544
16545       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16546           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16547         {
16548           /* Detect if we have a unary selector.  */
16549           if (maybe_unary_selector_p)
16550             {
16551               sel_seq = selector;
16552               goto finish_selector;
16553             }
16554           else
16555             {
16556               cp_parser_error (parser, "expected %<:%>");
16557             }
16558         }
16559       maybe_unary_selector_p = false;
16560       token = cp_lexer_consume_token (parser->lexer);
16561       
16562       if (token->type == CPP_SCOPE)
16563         {
16564           sel_seq
16565             = chainon (sel_seq,
16566                        build_tree_list (selector, NULL_TREE));
16567           sel_seq
16568             = chainon (sel_seq,
16569                        build_tree_list (NULL_TREE, NULL_TREE));
16570         }
16571       else
16572         sel_seq
16573           = chainon (sel_seq,
16574                      build_tree_list (selector, NULL_TREE));
16575
16576       token = cp_lexer_peek_token (parser->lexer);
16577     }
16578
16579  finish_selector:
16580   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16581
16582   return objc_build_selector_expr (sel_seq);
16583 }
16584
16585 /* Parse a list of identifiers.
16586
16587    objc-identifier-list:
16588      identifier
16589      objc-identifier-list , identifier
16590
16591    Returns a TREE_LIST of identifier nodes.  */
16592
16593 static tree
16594 cp_parser_objc_identifier_list (cp_parser* parser)
16595 {
16596   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16597   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16598
16599   while (sep->type == CPP_COMMA)
16600     {
16601       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16602       list = chainon (list,
16603                       build_tree_list (NULL_TREE,
16604                                        cp_parser_identifier (parser)));
16605       sep = cp_lexer_peek_token (parser->lexer);
16606     }
16607
16608   return list;
16609 }
16610
16611 /* Parse an Objective-C alias declaration.
16612
16613    objc-alias-declaration:
16614      @compatibility_alias identifier identifier ;
16615
16616    This function registers the alias mapping with the Objective-C front-end.
16617    It returns nothing.  */
16618
16619 static void
16620 cp_parser_objc_alias_declaration (cp_parser* parser)
16621 {
16622   tree alias, orig;
16623
16624   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16625   alias = cp_parser_identifier (parser);
16626   orig = cp_parser_identifier (parser);
16627   objc_declare_alias (alias, orig);
16628   cp_parser_consume_semicolon_at_end_of_statement (parser);
16629 }
16630
16631 /* Parse an Objective-C class forward-declaration.
16632
16633    objc-class-declaration:
16634      @class objc-identifier-list ;
16635
16636    The function registers the forward declarations with the Objective-C
16637    front-end.  It returns nothing.  */
16638
16639 static void
16640 cp_parser_objc_class_declaration (cp_parser* parser)
16641 {
16642   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16643   objc_declare_class (cp_parser_objc_identifier_list (parser));
16644   cp_parser_consume_semicolon_at_end_of_statement (parser);
16645 }
16646
16647 /* Parse a list of Objective-C protocol references.
16648
16649    objc-protocol-refs-opt:
16650      objc-protocol-refs [opt]
16651
16652    objc-protocol-refs:
16653      < objc-identifier-list >
16654
16655    Returns a TREE_LIST of identifiers, if any.  */
16656
16657 static tree
16658 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16659 {
16660   tree protorefs = NULL_TREE;
16661
16662   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16663     {
16664       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16665       protorefs = cp_parser_objc_identifier_list (parser);
16666       cp_parser_require (parser, CPP_GREATER, "`>'");
16667     }
16668
16669   return protorefs;
16670 }
16671
16672 /* Parse a Objective-C visibility specification.  */
16673
16674 static void
16675 cp_parser_objc_visibility_spec (cp_parser* parser)
16676 {
16677   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16678
16679   switch (vis->keyword)
16680     {
16681     case RID_AT_PRIVATE:
16682       objc_set_visibility (2);
16683       break;
16684     case RID_AT_PROTECTED:
16685       objc_set_visibility (0);
16686       break;
16687     case RID_AT_PUBLIC:
16688       objc_set_visibility (1);
16689       break;
16690     default:
16691       return;
16692     }
16693
16694   /* Eat '@private'/'@protected'/'@public'.  */
16695   cp_lexer_consume_token (parser->lexer);
16696 }
16697
16698 /* Parse an Objective-C method type.  */
16699
16700 static void
16701 cp_parser_objc_method_type (cp_parser* parser)
16702 {
16703   objc_set_method_type
16704    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16705     ? PLUS_EXPR
16706     : MINUS_EXPR);
16707 }
16708
16709 /* Parse an Objective-C protocol qualifier.  */
16710
16711 static tree
16712 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16713 {
16714   tree quals = NULL_TREE, node;
16715   cp_token *token = cp_lexer_peek_token (parser->lexer);
16716
16717   node = token->value;
16718
16719   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16720          && (node == ridpointers [(int) RID_IN]
16721              || node == ridpointers [(int) RID_OUT]
16722              || node == ridpointers [(int) RID_INOUT]
16723              || node == ridpointers [(int) RID_BYCOPY]
16724              || node == ridpointers [(int) RID_BYREF]
16725              || node == ridpointers [(int) RID_ONEWAY]))
16726     {
16727       quals = tree_cons (NULL_TREE, node, quals);
16728       cp_lexer_consume_token (parser->lexer);
16729       token = cp_lexer_peek_token (parser->lexer);
16730       node = token->value;
16731     }
16732
16733   return quals;
16734 }
16735
16736 /* Parse an Objective-C typename.  */
16737
16738 static tree
16739 cp_parser_objc_typename (cp_parser* parser)
16740 {
16741   tree typename = NULL_TREE;
16742
16743   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16744     {
16745       tree proto_quals, cp_type = NULL_TREE;
16746
16747       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16748       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16749
16750       /* An ObjC type name may consist of just protocol qualifiers, in which
16751          case the type shall default to 'id'.  */
16752       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16753         cp_type = cp_parser_type_id (parser);
16754
16755       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16756       typename = build_tree_list (proto_quals, cp_type);
16757     }
16758
16759   return typename;
16760 }
16761
16762 /* Check to see if TYPE refers to an Objective-C selector name.  */
16763
16764 static bool
16765 cp_parser_objc_selector_p (enum cpp_ttype type)
16766 {
16767   return (type == CPP_NAME || type == CPP_KEYWORD
16768           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16769           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16770           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16771           || type == CPP_XOR || type == CPP_XOR_EQ);
16772 }
16773
16774 /* Parse an Objective-C selector.  */
16775
16776 static tree
16777 cp_parser_objc_selector (cp_parser* parser)
16778 {
16779   cp_token *token = cp_lexer_consume_token (parser->lexer);
16780
16781   if (!cp_parser_objc_selector_p (token->type))
16782     {
16783       error ("invalid Objective-C++ selector name");
16784       return error_mark_node;
16785     }
16786
16787   /* C++ operator names are allowed to appear in ObjC selectors.  */
16788   switch (token->type)
16789     {
16790     case CPP_AND_AND: return get_identifier ("and");
16791     case CPP_AND_EQ: return get_identifier ("and_eq");
16792     case CPP_AND: return get_identifier ("bitand");
16793     case CPP_OR: return get_identifier ("bitor");
16794     case CPP_COMPL: return get_identifier ("compl");
16795     case CPP_NOT: return get_identifier ("not");
16796     case CPP_NOT_EQ: return get_identifier ("not_eq");
16797     case CPP_OR_OR: return get_identifier ("or");
16798     case CPP_OR_EQ: return get_identifier ("or_eq");
16799     case CPP_XOR: return get_identifier ("xor");
16800     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16801     default: return token->value;
16802     }
16803 }
16804
16805 /* Parse an Objective-C params list.  */
16806
16807 static tree
16808 cp_parser_objc_method_keyword_params (cp_parser* parser)
16809 {
16810   tree params = NULL_TREE;
16811   bool maybe_unary_selector_p = true;
16812   cp_token *token = cp_lexer_peek_token (parser->lexer);
16813
16814   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16815     {
16816       tree selector = NULL_TREE, typename, identifier;
16817
16818       if (token->type != CPP_COLON)
16819         selector = cp_parser_objc_selector (parser);
16820
16821       /* Detect if we have a unary selector.  */
16822       if (maybe_unary_selector_p
16823           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16824         return selector;
16825
16826       maybe_unary_selector_p = false;
16827       cp_parser_require (parser, CPP_COLON, "`:'");
16828       typename = cp_parser_objc_typename (parser);
16829       identifier = cp_parser_identifier (parser);
16830
16831       params
16832         = chainon (params,
16833                    objc_build_keyword_decl (selector,
16834                                             typename,
16835                                             identifier));
16836
16837       token = cp_lexer_peek_token (parser->lexer);
16838     }
16839
16840   return params;
16841 }
16842
16843 /* Parse the non-keyword Objective-C params.  */
16844
16845 static tree
16846 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16847 {
16848   tree params = make_node (TREE_LIST);
16849   cp_token *token = cp_lexer_peek_token (parser->lexer);
16850   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16851
16852   while (token->type == CPP_COMMA)
16853     {
16854       cp_parameter_declarator *parmdecl;
16855       tree parm;
16856
16857       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16858       token = cp_lexer_peek_token (parser->lexer);
16859
16860       if (token->type == CPP_ELLIPSIS)
16861         {
16862           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16863           *ellipsisp = true;
16864           break;
16865         }
16866
16867       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16868       parm = grokdeclarator (parmdecl->declarator,
16869                              &parmdecl->decl_specifiers,
16870                              PARM, /*initialized=*/0,
16871                              /*attrlist=*/NULL);
16872
16873       chainon (params, build_tree_list (NULL_TREE, parm));
16874       token = cp_lexer_peek_token (parser->lexer);
16875     }
16876
16877   return params;
16878 }
16879
16880 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16881
16882 static void
16883 cp_parser_objc_interstitial_code (cp_parser* parser)
16884 {
16885   cp_token *token = cp_lexer_peek_token (parser->lexer);
16886
16887   /* If the next token is `extern' and the following token is a string
16888      literal, then we have a linkage specification.  */
16889   if (token->keyword == RID_EXTERN
16890       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16891     cp_parser_linkage_specification (parser);
16892   /* Handle #pragma, if any.  */
16893   else if (token->type == CPP_PRAGMA)
16894     cp_lexer_handle_pragma (parser->lexer);
16895   /* Allow stray semicolons.  */
16896   else if (token->type == CPP_SEMICOLON)
16897     cp_lexer_consume_token (parser->lexer);
16898   /* Finally, try to parse a block-declaration, or a function-definition.  */
16899   else
16900     cp_parser_block_declaration (parser, /*statement_p=*/false);
16901 }
16902
16903 /* Parse a method signature.  */
16904
16905 static tree
16906 cp_parser_objc_method_signature (cp_parser* parser)
16907 {
16908   tree rettype, kwdparms, optparms;
16909   bool ellipsis = false;
16910
16911   cp_parser_objc_method_type (parser);
16912   rettype = cp_parser_objc_typename (parser);
16913   kwdparms = cp_parser_objc_method_keyword_params (parser);
16914   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16915
16916   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16917 }
16918
16919 /* Pars an Objective-C method prototype list.  */
16920
16921 static void
16922 cp_parser_objc_method_prototype_list (cp_parser* parser)
16923 {
16924   cp_token *token = cp_lexer_peek_token (parser->lexer);
16925
16926   while (token->keyword != RID_AT_END)
16927     {
16928       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16929         {
16930           objc_add_method_declaration
16931            (cp_parser_objc_method_signature (parser));
16932           cp_parser_consume_semicolon_at_end_of_statement (parser);
16933         }
16934       else
16935         /* Allow for interspersed non-ObjC++ code.  */
16936         cp_parser_objc_interstitial_code (parser);
16937
16938       token = cp_lexer_peek_token (parser->lexer);
16939     }
16940
16941   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16942   objc_finish_interface ();
16943 }
16944
16945 /* Parse an Objective-C method definition list.  */
16946
16947 static void
16948 cp_parser_objc_method_definition_list (cp_parser* parser)
16949 {
16950   cp_token *token = cp_lexer_peek_token (parser->lexer);
16951
16952   while (token->keyword != RID_AT_END)
16953     {
16954       tree meth;
16955
16956       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16957         {
16958           push_deferring_access_checks (dk_deferred);
16959           objc_start_method_definition
16960            (cp_parser_objc_method_signature (parser));
16961
16962           /* For historical reasons, we accept an optional semicolon.  */
16963           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16964             cp_lexer_consume_token (parser->lexer);
16965
16966           perform_deferred_access_checks ();
16967           stop_deferring_access_checks ();
16968           meth = cp_parser_function_definition_after_declarator (parser,
16969                                                                  false);
16970           pop_deferring_access_checks ();
16971           objc_finish_method_definition (meth);
16972         }
16973       else
16974         /* Allow for interspersed non-ObjC++ code.  */
16975         cp_parser_objc_interstitial_code (parser);
16976
16977       token = cp_lexer_peek_token (parser->lexer);
16978     }
16979
16980   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16981   objc_finish_implementation ();
16982 }
16983
16984 /* Parse Objective-C ivars.  */
16985
16986 static void
16987 cp_parser_objc_class_ivars (cp_parser* parser)
16988 {
16989   cp_token *token = cp_lexer_peek_token (parser->lexer);
16990
16991   if (token->type != CPP_OPEN_BRACE)
16992     return;     /* No ivars specified.  */
16993
16994   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
16995   token = cp_lexer_peek_token (parser->lexer);
16996
16997   while (token->type != CPP_CLOSE_BRACE)
16998     {
16999       cp_decl_specifier_seq declspecs;
17000       int decl_class_or_enum_p;
17001       tree prefix_attributes;
17002
17003       cp_parser_objc_visibility_spec (parser);
17004
17005       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17006         break;
17007
17008       cp_parser_decl_specifier_seq (parser,
17009                                     CP_PARSER_FLAGS_OPTIONAL,
17010                                     &declspecs,
17011                                     &decl_class_or_enum_p);
17012       prefix_attributes = declspecs.attributes;
17013       declspecs.attributes = NULL_TREE;
17014
17015       /* Keep going until we hit the `;' at the end of the
17016          declaration.  */
17017       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17018         {
17019           tree width = NULL_TREE, attributes, first_attribute, decl;
17020           cp_declarator *declarator = NULL;
17021           int ctor_dtor_or_conv_p;
17022
17023           /* Check for a (possibly unnamed) bitfield declaration.  */
17024           token = cp_lexer_peek_token (parser->lexer);
17025           if (token->type == CPP_COLON)
17026             goto eat_colon;
17027
17028           if (token->type == CPP_NAME
17029               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17030                   == CPP_COLON))
17031             {
17032               /* Get the name of the bitfield.  */
17033               declarator = make_id_declarator (NULL_TREE,
17034                                                cp_parser_identifier (parser));
17035
17036              eat_colon:
17037               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17038               /* Get the width of the bitfield.  */
17039               width
17040                 = cp_parser_constant_expression (parser,
17041                                                  /*allow_non_constant=*/false,
17042                                                  NULL);
17043             }
17044           else
17045             {
17046               /* Parse the declarator.  */
17047               declarator
17048                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17049                                         &ctor_dtor_or_conv_p,
17050                                         /*parenthesized_p=*/NULL,
17051                                         /*member_p=*/false);
17052             }
17053
17054           /* Look for attributes that apply to the ivar.  */
17055           attributes = cp_parser_attributes_opt (parser);
17056           /* Remember which attributes are prefix attributes and
17057              which are not.  */
17058           first_attribute = attributes;
17059           /* Combine the attributes.  */
17060           attributes = chainon (prefix_attributes, attributes);
17061
17062           if (width)
17063             {
17064               /* Create the bitfield declaration.  */
17065               decl = grokbitfield (declarator, &declspecs, width);
17066               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17067             }
17068           else
17069             decl = grokfield (declarator, &declspecs, NULL_TREE,
17070                               NULL_TREE, attributes);
17071
17072           /* Add the instance variable.  */
17073           objc_add_instance_variable (decl);
17074
17075           /* Reset PREFIX_ATTRIBUTES.  */
17076           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17077             attributes = TREE_CHAIN (attributes);
17078           if (attributes)
17079             TREE_CHAIN (attributes) = NULL_TREE;
17080
17081           token = cp_lexer_peek_token (parser->lexer);
17082
17083           if (token->type == CPP_COMMA)
17084             {
17085               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17086               continue;
17087             }
17088           break;
17089         }
17090
17091       cp_parser_consume_semicolon_at_end_of_statement (parser);
17092       token = cp_lexer_peek_token (parser->lexer);
17093     }
17094
17095   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17096   /* For historical reasons, we accept an optional semicolon.  */
17097   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17098     cp_lexer_consume_token (parser->lexer);
17099 }
17100
17101 /* Parse an Objective-C protocol declaration.  */
17102
17103 static void
17104 cp_parser_objc_protocol_declaration (cp_parser* parser)
17105 {
17106   tree proto, protorefs;
17107   cp_token *tok;
17108
17109   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17110   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17111     {
17112       error ("identifier expected after %<@protocol%>");
17113       goto finish;
17114     }
17115
17116   /* See if we have a forward declaration or a definition.  */
17117   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17118
17119   /* Try a forward declaration first.  */
17120   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17121     {
17122       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17123      finish:
17124       cp_parser_consume_semicolon_at_end_of_statement (parser);
17125     }
17126
17127   /* Ok, we got a full-fledged definition (or at least should).  */
17128   else
17129     {
17130       proto = cp_parser_identifier (parser);
17131       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17132       objc_start_protocol (proto, protorefs);
17133       cp_parser_objc_method_prototype_list (parser);
17134     }
17135 }
17136
17137 /* Parse an Objective-C superclass or category.  */
17138
17139 static void
17140 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17141                                                           tree *categ)
17142 {
17143   cp_token *next = cp_lexer_peek_token (parser->lexer);
17144
17145   *super = *categ = NULL_TREE;
17146   if (next->type == CPP_COLON)
17147     {
17148       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17149       *super = cp_parser_identifier (parser);
17150     }
17151   else if (next->type == CPP_OPEN_PAREN)
17152     {
17153       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17154       *categ = cp_parser_identifier (parser);
17155       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17156     }
17157 }
17158
17159 /* Parse an Objective-C class interface.  */
17160
17161 static void
17162 cp_parser_objc_class_interface (cp_parser* parser)
17163 {
17164   tree name, super, categ, protos;
17165
17166   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17167   name = cp_parser_identifier (parser);
17168   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17169   protos = cp_parser_objc_protocol_refs_opt (parser);
17170
17171   /* We have either a class or a category on our hands.  */
17172   if (categ)
17173     objc_start_category_interface (name, categ, protos);
17174   else
17175     {
17176       objc_start_class_interface (name, super, protos);
17177       /* Handle instance variable declarations, if any.  */
17178       cp_parser_objc_class_ivars (parser);
17179       objc_continue_interface ();
17180     }
17181
17182   cp_parser_objc_method_prototype_list (parser);
17183 }
17184
17185 /* Parse an Objective-C class implementation.  */
17186
17187 static void
17188 cp_parser_objc_class_implementation (cp_parser* parser)
17189 {
17190   tree name, super, categ;
17191
17192   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17193   name = cp_parser_identifier (parser);
17194   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17195
17196   /* We have either a class or a category on our hands.  */
17197   if (categ)
17198     objc_start_category_implementation (name, categ);
17199   else
17200     {
17201       objc_start_class_implementation (name, super);
17202       /* Handle instance variable declarations, if any.  */
17203       cp_parser_objc_class_ivars (parser);
17204       objc_continue_implementation ();
17205     }
17206
17207   cp_parser_objc_method_definition_list (parser);
17208 }
17209
17210 /* Consume the @end token and finish off the implementation.  */
17211
17212 static void
17213 cp_parser_objc_end_implementation (cp_parser* parser)
17214 {
17215   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17216   objc_finish_implementation ();
17217 }
17218
17219 /* Parse an Objective-C declaration.  */
17220
17221 static void
17222 cp_parser_objc_declaration (cp_parser* parser)
17223 {
17224   /* Try to figure out what kind of declaration is present.  */
17225   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17226
17227   switch (kwd->keyword)
17228     {
17229     case RID_AT_ALIAS:
17230       cp_parser_objc_alias_declaration (parser);
17231       break;
17232     case RID_AT_CLASS:
17233       cp_parser_objc_class_declaration (parser);
17234       break;
17235     case RID_AT_PROTOCOL:
17236       cp_parser_objc_protocol_declaration (parser);
17237       break;
17238     case RID_AT_INTERFACE:
17239       cp_parser_objc_class_interface (parser);
17240       break;
17241     case RID_AT_IMPLEMENTATION:
17242       cp_parser_objc_class_implementation (parser);
17243       break;
17244     case RID_AT_END:
17245       cp_parser_objc_end_implementation (parser);
17246       break;
17247     default:
17248       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17249       cp_parser_skip_to_end_of_block_or_statement (parser);
17250     }
17251 }
17252
17253 /* Parse an Objective-C try-catch-finally statement.
17254
17255    objc-try-catch-finally-stmt:
17256      @try compound-statement objc-catch-clause-seq [opt]
17257        objc-finally-clause [opt]
17258
17259    objc-catch-clause-seq:
17260      objc-catch-clause objc-catch-clause-seq [opt]
17261
17262    objc-catch-clause:
17263      @catch ( exception-declaration ) compound-statement
17264
17265    objc-finally-clause
17266      @finally compound-statement
17267
17268    Returns NULL_TREE.  */
17269
17270 static tree
17271 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17272   location_t location;
17273   tree stmt;
17274
17275   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17276   location = cp_lexer_peek_token (parser->lexer)->location;
17277   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17278      node, lest it get absorbed into the surrounding block.  */
17279   stmt = push_stmt_list ();
17280   cp_parser_compound_statement (parser, NULL, false);
17281   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17282
17283   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17284     {
17285       cp_parameter_declarator *parmdecl;
17286       tree parm;
17287
17288       cp_lexer_consume_token (parser->lexer);
17289       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17290       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17291       parm = grokdeclarator (parmdecl->declarator,
17292                              &parmdecl->decl_specifiers,
17293                              PARM, /*initialized=*/0,
17294                              /*attrlist=*/NULL);
17295       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17296       objc_begin_catch_clause (parm);
17297       cp_parser_compound_statement (parser, NULL, false);
17298       objc_finish_catch_clause ();
17299     }
17300
17301   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17302     {
17303       cp_lexer_consume_token (parser->lexer);
17304       location = cp_lexer_peek_token (parser->lexer)->location;
17305       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17306          node, lest it get absorbed into the surrounding block.  */
17307       stmt = push_stmt_list ();
17308       cp_parser_compound_statement (parser, NULL, false);
17309       objc_build_finally_clause (location, pop_stmt_list (stmt));
17310     }
17311
17312   return objc_finish_try_stmt ();
17313 }
17314
17315 /* Parse an Objective-C synchronized statement.
17316
17317    objc-synchronized-stmt:
17318      @synchronized ( expression ) compound-statement
17319
17320    Returns NULL_TREE.  */
17321
17322 static tree
17323 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17324   location_t location;
17325   tree lock, stmt;
17326
17327   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17328
17329   location = cp_lexer_peek_token (parser->lexer)->location;
17330   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17331   lock = cp_parser_expression (parser, false);
17332   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17333
17334   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17335      node, lest it get absorbed into the surrounding block.  */
17336   stmt = push_stmt_list ();
17337   cp_parser_compound_statement (parser, NULL, false);
17338
17339   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17340 }
17341
17342 /* Parse an Objective-C throw statement.
17343
17344    objc-throw-stmt:
17345      @throw assignment-expression [opt] ;
17346
17347    Returns a constructed '@throw' statement.  */
17348
17349 static tree
17350 cp_parser_objc_throw_statement (cp_parser *parser) {
17351   tree expr = NULL_TREE;
17352
17353   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17354
17355   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17356     expr = cp_parser_assignment_expression (parser, false);
17357
17358   cp_parser_consume_semicolon_at_end_of_statement (parser);
17359
17360   return objc_build_throw_stmt (expr);
17361 }
17362
17363 /* Parse an Objective-C statement.  */
17364
17365 static tree
17366 cp_parser_objc_statement (cp_parser * parser) {
17367   /* Try to figure out what kind of declaration is present.  */
17368   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17369
17370   switch (kwd->keyword)
17371     {
17372     case RID_AT_TRY:
17373       return cp_parser_objc_try_catch_finally_statement (parser);
17374     case RID_AT_SYNCHRONIZED:
17375       return cp_parser_objc_synchronized_statement (parser);
17376     case RID_AT_THROW:
17377       return cp_parser_objc_throw_statement (parser);
17378     default:
17379       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17380       cp_parser_skip_to_end_of_block_or_statement (parser);
17381     }
17382
17383   return error_mark_node;
17384 }
17385 \f
17386 /* The parser.  */
17387
17388 static GTY (()) cp_parser *the_parser;
17389
17390 /* External interface.  */
17391
17392 /* Parse one entire translation unit.  */
17393
17394 void
17395 c_parse_file (void)
17396 {
17397   bool error_occurred;
17398   static bool already_called = false;
17399
17400   if (already_called)
17401     {
17402       sorry ("inter-module optimizations not implemented for C++");
17403       return;
17404     }
17405   already_called = true;
17406
17407   the_parser = cp_parser_new ();
17408   push_deferring_access_checks (flag_access_control
17409                                 ? dk_no_deferred : dk_no_check);
17410   error_occurred = cp_parser_translation_unit (the_parser);
17411   the_parser = NULL;
17412 }
17413
17414 /* This variable must be provided by every front end.  */
17415
17416 int yydebug;
17417
17418 #include "gt-cp-parser.h"